From 4e558f0cfe6096244d95fa5462753e7e686f7545 Mon Sep 17 00:00:00 2001
From: Andrey Sobolev <haiodo@users.noreply.github.com>
Date: Wed, 11 Oct 2023 21:04:23 +0700
Subject: [PATCH] UBER-1038: Fix flicking during issue creation (#3826)

Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
---
 packages/query/src/index.ts                   |  6 +--
 .../src/components/list/List.svelte           | 48 +++++--------------
 2 files changed, 14 insertions(+), 40 deletions(-)

diff --git a/packages/query/src/index.ts b/packages/query/src/index.ts
index f1c07946cc..d2c36e7d37 100644
--- a/packages/query/src/index.ts
+++ b/packages/query/src/index.ts
@@ -419,7 +419,7 @@ export class LiveQuery extends TxProcessor implements Client {
 
     for (const queries of this.queries) {
       const isTx = hierarchy.isDerived(queries[0], core.class.Tx)
-      const isMixin = hierarchy.isDerived(tx.mixin, queries[0])
+
       for (const q of queries[1]) {
         if (isTx) {
           // handle add since Txes are immutable
@@ -455,9 +455,9 @@ export class LiveQuery extends TxProcessor implements Client {
           await this.sort(q, tx)
           const udoc = q.result.find((p) => p._id === tx.objectId)
           await this.updatedDocCallback(udoc, q)
-        } else if (isMixin) {
+        } else if (queries[0] === tx.mixin) {
           // Mixin potentially added to object we doesn't have in out results
-          const doc = await this.client.findOne(q._class, { _id: tx.objectId }, q.options)
+          const doc = await this.client.findOne(q._class, { ...q.query, _id: tx.objectId }, q.options)
           if (doc !== undefined) {
             await this.handleDocAdd(q, doc, false)
           }
diff --git a/plugins/view-resources/src/components/list/List.svelte b/plugins/view-resources/src/components/list/List.svelte
index 9819564984..aec76cfaef 100644
--- a/plugins/view-resources/src/components/list/List.svelte
+++ b/plugins/view-resources/src/components/list/List.svelte
@@ -62,7 +62,7 @@
 
   $: queryNoLookup = noLookup(resultQuery)
 
-  let fastQueryIds: Ref<Doc>[] = []
+  let fastQueryIds: Set<Ref<Doc>> = new Set()
 
   let categoryQueryOptions: Partial<FindOptions<Doc>>
   $: categoryQueryOptions = {
@@ -79,46 +79,20 @@
     queryNoLookup,
     (res) => {
       fastDocs = res
-      fastQueryIds = res.map((it) => it._id)
+      fastQueryIds = new Set(res.map((it) => it._id))
     },
     { ...categoryQueryOptions, limit: 1000 }
   )
-  $: {
-    let doQuery = false
-    let _idQuery: DocumentQuery<Doc>['_id'] = { $nin: fastQueryIds }
+  $: docsQuerySlow.query(
+    _class,
+    queryNoLookup,
+    (res) => {
+      slowDocs = res
+    },
+    categoryQueryOptions
+  )
 
-    if (fastQueryIds.length > 0) {
-      doQuery = true
-
-      if (typeof resultQuery._id === 'object' && resultQuery._id?.$in !== undefined) {
-        const inIds = new Set(resultQuery._id.$in)
-        fastQueryIds.forEach((it) => inIds.delete(it))
-        _idQuery = { $in: Array.from(inIds) }
-        if (inIds.size === 0) {
-          doQuery = false
-        }
-      } else if (typeof resultQuery._id === 'object' && resultQuery._id?.$nin !== undefined) {
-        const ninIds = new Set(resultQuery._id.$in)
-        fastQueryIds.forEach((it) => ninIds.add(it))
-        _idQuery = { $nin: Array.from(ninIds) }
-      }
-    }
-
-    if (doQuery) {
-      docsQuerySlow.query(
-        _class,
-        { ...queryNoLookup, _id: _idQuery },
-        (res) => {
-          slowDocs = res
-        },
-        categoryQueryOptions
-      )
-    } else {
-      docsQuerySlow.unsubscribe()
-    }
-  }
-
-  $: docs = [...fastDocs, ...slowDocs]
+  $: docs = [...fastDocs, ...slowDocs.filter((it) => !fastQueryIds.has(it._id))]
 
   function getProjection (fields: string[], query: DocumentQuery<Doc>): Record<string, number> {
     const res: Record<string, number> = {}