Skip to content

Commit ba563bb

Browse files
committed
patch(feat): octo post-transaction hook support.
1 parent 339e652 commit ba563bb

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

packages/octo/src/decorators/module.decorator.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import { PostModelActionCallback, PostModelActionHandleHook } from '../functions/hook/post-model-action-handle.hook.js';
2+
import {
3+
PostModelTransactionCallback,
4+
PostModelTransactionHandleHook,
5+
} from '../functions/hook/post-model-transaction-handle.hook.js';
26
import { AModule } from '../functions/module/module.abstract.js';
37
import { ModelSerializationService } from '../services/serialization/model/model-serialization.service.js';
48
import { Container } from './container.js';
59

610
export function Module({
711
postModelActionHandles = [],
12+
postModelTransactionHandles = [],
813
}: {
9-
postModelActionHandles: { ACTION_NAME: string; callback: PostModelActionCallback }[];
14+
postModelActionHandles?: { ACTION_NAME: string; callback: PostModelActionCallback }[];
15+
postModelTransactionHandles?: { callback: PostModelTransactionCallback }[];
1016
}): (constructor: any) => void {
1117
return function (constructor: any) {
1218
Container.get(ModelSerializationService).then((modelSerializationService) => {
@@ -18,5 +24,9 @@ export function Module({
1824
for (const { ACTION_NAME, callback } of postModelActionHandles) {
1925
PostModelActionHandleHook.register(ACTION_NAME, callback);
2026
}
27+
28+
for (const { callback } of postModelTransactionHandles) {
29+
PostModelTransactionHandleHook.register(callback);
30+
}
2131
};
2232
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { DiffMetadata } from '../diff/diff-metadata.js';
2+
import { AHook } from './hook.abstract.js';
3+
4+
export type PostModelTransactionCallback = (modelTransaction: DiffMetadata[][]) => Promise<void>;
5+
6+
export class PostModelTransactionHandleHook extends AHook {
7+
private static readonly callbacks: PostModelTransactionCallback[] = [];
8+
9+
static async apply(modelTransaction: DiffMetadata[][]): Promise<void> {
10+
for (const callback of this.callbacks) {
11+
await callback(modelTransaction);
12+
}
13+
}
14+
15+
static register(callback: PostModelTransactionCallback): PostModelTransactionHandleHook {
16+
this.callbacks.push(callback);
17+
return this;
18+
}
19+
}

packages/octo/src/services/transaction/transaction.service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { Container } from '../../decorators/container.js';
1010
import { Factory } from '../../decorators/factory.decorator.js';
1111
import { DiffMetadata } from '../../functions/diff/diff-metadata.js';
1212
import { Diff, DiffAction } from '../../functions/diff/diff.js';
13+
import { PostModelTransactionHandleHook } from '../../functions/hook/post-model-transaction-handle.hook.js';
1314
import { IModelAction } from '../../models/model-action.interface.js';
1415
import { OverlayDataRepository } from '../../overlays/overlay-data.repository.js';
1516
import { IResourceAction } from '../../resources/resource-action.interface.js';
@@ -289,6 +290,9 @@ export class TransactionService {
289290
yield resourceTransaction;
290291
}
291292

293+
// Run post transaction hooks.
294+
await PostModelTransactionHandleHook.apply(modelTransaction);
295+
292296
return modelTransaction;
293297
}
294298

0 commit comments

Comments
 (0)