@@ -27,6 +27,10 @@ export const isHocuspocusProviderConfigurationUrl = (
27
27
return 'url' in data ;
28
28
} ;
29
29
30
+ type CollaborationProviderConfiguration = HocuspocusProviderConfiguration & {
31
+ canEdit : boolean ;
32
+ } ;
33
+
30
34
export class CollaborationProvider extends HocuspocusProvider {
31
35
private websocketFailureCount = 0 ;
32
36
private websocketMaxFailureCount = 2 ;
@@ -37,7 +41,7 @@ export class CollaborationProvider extends HocuspocusProvider {
37
41
// Server-Sent Events
38
42
private sse : EventSource | null = null ;
39
43
40
- public constructor ( configuration : HocuspocusProviderConfiguration ) {
44
+ public constructor ( configuration : CollaborationProviderConfiguration ) {
41
45
const withWS = isFirefox ( ) ;
42
46
43
47
let url = '' ;
@@ -50,7 +54,10 @@ export class CollaborationProvider extends HocuspocusProvider {
50
54
51
55
this . url = url ;
52
56
53
- this . on ( 'outgoingMessage' , this . onPollOutgoingMessage . bind ( this ) ) ;
57
+ if ( configuration . canEdit ) {
58
+ this . on ( 'outgoingMessage' , this . onPollOutgoingMessage . bind ( this ) ) ;
59
+ }
60
+
54
61
this . configuration . websocketProvider . on (
55
62
'connect' ,
56
63
this . onWebsocketConnect . bind ( this ) ,
@@ -92,7 +99,6 @@ export class CollaborationProvider extends HocuspocusProvider {
92
99
this . isLongPollingStarted = true ;
93
100
void this . pollSync ( ) ;
94
101
this . initCollaborationSSE ( ) ;
95
- //void this.longPollAwareness();
96
102
}
97
103
}
98
104
}
@@ -116,14 +122,18 @@ export class CollaborationProvider extends HocuspocusProvider {
116
122
117
123
//console.log('outgoingMessage', message.description);
118
124
119
- const { updated } = await pollOutgoingMessageRequest ( {
120
- pollUrl : this . toPollUrl ( 'message' ) ,
121
- message64 : Buffer . from ( message . toUint8Array ( ) ) . toString ( 'base64' ) ,
122
- } ) ;
125
+ try {
126
+ const { updated } = await pollOutgoingMessageRequest ( {
127
+ pollUrl : this . toPollUrl ( 'message' ) ,
128
+ message64 : Buffer . from ( message . toUint8Array ( ) ) . toString ( 'base64' ) ,
129
+ } ) ;
123
130
124
- if ( ! updated ) {
125
- console . error ( 'Message not updated' ) ;
126
- await this . pollSync ( ) ;
131
+ if ( ! updated ) {
132
+ console . error ( 'Message not updated' ) ;
133
+ await this . pollSync ( ) ;
134
+ }
135
+ } catch ( error : unknown ) {
136
+ console . error ( 'Polling message failed:' , error ) ;
127
137
}
128
138
}
129
139
@@ -134,52 +144,60 @@ export class CollaborationProvider extends HocuspocusProvider {
134
144
135
145
console . log ( 'initCollaborationSSE' ) ;
136
146
137
- const eventSource = new EventSource ( this . toPollUrl ( 'message' ) , {
147
+ this . sse = new EventSource ( this . toPollUrl ( 'message' ) , {
138
148
withCredentials : true ,
139
149
} ) ;
140
150
151
+ //eventSource.close();
152
+
141
153
// 1. onmessage handles messages sent with `data:` lines
142
- eventSource . onmessage = async ( event ) => {
143
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-argument
154
+ this . sse . onmessage = ( event ) => {
144
155
const { updatedDoc64, stateFingerprint, awareness64 } = JSON . parse (
156
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
145
157
event . data ,
146
158
) as {
147
159
updatedDoc64 : string ;
148
160
stateFingerprint : string ;
149
161
awareness64 : string ;
150
162
} ;
151
163
console . log ( 'Received SSE event:' , event . data ) ;
164
+ console . log ( 'CLineId:' , this . document . clientID ) ;
165
+
166
+ const localStateFingerprint = this . getStateFingerprint ( this . document ) ;
167
+ console . log ( 'EQUAL BEF' , localStateFingerprint === stateFingerprint ) ;
152
168
153
169
if ( awareness64 ) {
154
170
const awareness = Buffer . from ( awareness64 , 'base64' ) ;
155
171
156
172
this . onMessage ( {
157
173
data : awareness ,
158
174
} as MessageEvent ) ;
175
+
176
+ console . log ( 'EQUAL AWA' , localStateFingerprint === stateFingerprint ) ;
177
+ // if (localStateFingerprint !== stateFingerprint) {
178
+ // await this.pollSync();
179
+ // }
159
180
}
160
181
161
182
if ( updatedDoc64 ) {
162
183
const uint8Array = Buffer . from ( updatedDoc64 , 'base64' ) ;
163
184
Y . applyUpdate ( this . document , uint8Array ) ;
164
185
165
- const localStateFingerprint = this . getStateFingerprint ( this . document ) ;
166
- console . log ( 'stateFingerprint' , stateFingerprint ) ;
167
- console . log ( 'localStateFingerprint' , localStateFingerprint ) ;
168
186
console . log ( 'EQUAL' , localStateFingerprint === stateFingerprint ) ;
169
187
170
- if ( localStateFingerprint !== stateFingerprint ) {
171
- await this . pollSync ( ) ;
172
- }
188
+ // if (localStateFingerprint !== stateFingerprint) {
189
+ // await this.pollSync();
190
+ // }
173
191
}
174
192
} ;
175
193
176
194
// 2. onopen is triggered when the connection is first established
177
- eventSource . onopen = ( ) => {
195
+ this . sse . onopen = ( ) => {
178
196
console . log ( 'SSE connection opened.' ) ;
179
197
} ;
180
198
181
199
// 3. onerror is triggered if there's a connection issue
182
- eventSource . onerror = ( err ) => {
200
+ this . sse . onerror = ( err ) => {
183
201
console . error ( 'SSE error:' , err ) ;
184
202
// Depending on the error, the browser may or may not automatically reconnect
185
203
} ;
@@ -190,9 +208,9 @@ export class CollaborationProvider extends HocuspocusProvider {
190
208
public onMessage ( event : MessageEvent ) {
191
209
super . onMessage ( event ) ;
192
210
193
- console . log ( 'onMessage' , event ) ;
194
- console . log ( 'isSynced' , this . isSynced ) ;
195
- console . log ( 'unsyncedChanges' , this . unsyncedChanges ) ;
211
+ // console.log('onMessage', event);
212
+ // console.log('isSynced', this.isSynced);
213
+ // console.log('unsyncedChanges', this.unsyncedChanges);
196
214
197
215
// if (this.hasUnsyncedChanges) {
198
216
// this.unsyncedChanges = 0;
@@ -216,7 +234,6 @@ export class CollaborationProvider extends HocuspocusProvider {
216
234
if ( syncDoc64 ) {
217
235
const uint8Array = Buffer . from ( syncDoc64 , 'base64' ) ;
218
236
Y . applyUpdate ( this . document , uint8Array ) ;
219
- this . synced = true ;
220
237
}
221
238
} catch ( error ) {
222
239
console . error ( 'Polling sync failed:' , error ) ;
0 commit comments