Fix query update when refresh (#1006)

Signed-off-by: Denis Bykhov <80476319+BykhovDenis@users.noreply.github.com>
This commit is contained in:
Denis Bykhov 2022-02-14 15:52:52 +06:00 committed by GitHub
parent c88fa75208
commit 4c60b40eb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -23,9 +23,13 @@ import core, {
findProperty,
FindResult,
getObjectValue,
Hierarchy, Lookup,
Hierarchy,
Lookup,
LookupData,
ModelDb, Ref, resultSort, ReverseLookups,
ModelDb,
Ref,
resultSort,
ReverseLookups,
SortingQuery,
Tx,
TxBulkWrite,
@ -174,7 +178,10 @@ export class LiveQuery extends TxProcessor implements Client {
} else {
if (this.getHierarchy().isDerived(tx.mixin, q._class)) {
// Mixin potentially added to object we doesn't have in out results
await this.refresh(q)
const doc = await this.findOne(q._class, { _id: tx.objectId }, q.options)
if (doc !== undefined) {
await this.handleDocAdd(q, doc)
}
}
}
}
@ -238,6 +245,15 @@ export class LiveQuery extends TxProcessor implements Client {
}
} else {
const updatedDoc = q.result[pos]
if (updatedDoc.modifiedOn > tx.modifiedOn) return
if (updatedDoc.modifiedOn === tx.modifiedOn) {
const current = await this.findOne(q._class, { _id: updatedDoc._id })
if (current !== undefined) {
q.result[pos] = current
} else {
q.result.splice(pos, 1)
}
} else {
await this.__updateDoc(q, updatedDoc, tx)
if (!this.match(q, updatedDoc)) {
q.result.splice(pos, 1)
@ -245,6 +261,7 @@ export class LiveQuery extends TxProcessor implements Client {
q.result[pos] = updatedDoc
}
}
}
this.sort(q, tx)
await this.callback(q.result[pos], q)
} else if (this.matchQuery(q, tx)) {
@ -310,9 +327,9 @@ export class LiveQuery extends TxProcessor implements Client {
}
private async refresh (q: Query): Promise<void> {
const res = await this.client.findAll(q._class, q.query, q.options)
q.result = res
q.callback(this.clone(res))
q.result = this.client.findAll(q._class, q.query, q.options)
q.result = await q.result
q.callback(this.clone(q.result))
}
// Check if query is partially matched.
@ -355,7 +372,11 @@ export class LiveQuery extends TxProcessor implements Client {
}
}
private async getReverseLookupValue<T extends Doc> (doc: T, lookup: ReverseLookups, result: LookupData<T>): Promise<void> {
private async getReverseLookupValue<T extends Doc>(
doc: T,
lookup: ReverseLookups,
result: LookupData<T>
): Promise<void> {
for (const key in lookup._id) {
const value = lookup._id[key]
const objects = await this.findAll(value, { attachedTo: doc._id })