Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

24 use non block asnyc to default #26

Merged
merged 14 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,16 @@ await emitter.emit("event");

### Conditional event handlers.

IMPORTANT: conditional handlers not supported to conjoined event.
IMPORTANT:

- NOT supported in conjoined events.
- NOT supported any arguments in handlers.
- It will be executed after the triggered event finished. (Blocking mode.)

```typescript
const emitter = new Xevt();
const result: any[] = [];
emitter.on("event", (arg: number) => {
emitter.on("event", async (arg: number) => {
result.push(arg);
return arg % 2 === 0;
});
Expand Down
8 changes: 4 additions & 4 deletions modules/runners/dual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ export class DualRunner<N = any> {
* Create a new instance of the DualRunner.
* @param handlers The dual handler profile.
*/
constructor(
private handlers: DualEventHandlerSignature<N>[],
) {
constructor(private handlers: DualEventHandlerSignature<N>[]) {
this.handlers = handlers;
}

Expand Down Expand Up @@ -54,6 +52,8 @@ export class DualRunner<N = any> {
* @param args The arguments to pass to the dual handler.
*/
exec(result: any) {
return new RelayRunner().exec(result, (p) => this.dualExec(p));
return new RelayRunner().exec(result, (p) => this.dualExec(p), {
async: true,
});
}
}
45 changes: 24 additions & 21 deletions modules/runners/step.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ export class StepRunner {
const result = new SingleRunner(handler).exec(args);

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

Expand All @@ -72,8 +71,9 @@ export class StepRunner {
for (const p of handlers.filter((e) => !!e.options?.once)) {
this.remove(step, p);
}
return new SequenceRunner(handlers as GeneralEventHandlerSignature<any>[])
.exec(args);
return new SequenceRunner(
handlers as GeneralEventHandlerSignature<any>[],
).exec(args);
}

/**
Expand All @@ -84,21 +84,24 @@ export class StepRunner {
handlers: EventHandlerSignature<any>[],
args?: any[],
) {
const categories = handlers.reduce((y, x) => {
if (x.options?.once) {
y.once.push(x);
}
if (helpers.isDualSignature(x)) {
y.duals.push(x);
} else {
y.handlers.push(x);
}
return y;
}, {
handlers: [] as GeneralEventHandlerSignature<any>[],
duals: [] as DualEventHandlerSignature<any>[],
once: [] as EventHandlerSignature<any>[],
});
const categories = handlers.reduce(
(y, x) => {
if (x.options?.once) {
y.once.push(x);
}
if (helpers.isDualSignature(x)) {
y.duals.push(x);
} else {
y.handlers.push(x);
}
return y;
},
{
handlers: [] as GeneralEventHandlerSignature<any>[],
duals: [] as DualEventHandlerSignature<any>[],
once: [] as EventHandlerSignature<any>[],
},
);
if (!categories.handlers.length) return;

for (const p of categories.once) {
Expand Down
72 changes: 64 additions & 8 deletions tests/deno/single_event_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ describe("Xevt - unscriber", () => {
});
});

describe("Xevt - on", () => {
describe("Xevt - conditional event handlers", () => {
it('should listen event with "on"', () => {
const emitter = new Xevt();
const result: number[] = [];
Expand All @@ -206,6 +206,58 @@ describe("Xevt - on", () => {
assertEquals(result, [1, 99, 2, 100]);
});

it("should executed after async function", async () => {
const emitter = new Xevt();
const result: number[] = [];
// deno-lint-ignore require-await
emitter.on("event", async (arg: number) => {
result.push(arg);
return arg % 2 === 0;
});

emitter.on("event", {
true: () => {
result.push(100);
},
false: () => {
result.push(99);
},
});

emitter.emit("event", 1);
emitter.emit("event", 2);
await delay(0);
assertEquals(result, [1, 2, 99, 100]);
});

it("should executed after async function - blocking", async () => {
const emitter = new Xevt();
const result: number[] = [];
emitter.on(
"event",
// deno-lint-ignore require-await
async (arg: number) => {
result.push(arg);
return arg % 2 === 0;
},
{ async: true },
);

emitter.on("event", {
true: () => {
result.push(100);
},
false: () => {
result.push(99);
},
});

emitter.emit("event", 1);
emitter.emit("event", 2);
await delay(0);
assertEquals(result, [1, 99, 2, 100]);
});

it("should listen multiple on events", () => {
const emitter = new Xevt();
const result: any[] = [];
Expand Down Expand Up @@ -260,14 +312,18 @@ describe("Xevt - on", () => {
},
});

emitter.on("event", {
true: () => {
result.push(100);
},
false: () => {
result.push(99);
emitter.on(
"event",
{
true: () => {
result.push(100);
},
false: () => {
result.push(99);
},
},
}, { once: true });
{ once: true },
);

emitter.emit("event", 1);
emitter.emit("event", 2);
Expand Down
Loading