Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
lisez committed May 28, 2024
1 parent e50ba3c commit 6b0214d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 29 deletions.
6 changes: 2 additions & 4 deletions modules/conjoin_emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { CoreEmitter } from "modules/core_emitter.ts";
import { Emitter } from "modules/emitter.ts";
import { SeriesRunner } from "modules/runners/series.ts";
import { ConjoinQueue } from "modules/conjoin_queue.ts";
import { RelayRunner } from "modules/runners/relay.ts";
import * as helpers from "modules/helpers.ts";

export class ConjoinEmitter extends CoreEmitter<ConjoinEvents>
Expand Down Expand Up @@ -136,10 +137,7 @@ export class ConjoinEmitter extends CoreEmitter<ConjoinEvents>
return this.prevEvents;
};

if (this.prevEvents instanceof Promise) {
return Promise.resolve(this.prevEvents).then(next);
}
return next();
return new RelayRunner().exec(this.prevEvents, next, { async: true });
}

off(event: ConjoinEvents, handler?: EventHandler): void {
Expand Down
6 changes: 2 additions & 4 deletions modules/runners/dual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
} from "modules/types.ts";

import { SequenceRunner } from "modules/runners/sequence.ts";
import { RelayRunner } from "modules/runners/relay.ts";

/**
* Run a dual event handler.
Expand Down Expand Up @@ -53,9 +54,6 @@ export class DualRunner<N = any> {
* @param args The arguments to pass to the dual handler.
*/
exec(result: any) {
if (result instanceof Promise) {
return Promise.resolve(result).then((res) => this.dualExec(res));
}
return this.dualExec(result);
return new RelayRunner().exec(result, (p) => this.dualExec(p));
}
}
30 changes: 30 additions & 0 deletions modules/runners/relay.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { EventHandler } from "modules/types.ts";

/** Options to RelayRunner */
export type RelayRunnerOptions = {
/**
* If the prev handler is a promise, it will await for it before executing the next handler.
* @default false
*/
async: boolean;
};

/** Handle how to run before and after handler. */
export class RelayRunner {
/**
* Execute the handler.
* @param prev The result of the previous handler.
* @param next The next handler.
* @param options The options to run the next handler.
*/
exec<T extends EventHandler>(
prev: any,
next: T,
options?: Partial<RelayRunnerOptions>,
): Promise<ReturnType<T>> | ReturnType<T> {
if (prev instanceof Promise) {
return Promise.resolve(prev).then((res) => next(res));
}
return next(prev);
}
}
14 changes: 6 additions & 8 deletions modules/runners/sequence.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { GeneralEventHandlerSignature } from "modules/types.ts";

import { SingleRunner } from "modules/runners/single.ts";
import { RelayRunner } from "modules/runners/relay.ts";

/**
* Run handlers in sequence.
Expand All @@ -26,18 +27,15 @@ export class SequenceRunner<
exec(
args: Parameters<N["handler"]>,
index: number = 0,
): void | Promise<void> {
): ReturnType<N["handler"]> | Promise<ReturnType<N["handler"]>> | void {
const profile = this.handlers[index];
if (!profile) return;

const result = new SingleRunner<N>(profile).exec(args) as any;

/**
* Wait for the handler to finish before moving to the next handler.
*/
if (profile.options?.async || result instanceof Promise) {
return Promise.resolve(result).then(() => this.exec(args, index + 1));
}
return this.exec(args, index + 1);
return new RelayRunner().exec(
result,
() => this.exec(args, index + 1),
) as any;
}
}
9 changes: 5 additions & 4 deletions modules/runners/series.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { EventName, RegisteredHandlers } from "modules/types.ts";

import { StepRunner } from "modules/runners/step.ts";
import { RelayRunner } from "modules/runners/relay.ts";

/**
* Run handlers each in series.
Expand All @@ -24,9 +25,9 @@ export class SeriesRunner {
if (!key) return;

const step = new StepRunner(this.handlers).exec(key);
if (step instanceof Promise) {
return Promise.resolve(step).then(() => this.exec(series, idx + 1));
}
return this.exec(series, idx + 1);
return new RelayRunner().exec(
step,
() => this.exec(series, idx + 1),
) as any;
}
}
15 changes: 6 additions & 9 deletions modules/runners/step.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
import { DualRunner } from "modules/runners/dual.ts";
import { SingleRunner } from "modules/runners/single.ts";
import { SequenceRunner } from "modules/runners/sequence.ts";
import { RelayRunner } from "modules/runners/relay.ts";
import * as helpers from "modules/helpers.ts";

/**
Expand Down Expand Up @@ -51,17 +52,13 @@ export class StepRunner {

const next = (result: any) => {
const dualResult = new DualRunner(duals).exec(!!result);
if (dualResult instanceof Promise) {
return dualResult.then(() =>
this.execByIndex(handlers, duals, args, idx + 1)
);
}
return new RelayRunner().exec(
dualResult,
() => this.execByIndex(handlers, duals, args, idx + 1),
);
};

if (handler.options?.async) {
return Promise.resolve(result).then(() => next(result));
}
return next(result);
return new RelayRunner().exec(result, next) as any;
}

/**
Expand Down

0 comments on commit 6b0214d

Please sign in to comment.