@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.
@@ -122,7 +122,7 @@ export declare class HocuspocusProvider extends EventEmitter {
122
122
  } | null;
123
123
  constructor(options?: Partial<HocuspocusProviderOptions>);
124
124
  setOptions(options?: Partial<HocuspocusProviderOptions>): void;
125
- connect(): void;
125
+ connect(): Promise<void>;
126
126
  createWebSocketConnection(): Promise<unknown>;
127
127
  resolveConnectionAttempt(): void;
128
128
  rejectConnectionAttempt(): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hocuspocus/provider",
3
- "version": "1.0.0-alpha.17",
3
+ "version": "1.0.0-alpha.18",
4
4
  "description": "hocuspocus provider",
5
5
  "homepage": "https://hocuspocus.dev",
6
6
  "keywords": [
@@ -28,7 +28,7 @@
28
28
  "y-protocols": "^1.0.5",
29
29
  "yjs": "^13.5.8"
30
30
  },
31
- "gitHead": "b2c6c6711d5de4dd352aaaf32e43e4f33d46068e",
31
+ "gitHead": "b3f043fa2f99dd35bd68940efcb0d5b86a165585",
32
32
  "publishConfig": {
33
33
  "access": "public"
34
34
  }
@@ -251,7 +251,7 @@ export class HocuspocusProvider extends EventEmitter {
251
251
  this.options = { ...this.options, ...options }
252
252
  }
253
253
 
254
- connect() {
254
+ async connect() {
255
255
  if (this.status === WebSocketStatus.Connected) {
256
256
  return
257
257
  }
@@ -259,21 +259,29 @@ export class HocuspocusProvider extends EventEmitter {
259
259
  this.shouldConnect = true
260
260
  this.subscribeToBroadcastChannel()
261
261
 
262
- retry(this.createWebSocketConnection.bind(this), {
263
- delay: this.options.delay,
264
- initialDelay: this.options.initialDelay,
265
- factor: this.options.factor,
266
- maxAttempts: this.options.maxAttempts,
267
- minDelay: this.options.minDelay,
268
- maxDelay: this.options.maxDelay,
269
- jitter: this.options.jitter,
270
- timeout: this.options.timeout,
271
- beforeAttempt: context => {
272
- if (!this.shouldConnect) {
273
- context.abort()
274
- }
275
- },
276
- })
262
+ try {
263
+ await retry(this.createWebSocketConnection.bind(this), {
264
+ delay: this.options.delay,
265
+ initialDelay: this.options.initialDelay,
266
+ factor: this.options.factor,
267
+ maxAttempts: this.options.maxAttempts,
268
+ minDelay: this.options.minDelay,
269
+ maxDelay: this.options.maxDelay,
270
+ jitter: this.options.jitter,
271
+ timeout: this.options.timeout,
272
+ beforeAttempt: context => {
273
+ if (!this.shouldConnect) {
274
+ context.abort()
275
+ }
276
+ },
277
+ })
278
+ } catch (err: any) {
279
+ // If we aborted the connection attempt then don't throw an error
280
+ // ref: https://github.com/lifeomic/attempt/blob/master/src/index.ts#L136
281
+ if (err.code !== 'ATTEMPT_ABORTED') {
282
+ throw err
283
+ }
284
+ }
277
285
  }
278
286
 
279
287
  createWebSocketConnection() {
@@ -557,10 +565,15 @@ export class HocuspocusProvider extends EventEmitter {
557
565
 
558
566
  clearInterval(this.intervals.connectionChecker)
559
567
 
560
- this.disconnect()
561
-
562
568
  removeAwarenessStates(this.awareness, [this.document.clientID], 'provider destroy')
563
569
 
570
+ // If there is still a connection attempt outstanding then we should resolve
571
+ // it before calling disconnect, otherwise it will be rejected in the onClose
572
+ // handler and trigger a retry
573
+ this.resolveConnectionAttempt()
574
+
575
+ this.disconnect()
576
+
564
577
  this.awareness.off('update', this.awarenessUpdateHandler)
565
578
  this.document.off('update', this.documentUpdateHandler)
566
579