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, findProperty,
FindResult, FindResult,
getObjectValue, getObjectValue,
Hierarchy, Lookup, Hierarchy,
Lookup,
LookupData, LookupData,
ModelDb, Ref, resultSort, ReverseLookups, ModelDb,
Ref,
resultSort,
ReverseLookups,
SortingQuery, SortingQuery,
Tx, Tx,
TxBulkWrite, TxBulkWrite,
@ -174,7 +178,10 @@ export class LiveQuery extends TxProcessor implements Client {
} else { } else {
if (this.getHierarchy().isDerived(tx.mixin, q._class)) { if (this.getHierarchy().isDerived(tx.mixin, q._class)) {
// Mixin potentially added to object we doesn't have in out results // 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,11 +245,21 @@ export class LiveQuery extends TxProcessor implements Client {
} }
} else { } else {
const updatedDoc = q.result[pos] const updatedDoc = q.result[pos]
await this.__updateDoc(q, updatedDoc, tx) if (updatedDoc.modifiedOn > tx.modifiedOn) return
if (!this.match(q, updatedDoc)) { if (updatedDoc.modifiedOn === tx.modifiedOn) {
q.result.splice(pos, 1) const current = await this.findOne(q._class, { _id: updatedDoc._id })
if (current !== undefined) {
q.result[pos] = current
} else {
q.result.splice(pos, 1)
}
} else { } else {
q.result[pos] = updatedDoc await this.__updateDoc(q, updatedDoc, tx)
if (!this.match(q, updatedDoc)) {
q.result.splice(pos, 1)
} else {
q.result[pos] = updatedDoc
}
} }
} }
this.sort(q, tx) this.sort(q, tx)
@ -310,9 +327,9 @@ export class LiveQuery extends TxProcessor implements Client {
} }
private async refresh (q: Query): Promise<void> { private async refresh (q: Query): Promise<void> {
const res = await this.client.findAll(q._class, q.query, q.options) q.result = this.client.findAll(q._class, q.query, q.options)
q.result = res q.result = await q.result
q.callback(this.clone(res)) q.callback(this.clone(q.result))
} }
// Check if query is partially matched. // Check if query is partially matched.
@ -331,7 +348,7 @@ export class LiveQuery extends TxProcessor implements Client {
return false return false
} }
private async getLookupValue<T extends Doc> (doc: T, lookup: Lookup<T>, result: LookupData<T>): Promise<void> { private async getLookupValue<T extends Doc>(doc: T, lookup: Lookup<T>, result: LookupData<T>): Promise<void> {
for (const key in lookup) { for (const key in lookup) {
if (key === '_id') { if (key === '_id') {
await this.getReverseLookupValue(doc, lookup, result) await this.getReverseLookupValue(doc, lookup, result)
@ -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) { for (const key in lookup._id) {
const value = lookup._id[key] const value = lookup._id[key]
const objects = await this.findAll(value, { attachedTo: doc._id }) const objects = await this.findAll(value, { attachedTo: doc._id })
@ -363,7 +384,7 @@ export class LiveQuery extends TxProcessor implements Client {
} }
} }
private async lookup<T extends Doc> (doc: T, lookup: Lookup<T>): Promise<void> { private async lookup<T extends Doc>(doc: T, lookup: Lookup<T>): Promise<void> {
const result: LookupData<Doc> = {} const result: LookupData<Doc> = {}
await this.getLookupValue(doc, lookup, result) await this.getLookupValue(doc, lookup, result)
;(doc as WithLookup<Doc>).$lookup = result ;(doc as WithLookup<Doc>).$lookup = result