diff --git a/packages/automerge-repo-network-messagechannel/src/MessagePortRef.ts b/packages/automerge-repo-network-messagechannel/src/MessagePortRef.ts index 3ea3cf09..b6a409f6 100644 --- a/packages/automerge-repo-network-messagechannel/src/MessagePortRef.ts +++ b/packages/automerge-repo-network-messagechannel/src/MessagePortRef.ts @@ -7,6 +7,7 @@ export interface PortRefEvents { export interface MessagePortRef extends EventEmitter { start(): void + stop(): void postMessage(message: any, transferable?: Transferable[]): void isAlive(): boolean } diff --git a/packages/automerge-repo-network-messagechannel/src/StrongMessagePortRef.ts b/packages/automerge-repo-network-messagechannel/src/StrongMessagePortRef.ts index 87f7524d..5d28779c 100644 --- a/packages/automerge-repo-network-messagechannel/src/StrongMessagePortRef.ts +++ b/packages/automerge-repo-network-messagechannel/src/StrongMessagePortRef.ts @@ -5,22 +5,33 @@ export class StrongMessagePortRef extends EventEmitter implements MessagePortRef { + isDisconnected: boolean = false + constructor(private port: MessagePort) { port.addEventListener("message", event => { - this.emit("message", event) + if (!this.isDisconnected) { + this.emit("message", event) + } }) super() } postMessage(message: any, transfer: Transferable[]): void { - this.port.postMessage(message, transfer) + if (!this.isDisconnected) { + this.port.postMessage(message, transfer) + } } start(): void { + this.isDisconnected = false this.port.start() } + stop() { + this.isDisconnected = true + } + isAlive(): boolean { /* c8 ignore next */ return true diff --git a/packages/automerge-repo-network-messagechannel/src/WeakMessagePortRef.ts b/packages/automerge-repo-network-messagechannel/src/WeakMessagePortRef.ts index f173a630..bc33e3f6 100644 --- a/packages/automerge-repo-network-messagechannel/src/WeakMessagePortRef.ts +++ b/packages/automerge-repo-network-messagechannel/src/WeakMessagePortRef.ts @@ -15,7 +15,9 @@ export class WeakMessagePortRef this.weakRef = new WeakRef(port) port.addEventListener("message", event => { - this.emit("message", event) + if (!this.isDisconnected) { + this.emit("message", event) + } }) } @@ -27,6 +29,10 @@ export class WeakMessagePortRef return } + if (this.isDisconnected) { + return + } + try { port.postMessage(message, transfer) } catch (err) { @@ -42,6 +48,8 @@ export class WeakMessagePortRef return } + this.isDisconnected = false + try { port.start() } catch (err) { @@ -49,6 +57,10 @@ export class WeakMessagePortRef } } + stop() { + this.isDisconnected = true + } + private disconnnect() { if (!this.isDisconnected) { this.emit("close") diff --git a/packages/automerge-repo-network-messagechannel/src/index.ts b/packages/automerge-repo-network-messagechannel/src/index.ts index 4b2b5adc..e3549b19 100644 --- a/packages/automerge-repo-network-messagechannel/src/index.ts +++ b/packages/automerge-repo-network-messagechannel/src/index.ts @@ -62,6 +62,7 @@ export class MessageChannelNetworkAdapter extends NetworkAdapter { log("messageport connecting") this.peerId = peerId this.peerMetadata = peerMetadata + this.messagePortRef.start() this.messagePortRef.addListener( "message", @@ -153,8 +154,7 @@ export class MessageChannelNetworkAdapter extends NetworkAdapter { } disconnect() { - // TODO - throw new Error("Unimplemented: leave on MessagePortNetworkAdapter") + this.messagePortRef.stop() } }