feat(core-executor): CB-1579 initial data getter

This commit is contained in:
Wroud
2022-01-30 15:30:02 +03:00
parent 43720a1a7d
commit 3da526bb02
4 changed files with 55 additions and 4 deletions
+22 -2
View File
@@ -22,10 +22,10 @@ export class Executor<T = void> extends ExecutorHandlersCollection<T> implements
return this.scheduler.executing;
}
private scheduler: TaskScheduler<T>;
private readonly scheduler: TaskScheduler<T>;
constructor(
private defaultData: T | null = null,
private readonly defaultData: T | null = null,
isBlocked: BlockedExecution<T> | null = null
) {
super();
@@ -144,4 +144,24 @@ export class Executor<T = void> extends ExecutorHandlersCollection<T> implements
}
return data;
}
protected executeHandlerWithInitialData(handler: IExecutorHandler<T>) {
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;
});
}
}
@@ -15,11 +15,18 @@ implements IExecutorHandlersCollection<T, TResult> {
postHandlers: Array<IExecutorHandler<T, TResult>> = [];
chain: Array<IChainLink<T, TResult>> = [];
readonly collections: Array<IExecutorHandlersCollection<T, TResult>>;
private links: Map<IExecutorHandlersCollection<any, TResult>, IExecutorHandlersCollection<T, TResult>>;
protected initialDataGetter: (() => T) | null;
private readonly links: Map<IExecutorHandlersCollection<any, TResult>, IExecutorHandlersCollection<T, TResult>>;
constructor() {
this.links = new Map();
this.collections = [];
this.initialDataGetter = null;
}
setInitialDataGetter(getter: (() => T) | null): this {
this.initialDataGetter = getter;
return this;
}
addCollection(collection: IExecutorHandlersCollection<T, TResult>): this {
@@ -65,6 +72,7 @@ implements IExecutorHandlersCollection<T, TResult> {
addHandler(handler: IExecutorHandler<T, TResult>): this {
this.handlers.push(handler);
this.executeHandlerWithInitialData(handler);
return this;
}
@@ -80,4 +88,10 @@ implements IExecutorHandlersCollection<T, TResult> {
removePostHandler(handler: IExecutorHandler<T, TResult>): void {
this.postHandlers = this.postHandlers.filter(h => h !== handler);
}
protected executeHandlerWithInitialData(handler: IExecutorHandler<T, TResult>) {
if (!this.initialDataGetter) {
return;
}
}
}
@@ -24,6 +24,8 @@ export interface IExecutorHandlersCollection<T = unknown, TResult = any | Promis
readonly chain: Array<IChainLink<T, TResult>>;
readonly collections: Array<IExecutorHandlersCollection<T, TResult>>;
setInitialDataGetter(getter: () => T): this;
before: <TNext>(executor: IExecutorHandlersCollection<TNext, TResult>, map?: ExecutorDataMap<T, TNext>) => this;
next: <TNext>(executor: IExecutorHandlersCollection<TNext, TResult>, map?: ExecutorDataMap<T, TNext>) => this;
addCollection: (collection: IExecutorHandlersCollection<T, TResult>) => this;
@@ -18,7 +18,7 @@ import type { ISyncExecutor } from './ISyncExecutor';
export class SyncExecutor<T = void> extends ExecutorHandlersCollection<T> implements ISyncExecutor<T> {
constructor(
private defaultData: T | null = null
private readonly defaultData: T | null = null
) {
super();
}
@@ -131,4 +131,19 @@ export class SyncExecutor<T = void> extends ExecutorHandlersCollection<T> implem
}
return data;
}
protected executeHandlerWithInitialData(handler: IExecutorHandler<T>) {
if (!this.initialDataGetter) {
return;
}
const data = this.initialDataGetter();
const context = new ExecutionContext(data);
try {
handler(data, context);
} finally {
this.executeHandlers(data, context, this.postHandlers);
}
}
}