@hocuspocus/provider 2.3.1 → 2.4.0-rc.1

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.
@@ -1618,8 +1618,6 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1618
1618
  url: '',
1619
1619
  // @ts-ignore
1620
1620
  document: undefined,
1621
- // @ts-ignore
1622
- awareness: undefined,
1623
1621
  WebSocketPolyfill: undefined,
1624
1622
  parameters: {},
1625
1623
  connect: true,
@@ -1821,7 +1819,7 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1821
1819
  if (this.status !== WebSocketStatus.Connected) {
1822
1820
  return;
1823
1821
  }
1824
- // Don’t close then connection while waiting for the first message
1822
+ // Don’t close the connection while waiting for the first message
1825
1823
  if (!this.lastMessageReceived) {
1826
1824
  return;
1827
1825
  }
@@ -1830,7 +1828,8 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1830
1828
  return;
1831
1829
  }
1832
1830
  // No message received in a long time, not even your own
1833
- // Awareness updates, which are updated every 15 seconds.
1831
+ // Awareness updates, which are updated every 15 seconds
1832
+ // if awareness is enabled.
1834
1833
  this.closeTries += 1;
1835
1834
  // https://bugs.webkit.org/show_bug.cgi?id=247943
1836
1835
  if (this.closeTries > 2) {
@@ -2186,6 +2185,8 @@ class MessageReceiver {
2186
2185
  }
2187
2186
  }
2188
2187
  applyAwarenessMessage(provider) {
2188
+ if (!provider.awareness)
2189
+ return;
2189
2190
  const { message } = this;
2190
2191
  applyAwarenessUpdate(provider.awareness, message.readVarUint8Array(), provider);
2191
2192
  }
@@ -2194,6 +2195,8 @@ class MessageReceiver {
2194
2195
  readAuthMessage(message.decoder, provider.permissionDeniedHandler.bind(provider), provider.authenticatedHandler.bind(provider));
2195
2196
  }
2196
2197
  applyQueryAwarenessMessage(provider) {
2198
+ if (!provider.awareness)
2199
+ return;
2197
2200
  const { message } = this;
2198
2201
  message.writeVarUint(MessageType.Awareness);
2199
2202
  message.writeVarUint8Array(encodeAwarenessUpdate(provider.awareness, Array.from(provider.awareness.getStates().keys())));
@@ -2351,8 +2354,15 @@ class UpdateMessage extends OutgoingMessage {
2351
2354
  }
2352
2355
  }
2353
2356
 
2357
+ class AwarenessError extends Error {
2358
+ constructor() {
2359
+ super(...arguments);
2360
+ this.code = 1001;
2361
+ }
2362
+ }
2354
2363
  class HocuspocusProvider extends EventEmitter {
2355
2364
  constructor(configuration) {
2365
+ var _a, _b, _c;
2356
2366
  super();
2357
2367
  this.configuration = {
2358
2368
  name: '',
@@ -2406,7 +2416,7 @@ class HocuspocusProvider extends EventEmitter {
2406
2416
  this.forwardDestroy = (e) => this.emit('destroy', e);
2407
2417
  this.setConfiguration(configuration);
2408
2418
  this.configuration.document = configuration.document ? configuration.document : new Y.Doc();
2409
- this.configuration.awareness = configuration.awareness ? configuration.awareness : new Awareness(this.document);
2419
+ this.configuration.awareness = configuration.awareness !== undefined ? configuration.awareness : new Awareness(this.document);
2410
2420
  this.on('open', this.configuration.onOpen);
2411
2421
  this.on('message', this.configuration.onMessage);
2412
2422
  this.on('outgoingMessage', this.configuration.onOutgoingMessage);
@@ -2430,14 +2440,14 @@ class HocuspocusProvider extends EventEmitter {
2430
2440
  this.configuration.websocketProvider.on('disconnect', this.forwardDisconnect);
2431
2441
  this.configuration.websocketProvider.on('destroy', this.configuration.onDestroy);
2432
2442
  this.configuration.websocketProvider.on('destroy', this.forwardDestroy);
2433
- this.awareness.on('update', () => {
2443
+ (_a = this.awareness) === null || _a === void 0 ? void 0 : _a.on('update', () => {
2434
2444
  this.emit('awarenessUpdate', { states: awarenessStatesToArray(this.awareness.getStates()) });
2435
2445
  });
2436
- this.awareness.on('change', () => {
2446
+ (_b = this.awareness) === null || _b === void 0 ? void 0 : _b.on('change', () => {
2437
2447
  this.emit('awarenessChange', { states: awarenessStatesToArray(this.awareness.getStates()) });
2438
2448
  });
2439
2449
  this.document.on('update', this.documentUpdateHandler.bind(this));
2440
- this.awareness.on('update', this.awarenessUpdateHandler.bind(this));
2450
+ (_c = this.awareness) === null || _c === void 0 ? void 0 : _c.on('update', this.awarenessUpdateHandler.bind(this));
2441
2451
  this.registerEventListeners();
2442
2452
  if (this.configuration.forceSyncInterval) {
2443
2453
  this.intervals.forceSync = setInterval(this.forceSync.bind(this), this.configuration.forceSyncInterval);
@@ -2484,7 +2494,9 @@ class HocuspocusProvider extends EventEmitter {
2484
2494
  this.send(SyncStepOneMessage, { document: this.document, documentName: this.configuration.name });
2485
2495
  }
2486
2496
  pageUnload() {
2487
- removeAwarenessStates(this.awareness, [this.document.clientID], 'window unload');
2497
+ if (this.awareness) {
2498
+ removeAwarenessStates(this.awareness, [this.document.clientID], 'window unload');
2499
+ }
2488
2500
  }
2489
2501
  registerEventListeners() {
2490
2502
  if (typeof window === 'undefined') {
@@ -2565,7 +2577,7 @@ class HocuspocusProvider extends EventEmitter {
2565
2577
  startSync() {
2566
2578
  this.incrementUnsyncedChanges();
2567
2579
  this.send(SyncStepOneMessage, { document: this.document, documentName: this.configuration.name });
2568
- if (this.awareness.getLocalState() !== null) {
2580
+ if (this.awareness && this.awareness.getLocalState() !== null) {
2569
2581
  this.send(AwarenessMessage, {
2570
2582
  awareness: this.awareness,
2571
2583
  clients: [this.document.clientID],
@@ -2598,16 +2610,21 @@ class HocuspocusProvider extends EventEmitter {
2598
2610
  this.isAuthenticated = false;
2599
2611
  this.synced = false;
2600
2612
  // update awareness (all users except local left)
2601
- removeAwarenessStates(this.awareness, Array.from(this.awareness.getStates().keys()).filter(client => client !== this.document.clientID), this);
2613
+ if (this.awareness) {
2614
+ removeAwarenessStates(this.awareness, Array.from(this.awareness.getStates().keys()).filter(client => client !== this.document.clientID), this);
2615
+ }
2602
2616
  }
2603
2617
  destroy() {
2618
+ var _a;
2604
2619
  this.emit('destroy');
2605
2620
  if (this.intervals.forceSync) {
2606
2621
  clearInterval(this.intervals.forceSync);
2607
2622
  }
2608
- removeAwarenessStates(this.awareness, [this.document.clientID], 'provider destroy');
2623
+ if (this.awareness) {
2624
+ removeAwarenessStates(this.awareness, [this.document.clientID], 'provider destroy');
2625
+ }
2609
2626
  this.disconnect();
2610
- this.awareness.off('update', this.awarenessUpdateHandler);
2627
+ (_a = this.awareness) === null || _a === void 0 ? void 0 : _a.off('update', this.awarenessUpdateHandler);
2611
2628
  this.document.off('update', this.documentUpdateHandler);
2612
2629
  this.removeAllListeners();
2613
2630
  this.configuration.websocketProvider.off('connect', this.configuration.onConnect);
@@ -2664,17 +2681,21 @@ class HocuspocusProvider extends EventEmitter {
2664
2681
  this.broadcast(SyncStepOneMessage, { document: this.document });
2665
2682
  this.broadcast(SyncStepTwoMessage, { document: this.document });
2666
2683
  this.broadcast(QueryAwarenessMessage, { document: this.document });
2667
- this.broadcast(AwarenessMessage, { awareness: this.awareness, clients: [this.document.clientID], document: this.document });
2684
+ if (this.awareness) {
2685
+ this.broadcast(AwarenessMessage, { awareness: this.awareness, clients: [this.document.clientID], document: this.document });
2686
+ }
2668
2687
  });
2669
2688
  }
2670
2689
  disconnectBroadcastChannel() {
2671
2690
  // broadcast message with local awareness state set to null (indicating disconnect)
2672
- this.send(AwarenessMessage, {
2673
- awareness: this.awareness,
2674
- clients: [this.document.clientID],
2675
- states: new Map(),
2676
- documentName: this.configuration.name,
2677
- }, true);
2691
+ if (this.awareness) {
2692
+ this.send(AwarenessMessage, {
2693
+ awareness: this.awareness,
2694
+ clients: [this.document.clientID],
2695
+ states: new Map(),
2696
+ documentName: this.configuration.name,
2697
+ }, true);
2698
+ }
2678
2699
  if (this.subscribedToBroadcastChannel) {
2679
2700
  unsubscribe(this.broadcastChannel, this.boundBroadcastChannelSubscriber);
2680
2701
  this.subscribedToBroadcastChannel = false;
@@ -2690,6 +2711,9 @@ class HocuspocusProvider extends EventEmitter {
2690
2711
  new MessageSender(Message, args).broadcast(this.broadcastChannel);
2691
2712
  }
2692
2713
  setAwarenessField(key, value) {
2714
+ if (!this.awareness) {
2715
+ throw new AwarenessError(`Cannot set awareness field "${key}" to ${JSON.stringify(value)}. You have disabled Awareness for this provider by explicitly passing awareness: null in the provider configuration.`);
2716
+ }
2693
2717
  this.awareness.setLocalStateField(key, value);
2694
2718
  }
2695
2719
  }
@@ -2745,5 +2769,5 @@ class TiptapCollabProvider extends HocuspocusProvider {
2745
2769
  }
2746
2770
  }
2747
2771
 
2748
- export { HocuspocusProvider, HocuspocusProviderWebsocket, MessageType, TiptapCollabProvider, TiptapCollabProviderWebsocket, WebSocketStatus };
2772
+ export { AwarenessError, HocuspocusProvider, HocuspocusProviderWebsocket, MessageType, TiptapCollabProvider, TiptapCollabProviderWebsocket, WebSocketStatus };
2749
2773
  //# sourceMappingURL=hocuspocus-provider.esm.js.map