Skip to content

Commit

Permalink
FIX: errors in connecting and returning docs over network
Browse files Browse the repository at this point in the history
  • Loading branch information
philcockfield committed Sep 12, 2024
1 parent 26d44a4 commit 39ccf07
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 19 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ This project adheres to [Semantic Versioning](http://semver.org/).
#### Fixed
#### Security


## [1.2.3] - 2024-09-12
#### Fixed
- Problems with not returning remote documents over network after the "disconnect()" set of
that were introduced in prior version `1.2.2`



## [1.2.2] - 2024-08-27
#### Changed
- Updates to Network interface (via @pvg, see [notes](https://patchwork.inkandswitch.com/#automerge-repo-network-adapter-api-changes--4RicZ28GjFaTi12xssztkpDjqruu?type=essay))
- Updates to Network interface (via @pvh, see [notes](https://patchwork.inkandswitch.com/#automerge-repo-network-adapter-api-changes--4RicZ28GjFaTi12xssztkpDjqruu?type=essay))

- Removed emitting "ready" event.
- Implemented `isReady` and `whenReady`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "automerge-repo-network-peerjs",
"version": "1.2.2",
"version": "1.2.3",
"description": "Network adapter for automerge-repo using peerjs",
"author": "phil Cockfield",
"license": "MIT",
Expand Down
46 changes: 31 additions & 15 deletions src/NetworkAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ export class PeerjsNetworkAdapter
#ready = false;
#readyResolver?: () => void;
#readyPromise: Promise<void> = new Promise<void>((resolve) => (this.#readyResolver = resolve));
#forceReady() {
if (this.#ready) return;
this.#ready = true;
this.#readyResolver?.();
isReady() {
return this.#ready;
}
whenReady() {
return this.#readyPromise;
}

constructor(conn: t.DataConnection) {
Expand All @@ -36,14 +37,6 @@ export class PeerjsNetworkAdapter
this.#conn = conn;
}

isReady() {
return this.#ready;
}

whenReady() {
return this.#readyPromise;
}

connect(peerId: t.PeerId, meta?: t.PeerMetadata) {
const senderId = (this.peerId = peerId);
const conn = this.#conn;
Expand Down Expand Up @@ -74,6 +67,15 @@ export class PeerjsNetworkAdapter
return;
}

/**
* Leave.
*/
if (msg.type === "leave") {
if (this.peerId === msg.senderId) {
this.emit("peer-disconnected", { peerId });
}
}

/**
* Default (data payload).
*/
Expand All @@ -99,12 +101,15 @@ export class PeerjsNetworkAdapter
* must be something weird going on at the other end to cause us
* to receive no response.
*/
setTimeout(() => this.#forceReady(), 100);
setTimeout(() => this.#setAsReady(), 100);
}

disconnect() {
const peerId = this.peerId;
if (peerId) this.emit("peer-disconnected", { peerId });
if (peerId) {
this.#transmit({ type: "leave", senderId: peerId });
this.emit("peer-disconnected", { peerId });
}
}

onData(fn: (e: t.NetworkMessageAlert) => void) {
Expand All @@ -121,6 +126,10 @@ export class PeerjsNetworkAdapter
}
}

/**
* ↓ Internal Methods ↓
*/

#transmit(message: t.NetworkMessage) {
if (!this.#conn) throw new Error("Connection not ready");
this.#conn.send(message);
Expand All @@ -133,8 +142,15 @@ export class PeerjsNetworkAdapter
this.#events.emit("data", payload);
}

#setAsReady() {
if (this.#ready) return;
this.#ready = true;
this.emit("ready", { network: this });
this.#readyResolver?.();
}

#announceConnection(peerId: t.PeerId, peerMetadata: t.PeerMetadata) {
this.#forceReady();
this.#setAsReady();
this.emit("peer-candidate", { peerId, peerMetadata });
}
}
Expand Down
14 changes: 12 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ export type { DataConnection } from "peerjs";
* MessageChannelNetworkAdapter
* https://github.com/automerge/automerge-repo/blob/main/packages/automerge-repo-network-messagechannel/src/index.ts
*/
export type { PeerjsNetworkAdapter } from "./NetworkAdapter.js";
export type { PeerjsNetworkAdapter } from "./NetworkAdapter.ts";

export type IODirection = "incoming" | "outgoing";
export type NetworkMessage = ArriveMessage | WelcomeMessage | Message;
export type NetworkMessage = ArriveMessage | WelcomeMessage | Message | LeaveMessage;
export type NetworkMessageAlert = {
direction: IODirection;
message: NetworkMessage;
Expand Down Expand Up @@ -80,3 +80,13 @@ export type WelcomeMessage = {
/** The peer metadata of the sender of this message */
peerMetadata: PeerMetadata;
};

/**
* Notify the network that the peer is leaving.
*/
export type LeaveMessage = {
type: "leave";

/** The peer ID of the sender of this message */
senderId: PeerId;
};

0 comments on commit 39ccf07

Please sign in to comment.