Skip to content

Commit

Permalink
Update Adapter API (#8)
Browse files Browse the repository at this point in the history
* update refs

* Update network adapter for API changes (published 1.2.2)

Update notes (from @pvh): https://patchwork.inkandswitch.com/#automerge-repo-network-adapter-api-changes--4RicZ28GjFaTi12xssztkpDjqruu?type=essay

* tweak changelog
  • Loading branch information
philcockfield authored Aug 26, 2024
1 parent ec087e3 commit 26d44a4
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 56 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ This project adheres to [Semantic Versioning](http://semver.org/).
#### Fixed
#### Security

## [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))

- Removed emitting "ready" event.
- Implemented `isReady` and `whenReady`
- `disconnect()` already implemented.

## [1.2.1] - 2024-07-27
#### Changed
- Updated refs (automerge-repo)
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "automerge-repo-network-peerjs",
"version": "1.2.1",
"version": "1.2.2",
"description": "Network adapter for automerge-repo using peerjs",
"author": "phil Cockfield",
"license": "MIT",
Expand All @@ -21,7 +21,7 @@
"eventemitter3": ">=5.0.1",
"peerjs": ">=1.5.4",
"typescript": "5.5.4",
"vitest": "2.0.4"
"vitest": "2.0.5"
},
"publishConfig": {
"access": "public"
Expand Down
30 changes: 20 additions & 10 deletions src/NetworkAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,32 @@ export class PeerjsNetworkAdapter
peerId?: t.PeerId;
peerMetadata?: t.PeerMetadata;

#isReady = false;
#conn: t.DataConnection;
#events = new EventEmitter<EventTypes>();

#ready = false;
#readyResolver?: () => void;
#readyPromise: Promise<void> = new Promise<void>((resolve) => (this.#readyResolver = resolve));
#forceReady() {
if (this.#ready) return;
this.#ready = true;
this.#readyResolver?.();
}

constructor(conn: t.DataConnection) {
if (!conn) throw new Error(`A peerjs data-connection is required`);
super();
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 @@ -72,7 +88,7 @@ export class PeerjsNetworkAdapter
conn.on("data", handleData);

this.on("peer-disconnected", () => {
this.#isReady = false;
this.#ready = false;
conn.off("open", handleOpen);
conn.off("close", handleClose);
conn.off("data", handleData);
Expand All @@ -83,7 +99,7 @@ export class PeerjsNetworkAdapter
* must be something weird going on at the other end to cause us
* to receive no response.
*/
setTimeout(() => this.#setAsReady(), 100);
setTimeout(() => this.#forceReady(), 100);
}

disconnect() {
Expand Down Expand Up @@ -117,14 +133,8 @@ export class PeerjsNetworkAdapter
this.#events.emit("data", payload);
}

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

#announceConnection(peerId: t.PeerId, peerMetadata: t.PeerMetadata) {
this.#setAsReady();
this.#forceReady();
this.emit("peer-candidate", { peerId, peerMetadata });
}
}
Expand Down
88 changes: 44 additions & 44 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -345,53 +345,53 @@
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-1.0.5.tgz#a6ce3e556e00fd9895dd872dd172ad0d4bd687f4"
integrity sha512-/kYRxGDLWzHOB7q+wtSUQlFrtcdUccpfy+X+9iMBpHK8QLLhx2wIPYuS5DYtR9Wa/YlZAbIovy7qVdB1Aq6Lyw==

"@vitest/[email protected].4":
version "2.0.4"
resolved "https://registry.yarnpkg.com/@vitest/expect/-/expect-2.0.4.tgz#d365c106c84f2a3aae96000e95be21956acc099c"
integrity sha512-39jr5EguIoanChvBqe34I8m1hJFI4+jxvdOpD7gslZrVQBKhh8H9eD7J/LJX4zakrw23W+dITQTDqdt43xVcJw==
"@vitest/[email protected].5":
version "2.0.5"
resolved "https://registry.yarnpkg.com/@vitest/expect/-/expect-2.0.5.tgz#f3745a6a2c18acbea4d39f5935e913f40d26fa86"
integrity sha512-yHZtwuP7JZivj65Gxoi8upUN2OzHTi3zVfjwdpu2WrvCZPLwsJ2Ey5ILIPccoW23dd/zQBlJ4/dhi7DWNyXCpA==
dependencies:
"@vitest/spy" "2.0.4"
"@vitest/utils" "2.0.4"
"@vitest/spy" "2.0.5"
"@vitest/utils" "2.0.5"
chai "^5.1.1"
tinyrainbow "^1.2.0"

"@vitest/[email protected].4", "@vitest/pretty-format@^2.0.4":
version "2.0.4"
resolved "https://registry.yarnpkg.com/@vitest/pretty-format/-/pretty-format-2.0.4.tgz#9a3934932e7f8ddd836b38c34ddaeec91bd0f82e"
integrity sha512-RYZl31STbNGqf4l2eQM1nvKPXE0NhC6Eq0suTTePc4mtMQ1Fn8qZmjV4emZdEdG2NOWGKSCrHZjmTqDCDoeFBw==
"@vitest/[email protected].5", "@vitest/pretty-format@^2.0.5":
version "2.0.5"
resolved "https://registry.yarnpkg.com/@vitest/pretty-format/-/pretty-format-2.0.5.tgz#91d2e6d3a7235c742e1a6cc50e7786e2f2979b1e"
integrity sha512-h8k+1oWHfwTkyTkb9egzwNMfJAEx4veaPSnMeKbVSjp4euqGSbQlm5+6VHwTr7u4FJslVVsUG5nopCaAYdOmSQ==
dependencies:
tinyrainbow "^1.2.0"

"@vitest/[email protected].4":
version "2.0.4"
resolved "https://registry.yarnpkg.com/@vitest/runner/-/runner-2.0.4.tgz#0b1edb8ab5f81a1c7dfd50090e5e7e971a117891"
integrity sha512-Gk+9Su/2H2zNfNdeJR124gZckd5st4YoSuhF1Rebi37qTXKnqYyFCd9KP4vl2cQHbtuVKjfEKrNJxHHCW8thbQ==
"@vitest/[email protected].5":
version "2.0.5"
resolved "https://registry.yarnpkg.com/@vitest/runner/-/runner-2.0.5.tgz#89197e712bb93513537d6876995a4843392b2a84"
integrity sha512-TfRfZa6Bkk9ky4tW0z20WKXFEwwvWhRY+84CnSEtq4+3ZvDlJyY32oNTJtM7AW9ihW90tX/1Q78cb6FjoAs+ig==
dependencies:
"@vitest/utils" "2.0.4"
"@vitest/utils" "2.0.5"
pathe "^1.1.2"

"@vitest/[email protected].4":
version "2.0.4"
resolved "https://registry.yarnpkg.com/@vitest/snapshot/-/snapshot-2.0.4.tgz#7d7dea9df17c5c13386f1a7a433b99dc0ffe3c14"
integrity sha512-or6Mzoz/pD7xTvuJMFYEtso1vJo1S5u6zBTinfl+7smGUhqybn6VjzCDMhmTyVOFWwkCMuNjmNNxnyXPgKDoPw==
"@vitest/[email protected].5":
version "2.0.5"
resolved "https://registry.yarnpkg.com/@vitest/snapshot/-/snapshot-2.0.5.tgz#a2346bc5013b73c44670c277c430e0334690a162"
integrity sha512-SgCPUeDFLaM0mIUHfaArq8fD2WbaXG/zVXjRupthYfYGzc8ztbFbu6dUNOblBG7XLMR1kEhS/DNnfCZ2IhdDew==
dependencies:
"@vitest/pretty-format" "2.0.4"
"@vitest/pretty-format" "2.0.5"
magic-string "^0.30.10"
pathe "^1.1.2"

"@vitest/[email protected].4":
version "2.0.4"
resolved "https://registry.yarnpkg.com/@vitest/spy/-/spy-2.0.4.tgz#19083386a741a158c2f142beffe43be68b1375cf"
integrity sha512-uTXU56TNoYrTohb+6CseP8IqNwlNdtPwEO0AWl+5j7NelS6x0xZZtP0bDWaLvOfUbaYwhhWp1guzXUxkC7mW7Q==
"@vitest/[email protected].5":
version "2.0.5"
resolved "https://registry.yarnpkg.com/@vitest/spy/-/spy-2.0.5.tgz#590fc07df84a78b8e9dd976ec2090920084a2b9f"
integrity sha512-c/jdthAhvJdpfVuaexSrnawxZz6pywlTPe84LUB2m/4t3rl2fTo9NFGBG4oWgaD+FTgDDV8hJ/nibT7IfH3JfA==
dependencies:
tinyspy "^3.0.0"

"@vitest/[email protected].4":
version "2.0.4"
resolved "https://registry.yarnpkg.com/@vitest/utils/-/utils-2.0.4.tgz#2db1df35aaeb5caa932770a190df636a68d284d5"
integrity sha512-Zc75QuuoJhOBnlo99ZVUkJIuq4Oj0zAkrQ2VzCqNCx6wAwViHEh5Fnp4fiJTE9rA+sAoXRf00Z9xGgfEzV6fzQ==
"@vitest/[email protected].5":
version "2.0.5"
resolved "https://registry.yarnpkg.com/@vitest/utils/-/utils-2.0.5.tgz#6f8307a4b6bc6ceb9270007f73c67c915944e926"
integrity sha512-d8HKbqIcya+GR67mkZbrzhS5kKhtp8dQLcmRZLGTscGVg7yImT82cIrhtn2L8+VujWcy6KZweApgNmPsTAO/UQ==
dependencies:
"@vitest/pretty-format" "2.0.4"
"@vitest/pretty-format" "2.0.5"
estree-walker "^3.0.3"
loupe "^3.1.1"
tinyrainbow "^1.2.0"
Expand Down Expand Up @@ -857,10 +857,10 @@ v8-compile-cache-lib@^3.0.1:
resolved "https://registry.yarnpkg.com/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz#6336e8d71965cb3d35a1bbb7868445a7c05264bf"
integrity sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==

[email protected].4:
version "2.0.4"
resolved "https://registry.yarnpkg.com/vite-node/-/vite-node-2.0.4.tgz#5600cc9f0d9c3ff9a64050c6858e7e1b62fb3fcd"
integrity sha512-ZpJVkxcakYtig5iakNeL7N3trufe3M6vGuzYAr4GsbCTwobDeyPJpE4cjDhhPluv8OvQCFzu2LWp6GkoKRITXA==
[email protected].5:
version "2.0.5"
resolved "https://registry.yarnpkg.com/vite-node/-/vite-node-2.0.5.tgz#36d909188fc6e3aba3da5fc095b3637d0d18e27b"
integrity sha512-LdsW4pxj0Ot69FAoXZ1yTnA9bjGohr2yNBU7QKRxpz8ITSkhuDl6h3zS/tvgz4qrNjeRnvrWeXQ8ZF7Um4W00Q==
dependencies:
cac "^6.7.14"
debug "^4.3.5"
Expand All @@ -879,18 +879,18 @@ vite@^5.0.0:
optionalDependencies:
fsevents "~2.3.3"

[email protected].4:
version "2.0.4"
resolved "https://registry.yarnpkg.com/vitest/-/vitest-2.0.4.tgz#ac6bfbaee53e502cee864b07a5b2edf1fcba793e"
integrity sha512-luNLDpfsnxw5QSW4bISPe6tkxVvv5wn2BBs/PuDRkhXZ319doZyLOBr1sjfB5yCEpTiU7xCAdViM8TNVGPwoog==
[email protected].5:
version "2.0.5"
resolved "https://registry.yarnpkg.com/vitest/-/vitest-2.0.5.tgz#2f15a532704a7181528e399cc5b754c7f335fd62"
integrity sha512-8GUxONfauuIdeSl5f9GTgVEpg5BTOlplET4WEDaeY2QBiN8wSm68vxN/tb5z405OwppfoCavnwXafiaYBC/xOA==
dependencies:
"@ampproject/remapping" "^2.3.0"
"@vitest/expect" "2.0.4"
"@vitest/pretty-format" "^2.0.4"
"@vitest/runner" "2.0.4"
"@vitest/snapshot" "2.0.4"
"@vitest/spy" "2.0.4"
"@vitest/utils" "2.0.4"
"@vitest/expect" "2.0.5"
"@vitest/pretty-format" "^2.0.5"
"@vitest/runner" "2.0.5"
"@vitest/snapshot" "2.0.5"
"@vitest/spy" "2.0.5"
"@vitest/utils" "2.0.5"
chai "^5.1.1"
debug "^4.3.5"
execa "^8.0.1"
Expand All @@ -901,7 +901,7 @@ [email protected]:
tinypool "^1.0.0"
tinyrainbow "^1.2.0"
vite "^5.0.0"
vite-node "2.0.4"
vite-node "2.0.5"
why-is-node-running "^2.3.0"

webrtc-adapter@^9.0.0:
Expand Down

0 comments on commit 26d44a4

Please sign in to comment.