@hocuspocus/provider 1.0.0-alpha.32 → 1.0.0-alpha.33

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.
@@ -1,6 +1,6 @@
1
1
  import * as Y from 'yjs';
2
2
  import { retry } from '@lifeomic/attempt';
3
- import { readAuthMessage, writeAuthentication, awarenessStatesToArray, Unauthorized, Forbidden } from '@hocuspocus/common';
3
+ import { readAuthMessage, writeAuthentication, awarenessStatesToArray, WsReadyStates, Unauthorized, Forbidden } from '@hocuspocus/common';
4
4
 
5
5
  /**
6
6
  * Utility module to work with key-value stores.
@@ -1891,6 +1891,12 @@ class HocuspocusProvider extends EventEmitter {
1891
1891
  var _a;
1892
1892
  (_a = this.connectionAttempt) === null || _a === void 0 ? void 0 : _a.resolve();
1893
1893
  this.connectionAttempt = null;
1894
+ this.status = WebSocketStatus.Connected;
1895
+ this.emit('status', { status: 'connected' });
1896
+ this.emit('connect');
1897
+ }
1898
+ stopConnectionAttempt() {
1899
+ this.connectionAttempt = null;
1894
1900
  }
1895
1901
  rejectConnectionAttempt() {
1896
1902
  var _a;
@@ -1997,11 +2003,15 @@ class HocuspocusProvider extends EventEmitter {
1997
2003
  //
1998
2004
  }
1999
2005
  }
2000
- onOpen(event) {
2006
+ async onOpen(event) {
2001
2007
  this.emit('open', { event });
2002
- if (this.status !== WebSocketStatus.Connected) {
2003
- this.webSocketConnectionEstablished();
2008
+ if (this.isAuthenticationRequired) {
2009
+ this.send(AuthenticationMessage, {
2010
+ token: await this.getToken(),
2011
+ });
2012
+ return;
2004
2013
  }
2014
+ this.startSync();
2005
2015
  }
2006
2016
  async getToken() {
2007
2017
  if (typeof this.configuration.token === 'function') {
@@ -2010,18 +2020,6 @@ class HocuspocusProvider extends EventEmitter {
2010
2020
  }
2011
2021
  return this.configuration.token;
2012
2022
  }
2013
- async webSocketConnectionEstablished() {
2014
- this.status = WebSocketStatus.Connected;
2015
- this.emit('status', { status: 'connected' });
2016
- this.emit('connect');
2017
- if (this.isAuthenticationRequired) {
2018
- this.send(AuthenticationMessage, {
2019
- token: await this.getToken(),
2020
- });
2021
- return;
2022
- }
2023
- this.startSync();
2024
- }
2025
2023
  startSync() {
2026
2024
  this.send(SyncStepOneMessage, { document: this.document });
2027
2025
  if (this.awareness.getLocalState() !== null) {
@@ -2032,10 +2030,11 @@ class HocuspocusProvider extends EventEmitter {
2032
2030
  }
2033
2031
  }
2034
2032
  send(Message, args, broadcast = false) {
2033
+ var _a;
2035
2034
  if (broadcast) {
2036
2035
  this.mux(() => { this.broadcast(Message, args); });
2037
2036
  }
2038
- if (this.status === WebSocketStatus.Connected) {
2037
+ if (((_a = this.webSocket) === null || _a === void 0 ? void 0 : _a.readyState) === WsReadyStates.Open) {
2039
2038
  const messageSender = new MessageSender(Message, args);
2040
2039
  this.emit('outgoingMessage', { message: messageSender.message });
2041
2040
  messageSender.send(this.webSocket);
@@ -2099,10 +2098,10 @@ class HocuspocusProvider extends EventEmitter {
2099
2098
  }
2100
2099
  clearInterval(this.intervals.connectionChecker);
2101
2100
  removeAwarenessStates(this.awareness, [this.document.clientID], 'provider destroy');
2102
- // If there is still a connection attempt outstanding then we should resolve
2101
+ // If there is still a connection attempt outstanding then we should stop
2103
2102
  // it before calling disconnect, otherwise it will be rejected in the onClose
2104
2103
  // handler and trigger a retry
2105
- this.resolveConnectionAttempt();
2104
+ this.stopConnectionAttempt();
2106
2105
  this.disconnect();
2107
2106
  this.awareness.off('update', this.awarenessUpdateHandler);
2108
2107
  this.document.off('update', this.documentUpdateHandler);