@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.
@@ -1915,6 +1915,12 @@ class HocuspocusProvider extends EventEmitter {
1915
1915
  var _a;
1916
1916
  (_a = this.connectionAttempt) === null || _a === void 0 ? void 0 : _a.resolve();
1917
1917
  this.connectionAttempt = null;
1918
+ this.status = exports.WebSocketStatus.Connected;
1919
+ this.emit('status', { status: 'connected' });
1920
+ this.emit('connect');
1921
+ }
1922
+ stopConnectionAttempt() {
1923
+ this.connectionAttempt = null;
1918
1924
  }
1919
1925
  rejectConnectionAttempt() {
1920
1926
  var _a;
@@ -2021,11 +2027,15 @@ class HocuspocusProvider extends EventEmitter {
2021
2027
  //
2022
2028
  }
2023
2029
  }
2024
- onOpen(event) {
2030
+ async onOpen(event) {
2025
2031
  this.emit('open', { event });
2026
- if (this.status !== exports.WebSocketStatus.Connected) {
2027
- this.webSocketConnectionEstablished();
2032
+ if (this.isAuthenticationRequired) {
2033
+ this.send(AuthenticationMessage, {
2034
+ token: await this.getToken(),
2035
+ });
2036
+ return;
2028
2037
  }
2038
+ this.startSync();
2029
2039
  }
2030
2040
  async getToken() {
2031
2041
  if (typeof this.configuration.token === 'function') {
@@ -2034,18 +2044,6 @@ class HocuspocusProvider extends EventEmitter {
2034
2044
  }
2035
2045
  return this.configuration.token;
2036
2046
  }
2037
- async webSocketConnectionEstablished() {
2038
- this.status = exports.WebSocketStatus.Connected;
2039
- this.emit('status', { status: 'connected' });
2040
- this.emit('connect');
2041
- if (this.isAuthenticationRequired) {
2042
- this.send(AuthenticationMessage, {
2043
- token: await this.getToken(),
2044
- });
2045
- return;
2046
- }
2047
- this.startSync();
2048
- }
2049
2047
  startSync() {
2050
2048
  this.send(SyncStepOneMessage, { document: this.document });
2051
2049
  if (this.awareness.getLocalState() !== null) {
@@ -2056,10 +2054,11 @@ class HocuspocusProvider extends EventEmitter {
2056
2054
  }
2057
2055
  }
2058
2056
  send(Message, args, broadcast = false) {
2057
+ var _a;
2059
2058
  if (broadcast) {
2060
2059
  this.mux(() => { this.broadcast(Message, args); });
2061
2060
  }
2062
- if (this.status === exports.WebSocketStatus.Connected) {
2061
+ if (((_a = this.webSocket) === null || _a === void 0 ? void 0 : _a.readyState) === common.WsReadyStates.Open) {
2063
2062
  const messageSender = new MessageSender(Message, args);
2064
2063
  this.emit('outgoingMessage', { message: messageSender.message });
2065
2064
  messageSender.send(this.webSocket);
@@ -2123,10 +2122,10 @@ class HocuspocusProvider extends EventEmitter {
2123
2122
  }
2124
2123
  clearInterval(this.intervals.connectionChecker);
2125
2124
  removeAwarenessStates(this.awareness, [this.document.clientID], 'provider destroy');
2126
- // If there is still a connection attempt outstanding then we should resolve
2125
+ // If there is still a connection attempt outstanding then we should stop
2127
2126
  // it before calling disconnect, otherwise it will be rejected in the onClose
2128
2127
  // handler and trigger a retry
2129
- this.resolveConnectionAttempt();
2128
+ this.stopConnectionAttempt();
2130
2129
  this.disconnect();
2131
2130
  this.awareness.off('update', this.awarenessUpdateHandler);
2132
2131
  this.document.off('update', this.documentUpdateHandler);