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

Auto async #19

Merged
merged 7 commits into from
May 27, 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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ emitter.emit("event");
const emitter = new Xevt();

let result = 0;
emitter.onAsync("event", async () => {
emitter.on("event", async () => {
result++;
});
await emitter.emit("event");
Expand Down Expand Up @@ -73,7 +73,7 @@ const result: number[] = [];
emitter.on("event", (data) => {
result.push(data);
});
emitter.onAsync(
emitter.on(
"event",
async (data) =>
new Promise((res) => {
Expand All @@ -97,7 +97,7 @@ const result: number[] = [];
emitter.conjoin(["event1", "event2"], async () => {
result.push(1);
});
emitter.conjoinAsync(["event1", "event2"], async () => {
emitter.conjoin(["event1", "event2"], async () => {
result.push(2);
});

Expand Down
16 changes: 0 additions & 16 deletions modules/conjoin_emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,6 @@ export class ConjoinEmitter extends CoreEmitter<ConjoinEvents>
return this.conjoin;
}

conjoinAsync(
events: ConjoinEvents,
handler: EventHandler,
options?: Partial<EventOptions>,
) {
const signature = {
name: events,
handler,
options: {
once: options?.once || false,
async: true,
},
};
return this.internalConjoinOn(signature);
}

error(handler: ErrorHandler) {
this.errorEmitter.on("error", handler);
}
Expand Down
14 changes: 6 additions & 8 deletions modules/core_emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,12 @@ export abstract class CoreEmitter<T> implements XCoreEmitter<T> {
name: EventName,
signature: EventHandlerSignature<any>,
): EventUnscriber {
if (
signature.options?.async &&
// @ts-ignore TS7053
signature.handler[Symbol.toStringTag] !== "AsyncFunction" &&
!("then" in signature.handler)
) {
throw new Error("Async handler must be a promise or thenable");
}
// @ts-ignore TS7053
const async = signature.handler[Symbol.toStringTag] === "AsyncFunction" ||
("then" in signature.handler);

signature.options ??= {};
signature.options.async = async;

if (this.debug) this.logger.debug("on", name, signature);

Expand Down
16 changes: 0 additions & 16 deletions modules/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,6 @@ export class Emitter extends CoreEmitter<EventName> implements XevtEmitter {
return this.on;
}

onAsync(
event: EventName,
handler: EventHandler,
options?: Partial<EventOptions>,
) {
const signature = {
name: event,
handler,
options: {
once: options?.once || false,
async: true,
},
};
return this.onBySignature(event, signature);
}

error(handler: ErrorHandler) {
this.on("error", handler);
}
Expand Down
6 changes: 2 additions & 4 deletions modules/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ export type XevtEmitter =
& XCoreEmitter<EventName>
& Record<
| "addEventListener"
| Uncapitalize<EventRegisterName>
| Uncapitalize<`${EventRegisterName}Async`>,
| Uncapitalize<EventRegisterName>,
EventRegister
>;

Expand All @@ -78,8 +77,7 @@ export type XConjoinEmitter =
& Record<
| "addEventListener"
| "on"
| Uncapitalize<ConjoinEventsRegisterName>
| Uncapitalize<`${ConjoinEventsRegisterName}Async`>,
| Uncapitalize<ConjoinEventsRegisterName>,
ConjoinEventsRegister
>;

Expand Down
8 changes: 0 additions & 8 deletions modules/xevt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,10 @@ export class Xevt extends CoreEmitter<XeventName>
return this.on;
}

get onAsync() {
return this.emitter.onAsync.bind(this.emitter);
}

get conjoin() {
return this.conjoinEmitter.conjoin.bind(this.conjoinEmitter);
}

get conjoinAsync() {
return this.conjoinEmitter.conjoinAsync.bind(this.conjoinEmitter);
}

error(handler: ErrorHandler) {
this.emitter.error(handler);
this.conjoinEmitter.error(handler);
Expand Down
8 changes: 4 additions & 4 deletions tests/deno/multiple_events_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ describe("Xevt - multiple events", () => {
it("should listen multiple async handlers", async () => {
const emitter = new Xevt();
const result: number[] = [];
emitter.conjoinAsync(["event1", "event2"], async () => {
emitter.conjoin(["event1", "event2"], async () => {
result.push(1);
});
emitter.conjoinAsync(["event1", "event2"], async () => {
emitter.conjoin(["event1", "event2"], async () => {
result.push(2);
});
emitter.emit("event1");
Expand Down Expand Up @@ -132,7 +132,7 @@ describe("Xevt - multiple events", () => {
it("should take every async events", async () => {
const emitter = new Xevt();
let result: number = 0;
emitter.conjoinAsync(["event1", "event2"], async () => {
emitter.conjoin(["event1", "event2"], async () => {
await new Promise((resolve) =>
setTimeout(() => {
result++;
Expand All @@ -158,7 +158,7 @@ describe("Xevt - multiple events", () => {
emitter.conjoin(["event1", "event2"], () => {
result.push(1);
});
emitter.conjoinAsync(["event1", "event2"], async () => {
emitter.conjoin(["event1", "event2"], async () => {
result.push(2);
});

Expand Down
4 changes: 2 additions & 2 deletions tests/deno/single_event_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe("Xevt - single event", () => {
it("should listen every async events", async () => {
const emitter = new Xevt();
const result: number[] = [];
emitter.onAsync("event", async (arg) => {
emitter.on("event", async (arg) => {
await new Promise((resolve) =>
setTimeout(() => {
result.push(arg);
Expand All @@ -122,7 +122,7 @@ describe("Xevt - single event", () => {
emitter.on("event", (data) => {
result.push(data);
});
emitter.onAsync(
emitter.on(
"event",
async (data) =>
new Promise((res) => {
Expand Down
Loading