Fix activity query (#3234)

Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
This commit is contained in:
Denis Bykhov 2023-05-22 22:13:06 +06:00 committed by GitHub
parent 391489f999
commit 532b140d3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 31 deletions

View File

@ -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)
}

View File

@ -59,7 +59,8 @@ const combineThreshold = 5 * 60 * 1000
*/
export interface Activity {
update: (
object: Doc,
objectId: Ref<Doc>,
objectClass: Ref<Class<Doc>>,
listener: DisplayTxListener,
sort: SortingOrder,
editable: Map<Ref<Class<Doc>>, boolean>
@ -71,6 +72,8 @@ class ActivityImpl implements Activity {
private readonly attachedTxQuery: LiveQuery
private readonly attachedChangeTxQuery: LiveQuery
private readonly hiddenAttributes: Set<string>
private prevObjectId: Ref<Doc> | undefined
private prevObjectClass: Ref<Class<Doc>> | undefined
private editable: Map<Ref<Class<Doc>>, boolean> | undefined
private ownTxes: Array<TxCUD<Doc>> = []
@ -87,9 +90,9 @@ class ActivityImpl implements Activity {
this.attachedChangeTxQuery = createQuery()
}
private notify (object: Doc, listener: DisplayTxListener, sort: SortingOrder): void {
private notify (objectId: Ref<Doc>, 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<Ref<Class<Doc>>, boolean>): void {
update (
objectId: Ref<Doc>,
objectClass: Ref<Class<Doc>>,
listener: DisplayTxListener,
sort: SortingOrder,
editable: Map<Ref<Class<Doc>>, 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<TxCUD<Doc>>(
isAttached ? core.class.TxCollectionCUD : core.class.TxCUD,
isAttached
? { 'tx.objectId': object._id as Ref<AttachedDoc> }
? { 'tx.objectId': objectId as Ref<AttachedDoc> }
: {
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<TxCollectionCUD<Doc, AttachedDoc>>(
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<TxCollectionCUD<Doc, AttachedDoc>>(
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<Doc>,
ownTxes: Array<TxCUD<Doc>>,
attachedTxes: Array<TxCollectionCUD<Doc, AttachedDoc>>,
attachedChangeTxes: Array<TxCollectionCUD<Doc, AttachedDoc>>,
@ -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<Doc>,
cltx: TxCollectionCUD<Doc, AttachedDoc>
): Promise<TxCollectionCUD<Doc, AttachedDoc>> {
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, {

View File

@ -65,10 +65,15 @@
let loading = false
function updateTxes (object: Doc): void {
function updateTxes (
objectId: Ref<Doc>,
objectClass: Ref<Class<Doc>>,
editableMap: Map<Ref<Class<Doc>>, 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 @@
</div>
{/if}
</span>
<ActivityFilter {txes} {object} on:update={(e) => (filtered = e.detail)} />
<ActivityFilter
{txes}
{object}
on:update={(e) => {
filtered = e.detail
}}
/>
</div>
<div class="p-activity select-text" id={activity.string.Activity}>
{#if filtered}

View File

@ -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<Doc>, _class: Ref<Class<Doc>>) {
if (_id && _class) {
query.query(_class, { _id }, (result) => {
object = result[0]
if (object != null) {
realObjectClass = object._class
}
})
} else {
query.unsubscribe()
}
}
let oldClass: Ref<Class<Doc>>