Skip to content
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

UBER-331: Fix live query update #3305

Merged
merged 1 commit into from
May 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions packages/presentation/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(_class, query, callback, options)
void this.doQuery<T>(++this.reqId, _class, query, callback, options)
return true
}

private async doQuery<T extends Doc>(
id: number,
_class: Ref<Class<T>>,
query: DocumentQuery<T>,
callback: (result: FindResult<T>) => void,
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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)
}
}

Expand Down