feat(modules-sdk, types, user, utils):init user module events (#6431)
* init user module events * refactor utils * undo test script update * fix feedback * add eventbus service to module test-runner * add injected dependencies * move events to utils * use const eventname in tests * rm withTransaction
This commit is contained in:
@@ -474,6 +474,19 @@ export function abstractModuleServiceFactory<
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
protected async emitEvents_(groupedEvents) {
|
||||
if (!this.eventBusModuleService_ || !groupedEvents) {
|
||||
return
|
||||
}
|
||||
|
||||
const promises: Promise<void>[] = []
|
||||
for (const group of Object.keys(groupedEvents)) {
|
||||
promises.push(this.eventBusModuleService_?.emit(groupedEvents[group]))
|
||||
}
|
||||
|
||||
await Promise.all(promises)
|
||||
}
|
||||
}
|
||||
|
||||
const mainModelMethods = buildMethodNamesFromModel(mainModel, false)
|
||||
|
||||
26
packages/utils/src/modules-sdk/decorators/emit-events.ts
Normal file
26
packages/utils/src/modules-sdk/decorators/emit-events.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { MessageAggregator } from "../../event-bus"
|
||||
import { InjectIntoContext } from "./inject-into-context"
|
||||
|
||||
export function EmitEvents() {
|
||||
return function (
|
||||
target: any,
|
||||
propertyKey: string | symbol,
|
||||
descriptor: any
|
||||
): void {
|
||||
const aggregator = new MessageAggregator()
|
||||
InjectIntoContext({
|
||||
messageAggregator: () => aggregator,
|
||||
})(target, propertyKey, descriptor)
|
||||
|
||||
const original = descriptor.value
|
||||
|
||||
descriptor.value = async function (...args: any[]) {
|
||||
const result = await original.apply(this, args)
|
||||
|
||||
await target.emitEvents_.apply(this, [aggregator.getMessages()])
|
||||
|
||||
aggregator.clearMessages()
|
||||
return result
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,3 +2,5 @@ export * from "./context-parameter"
|
||||
export * from "./inject-manager"
|
||||
export * from "./inject-shared-context"
|
||||
export * from "./inject-transaction-manager"
|
||||
export * from "./inject-into-context"
|
||||
export * from "./emit-events"
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { MessageAggregator } from "../../event-bus"
|
||||
|
||||
export function InjectIntoContext(
|
||||
properties: Record<string, unknown | Function>
|
||||
): MethodDecorator {
|
||||
|
||||
Reference in New Issue
Block a user