From 3da526bb0225307fc07a89a5c8b3b646b51a6499 Mon Sep 17 00:00:00 2001 From: Wroud Date: Sun, 30 Jan 2022 15:30:02 +0300 Subject: [PATCH] feat(core-executor): CB-1579 initial data getter --- webapp/packages/core-executor/src/Executor.ts | 24 +++++++++++++++++-- .../src/ExecutorHandlersCollection.ts | 16 ++++++++++++- .../src/IExecutorHandlersCollection.ts | 2 ++ .../core-executor/src/SyncExecutor.ts | 17 ++++++++++++- 4 files changed, 55 insertions(+), 4 deletions(-) diff --git a/webapp/packages/core-executor/src/Executor.ts b/webapp/packages/core-executor/src/Executor.ts index d6ab17b149..7ee9c1bd4c 100644 --- a/webapp/packages/core-executor/src/Executor.ts +++ b/webapp/packages/core-executor/src/Executor.ts @@ -22,10 +22,10 @@ export class Executor extends ExecutorHandlersCollection implements return this.scheduler.executing; } - private scheduler: TaskScheduler; + private readonly scheduler: TaskScheduler; constructor( - private defaultData: T | null = null, + private readonly defaultData: T | null = null, isBlocked: BlockedExecution | null = null ) { super(); @@ -144,4 +144,24 @@ export class Executor extends ExecutorHandlersCollection implements } return data; } + + protected executeHandlerWithInitialData(handler: IExecutorHandler) { + if (!this.initialDataGetter) { + return; + } + + const data = this.initialDataGetter(); + + this.scheduler.schedule(data, async () => { + const context = new ExecutionContext(data); + + try { + await handler(data, context); + } finally { + await this.executeHandlers(data, context, this.postHandlers); + } + + return context; + }); + } } diff --git a/webapp/packages/core-executor/src/ExecutorHandlersCollection.ts b/webapp/packages/core-executor/src/ExecutorHandlersCollection.ts index 92e8ec548e..9b18d601a3 100644 --- a/webapp/packages/core-executor/src/ExecutorHandlersCollection.ts +++ b/webapp/packages/core-executor/src/ExecutorHandlersCollection.ts @@ -15,11 +15,18 @@ implements IExecutorHandlersCollection { postHandlers: Array> = []; chain: Array> = []; readonly collections: Array>; - private links: Map, IExecutorHandlersCollection>; + protected initialDataGetter: (() => T) | null; + private readonly links: Map, IExecutorHandlersCollection>; constructor() { this.links = new Map(); this.collections = []; + this.initialDataGetter = null; + } + + setInitialDataGetter(getter: (() => T) | null): this { + this.initialDataGetter = getter; + return this; } addCollection(collection: IExecutorHandlersCollection): this { @@ -65,6 +72,7 @@ implements IExecutorHandlersCollection { addHandler(handler: IExecutorHandler): this { this.handlers.push(handler); + this.executeHandlerWithInitialData(handler); return this; } @@ -80,4 +88,10 @@ implements IExecutorHandlersCollection { removePostHandler(handler: IExecutorHandler): void { this.postHandlers = this.postHandlers.filter(h => h !== handler); } + + protected executeHandlerWithInitialData(handler: IExecutorHandler) { + if (!this.initialDataGetter) { + return; + } + } } diff --git a/webapp/packages/core-executor/src/IExecutorHandlersCollection.ts b/webapp/packages/core-executor/src/IExecutorHandlersCollection.ts index fa91e2511a..552478ed0b 100644 --- a/webapp/packages/core-executor/src/IExecutorHandlersCollection.ts +++ b/webapp/packages/core-executor/src/IExecutorHandlersCollection.ts @@ -24,6 +24,8 @@ export interface IExecutorHandlersCollection>; readonly collections: Array>; + setInitialDataGetter(getter: () => T): this; + before: (executor: IExecutorHandlersCollection, map?: ExecutorDataMap) => this; next: (executor: IExecutorHandlersCollection, map?: ExecutorDataMap) => this; addCollection: (collection: IExecutorHandlersCollection) => this; diff --git a/webapp/packages/core-executor/src/SyncExecutor.ts b/webapp/packages/core-executor/src/SyncExecutor.ts index 53642b10a7..2c39601767 100644 --- a/webapp/packages/core-executor/src/SyncExecutor.ts +++ b/webapp/packages/core-executor/src/SyncExecutor.ts @@ -18,7 +18,7 @@ import type { ISyncExecutor } from './ISyncExecutor'; export class SyncExecutor extends ExecutorHandlersCollection implements ISyncExecutor { constructor( - private defaultData: T | null = null + private readonly defaultData: T | null = null ) { super(); } @@ -131,4 +131,19 @@ export class SyncExecutor extends ExecutorHandlersCollection implem } return data; } + + protected executeHandlerWithInitialData(handler: IExecutorHandler) { + if (!this.initialDataGetter) { + return; + } + + const data = this.initialDataGetter(); + const context = new ExecutionContext(data); + + try { + handler(data, context); + } finally { + this.executeHandlers(data, context, this.postHandlers); + } + } }