Skip to content

Commit 39ccf07

Browse files
committed
FIX: errors in connecting and returning docs over network
1 parent 26d44a4 commit 39ccf07

File tree

4 files changed

+53
-19
lines changed

4 files changed

+53
-19
lines changed

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,17 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1010
#### Fixed
1111
#### Security
1212

13+
14+
## [1.2.3] - 2024-09-12
15+
#### Fixed
16+
- Problems with not returning remote documents over network after the "disconnect()" set of
17+
that were introduced in prior version `1.2.2`
18+
19+
20+
1321
## [1.2.2] - 2024-08-27
1422
#### Changed
15-
- Updates to Network interface (via @pvg, see [notes](https://patchwork.inkandswitch.com/#automerge-repo-network-adapter-api-changes--4RicZ28GjFaTi12xssztkpDjqruu?type=essay))
23+
- Updates to Network interface (via @pvh, see [notes](https://patchwork.inkandswitch.com/#automerge-repo-network-adapter-api-changes--4RicZ28GjFaTi12xssztkpDjqruu?type=essay))
1624

1725
- Removed emitting "ready" event.
1826
- Implemented `isReady` and `whenReady`

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "automerge-repo-network-peerjs",
3-
"version": "1.2.2",
3+
"version": "1.2.3",
44
"description": "Network adapter for automerge-repo using peerjs",
55
"author": "phil Cockfield",
66
"license": "MIT",

src/NetworkAdapter.ts

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ export class PeerjsNetworkAdapter
2424
#ready = false;
2525
#readyResolver?: () => void;
2626
#readyPromise: Promise<void> = new Promise<void>((resolve) => (this.#readyResolver = resolve));
27-
#forceReady() {
28-
if (this.#ready) return;
29-
this.#ready = true;
30-
this.#readyResolver?.();
27+
isReady() {
28+
return this.#ready;
29+
}
30+
whenReady() {
31+
return this.#readyPromise;
3132
}
3233

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

39-
isReady() {
40-
return this.#ready;
41-
}
42-
43-
whenReady() {
44-
return this.#readyPromise;
45-
}
46-
4740
connect(peerId: t.PeerId, meta?: t.PeerMetadata) {
4841
const senderId = (this.peerId = peerId);
4942
const conn = this.#conn;
@@ -74,6 +67,15 @@ export class PeerjsNetworkAdapter
7467
return;
7568
}
7669

70+
/**
71+
* Leave.
72+
*/
73+
if (msg.type === "leave") {
74+
if (this.peerId === msg.senderId) {
75+
this.emit("peer-disconnected", { peerId });
76+
}
77+
}
78+
7779
/**
7880
* Default (data payload).
7981
*/
@@ -99,12 +101,15 @@ export class PeerjsNetworkAdapter
99101
* must be something weird going on at the other end to cause us
100102
* to receive no response.
101103
*/
102-
setTimeout(() => this.#forceReady(), 100);
104+
setTimeout(() => this.#setAsReady(), 100);
103105
}
104106

105107
disconnect() {
106108
const peerId = this.peerId;
107-
if (peerId) this.emit("peer-disconnected", { peerId });
109+
if (peerId) {
110+
this.#transmit({ type: "leave", senderId: peerId });
111+
this.emit("peer-disconnected", { peerId });
112+
}
108113
}
109114

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

129+
/**
130+
* ↓ Internal Methods ↓
131+
*/
132+
124133
#transmit(message: t.NetworkMessage) {
125134
if (!this.#conn) throw new Error("Connection not ready");
126135
this.#conn.send(message);
@@ -133,8 +142,15 @@ export class PeerjsNetworkAdapter
133142
this.#events.emit("data", payload);
134143
}
135144

145+
#setAsReady() {
146+
if (this.#ready) return;
147+
this.#ready = true;
148+
this.emit("ready", { network: this });
149+
this.#readyResolver?.();
150+
}
151+
136152
#announceConnection(peerId: t.PeerId, peerMetadata: t.PeerMetadata) {
137-
this.#forceReady();
153+
this.#setAsReady();
138154
this.emit("peer-candidate", { peerId, peerMetadata });
139155
}
140156
}

src/types.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ export type { DataConnection } from "peerjs";
2929
* MessageChannelNetworkAdapter
3030
* https://github.com/automerge/automerge-repo/blob/main/packages/automerge-repo-network-messagechannel/src/index.ts
3131
*/
32-
export type { PeerjsNetworkAdapter } from "./NetworkAdapter.js";
32+
export type { PeerjsNetworkAdapter } from "./NetworkAdapter.ts";
3333

3434
export type IODirection = "incoming" | "outgoing";
35-
export type NetworkMessage = ArriveMessage | WelcomeMessage | Message;
35+
export type NetworkMessage = ArriveMessage | WelcomeMessage | Message | LeaveMessage;
3636
export type NetworkMessageAlert = {
3737
direction: IODirection;
3838
message: NetworkMessage;
@@ -80,3 +80,13 @@ export type WelcomeMessage = {
8080
/** The peer metadata of the sender of this message */
8181
peerMetadata: PeerMetadata;
8282
};
83+
84+
/**
85+
* Notify the network that the peer is leaving.
86+
*/
87+
export type LeaveMessage = {
88+
type: "leave";
89+
90+
/** The peer ID of the sender of this message */
91+
senderId: PeerId;
92+
};

0 commit comments

Comments
 (0)