From e83fed7258d8e8ec3a109125fea2618945658f78 Mon Sep 17 00:00:00 2001 From: Denis Bykhov Date: Thu, 9 Feb 2023 09:10:36 +0600 Subject: [PATCH] Use query cache for find (#2607) Signed-off-by: Denis Bykhov --- packages/query/src/index.ts | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/query/src/index.ts b/packages/query/src/index.ts index 98a91a3278..2fa1ddb844 100644 --- a/packages/query/src/index.ts +++ b/packages/query/src/index.ts @@ -109,6 +109,16 @@ export class LiveQuery extends TxProcessor implements Client { query: DocumentQuery, options?: FindOptions ): Promise> { + const q = this.findQuery(_class, query, options) + if (q !== undefined) { + if (q.result instanceof Promise) { + q.result = await q.result + } + if (this.removeFromQueue(q)) { + this.queue.push(q) + } + return toFindResult(this.clone(q.result), q.total) as FindResult + } return await this.client.findAll(_class, query, options) } @@ -117,6 +127,16 @@ export class LiveQuery extends TxProcessor implements Client { query: DocumentQuery, options?: FindOptions ): Promise | undefined> { + const q = this.findQuery(_class, query, options) + if (q !== undefined) { + if (q.result instanceof Promise) { + q.result = await q.result + } + if (this.removeFromQueue(q)) { + this.queue.push(q) + } + return this.clone(q.result)[0] as WithLookup + } return await this.client.findOne(_class, query, options) } @@ -133,13 +153,15 @@ export class LiveQuery extends TxProcessor implements Client { } } - private removeFromQueue (q: Query): void { + private removeFromQueue (q: Query): boolean { if (q.callbacks.length === 0) { const queueIndex = this.queue.indexOf(q) if (queueIndex !== -1) { this.queue.splice(queueIndex, 1) + return true } } + return false } private pushCallback (q: Query, callback: (result: Doc[]) => void): void {