@automerge/automerge-repo-network-messagechannel 1.0.0-alpha.3 → 1.0.0-alpha.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,14 +1,14 @@
1
1
  import { type Message, NetworkAdapter, type PeerId } from "@automerge/automerge-repo";
2
2
  import { MessagePortRef } from "./MessagePortRef.js";
3
3
  export declare class MessageChannelNetworkAdapter extends NetworkAdapter {
4
+ #private;
4
5
  channels: {};
5
6
  messagePortRef: MessagePortRef;
6
7
  constructor(messagePort: MessagePort, config?: MessageChannelNetworkAdapterConfig);
7
8
  connect(peerId: PeerId): void;
8
9
  send(message: Message): void;
9
10
  announceConnection(peerId: PeerId): void;
10
- join(): void;
11
- leave(): void;
11
+ disconnect(): void;
12
12
  }
13
13
  interface MessageChannelNetworkAdapterConfig {
14
14
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,OAAO,EACZ,cAAc,EACd,KAAK,MAAM,EAEZ,MAAM,2BAA2B,CAAA;AAClC,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AAOpD,qBAAa,4BAA6B,SAAQ,cAAc;IAC9D,QAAQ,KAAK;IACb,cAAc,EAAE,cAAc,CAAA;gBAG5B,WAAW,EAAE,WAAW,EACxB,MAAM,GAAE,kCAAuC;IAWjD,OAAO,CAAC,MAAM,EAAE,MAAM;IAiDtB,IAAI,CAAC,OAAO,EAAE,OAAO;IAmBrB,kBAAkB,CAAC,MAAM,EAAE,MAAM;IAIjC,IAAI;IAOJ,KAAK;CAIN;AAED,UAAU,kCAAkC;IAC1C;;;;;;;OAOG;IACH,UAAU,CAAC,EAAE,OAAO,CAAA;CACrB"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,OAAO,EACZ,cAAc,EACd,KAAK,MAAM,EAEZ,MAAM,2BAA2B,CAAA;AAClC,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AAOpD,qBAAa,4BAA6B,SAAQ,cAAc;;IAC9D,QAAQ,KAAK;IACb,cAAc,EAAE,cAAc,CAAA;gBAI5B,WAAW,EAAE,WAAW,EACxB,MAAM,GAAE,kCAAuC;IAWjD,OAAO,CAAC,MAAM,EAAE,MAAM;IAgEtB,IAAI,CAAC,OAAO,EAAE,OAAO;IAmBrB,kBAAkB,CAAC,MAAM,EAAE,MAAM;IAQjC,UAAU;CAIX;AAED,UAAU,kCAAkC;IAC1C;;;;;;;OAOG;IACH,UAAU,CAAC,EAAE,OAAO,CAAA;CACrB"}
package/dist/index.js CHANGED
@@ -6,6 +6,7 @@ const log = debug("automerge-repo:messagechannel");
6
6
  export class MessageChannelNetworkAdapter extends NetworkAdapter {
7
7
  channels = {};
8
8
  messagePortRef;
9
+ #startupComplete = false;
9
10
  constructor(messagePort, config = {}) {
10
11
  super();
11
12
  const useWeakRef = config.useWeakRef ?? false;
@@ -52,6 +53,19 @@ export class MessageChannelNetworkAdapter extends NetworkAdapter {
52
53
  this.messagePortRef.addListener("close", () => {
53
54
  this.emit("close");
54
55
  });
56
+ this.messagePortRef.postMessage({
57
+ senderId: this.peerId,
58
+ type: "arrive",
59
+ });
60
+ // Mark this messagechannel as ready after 50 ms, at this point there
61
+ // must be something weird going on on the other end to cause us to receive
62
+ // no response
63
+ setTimeout(() => {
64
+ if (!this.#startupComplete) {
65
+ this.#startupComplete = true;
66
+ this.emit("ready", { network: this });
67
+ }
68
+ }, 100);
55
69
  }
56
70
  send(message) {
57
71
  if ("data" in message) {
@@ -66,15 +80,13 @@ export class MessageChannelNetworkAdapter extends NetworkAdapter {
66
80
  }
67
81
  }
68
82
  announceConnection(peerId) {
83
+ if (!this.#startupComplete) {
84
+ this.#startupComplete = true;
85
+ this.emit("ready", { network: this });
86
+ }
69
87
  this.emit("peer-candidate", { peerId });
70
88
  }
71
- join() {
72
- this.messagePortRef.postMessage({
73
- senderId: this.peerId,
74
- type: "arrive",
75
- });
76
- }
77
- leave() {
89
+ disconnect() {
78
90
  // TODO
79
91
  throw new Error("Unimplemented: leave on MessagePortNetworkAdapter");
80
92
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@automerge/automerge-repo-network-messagechannel",
3
- "version": "1.0.0-alpha.3",
3
+ "version": "1.0.0-alpha.5",
4
4
  "description": "MessageChannel network adapter for Automerge Repo",
5
5
  "repository": "https://github.com/automerge/automerge-repo",
6
6
  "author": "Peter van Hardenberg <pvh@pvh.ca>",
@@ -14,10 +14,10 @@
14
14
  "test": "mocha --no-warnings --experimental-specifier-resolution=node --exit"
15
15
  },
16
16
  "dependencies": {
17
- "@automerge/automerge-repo": "^1.0.0-alpha.3"
17
+ "@automerge/automerge-repo": "^1.0.0-alpha.5"
18
18
  },
19
19
  "peerDependencies": {
20
- "@automerge/automerge": "^2.1.0-alpha.10"
20
+ "@automerge/automerge": "^2.1.0-alpha.13"
21
21
  },
22
22
  "watch": {
23
23
  "build": {
@@ -30,5 +30,5 @@
30
30
  "publishConfig": {
31
31
  "access": "public"
32
32
  },
33
- "gitHead": "0ed108273084319aeea64ceccb49c3d58709f107"
33
+ "gitHead": "9cd9be160ebda37c8f0d70f2d5cadea5b951a3c3"
34
34
  }
package/src/index.ts CHANGED
@@ -14,6 +14,7 @@ const log = debug("automerge-repo:messagechannel")
14
14
  export class MessageChannelNetworkAdapter extends NetworkAdapter {
15
15
  channels = {}
16
16
  messagePortRef: MessagePortRef
17
+ #startupComplete = false
17
18
 
18
19
  constructor(
19
20
  messagePort: MessagePort,
@@ -75,6 +76,21 @@ export class MessageChannelNetworkAdapter extends NetworkAdapter {
75
76
  this.messagePortRef.addListener("close", () => {
76
77
  this.emit("close")
77
78
  })
79
+
80
+ this.messagePortRef.postMessage({
81
+ senderId: this.peerId,
82
+ type: "arrive",
83
+ })
84
+
85
+ // Mark this messagechannel as ready after 50 ms, at this point there
86
+ // must be something weird going on on the other end to cause us to receive
87
+ // no response
88
+ setTimeout(() => {
89
+ if (!this.#startupComplete) {
90
+ this.#startupComplete = true
91
+ this.emit("ready", { network: this })
92
+ }
93
+ }, 100)
78
94
  }
79
95
 
80
96
  send(message: Message) {
@@ -97,17 +113,14 @@ export class MessageChannelNetworkAdapter extends NetworkAdapter {
97
113
  }
98
114
 
99
115
  announceConnection(peerId: PeerId) {
116
+ if (!this.#startupComplete) {
117
+ this.#startupComplete = true
118
+ this.emit("ready", { network: this })
119
+ }
100
120
  this.emit("peer-candidate", { peerId })
101
121
  }
102
122
 
103
- join() {
104
- this.messagePortRef.postMessage({
105
- senderId: this.peerId,
106
- type: "arrive",
107
- })
108
- }
109
-
110
- leave() {
123
+ disconnect() {
111
124
  // TODO
112
125
  throw new Error("Unimplemented: leave on MessagePortNetworkAdapter")
113
126
  }
@@ -1,5 +1,5 @@
1
- import { runAdapterTests } from "../../automerge-repo/src/helpers/tests/network-adapter-tests"
2
- import { MessageChannelNetworkAdapter as Adapter } from "../src"
1
+ import { runAdapterTests } from "../../automerge-repo/src/helpers/tests/network-adapter-tests.js"
2
+ import { MessageChannelNetworkAdapter as Adapter } from "../src/index.js"
3
3
 
4
4
  // bob is the hub, alice and charlie are spokes
5
5
  describe("MessageChannelNetworkAdapter", () => {
package/tsconfig.json CHANGED
@@ -2,8 +2,8 @@
2
2
  "compilerOptions": {
3
3
  "target": "ESNext",
4
4
  "jsx": "react",
5
- "module": "ESNext",
6
- "moduleResolution": "node",
5
+ "module": "NodeNext",
6
+ "moduleResolution": "Node16",
7
7
  "declaration": true,
8
8
  "declarationMap": true,
9
9
  "outDir": "./dist",