-
Notifications
You must be signed in to change notification settings - Fork 948
Simplify Listen API #3491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simplify Listen API #3491
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -35,7 +35,6 @@ import { | |||
} from './event_manager'; | ||||
import { SyncEngine } from './sync_engine'; | ||||
import { View } from './view'; | ||||
|
||||
import { SharedClientState } from '../local/shared_client_state'; | ||||
import { AutoId } from '../util/misc'; | ||||
import { DatabaseId, DatabaseInfo } from './database_info'; | ||||
|
@@ -47,6 +46,7 @@ import { | |||
MemoryOfflineComponentProvider, | ||||
OfflineComponentProvider | ||||
} from './component_provider'; | ||||
import { AsyncObserver } from '../util/async_observer'; | ||||
|
||||
const LOG_TAG = 'FirestoreClient'; | ||||
const MAX_CONCURRENT_LIMBO_RESOLUTIONS = 100; | ||||
|
@@ -391,24 +391,17 @@ export class FirestoreClient { | |||
|
||||
listen( | ||||
query: Query, | ||||
observer: Observer<ViewSnapshot>, | ||||
options: ListenOptions | ||||
): QueryListener { | ||||
options: ListenOptions, | ||||
observer: Partial<Observer<ViewSnapshot>> | ||||
): () => void { | ||||
this.verifyNotTerminated(); | ||||
const listener = new QueryListener(query, observer, options); | ||||
const wrappedObserver = new AsyncObserver(observer); | ||||
const listener = new QueryListener(query, wrappedObserver, options); | ||||
this.asyncQueue.enqueueAndForget(() => this.eventMgr.listen(listener)); | ||||
return listener; | ||||
} | ||||
|
||||
unlisten(listener: QueryListener): void { | ||||
// Checks for termination but does not raise error, allowing unlisten after | ||||
// termination to be a no-op. | ||||
if (this.clientTerminated) { | ||||
return; | ||||
} | ||||
this.asyncQueue.enqueueAndForget(() => { | ||||
return this.eventMgr.unlisten(listener); | ||||
}); | ||||
return () => { | ||||
wrappedObserver.mute(); | ||||
this.asyncQueue.enqueueAndForget(() => this.eventMgr.unlisten(listener)); | ||||
}; | ||||
} | ||||
|
||||
async getDocumentFromLocalCache( | ||||
|
@@ -486,24 +479,18 @@ export class FirestoreClient { | |||
return this.databaseInfo.databaseId; | ||||
} | ||||
|
||||
addSnapshotsInSyncListener(observer: Observer<void>): void { | ||||
addSnapshotsInSyncListener(observer: Partial<Observer<void>>): () => void { | ||||
this.verifyNotTerminated(); | ||||
this.asyncQueue.enqueueAndForget(() => { | ||||
this.eventMgr.addSnapshotsInSyncListener(observer); | ||||
return Promise.resolve(); | ||||
}); | ||||
} | ||||
|
||||
removeSnapshotsInSyncListener(observer: Observer<void>): void { | ||||
// Checks for shutdown but does not raise error, allowing remove after | ||||
// shutdown to be a no-op. | ||||
if (this.clientTerminated) { | ||||
return; | ||||
} | ||||
this.asyncQueue.enqueueAndForget(() => { | ||||
this.eventMgr.removeSnapshotsInSyncListener(observer); | ||||
return Promise.resolve(); | ||||
}); | ||||
const wrappedObserver = new AsyncObserver(observer); | ||||
this.asyncQueue.enqueueAndForget(async () => | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is it necessary to specify an async function here to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a test already:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh perfect. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry - I replied to the wrong comment (but looks like you managed to restore the context). This change is not needed, it just felt right to me. Manually returning There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense. I forgot to check the return type of |
||||
this.eventMgr.addSnapshotsInSyncListener(wrappedObserver) | ||||
); | ||||
return () => { | ||||
wrappedObserver.mute(); | ||||
this.asyncQueue.enqueueAndForget(async () => | ||||
this.eventMgr.removeSnapshotsInSyncListener(wrappedObserver) | ||||
); | ||||
}; | ||||
} | ||||
|
||||
get clientTerminated(): boolean { | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@schmidt-sebastian Can you just confirm that it is indeed unnecessary to check for
if (this.clientTerminated)
before callingthis.asyncQueue.enqueueAndForget()
? I'm not familiar enough withAsyncQueue
to know, but the old code avoids callingthis.asyncQueue.enqueueAndForget()
in the case thatthis.clientTerminated
is false. The new code will call it regardless of the value ofthis.clientTerminated
.This comment also applies to
addSnapshotsInSyncListener()
below.