UBER-331: Fix live query update (#3305)

Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
This commit is contained in:
Andrey Sobolev 2023-05-31 22:06:25 +07:00 committed by GitHub
parent d21cae9f32
commit dbb08b7465
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -175,13 +175,15 @@ export class LiveQuery {
if (!this.needUpdate(_class, query, callback, options) && !this.clientRecreated) { if (!this.needUpdate(_class, query, callback, options) && !this.clientRecreated) {
return false return false
} }
// We need to prevent callback with old values to be happening
// One time refresh in case of client recreation // One time refresh in case of client recreation
this.clientRecreated = false this.clientRecreated = false
void this.doQuery<T>(_class, query, callback, options) void this.doQuery<T>(++this.reqId, _class, query, callback, options)
return true return true
} }
private async doQuery<T extends Doc>( private async doQuery<T extends Doc>(
id: number,
_class: Ref<Class<T>>, _class: Ref<Class<T>>,
query: DocumentQuery<T>, query: DocumentQuery<T>,
callback: (result: FindResult<T>) => void, callback: (result: FindResult<T>) => void,
@ -196,7 +198,6 @@ export class LiveQuery {
return return
} }
const id = ++this.reqId
const piplineQuery = await pipeline.subscribe(_class, query, options, () => { const piplineQuery = await pipeline.subscribe(_class, query, options, () => {
// Refresh query if pipeline decide it is required. // Refresh query if pipeline decide it is required.
this.refreshClient() this.refreshClient()
@ -213,7 +214,17 @@ export class LiveQuery {
this.oldOptions = options this.oldOptions = options
this.oldQuery = query this.oldQuery = query
const unsub = liveQuery.query(_class, piplineQuery.query ?? query, callback, piplineQuery.options ?? options) const unsub = liveQuery.query(
_class,
piplineQuery.query ?? query,
(result) => {
// If we have one more request after this one, no need to do something.
if (id === this.reqId) {
callback(result)
}
},
piplineQuery.options ?? options
)
this.unsubscribe = () => { this.unsubscribe = () => {
unsub() unsub()
piplineQuery.unsubscribe() piplineQuery.unsubscribe()
@ -232,7 +243,7 @@ export class LiveQuery {
const query = this.oldQuery const query = this.oldQuery
const callback = this.oldCallback const callback = this.oldCallback
const options = this.oldOptions const options = this.oldOptions
void this.doQuery(_class, query, callback, options) void this.doQuery(++this.reqId, _class, query, callback, options)
} }
} }