Skip to content

Commit

Permalink
Merge branch 'main' of github.com:nakasyou/line.js into refactor/pack…
Browse files Browse the repository at this point in the history
…ages
  • Loading branch information
nakasyou committed Sep 14, 2024
2 parents b5ed701 + 51c5a4a commit 1a16694
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"tweetnacl": "npm:tweetnacl@^1.0.3"
},
"tasks": {
"format": "deno fmt & deno run -A npm:@biomejs/biome format --write ./packages/**/*.ts",
"format": "deno run -A npm:@biomejs/biome format --write ./packages/**/*.ts",
"lint": "deno run -A npm:@biomejs/biome lint ./packages/**/*.ts",
"check": "deno run -A npm:@biomejs/biome check ./packages/**/*.ts"
}
Expand Down
18 changes: 18 additions & 0 deletions packages/linejs/client/libs/typed-event-emitter/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { assertEquals } from "@std/assert";
import { TypedEventEmitter } from "./index.ts";

Deno.test("promise() should be vaild", async () => {
type Events = {
example: (v: number) => void;
};
class Client extends TypedEventEmitter<Events> {}
const client = new Client();

const promise = client.waitFor("example");

client.emit("example", 123456);

const [v] = await promise;

assertEquals(v, 123456);
});
16 changes: 16 additions & 0 deletions packages/linejs/client/libs/typed-event-emitter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,20 @@ export class TypedEventEmitter<

return this;
}

/**
* This creates a promise that you can use for a single event.
* @param event A event name
*/
public waitFor<E2 extends E, P = Parameters<T[E2]>>(
event: E2,
): Promise<P> {
return new Promise<P>((resolve) => {
const listener = ((...args) => {
this.off(event, listener);
resolve(args as P);
}) as T[E2];
this.on(event, listener);
});
}
}

0 comments on commit 1a16694

Please sign in to comment.