@dxos/messaging 0.1.56-main.f3d4052 → 0.1.56-main.fd2d497

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.
@@ -2,13 +2,13 @@
2
2
  // Copyright 2020 DXOS.org
3
3
  //
4
4
 
5
- import invariant from 'tiny-invariant';
6
-
7
- import { Event } from '@dxos/async';
5
+ import { Event, Trigger } from '@dxos/async';
8
6
  import { Any } from '@dxos/codec-protobuf';
9
7
  import { Context } from '@dxos/context';
8
+ import { invariant } from '@dxos/invariant';
10
9
  import { PublicKey } from '@dxos/keys';
11
10
  import { log } from '@dxos/log';
11
+ import { schema } from '@dxos/protocols';
12
12
  import { SwarmEvent } from '@dxos/protocols/proto/dxos/mesh/signal';
13
13
  import { ComplexMap, ComplexSet } from '@dxos/util';
14
14
 
@@ -56,6 +56,9 @@ export class MemorySignalManager implements SignalManager {
56
56
 
57
57
  private _ctx!: Context;
58
58
 
59
+ // TODO(dmaretskyi): Replace with callback.
60
+ private readonly _freezeTrigger = new Trigger().wake();
61
+
59
62
  // prettier-ignore
60
63
  constructor(
61
64
  private readonly _context: MemorySignalManagerContext
@@ -154,16 +157,39 @@ export class MemorySignalManager implements SignalManager {
154
157
  }
155
158
 
156
159
  async sendMessage({ author, recipient, payload }: { author: PublicKey; recipient: PublicKey; payload: Any }) {
160
+ log('send message', { author, recipient, ...dec(payload) });
161
+
157
162
  invariant(recipient);
158
163
  invariant(!this._ctx.disposed, 'Closed');
159
- if (!this._context.connections.has(recipient)) {
164
+
165
+ await this._freezeTrigger.wait();
166
+
167
+ const remote = this._context.connections.get(recipient);
168
+ if (!remote) {
160
169
  log.warn('recipient is not subscribed for messages', { author, recipient });
161
170
  return;
162
171
  }
163
172
 
164
- if (!this._context.connections.get(recipient)?._ctx.disposed) {
165
- this._context.connections.get(recipient)!.onMessage.emit({ author, recipient, payload });
173
+ if (remote._ctx.disposed) {
174
+ log.warn('recipient is disposed', { author, recipient });
175
+ return;
166
176
  }
177
+
178
+ remote._freezeTrigger
179
+ .wait()
180
+ .then(() => {
181
+ if (remote._ctx.disposed) {
182
+ log.warn('recipient is disposed', { author, recipient });
183
+ return;
184
+ }
185
+
186
+ log('receive message', { author, recipient, ...dec(payload) });
187
+
188
+ remote.onMessage.emit({ author, recipient, payload });
189
+ })
190
+ .catch((err) => {
191
+ log.error('error while waiting for freeze', { err });
192
+ });
167
193
  }
168
194
 
169
195
  async subscribeMessages(peerId: PublicKey) {
@@ -175,4 +201,25 @@ export class MemorySignalManager implements SignalManager {
175
201
  log('unsubscribing', { peerId });
176
202
  this._context.connections.delete(peerId);
177
203
  }
204
+
205
+ freeze() {
206
+ this._freezeTrigger.reset();
207
+ }
208
+
209
+ unfreeze() {
210
+ this._freezeTrigger.wake();
211
+ }
178
212
  }
213
+ const dec = (payload: Any) => {
214
+ if (!payload.type_url.endsWith('ReliablePayload')) {
215
+ return {};
216
+ }
217
+
218
+ const relPayload = schema.getCodecForType('dxos.mesh.messaging.ReliablePayload').decode(payload.value);
219
+
220
+ if (typeof relPayload?.payload?.data === 'object') {
221
+ return { payload: Object.keys(relPayload?.payload?.data)[0], sessionId: relPayload?.payload?.sessionId };
222
+ }
223
+
224
+ return {};
225
+ };
@@ -2,11 +2,10 @@
2
2
  // Copyright 2020 DXOS.org
3
3
  //
4
4
 
5
- import invariant from 'tiny-invariant';
6
-
7
5
  import { Event, synchronized } from '@dxos/async';
8
6
  import { Any } from '@dxos/codec-protobuf';
9
7
  import { Context } from '@dxos/context';
8
+ import { invariant } from '@dxos/invariant';
10
9
  import { PublicKey } from '@dxos/keys';
11
10
  import { log } from '@dxos/log';
12
11
  import { trace } from '@dxos/protocols';