@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.
@@ -1351,7 +1351,6 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1351
1351
  };
1352
1352
  this.connectionAttempt = null;
1353
1353
  this.receivedOnOpenPayload = undefined;
1354
- this.receivedOnStatusPayload = undefined;
1355
1354
  this.closeTries = 0;
1356
1355
  this.setConfiguration(configuration);
1357
1356
  this.configuration.WebSocketPolyfill = configuration.WebSocketPolyfill
@@ -1363,7 +1362,6 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1363
1362
  this.on('message', this.configuration.onMessage);
1364
1363
  this.on('outgoingMessage', this.configuration.onOutgoingMessage);
1365
1364
  this.on('status', this.configuration.onStatus);
1366
- this.on('status', this.onStatus.bind(this));
1367
1365
  this.on('disconnect', this.configuration.onDisconnect);
1368
1366
  this.on('close', this.configuration.onClose);
1369
1367
  this.on('destroy', this.configuration.onDestroy);
@@ -1384,15 +1382,12 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1384
1382
  this.cancelWebsocketRetry = undefined;
1385
1383
  this.receivedOnOpenPayload = event;
1386
1384
  }
1387
- async onStatus(data) {
1388
- this.receivedOnStatusPayload = data;
1389
- }
1390
1385
  attach(provider) {
1391
1386
  this.configuration.providerMap.set(provider.configuration.name, provider);
1392
1387
  if (this.status === exports.WebSocketStatus.Disconnected && this.shouldConnect) {
1393
1388
  this.connect();
1394
1389
  }
1395
- if (this.receivedOnOpenPayload) {
1390
+ if (this.receivedOnOpenPayload && this.status === exports.WebSocketStatus.Connected) {
1396
1391
  provider.onOpen(this.receivedOnOpenPayload);
1397
1392
  }
1398
1393
  }
@@ -1415,7 +1410,6 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1415
1410
  this.cancelWebsocketRetry = undefined;
1416
1411
  }
1417
1412
  this.receivedOnOpenPayload = undefined;
1418
- this.receivedOnStatusPayload = undefined;
1419
1413
  this.shouldConnect = true;
1420
1414
  const abortableRetry = () => {
1421
1415
  let cancelAttempt = false;
@@ -2342,6 +2336,7 @@ class HocuspocusProvider extends EventEmitter {
2342
2336
  onAwarenessUpdate: () => null,
2343
2337
  onAwarenessChange: () => null,
2344
2338
  onStateless: () => null,
2339
+ onUnsyncedChanges: () => null,
2345
2340
  };
2346
2341
  this.isSynced = false;
2347
2342
  this.unsyncedChanges = 0;
@@ -2349,7 +2344,7 @@ class HocuspocusProvider extends EventEmitter {
2349
2344
  this.authorizedScope = undefined;
2350
2345
  // @internal
2351
2346
  this.manageSocket = false;
2352
- this.isAttached = false;
2347
+ this._isAttached = false;
2353
2348
  this.intervals = {
2354
2349
  forceSync: null,
2355
2350
  };
@@ -2359,7 +2354,6 @@ class HocuspocusProvider extends EventEmitter {
2359
2354
  this.boundOnOpen = this.onOpen.bind(this);
2360
2355
  this.boundOnClose = this.onClose.bind(this);
2361
2356
  this.forwardConnect = (e) => this.emit('connect', e);
2362
- // forwardOpen = (e: any) => this.emit('open', e)
2363
2357
  this.forwardClose = (e) => this.emit('close', e);
2364
2358
  this.forwardDisconnect = (e) => this.emit('disconnect', e);
2365
2359
  this.forwardDestroy = (e) => this.emit('destroy', e);
@@ -2374,6 +2368,7 @@ class HocuspocusProvider extends EventEmitter {
2374
2368
  this.on('awarenessUpdate', this.configuration.onAwarenessUpdate);
2375
2369
  this.on('awarenessChange', this.configuration.onAwarenessChange);
2376
2370
  this.on('stateless', this.configuration.onStateless);
2371
+ this.on('unsyncedChanges', this.configuration.onUnsyncedChanges);
2377
2372
  this.on('authenticated', this.configuration.onAuthenticated);
2378
2373
  this.on('authenticationFailed', this.configuration.onAuthenticationFailed);
2379
2374
  (_a = this.awareness) === null || _a === void 0 ? void 0 : _a.on('update', () => {
@@ -2406,6 +2401,9 @@ class HocuspocusProvider extends EventEmitter {
2406
2401
  get document() {
2407
2402
  return this.configuration.document;
2408
2403
  }
2404
+ get isAttached() {
2405
+ return this._isAttached;
2406
+ }
2409
2407
  get awareness() {
2410
2408
  return this.configuration.awareness;
2411
2409
  }
@@ -2414,11 +2412,11 @@ class HocuspocusProvider extends EventEmitter {
2414
2412
  }
2415
2413
  resetUnsyncedChanges() {
2416
2414
  this.unsyncedChanges = 1;
2417
- this.emit('unsyncedChanges', this.unsyncedChanges);
2415
+ this.emit('unsyncedChanges', { number: this.unsyncedChanges });
2418
2416
  }
2419
2417
  incrementUnsyncedChanges() {
2420
2418
  this.unsyncedChanges += 1;
2421
- this.emit('unsyncedChanges', this.unsyncedChanges);
2419
+ this.emit('unsyncedChanges', { number: this.unsyncedChanges });
2422
2420
  }
2423
2421
  decrementUnsyncedChanges() {
2424
2422
  if (this.unsyncedChanges > 0) {
@@ -2427,7 +2425,7 @@ class HocuspocusProvider extends EventEmitter {
2427
2425
  if (this.unsyncedChanges === 0) {
2428
2426
  this.synced = true;
2429
2427
  }
2430
- this.emit('unsyncedChanges', this.unsyncedChanges);
2428
+ this.emit('unsyncedChanges', { number: this.unsyncedChanges });
2431
2429
  }
2432
2430
  forceSync() {
2433
2431
  this.resetUnsyncedChanges();
@@ -2485,9 +2483,15 @@ class HocuspocusProvider extends EventEmitter {
2485
2483
  }
2486
2484
  // not needed, but provides backward compatibility with e.g. lexical/yjs
2487
2485
  async connect() {
2486
+ if (this.manageSocket) {
2487
+ return this.configuration.websocketProvider.connect();
2488
+ }
2488
2489
  console.warn('HocuspocusProvider::connect() is deprecated and does not do anything. Please connect/disconnect on the websocketProvider, or attach/deattach providers.');
2489
2490
  }
2490
2491
  disconnect() {
2492
+ if (this.manageSocket) {
2493
+ return this.configuration.websocketProvider.disconnect();
2494
+ }
2491
2495
  console.warn('HocuspocusProvider::disconnect() is deprecated and does not do anything. Please connect/disconnect on the websocketProvider, or attach/deattach providers.');
2492
2496
  }
2493
2497
  async onOpen(event) {
@@ -2526,6 +2530,8 @@ class HocuspocusProvider extends EventEmitter {
2526
2530
  }
2527
2531
  }
2528
2532
  send(message, args) {
2533
+ if (!this._isAttached)
2534
+ return;
2529
2535
  const messageSender = new MessageSender(message, args);
2530
2536
  this.emit('outgoingMessage', { message: messageSender.message });
2531
2537
  messageSender.send(this.configuration.websocketProvider);
@@ -2578,10 +2584,10 @@ class HocuspocusProvider extends EventEmitter {
2578
2584
  this.configuration.websocketProvider.off('destroy', this.configuration.onDestroy);
2579
2585
  this.configuration.websocketProvider.off('destroy', this.forwardDestroy);
2580
2586
  this.configuration.websocketProvider.detach(this);
2581
- this.isAttached = false;
2587
+ this._isAttached = false;
2582
2588
  }
2583
2589
  attach() {
2584
- if (this.isAttached)
2590
+ if (this._isAttached)
2585
2591
  return;
2586
2592
  this.configuration.websocketProvider.on('connect', this.configuration.onConnect);
2587
2593
  this.configuration.websocketProvider.on('connect', this.forwardConnect);
@@ -2594,7 +2600,7 @@ class HocuspocusProvider extends EventEmitter {
2594
2600
  this.configuration.websocketProvider.on('destroy', this.configuration.onDestroy);
2595
2601
  this.configuration.websocketProvider.on('destroy', this.forwardDestroy);
2596
2602
  this.configuration.websocketProvider.attach(this);
2597
- this.isAttached = true;
2603
+ this._isAttached = true;
2598
2604
  }
2599
2605
  permissionDeniedHandler(reason) {
2600
2606
  this.emit('authenticationFailed', { reason });
@@ -2603,7 +2609,7 @@ class HocuspocusProvider extends EventEmitter {
2603
2609
  authenticatedHandler(scope) {
2604
2610
  this.isAuthenticated = true;
2605
2611
  this.authorizedScope = scope;
2606
- this.emit('authenticated');
2612
+ this.emit('authenticated', { scope });
2607
2613
  }
2608
2614
  setAwarenessField(key, value) {
2609
2615
  if (!this.awareness) {