mirror of
https://github.com/dbeaver/cloudbeaver.git
synced 2026-09-24 16:04:36 +08:00
feat(core-executor): CB-1579 initial data getter
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user