Skip to content

Commit

Permalink
simplify client ids (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
zephraph authored Feb 18, 2025
1 parent 5e60297 commit 9f72aa1
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 120 deletions.
9 changes: 6 additions & 3 deletions schemas/WebViewMessage.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 28 additions & 14 deletions schemas/WebViewRequest.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions schemas/WebViewResponse.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/clients/deno/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"tracing": "jsr:@bcheidemann/tracing@^0.6.3",
"jsr:@std/fs": "jsr:@std/fs@^1.0.3",
"jsr:@std/path": "jsr:@std/path@^1.0.6",
"jsr:@std/ulid": "jsr:@std/ulid@^1.0.0",
"npm:zod": "npm:zod@^3.23.8",
"npm:type-fest": "npm:type-fest@^4.26.1"
}
Expand Down
5 changes: 0 additions & 5 deletions src/clients/deno/deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/clients/deno/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
type Request as WebViewRequest,
Response as WebViewResponse,
} from "./schemas.ts";
import { monotonicUlid as ulid } from "jsr:@std/ulid";
import type { Except, Simplify } from "npm:type-fest";
import { join } from "jsr:@std/path";
import { ensureDir, exists } from "jsr:@std/fs";
Expand Down Expand Up @@ -200,6 +199,7 @@ export class WebView implements Disposable {
#externalEvent = new EventEmitter();
#messageLoop: Promise<void>;
#options: Options;
#messageId = 0;

/**
* Creates a new webview window.
Expand All @@ -221,10 +221,10 @@ export class WebView implements Disposable {
}

#send(request: Except<WebViewRequest, "id">): Promise<WebViewResponse> {
const id = ulid();
const id = this.#messageId++;
return new Promise((resolve) => {
// Setup listener before sending the message to avoid race conditions
this.#internalEvent.once(id, (event) => {
this.#internalEvent.once(id.toString(), (event) => {
const result = WebViewResponse.safeParse(event);
if (result.success) {
resolve(result.data);
Expand Down Expand Up @@ -288,7 +288,7 @@ export class WebView implements Disposable {
}
})
.with({ $type: "response" }, ({ data }) => {
this.#internalEvent.emit(data.id, data);
this.#internalEvent.emit(data.id.toString(), data);
})
.exhaustive();
}
Expand Down
Loading

0 comments on commit 9f72aa1

Please sign in to comment.