@hocuspocus/provider 3.0.6-rc.0 → 3.0.7-rc.0

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.
@@ -1330,7 +1330,6 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1330
1330
  };
1331
1331
  this.connectionAttempt = null;
1332
1332
  this.receivedOnOpenPayload = undefined;
1333
- this.receivedOnStatusPayload = undefined;
1334
1333
  this.closeTries = 0;
1335
1334
  this.setConfiguration(configuration);
1336
1335
  this.configuration.WebSocketPolyfill = configuration.WebSocketPolyfill
@@ -1342,7 +1341,6 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1342
1341
  this.on('message', this.configuration.onMessage);
1343
1342
  this.on('outgoingMessage', this.configuration.onOutgoingMessage);
1344
1343
  this.on('status', this.configuration.onStatus);
1345
- this.on('status', this.onStatus.bind(this));
1346
1344
  this.on('disconnect', this.configuration.onDisconnect);
1347
1345
  this.on('close', this.configuration.onClose);
1348
1346
  this.on('destroy', this.configuration.onDestroy);
@@ -1363,15 +1361,12 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1363
1361
  this.cancelWebsocketRetry = undefined;
1364
1362
  this.receivedOnOpenPayload = event;
1365
1363
  }
1366
- async onStatus(data) {
1367
- this.receivedOnStatusPayload = data;
1368
- }
1369
1364
  attach(provider) {
1370
1365
  this.configuration.providerMap.set(provider.configuration.name, provider);
1371
1366
  if (this.status === WebSocketStatus.Disconnected && this.shouldConnect) {
1372
1367
  this.connect();
1373
1368
  }
1374
- if (this.receivedOnOpenPayload) {
1369
+ if (this.receivedOnOpenPayload && this.status === WebSocketStatus.Connected) {
1375
1370
  provider.onOpen(this.receivedOnOpenPayload);
1376
1371
  }
1377
1372
  }
@@ -1394,7 +1389,6 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1394
1389
  this.cancelWebsocketRetry = undefined;
1395
1390
  }
1396
1391
  this.receivedOnOpenPayload = undefined;
1397
- this.receivedOnStatusPayload = undefined;
1398
1392
  this.shouldConnect = true;
1399
1393
  const abortableRetry = () => {
1400
1394
  let cancelAttempt = false;
@@ -2321,6 +2315,7 @@ class HocuspocusProvider extends EventEmitter {
2321
2315
  onAwarenessUpdate: () => null,
2322
2316
  onAwarenessChange: () => null,
2323
2317
  onStateless: () => null,
2318
+ onUnsyncedChanges: () => null,
2324
2319
  };
2325
2320
  this.isSynced = false;
2326
2321
  this.unsyncedChanges = 0;
@@ -2328,7 +2323,7 @@ class HocuspocusProvider extends EventEmitter {
2328
2323
  this.authorizedScope = undefined;
2329
2324
  // @internal
2330
2325
  this.manageSocket = false;
2331
- this.isAttached = false;
2326
+ this._isAttached = false;
2332
2327
  this.intervals = {
2333
2328
  forceSync: null,
2334
2329
  };
@@ -2338,7 +2333,6 @@ class HocuspocusProvider extends EventEmitter {
2338
2333
  this.boundOnOpen = this.onOpen.bind(this);
2339
2334
  this.boundOnClose = this.onClose.bind(this);
2340
2335
  this.forwardConnect = (e) => this.emit('connect', e);
2341
- // forwardOpen = (e: any) => this.emit('open', e)
2342
2336
  this.forwardClose = (e) => this.emit('close', e);
2343
2337
  this.forwardDisconnect = (e) => this.emit('disconnect', e);
2344
2338
  this.forwardDestroy = (e) => this.emit('destroy', e);
@@ -2353,6 +2347,7 @@ class HocuspocusProvider extends EventEmitter {
2353
2347
  this.on('awarenessUpdate', this.configuration.onAwarenessUpdate);
2354
2348
  this.on('awarenessChange', this.configuration.onAwarenessChange);
2355
2349
  this.on('stateless', this.configuration.onStateless);
2350
+ this.on('unsyncedChanges', this.configuration.onUnsyncedChanges);
2356
2351
  this.on('authenticated', this.configuration.onAuthenticated);
2357
2352
  this.on('authenticationFailed', this.configuration.onAuthenticationFailed);
2358
2353
  (_a = this.awareness) === null || _a === void 0 ? void 0 : _a.on('update', () => {
@@ -2385,6 +2380,9 @@ class HocuspocusProvider extends EventEmitter {
2385
2380
  get document() {
2386
2381
  return this.configuration.document;
2387
2382
  }
2383
+ get isAttached() {
2384
+ return this._isAttached;
2385
+ }
2388
2386
  get awareness() {
2389
2387
  return this.configuration.awareness;
2390
2388
  }
@@ -2393,11 +2391,11 @@ class HocuspocusProvider extends EventEmitter {
2393
2391
  }
2394
2392
  resetUnsyncedChanges() {
2395
2393
  this.unsyncedChanges = 1;
2396
- this.emit('unsyncedChanges', this.unsyncedChanges);
2394
+ this.emit('unsyncedChanges', { number: this.unsyncedChanges });
2397
2395
  }
2398
2396
  incrementUnsyncedChanges() {
2399
2397
  this.unsyncedChanges += 1;
2400
- this.emit('unsyncedChanges', this.unsyncedChanges);
2398
+ this.emit('unsyncedChanges', { number: this.unsyncedChanges });
2401
2399
  }
2402
2400
  decrementUnsyncedChanges() {
2403
2401
  if (this.unsyncedChanges > 0) {
@@ -2406,7 +2404,7 @@ class HocuspocusProvider extends EventEmitter {
2406
2404
  if (this.unsyncedChanges === 0) {
2407
2405
  this.synced = true;
2408
2406
  }
2409
- this.emit('unsyncedChanges', this.unsyncedChanges);
2407
+ this.emit('unsyncedChanges', { number: this.unsyncedChanges });
2410
2408
  }
2411
2409
  forceSync() {
2412
2410
  this.resetUnsyncedChanges();
@@ -2464,9 +2462,15 @@ class HocuspocusProvider extends EventEmitter {
2464
2462
  }
2465
2463
  // not needed, but provides backward compatibility with e.g. lexical/yjs
2466
2464
  async connect() {
2465
+ if (this.manageSocket) {
2466
+ return this.configuration.websocketProvider.connect();
2467
+ }
2467
2468
  console.warn('HocuspocusProvider::connect() is deprecated and does not do anything. Please connect/disconnect on the websocketProvider, or attach/deattach providers.');
2468
2469
  }
2469
2470
  disconnect() {
2471
+ if (this.manageSocket) {
2472
+ return this.configuration.websocketProvider.disconnect();
2473
+ }
2470
2474
  console.warn('HocuspocusProvider::disconnect() is deprecated and does not do anything. Please connect/disconnect on the websocketProvider, or attach/deattach providers.');
2471
2475
  }
2472
2476
  async onOpen(event) {
@@ -2505,6 +2509,8 @@ class HocuspocusProvider extends EventEmitter {
2505
2509
  }
2506
2510
  }
2507
2511
  send(message, args) {
2512
+ if (!this._isAttached)
2513
+ return;
2508
2514
  const messageSender = new MessageSender(message, args);
2509
2515
  this.emit('outgoingMessage', { message: messageSender.message });
2510
2516
  messageSender.send(this.configuration.websocketProvider);
@@ -2557,10 +2563,10 @@ class HocuspocusProvider extends EventEmitter {
2557
2563
  this.configuration.websocketProvider.off('destroy', this.configuration.onDestroy);
2558
2564
  this.configuration.websocketProvider.off('destroy', this.forwardDestroy);
2559
2565
  this.configuration.websocketProvider.detach(this);
2560
- this.isAttached = false;
2566
+ this._isAttached = false;
2561
2567
  }
2562
2568
  attach() {
2563
- if (this.isAttached)
2569
+ if (this._isAttached)
2564
2570
  return;
2565
2571
  this.configuration.websocketProvider.on('connect', this.configuration.onConnect);
2566
2572
  this.configuration.websocketProvider.on('connect', this.forwardConnect);
@@ -2573,7 +2579,7 @@ class HocuspocusProvider extends EventEmitter {
2573
2579
  this.configuration.websocketProvider.on('destroy', this.configuration.onDestroy);
2574
2580
  this.configuration.websocketProvider.on('destroy', this.forwardDestroy);
2575
2581
  this.configuration.websocketProvider.attach(this);
2576
- this.isAttached = true;
2582
+ this._isAttached = true;
2577
2583
  }
2578
2584
  permissionDeniedHandler(reason) {
2579
2585
  this.emit('authenticationFailed', { reason });
@@ -2582,7 +2588,7 @@ class HocuspocusProvider extends EventEmitter {
2582
2588
  authenticatedHandler(scope) {
2583
2589
  this.isAuthenticated = true;
2584
2590
  this.authorizedScope = scope;
2585
- this.emit('authenticated');
2591
+ this.emit('authenticated', { scope });
2586
2592
  }
2587
2593
  setAwarenessField(key, value) {
2588
2594
  if (!this.awareness) {