Skip to content

Commit

Permalink
feat: .onmessage, .onmessageerror
Browse files Browse the repository at this point in the history
  • Loading branch information
hugojosefson committed Jun 26, 2023
1 parent d0d997c commit 4f221e5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { WebSocketBroadcastChannel } from "./web-socket-broadcast-channel.ts";
*/
export interface BroadcastChannelIsh extends EventTarget {
readonly name: string;
onmessage: ((ev: MessageEvent) => void) | null;
onmessageerror: ((ev: Event) => void) | null;
postMessage(message: unknown): void;
close(): void;
}
Expand Down
10 changes: 9 additions & 1 deletion src/web-socket-broadcast-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,23 @@ const log0: Logger = logger(import.meta.url);
*/
export class WebSocketBroadcastChannel extends EventTarget
implements BroadcastChannelIsh {
onmessage: ((ev: Event) => void) | null = null;
onmessageerror: ((ev: Event) => void) | null = null;
private readonly log: Logger = log0.sub(WebSocketBroadcastChannel.name);
private closed = false;
public readonly name: string;
readonly url: URL;
constructor(name: string, url: URL = new URL(DEFAULT_WEBSOCKET_URL)) {
super();
this.log.sub("constructor")(`name: ${s(name)}`);
this.name = name;
this.url = url;
this.log.sub("constructor")(`name: ${s(name)}`);
this.addEventListener("message", (e: Event) => this.onmessage?.(e));
this.addEventListener(
"messageerror",
(e: Event) => this.onmessageerror?.(e),
);

registerChannel(this);
}
postMessage(message: string): void {
Expand Down

0 comments on commit 4f221e5

Please sign in to comment.