diff --git a/packages/query/src/index.ts b/packages/query/src/index.ts index 8fb3b4ac42..23c6f73856 100644 --- a/packages/query/src/index.ts +++ b/packages/query/src/index.ts @@ -236,7 +236,10 @@ export class LiveQuery extends TxProcessor implements Client { q.callbacks.set(callback.callbackId, callback.callback) setTimeout(async () => { if (q !== undefined) { - await this.callback(q) + if (q.result instanceof Promise) { + q.result = await q.result + } + callback.callback(q.result) } }, 0) } diff --git a/plugins/activity-resources/src/activity.ts b/plugins/activity-resources/src/activity.ts index 8672115d59..bb9e6479f3 100644 --- a/plugins/activity-resources/src/activity.ts +++ b/plugins/activity-resources/src/activity.ts @@ -59,7 +59,8 @@ const combineThreshold = 5 * 60 * 1000 */ export interface Activity { update: ( - object: Doc, + objectId: Ref, + objectClass: Ref>, listener: DisplayTxListener, sort: SortingOrder, editable: Map>, boolean> @@ -71,6 +72,8 @@ class ActivityImpl implements Activity { private readonly attachedTxQuery: LiveQuery private readonly attachedChangeTxQuery: LiveQuery private readonly hiddenAttributes: Set + private prevObjectId: Ref | undefined + private prevObjectClass: Ref> | undefined private editable: Map>, boolean> | undefined private ownTxes: Array> = [] @@ -87,9 +90,9 @@ class ActivityImpl implements Activity { this.attachedChangeTxQuery = createQuery() } - private notify (object: Doc, listener: DisplayTxListener, sort: SortingOrder): void { + private notify (objectId: Ref, listener: DisplayTxListener, sort: SortingOrder): void { if (this.editable != null) { - this.combineTransactions(object, this.ownTxes, this.attachedTxes, this.attacheChangedTxes, this.editable).then( + this.combineTransactions(objectId, this.ownTxes, this.attachedTxes, this.attacheChangedTxes, this.editable).then( (result) => { const sorted = result.sort((a, b) => (a.tx.modifiedOn - b.tx.modifiedOn) * sort) listener(sorted) @@ -101,26 +104,35 @@ class ActivityImpl implements Activity { } } - update (object: Doc, listener: DisplayTxListener, sort: SortingOrder, editable: Map>, boolean>): void { + update ( + objectId: Ref, + objectClass: Ref>, + listener: DisplayTxListener, + sort: SortingOrder, + editable: Map>, boolean> + ): void { + if (objectId === this.prevObjectId && objectClass === this.prevObjectClass) return + this.prevObjectClass = objectClass + this.prevObjectId = objectId let isAttached = false - isAttached = this.hierarchy.isDerived(object._class, core.class.AttachedDoc) + isAttached = this.hierarchy.isDerived(objectClass, core.class.AttachedDoc) this.editable = editable this.ownTxQuery.query>( isAttached ? core.class.TxCollectionCUD : core.class.TxCUD, isAttached - ? { 'tx.objectId': object._id as Ref } + ? { 'tx.objectId': objectId as Ref } : { - objectId: object._id, + objectId, _class: { $in: [core.class.TxCreateDoc, core.class.TxUpdateDoc, core.class.TxRemoveDoc, core.class.TxMixin] } }, (result) => { this.ownTxes = result - this.notify(object, listener, sort) + this.notify(objectId, listener, sort) }, { sort: { modifiedOn: SortingOrder.Ascending } } ) @@ -128,12 +140,12 @@ class ActivityImpl implements Activity { this.attachedTxQuery.query>( core.class.TxCollectionCUD, { - objectId: object._id, + objectId, 'tx._class': { $in: [core.class.TxCreateDoc, core.class.TxUpdateDoc, core.class.TxRemoveDoc] } }, (result) => { this.attachedTxes = result - this.notify(object, listener, sort) + this.notify(objectId, listener, sort) }, { sort: { modifiedOn: SortingOrder.Ascending } } ) @@ -141,21 +153,21 @@ class ActivityImpl implements Activity { this.attachedChangeTxQuery.query>( core.class.TxCollectionCUD, { - 'tx.operations.attachedTo': object._id, + 'tx.operations.attachedTo': objectId, 'tx._class': core.class.TxUpdateDoc }, (result) => { this.attacheChangedTxes = result - this.notify(object, listener, sort) + this.notify(objectId, listener, sort) }, { sort: { modifiedOn: SortingOrder.Ascending } } ) // In case editable is changed - this.notify(object, listener, sort) + this.notify(objectId, listener, sort) } async combineTransactions ( - doc: Doc, + _id: Ref, ownTxes: Array>, attachedTxes: Array>, attachedChangeTxes: Array>, @@ -180,7 +192,7 @@ class ActivityImpl implements Activity { const changeAttached = this.isChangeAttachedTx(tx) if (changeAttached || this.isDisplayTxRequired(tx)) { if (changeAttached) { - tx = await this.createFakeTx(doc, tx) + tx = await this.createFakeTx(_id, tx) } const [result, isUpdated, isMixin] = this.createDisplayTx(tx, parents, false) if (!(isUpdated || isMixin)) { @@ -194,10 +206,10 @@ class ActivityImpl implements Activity { } private async createFakeTx ( - doc: Doc, + _id: Ref, cltx: TxCollectionCUD ): Promise> { - if (doc._id === cltx.objectId) { + if (_id === cltx.objectId) { cltx.tx._class = core.class.TxRemoveDoc } else { const createTx = await this.client.findOne(core.class.TxCollectionCUD, { diff --git a/plugins/activity-resources/src/components/Activity.svelte b/plugins/activity-resources/src/components/Activity.svelte index 4d1e6d6b00..c965b5781f 100644 --- a/plugins/activity-resources/src/components/Activity.svelte +++ b/plugins/activity-resources/src/components/Activity.svelte @@ -65,10 +65,15 @@ let loading = false - function updateTxes (object: Doc): void { + function updateTxes ( + objectId: Ref, + objectClass: Ref>, + editableMap: Map>, boolean> | undefined + ): void { loading = true activityQuery.update( - object, + objectId, + objectClass, (result) => { txes = filterCollectionTxes(result) @@ -81,7 +86,7 @@ ) } - $: if (editableMap) updateTxes(object) + $: updateTxes(object._id, object._class, editableMap) let filtered: DisplayTx[] = [] @@ -116,7 +121,13 @@ {/if} - (filtered = e.detail)} /> + { + filtered = e.detail + }} + />
{#if filtered} diff --git a/plugins/view-resources/src/components/EditDoc.svelte b/plugins/view-resources/src/components/EditDoc.svelte index b3d08cfeaa..837b3af232 100644 --- a/plugins/view-resources/src/components/EditDoc.svelte +++ b/plugins/view-resources/src/components/EditDoc.svelte @@ -64,15 +64,19 @@ }) const query = createQuery() - $: if (_id && _class) { - query.query(_class, { _id }, (result) => { - object = result[0] - if (object != null) { - realObjectClass = object._class - } - }) - } else { - query.unsubscribe() + $: updateQuery(_id, _class) + + function updateQuery (_id: Ref, _class: Ref>) { + if (_id && _class) { + query.query(_class, { _id }, (result) => { + object = result[0] + if (object != null) { + realObjectClass = object._class + } + }) + } else { + query.unsubscribe() + } } let oldClass: Ref>