Skip to content

Commit

Permalink
Add closed reason
Browse files Browse the repository at this point in the history
Signed-off-by: Marcos Candeia <[email protected]>
  • Loading branch information
mcandeia committed Aug 14, 2024
1 parent 24ba198 commit c30514a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
12 changes: 7 additions & 5 deletions client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface ConnectOptions {
* @property {Promise<void>} registered - A promise that resolves when the connection is registered.
*/
export interface Connected {
closed: Promise<void>;
closed: Promise<Error | undefined>;
registered: Promise<void>;
}

Expand All @@ -41,7 +41,7 @@ export interface Connected {
export const connectMainThread = async (
opts: ConnectOptions,
): Promise<Connected> => {
const closed = Promise.withResolvers<void>();
const closed = Promise.withResolvers<Error | undefined>();
const registered = Promise.withResolvers<void>();
const client = typeof Deno.createHttpClient === "function"
? Deno.createHttpClient({
Expand All @@ -66,6 +66,7 @@ export const connectMainThread = async (
const wsSockets: Record<string, WebSocket> = {};

(async () => {
let reason: undefined | Error;
const state: ClientState = {
client,
localAddr: opts.localAddr,
Expand All @@ -82,9 +83,10 @@ export const connectMainThread = async (
}
}
} catch (err) {
reason = err;
console.error(new Date(), "error handling message", err);
} finally {
closed.resolve();
closed.resolve(reason);
}
})();
return { closed: closed.promise, registered: registered.promise };
Expand All @@ -96,15 +98,15 @@ export const connectMainThread = async (
* @returns {Promise<Connected>} A promise that resolves with the connection status.
*/
export const connectSW = (opts: ConnectOptions): Promise<Connected> => {
const closed = Promise.withResolvers<void>();
const closed = Promise.withResolvers<Error | undefined>();
const registered = Promise.withResolvers<void>();
const worker = new Worker(import.meta.url, {
type: "module",
deno: { permissions: "inherit" },
});
worker.addEventListener("message", (message) => {
if (message.data === "closed") {
closed.resolve();
closed.resolve(undefined);
}
if (message.data === "registered") {
registered.resolve();
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@deco/warp",
"version": "0.3.3",
"version": "0.3.4",
"exports": "./mod.ts",
"tasks": {
"check": "deno fmt && deno lint && deno check mod.ts"
Expand Down

0 comments on commit c30514a

Please sign in to comment.