@dxos/messaging 0.1.56-main.c787da3 → 0.1.56-main.e47dfd1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/messaging",
3
- "version": "0.1.56-main.c787da3",
3
+ "version": "0.1.56-main.e47dfd1",
4
4
  "description": "Messaging",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -20,22 +20,22 @@
20
20
  "isomorphic-ws": "^4.0.1",
21
21
  "tiny-invariant": "^1.3.1",
22
22
  "ws": "^7.4.4",
23
- "@dxos/async": "0.1.56-main.c787da3",
24
- "@dxos/codec-protobuf": "0.1.56-main.c787da3",
25
- "@dxos/context": "0.1.56-main.c787da3",
26
- "@dxos/keys": "0.1.56-main.c787da3",
27
- "@dxos/log": "0.1.56-main.c787da3",
28
- "@dxos/node-std": "0.1.56-main.c787da3",
29
- "@dxos/protocols": "0.1.56-main.c787da3",
30
- "@dxos/rpc": "0.1.56-main.c787da3",
31
- "@dxos/util": "0.1.56-main.c787da3"
23
+ "@dxos/async": "0.1.56-main.e47dfd1",
24
+ "@dxos/codec-protobuf": "0.1.56-main.e47dfd1",
25
+ "@dxos/context": "0.1.56-main.e47dfd1",
26
+ "@dxos/keys": "0.1.56-main.e47dfd1",
27
+ "@dxos/log": "0.1.56-main.e47dfd1",
28
+ "@dxos/node-std": "0.1.56-main.e47dfd1",
29
+ "@dxos/protocols": "0.1.56-main.e47dfd1",
30
+ "@dxos/rpc": "0.1.56-main.e47dfd1",
31
+ "@dxos/util": "0.1.56-main.e47dfd1"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@types/node": "^18.11.9",
35
35
  "earljs": "~0.1.10",
36
36
  "typescript": "^5.0.4",
37
37
  "wait-for-expect": "^3.0.2",
38
- "@dxos/signal": "0.1.56-main.c787da3"
38
+ "@dxos/signal": "0.1.56-main.e47dfd1"
39
39
  },
40
40
  "publishConfig": {
41
41
  "access": "public"
@@ -4,11 +4,12 @@
4
4
 
5
5
  import invariant from 'tiny-invariant';
6
6
 
7
- import { Event } from '@dxos/async';
7
+ import { Event, Trigger } from '@dxos/async';
8
8
  import { Any } from '@dxos/codec-protobuf';
9
9
  import { Context } from '@dxos/context';
10
10
  import { PublicKey } from '@dxos/keys';
11
11
  import { log } from '@dxos/log';
12
+ import { schema } from '@dxos/protocols';
12
13
  import { SwarmEvent } from '@dxos/protocols/proto/dxos/mesh/signal';
13
14
  import { ComplexMap, ComplexSet } from '@dxos/util';
14
15
 
@@ -56,6 +57,9 @@ export class MemorySignalManager implements SignalManager {
56
57
 
57
58
  private _ctx!: Context;
58
59
 
60
+ // TODO(dmaretskyi): Replace with callback.
61
+ private readonly _freezeTrigger = new Trigger().wake();
62
+
59
63
  // prettier-ignore
60
64
  constructor(
61
65
  private readonly _context: MemorySignalManagerContext
@@ -154,16 +158,39 @@ export class MemorySignalManager implements SignalManager {
154
158
  }
155
159
 
156
160
  async sendMessage({ author, recipient, payload }: { author: PublicKey; recipient: PublicKey; payload: Any }) {
161
+ log('send message', { author, recipient, ...dec(payload) });
162
+
157
163
  invariant(recipient);
158
164
  invariant(!this._ctx.disposed, 'Closed');
159
- if (!this._context.connections.has(recipient)) {
165
+
166
+ await this._freezeTrigger.wait();
167
+
168
+ const remote = this._context.connections.get(recipient);
169
+ if (!remote) {
160
170
  log.warn('recipient is not subscribed for messages', { author, recipient });
161
171
  return;
162
172
  }
163
173
 
164
- if (!this._context.connections.get(recipient)?._ctx.disposed) {
165
- this._context.connections.get(recipient)!.onMessage.emit({ author, recipient, payload });
174
+ if (remote._ctx.disposed) {
175
+ log.warn('recipient is disposed', { author, recipient });
176
+ return;
166
177
  }
178
+
179
+ remote._freezeTrigger
180
+ .wait()
181
+ .then(() => {
182
+ if (remote._ctx.disposed) {
183
+ log.warn('recipient is disposed', { author, recipient });
184
+ return;
185
+ }
186
+
187
+ log('receive message', { author, recipient, ...dec(payload) });
188
+
189
+ remote.onMessage.emit({ author, recipient, payload });
190
+ })
191
+ .catch((err) => {
192
+ log.error('error while waiting for freeze', { err });
193
+ });
167
194
  }
168
195
 
169
196
  async subscribeMessages(peerId: PublicKey) {
@@ -175,4 +202,25 @@ export class MemorySignalManager implements SignalManager {
175
202
  log('unsubscribing', { peerId });
176
203
  this._context.connections.delete(peerId);
177
204
  }
205
+
206
+ freeze() {
207
+ this._freezeTrigger.reset();
208
+ }
209
+
210
+ unfreeze() {
211
+ this._freezeTrigger.wake();
212
+ }
178
213
  }
214
+ const dec = (payload: Any) => {
215
+ if (!payload.type_url.endsWith('ReliablePayload')) {
216
+ return {};
217
+ }
218
+
219
+ const relPayload = schema.getCodecForType('dxos.mesh.messaging.ReliablePayload').decode(payload.value);
220
+
221
+ if (typeof relPayload?.payload?.data === 'object') {
222
+ return { payload: Object.keys(relPayload?.payload?.data)[0], sessionId: relPayload?.payload?.sessionId };
223
+ }
224
+
225
+ return {};
226
+ };