@@ -5,10 +5,11 @@ import {
5
5
CompleteHocuspocusProviderWebsocketConfiguration ,
6
6
HocuspocusProvider ,
7
7
HocuspocusProviderConfiguration ,
8
+ WebSocketStatus ,
8
9
onOutgoingMessageParameters ,
9
10
onStatusParameters ,
10
11
} from '@hocuspocus/provider' ;
11
- import type { CloseEvent , MessageEvent } from 'ws' ;
12
+ import type { MessageEvent } from 'ws' ;
12
13
import * as Y from 'yjs' ;
13
14
14
15
import { isAPIError } from '@/api' ;
@@ -43,9 +44,8 @@ export class CollaborationProvider extends HocuspocusProvider {
43
44
public seemsUnsyncCount = 0 ;
44
45
public seemsUnsyncMaxCount = 5 ;
45
46
protected sse : EventSource | null = null ; // Server-Sent Events
47
+ protected pollTimeout : NodeJS . Timeout | null = null ;
46
48
protected url = '' ;
47
- public websocketFailureCount = 0 ;
48
- public websocketMaxFailureCount = 2 ;
49
49
50
50
public constructor ( configuration : CollaborationProviderConfiguration ) {
51
51
const withWS = false ;
@@ -77,12 +77,15 @@ export class CollaborationProvider extends HocuspocusProvider {
77
77
}
78
78
79
79
public setPollDefaultValues ( ) : void {
80
+ console . log ( 'setPollDefaultValues:' ) ;
80
81
this . isLongPollingStarted = false ;
81
82
this . isWebsocketFailed = false ;
82
83
this . seemsUnsyncCount = 0 ;
83
84
this . sse ?. close ( ) ;
84
85
this . sse = null ;
85
- this . websocketFailureCount = 0 ;
86
+ if ( this . pollTimeout ) {
87
+ clearTimeout ( this . pollTimeout ) ;
88
+ }
86
89
}
87
90
88
91
public destroy ( ) : void {
@@ -91,6 +94,7 @@ export class CollaborationProvider extends HocuspocusProvider {
91
94
}
92
95
93
96
public onWebsocketConnect = ( ) => {
97
+ console . log ( 'onWebsocketConnect:' ) ;
94
98
this . setPollDefaultValues ( ) ;
95
99
} ;
96
100
@@ -100,33 +104,44 @@ export class CollaborationProvider extends HocuspocusProvider {
100
104
101
105
public onStatus ( { status } : onStatusParameters ) {
102
106
console . log ( 'status:' , status ) ;
107
+ if ( status === WebSocketStatus . Connecting ) {
108
+ if ( this . pollTimeout ) {
109
+ clearTimeout ( this . pollTimeout ) ;
110
+ }
111
+ this . pollTimeout = setTimeout ( ( ) => {
112
+ this . initPolling ( ) ;
113
+ } , 5000 ) ;
114
+ } else if ( status === WebSocketStatus . Connected ) {
115
+ this . setPollDefaultValues ( ) ;
116
+ }
103
117
104
118
super . onStatus ( { status } ) ;
105
119
}
106
120
107
- public onClose ( event : CloseEvent ) : void {
108
- console . log ( 'close:' , event ) ;
109
- this . isAuthenticated = false ;
110
- this . synced = false ;
121
+ public initPolling ( ) {
122
+ this . isWebsocketFailed = true ;
111
123
112
- this . websocketFailureCount += 1 ;
124
+ if ( this . isLongPollingStarted ) {
125
+ return ;
126
+ }
113
127
114
- if (
115
- ! this . isWebsocketFailed &&
116
- this . websocketFailureCount > this . websocketMaxFailureCount
117
- ) {
118
- this . isWebsocketFailed = true ;
128
+ console . log ( 'initPolling:' , {
129
+ isLongPollingStarted : this . isLongPollingStarted ,
130
+ } ) ;
119
131
120
- if ( ! this . isLongPollingStarted ) {
121
- this . isLongPollingStarted = true ;
122
- void this . pollSync ( true ) ;
123
- this . initCollaborationSSE ( ) ;
124
- }
125
- } else if ( ! this . isWebsocketFailed ) {
126
- super . onClose ( event ) ;
127
- }
132
+ this . isLongPollingStarted = true ;
133
+ void this . pollSync ( true ) ;
134
+ this . initCollaborationSSE ( ) ;
128
135
}
129
136
137
+ // public onClose(event: CloseEvent): void {
138
+ // console.log('close:', event);
139
+
140
+ // if (!this.isWebsocketFailed) {
141
+ // super.onClose(event);
142
+ // }
143
+ // }
144
+
130
145
protected toPollUrl ( endpoint : string ) : string {
131
146
let pollUrl = this . url . replace ( 'ws:' , 'http:' ) ;
132
147
if ( pollUrl . includes ( 'wss:' ) ) {
0 commit comments