Skip to content

Commit

Permalink
feat: remove *Async
Browse files Browse the repository at this point in the history
  • Loading branch information
lisez committed May 27, 2024
1 parent ac02fbb commit ad48c54
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 58 deletions.
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

0 comments on commit ad48c54

Please sign in to comment.