From dbb08b7465e5f07c4c3cab61afd747e894b07e49 Mon Sep 17 00:00:00 2001 From: Andrey Sobolev Date: Wed, 31 May 2023 22:06:25 +0700 Subject: [PATCH] UBER-331: Fix live query update (#3305) Signed-off-by: Andrey Sobolev --- packages/presentation/src/utils.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/presentation/src/utils.ts b/packages/presentation/src/utils.ts index fda0a16c89..1bcfcddc62 100644 --- a/packages/presentation/src/utils.ts +++ b/packages/presentation/src/utils.ts @@ -175,13 +175,15 @@ export class LiveQuery { if (!this.needUpdate(_class, query, callback, options) && !this.clientRecreated) { return false } + // We need to prevent callback with old values to be happening // One time refresh in case of client recreation this.clientRecreated = false - void this.doQuery(_class, query, callback, options) + void this.doQuery(++this.reqId, _class, query, callback, options) return true } private async doQuery( + id: number, _class: Ref>, query: DocumentQuery, callback: (result: FindResult) => void, @@ -196,7 +198,6 @@ export class LiveQuery { return } - const id = ++this.reqId const piplineQuery = await pipeline.subscribe(_class, query, options, () => { // Refresh query if pipeline decide it is required. this.refreshClient() @@ -213,7 +214,17 @@ export class LiveQuery { this.oldOptions = options 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 = () => { unsub() piplineQuery.unsubscribe() @@ -232,7 +243,7 @@ export class LiveQuery { const query = this.oldQuery const callback = this.oldCallback const options = this.oldOptions - void this.doQuery(_class, query, callback, options) + void this.doQuery(++this.reqId, _class, query, callback, options) } }