@hocuspocus/provider 1.0.0-alpha.17 → 1.0.0-alpha.18

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.
@@ -1886,27 +1886,36 @@ class HocuspocusProvider extends EventEmitter {
1886
1886
  setOptions(options = {}) {
1887
1887
  this.options = { ...this.options, ...options };
1888
1888
  }
1889
- connect() {
1889
+ async connect() {
1890
1890
  if (this.status === exports.WebSocketStatus.Connected) {
1891
1891
  return;
1892
1892
  }
1893
1893
  this.shouldConnect = true;
1894
1894
  this.subscribeToBroadcastChannel();
1895
- attempt.retry(this.createWebSocketConnection.bind(this), {
1896
- delay: this.options.delay,
1897
- initialDelay: this.options.initialDelay,
1898
- factor: this.options.factor,
1899
- maxAttempts: this.options.maxAttempts,
1900
- minDelay: this.options.minDelay,
1901
- maxDelay: this.options.maxDelay,
1902
- jitter: this.options.jitter,
1903
- timeout: this.options.timeout,
1904
- beforeAttempt: context => {
1905
- if (!this.shouldConnect) {
1906
- context.abort();
1907
- }
1908
- },
1909
- });
1895
+ try {
1896
+ await attempt.retry(this.createWebSocketConnection.bind(this), {
1897
+ delay: this.options.delay,
1898
+ initialDelay: this.options.initialDelay,
1899
+ factor: this.options.factor,
1900
+ maxAttempts: this.options.maxAttempts,
1901
+ minDelay: this.options.minDelay,
1902
+ maxDelay: this.options.maxDelay,
1903
+ jitter: this.options.jitter,
1904
+ timeout: this.options.timeout,
1905
+ beforeAttempt: context => {
1906
+ if (!this.shouldConnect) {
1907
+ context.abort();
1908
+ }
1909
+ },
1910
+ });
1911
+ }
1912
+ catch (err) {
1913
+ // If we aborted the connection attempt then don't throw an error
1914
+ // ref: https://github.com/lifeomic/attempt/blob/master/src/index.ts#L136
1915
+ if (err.code !== 'ATTEMPT_ABORTED') {
1916
+ throw err;
1917
+ }
1918
+ }
1910
1919
  }
1911
1920
  createWebSocketConnection() {
1912
1921
  return new Promise((resolve, reject) => {
@@ -2128,8 +2137,12 @@ class HocuspocusProvider extends EventEmitter {
2128
2137
  clearInterval(this.intervals.forceSync);
2129
2138
  }
2130
2139
  clearInterval(this.intervals.connectionChecker);
2131
- this.disconnect();
2132
2140
  removeAwarenessStates(this.awareness, [this.document.clientID], 'provider destroy');
2141
+ // If there is still a connection attempt outstanding then we should resolve
2142
+ // it before calling disconnect, otherwise it will be rejected in the onClose
2143
+ // handler and trigger a retry
2144
+ this.resolveConnectionAttempt();
2145
+ this.disconnect();
2133
2146
  this.awareness.off('update', this.awarenessUpdateHandler);
2134
2147
  this.document.off('update', this.documentUpdateHandler);
2135
2148
  this.removeAllListeners();