pass hierarchy to trigger

Signed-off-by: Andrey Platov <andrey@hardcoreeng.com>
This commit is contained in:
Andrey Platov 2021-10-10 12:21:08 +02:00
parent 26c93b9f01
commit 3d833008f5
No known key found for this signature in database
GPG Key ID: C8787EFEB4B64AF0
4 changed files with 16 additions and 18 deletions

View File

@ -130,7 +130,7 @@ class TServerStorage implements ServerStorage {
// store object
await this.routeTx(tx)
// invoke triggers and store derived objects
const derived = await this.triggers.apply(tx.modifiedBy, tx, this.findAll.bind(this))
const derived = await this.triggers.apply(tx.modifiedBy, tx, this.findAll.bind(this), this.hierarchy)
for (const tx of derived) {
await this.routeTx(tx)
}

View File

@ -14,7 +14,7 @@
// limitations under the License.
//
import type { Tx, Doc, TxCreateDoc, Ref, Account } from '@anticrm/core'
import type { Tx, Doc, TxCreateDoc, Ref, Account, Hierarchy } from '@anticrm/core'
import core, { TxFactory } from '@anticrm/core'
import { getResource } from '@anticrm/platform'
@ -39,8 +39,8 @@ export class Triggers {
}
}
async apply (account: Ref<Account>, tx: Tx, findAll: FindAll<Doc>): Promise<Tx[]> {
const derived = this.triggers.map(trigger => trigger(tx, new TxFactory(account), findAll))
async apply (account: Ref<Account>, tx: Tx, findAll: FindAll<Doc>, hierarchy: Hierarchy): Promise<Tx[]> {
const derived = this.triggers.map(trigger => trigger(tx, new TxFactory(account), findAll, hierarchy))
const result = await Promise.all(derived)
return result.flatMap(x => x)
}

View File

@ -15,7 +15,7 @@
//
import type { Tx, Ref, Doc, Class, Space, Timestamp, Account, FindResult, DocumentQuery, FindOptions } from '@anticrm/core'
import { TxFactory } from '@anticrm/core'
import { TxFactory, Hierarchy } from '@anticrm/core'
import type { Resource } from '@anticrm/platform'
/**
@ -26,7 +26,7 @@ export type FindAll<T extends Doc> = (clazz: Ref<Class<T>>, query: DocumentQuery
/**
* @public
*/
export type TriggerFunc = (tx: Tx, txFactory: TxFactory, findAll: FindAll<Doc>) => Promise<Tx[]>
export type TriggerFunc = (tx: Tx, txFactory: TxFactory, findAll: FindAll<Doc>, hierarchy: Hierarchy) => Promise<Tx[]>
/**
* @public

View File

@ -14,23 +14,21 @@
// limitations under the License.
//
import type { Tx, TxFactory } from '@anticrm/core'
import type { Tx, TxFactory, Doc, TxCreateDoc, DocWithState } from '@anticrm/core'
import type { FindAll } from '@anticrm/server-core'
// import core from '@anticrm/core'
import core, { Hierarchy } from '@anticrm/core'
/**
* @public
*/
export async function OnDocWithState (tx: Tx, txFactory: TxFactory): Promise<Tx[]> {
// if (tx._class === core.class.TxCreateDoc) {
// const createTx = tx as TxCreateDoc<Message>
// if (createTx.objectClass === chunter.class.Message) {
// const content = createTx.attributes.content
// const backlinks = getBacklinks(createTx.objectId, content)
// return backlinks.map(backlink => txFactory.createTxCreateDoc(chunter.class.Backlink, chunter.space.Backlinks, backlink))
// }
// }
console.log('OnDocWithState here')
export async function OnDocWithState (tx: Tx, txFactory: TxFactory, findAll: FindAll<Doc>, hierarchy: Hierarchy): Promise<Tx[]> {
if (tx._class === core.class.TxCreateDoc) {
const createTx = tx as TxCreateDoc<DocWithState>
if (hierarchy.isDerived(createTx.objectClass, core.class.DocWithState)) {
console.log('OnDocWithState derived here')
}
}
return []
}