@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.
@@ -1862,27 +1862,36 @@ class HocuspocusProvider extends EventEmitter {
1862
1862
  setOptions(options = {}) {
1863
1863
  this.options = { ...this.options, ...options };
1864
1864
  }
1865
- connect() {
1865
+ async connect() {
1866
1866
  if (this.status === WebSocketStatus.Connected) {
1867
1867
  return;
1868
1868
  }
1869
1869
  this.shouldConnect = true;
1870
1870
  this.subscribeToBroadcastChannel();
1871
- retry(this.createWebSocketConnection.bind(this), {
1872
- delay: this.options.delay,
1873
- initialDelay: this.options.initialDelay,
1874
- factor: this.options.factor,
1875
- maxAttempts: this.options.maxAttempts,
1876
- minDelay: this.options.minDelay,
1877
- maxDelay: this.options.maxDelay,
1878
- jitter: this.options.jitter,
1879
- timeout: this.options.timeout,
1880
- beforeAttempt: context => {
1881
- if (!this.shouldConnect) {
1882
- context.abort();
1883
- }
1884
- },
1885
- });
1871
+ try {
1872
+ await retry(this.createWebSocketConnection.bind(this), {
1873
+ delay: this.options.delay,
1874
+ initialDelay: this.options.initialDelay,
1875
+ factor: this.options.factor,
1876
+ maxAttempts: this.options.maxAttempts,
1877
+ minDelay: this.options.minDelay,
1878
+ maxDelay: this.options.maxDelay,
1879
+ jitter: this.options.jitter,
1880
+ timeout: this.options.timeout,
1881
+ beforeAttempt: context => {
1882
+ if (!this.shouldConnect) {
1883
+ context.abort();
1884
+ }
1885
+ },
1886
+ });
1887
+ }
1888
+ catch (err) {
1889
+ // If we aborted the connection attempt then don't throw an error
1890
+ // ref: https://github.com/lifeomic/attempt/blob/master/src/index.ts#L136
1891
+ if (err.code !== 'ATTEMPT_ABORTED') {
1892
+ throw err;
1893
+ }
1894
+ }
1886
1895
  }
1887
1896
  createWebSocketConnection() {
1888
1897
  return new Promise((resolve, reject) => {
@@ -2104,8 +2113,12 @@ class HocuspocusProvider extends EventEmitter {
2104
2113
  clearInterval(this.intervals.forceSync);
2105
2114
  }
2106
2115
  clearInterval(this.intervals.connectionChecker);
2107
- this.disconnect();
2108
2116
  removeAwarenessStates(this.awareness, [this.document.clientID], 'provider destroy');
2117
+ // If there is still a connection attempt outstanding then we should resolve
2118
+ // it before calling disconnect, otherwise it will be rejected in the onClose
2119
+ // handler and trigger a retry
2120
+ this.resolveConnectionAttempt();
2121
+ this.disconnect();
2109
2122
  this.awareness.off('update', this.awarenessUpdateHandler);
2110
2123
  this.document.off('update', this.documentUpdateHandler);
2111
2124
  this.removeAllListeners();