@hocuspocus/provider 3.0.0-rc.0 → 3.0.6-rc.0
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.
- package/dist/hocuspocus-provider.cjs +773 -11306
- package/dist/hocuspocus-provider.cjs.map +1 -1
- package/dist/hocuspocus-provider.esm.js +753 -11303
- package/dist/hocuspocus-provider.esm.js.map +1 -1
- package/dist/packages/common/src/index.d.ts +4 -4
- package/dist/packages/extension-database/src/index.d.ts +1 -1
- package/dist/packages/extension-logger/src/index.d.ts +1 -1
- package/dist/packages/extension-redis/src/Redis.d.ts +6 -2
- package/dist/packages/extension-redis/src/index.d.ts +1 -1
- package/dist/packages/extension-sqlite/src/index.d.ts +1 -1
- package/dist/packages/provider/src/HocuspocusProvider.d.ts +14 -36
- package/dist/packages/provider/src/HocuspocusProviderWebsocket.d.ts +4 -8
- package/dist/packages/provider/src/IncomingMessage.d.ts +2 -2
- package/dist/packages/provider/src/MessageReceiver.d.ts +2 -4
- package/dist/packages/provider/src/MessageSender.d.ts +2 -2
- package/dist/packages/provider/src/OutgoingMessage.d.ts +2 -2
- package/dist/packages/provider/src/OutgoingMessages/AuthenticationMessage.d.ts +3 -3
- package/dist/packages/provider/src/OutgoingMessages/AwarenessMessage.d.ts +3 -3
- package/dist/packages/provider/src/OutgoingMessages/CloseMessage.d.ts +3 -3
- package/dist/packages/provider/src/OutgoingMessages/QueryAwarenessMessage.d.ts +3 -3
- package/dist/packages/provider/src/OutgoingMessages/StatelessMessage.d.ts +3 -3
- package/dist/packages/provider/src/OutgoingMessages/SyncStepOneMessage.d.ts +3 -3
- package/dist/packages/provider/src/OutgoingMessages/SyncStepTwoMessage.d.ts +3 -3
- package/dist/packages/provider/src/OutgoingMessages/UpdateMessage.d.ts +3 -3
- package/dist/packages/provider/src/index.d.ts +3 -5
- package/dist/packages/provider/src/types.d.ts +50 -10
- package/dist/packages/server/src/ClientConnection.d.ts +16 -8
- package/dist/packages/server/src/Connection.d.ts +14 -20
- package/dist/packages/server/src/DirectConnection.d.ts +4 -4
- package/dist/packages/server/src/Document.d.ts +2 -6
- package/dist/packages/server/src/Hocuspocus.d.ts +6 -15
- package/dist/packages/server/src/IncomingMessage.d.ts +4 -3
- package/dist/packages/server/src/MessageReceiver.d.ts +6 -8
- package/dist/packages/server/src/OutgoingMessage.d.ts +2 -1
- package/dist/packages/server/src/Server.d.ts +3 -3
- package/dist/packages/server/src/index.d.ts +9 -10
- package/dist/packages/server/src/types.d.ts +41 -13
- package/dist/packages/transformer/src/Prosemirror.d.ts +1 -1
- package/dist/packages/transformer/src/Tiptap.d.ts +1 -1
- package/dist/packages/transformer/src/index.d.ts +3 -3
- package/dist/playground/frontend/app/SocketContext.d.ts +2 -0
- package/dist/playground/frontend/next.config.d.ts +3 -0
- package/dist/tests/utils/index.d.ts +9 -9
- package/dist/tests/utils/newHocuspocusProvider.d.ts +2 -2
- package/package.json +3 -3
- package/src/HocuspocusProvider.ts +88 -210
- package/src/HocuspocusProviderWebsocket.ts +23 -73
- package/src/IncomingMessage.ts +1 -1
- package/src/MessageReceiver.ts +22 -21
- package/src/MessageSender.ts +1 -1
- package/src/OutgoingMessage.ts +1 -1
- package/src/OutgoingMessages/AuthenticationMessage.ts +3 -3
- package/src/OutgoingMessages/AwarenessMessage.ts +3 -3
- package/src/OutgoingMessages/CloseMessage.ts +3 -3
- package/src/OutgoingMessages/QueryAwarenessMessage.ts +3 -3
- package/src/OutgoingMessages/StatelessMessage.ts +3 -3
- package/src/OutgoingMessages/SyncStepOneMessage.ts +3 -3
- package/src/OutgoingMessages/SyncStepTwoMessage.ts +3 -3
- package/src/OutgoingMessages/UpdateMessage.ts +3 -3
- package/src/index.ts +3 -5
- package/src/types.ts +56 -10
- package/dist/packages/provider/src/TiptapCollabProvider.d.ts +0 -64
- package/dist/packages/provider/src/TiptapCollabProviderWebsocket.d.ts +0 -20
- package/dist/packages/server/src/Debugger.d.ts +0 -14
- package/dist/playground/frontend/vite.config.d.ts +0 -2
- package/dist/tests/server/getMessageLogs.d.ts +0 -1
- package/dist/tests/server/requiresAuthentication.d.ts +0 -1
- package/src/TiptapCollabProvider.ts +0 -321
- package/src/TiptapCollabProviderWebsocket.ts +0 -39
- /package/dist/{playground/frontend/src/main.d.ts → tests/server/beforeSync.d.ts} +0 -0
|
@@ -1,23 +1,21 @@
|
|
|
1
1
|
import { awarenessStatesToArray } from '@hocuspocus/common'
|
|
2
|
-
import
|
|
3
|
-
import type { CloseEvent, Event, MessageEvent } from 'ws'
|
|
2
|
+
import type { Event, MessageEvent } from 'ws'
|
|
4
3
|
import { Awareness, removeAwarenessStates } from 'y-protocols/awareness'
|
|
5
4
|
import * as Y from 'yjs'
|
|
6
|
-
import EventEmitter from './EventEmitter.
|
|
5
|
+
import EventEmitter from './EventEmitter.ts'
|
|
7
6
|
import type {
|
|
8
|
-
CompleteHocuspocusProviderWebsocketConfiguration} from './HocuspocusProviderWebsocket.
|
|
7
|
+
CompleteHocuspocusProviderWebsocketConfiguration} from './HocuspocusProviderWebsocket.ts'
|
|
9
8
|
import {
|
|
10
9
|
HocuspocusProviderWebsocket,
|
|
11
|
-
} from './HocuspocusProviderWebsocket.
|
|
12
|
-
import { IncomingMessage } from './IncomingMessage.
|
|
13
|
-
import { MessageReceiver } from './MessageReceiver.
|
|
14
|
-
import { MessageSender } from './MessageSender.
|
|
15
|
-
import { AuthenticationMessage } from './OutgoingMessages/AuthenticationMessage.
|
|
16
|
-
import { AwarenessMessage } from './OutgoingMessages/AwarenessMessage.
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
import { UpdateMessage } from './OutgoingMessages/UpdateMessage.js'
|
|
10
|
+
} from './HocuspocusProviderWebsocket.ts'
|
|
11
|
+
import { IncomingMessage } from './IncomingMessage.ts'
|
|
12
|
+
import { MessageReceiver } from './MessageReceiver.ts'
|
|
13
|
+
import { MessageSender } from './MessageSender.ts'
|
|
14
|
+
import { AuthenticationMessage } from './OutgoingMessages/AuthenticationMessage.ts'
|
|
15
|
+
import { AwarenessMessage } from './OutgoingMessages/AwarenessMessage.ts'
|
|
16
|
+
import { StatelessMessage } from './OutgoingMessages/StatelessMessage.ts'
|
|
17
|
+
import { SyncStepOneMessage } from './OutgoingMessages/SyncStepOneMessage.ts'
|
|
18
|
+
import { UpdateMessage } from './OutgoingMessages/UpdateMessage.ts'
|
|
21
19
|
import type {
|
|
22
20
|
ConstructableOutgoingMessage,
|
|
23
21
|
onAuthenticationFailedParameters,
|
|
@@ -28,9 +26,8 @@ import type {
|
|
|
28
26
|
onMessageParameters,
|
|
29
27
|
onOpenParameters,
|
|
30
28
|
onOutgoingMessageParameters, onStatelessParameters,
|
|
31
|
-
onStatusParameters,
|
|
32
29
|
onSyncedParameters,
|
|
33
|
-
} from './types.
|
|
30
|
+
} from './types.ts'
|
|
34
31
|
|
|
35
32
|
export type HocuspocusProviderConfiguration =
|
|
36
33
|
Required<Pick<CompleteHocuspocusProviderConfiguration, 'name'>>
|
|
@@ -49,10 +46,6 @@ export interface CompleteHocuspocusProviderConfiguration {
|
|
|
49
46
|
*/
|
|
50
47
|
document: Y.Doc,
|
|
51
48
|
|
|
52
|
-
/**
|
|
53
|
-
* Pass false to disable broadcasting between browser tabs.
|
|
54
|
-
*/
|
|
55
|
-
broadcast: boolean,
|
|
56
49
|
/**
|
|
57
50
|
* An Awareness instance to keep the presence state of all clients.
|
|
58
51
|
*
|
|
@@ -62,18 +55,17 @@ export interface CompleteHocuspocusProviderConfiguration {
|
|
|
62
55
|
* socket connection, or ensure that the Provider receives messages before running into `HocuspocusProviderWebsocket.messageReconnectTimeout`.
|
|
63
56
|
*/
|
|
64
57
|
awareness: Awareness | null,
|
|
58
|
+
|
|
65
59
|
/**
|
|
66
60
|
* A token that’s sent to the backend for authentication purposes.
|
|
67
61
|
*/
|
|
68
62
|
token: string | (() => string) | (() => Promise<string>) | null,
|
|
69
|
-
|
|
70
|
-
* URL parameters that should be added.
|
|
71
|
-
*/
|
|
72
|
-
parameters: { [key: string]: any },
|
|
63
|
+
|
|
73
64
|
/**
|
|
74
65
|
* Hocuspocus websocket provider
|
|
75
66
|
*/
|
|
76
67
|
websocketProvider: HocuspocusProviderWebsocket,
|
|
68
|
+
|
|
77
69
|
/**
|
|
78
70
|
* Force syncing the document in the defined interval.
|
|
79
71
|
*/
|
|
@@ -85,7 +77,6 @@ export interface CompleteHocuspocusProviderConfiguration {
|
|
|
85
77
|
onConnect: () => void,
|
|
86
78
|
onMessage: (data: onMessageParameters) => void,
|
|
87
79
|
onOutgoingMessage: (data: onOutgoingMessageParameters) => void,
|
|
88
|
-
onStatus: (data: onStatusParameters) => void,
|
|
89
80
|
onSynced: (data: onSyncedParameters) => void,
|
|
90
81
|
onDisconnect: (data: onDisconnectParameters) => void,
|
|
91
82
|
onClose: (data: onCloseParameters) => void,
|
|
@@ -93,21 +84,6 @@ export interface CompleteHocuspocusProviderConfiguration {
|
|
|
93
84
|
onAwarenessUpdate: (data: onAwarenessUpdateParameters) => void,
|
|
94
85
|
onAwarenessChange: (data: onAwarenessChangeParameters) => void,
|
|
95
86
|
onStateless: (data: onStatelessParameters) => void
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Don’t output any warnings.
|
|
99
|
-
*/
|
|
100
|
-
quiet: boolean,
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Pass `false` to start the connection manually.
|
|
104
|
-
*/
|
|
105
|
-
connect: boolean,
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Pass `false` to close the connection manually.
|
|
109
|
-
*/
|
|
110
|
-
preserveConnection: boolean,
|
|
111
87
|
}
|
|
112
88
|
|
|
113
89
|
export class AwarenessError extends Error {
|
|
@@ -122,8 +98,6 @@ export class HocuspocusProvider extends EventEmitter {
|
|
|
122
98
|
// @ts-ignore
|
|
123
99
|
awareness: undefined,
|
|
124
100
|
token: null,
|
|
125
|
-
parameters: {},
|
|
126
|
-
broadcast: true,
|
|
127
101
|
forceSyncInterval: false,
|
|
128
102
|
onAuthenticated: () => null,
|
|
129
103
|
onAuthenticationFailed: () => null,
|
|
@@ -131,7 +105,6 @@ export class HocuspocusProvider extends EventEmitter {
|
|
|
131
105
|
onConnect: () => null,
|
|
132
106
|
onMessage: () => null,
|
|
133
107
|
onOutgoingMessage: () => null,
|
|
134
|
-
onStatus: () => null,
|
|
135
108
|
onSynced: () => null,
|
|
136
109
|
onDisconnect: () => null,
|
|
137
110
|
onClose: () => null,
|
|
@@ -139,31 +112,25 @@ export class HocuspocusProvider extends EventEmitter {
|
|
|
139
112
|
onAwarenessUpdate: () => null,
|
|
140
113
|
onAwarenessChange: () => null,
|
|
141
114
|
onStateless: () => null,
|
|
142
|
-
quiet: false,
|
|
143
|
-
connect: true,
|
|
144
|
-
preserveConnection: true,
|
|
145
115
|
}
|
|
146
116
|
|
|
147
|
-
subscribedToBroadcastChannel = false
|
|
148
|
-
|
|
149
117
|
isSynced = false
|
|
150
118
|
|
|
151
119
|
unsyncedChanges = 0
|
|
152
120
|
|
|
153
|
-
// status = WebSocketStatus.Disconnected
|
|
154
|
-
|
|
155
121
|
isAuthenticated = false
|
|
156
122
|
|
|
157
123
|
authorizedScope: string | undefined = undefined
|
|
158
124
|
|
|
159
|
-
|
|
125
|
+
// @internal
|
|
126
|
+
manageSocket = false
|
|
127
|
+
|
|
128
|
+
private isAttached = false
|
|
160
129
|
|
|
161
130
|
intervals: any = {
|
|
162
131
|
forceSync: null,
|
|
163
132
|
}
|
|
164
133
|
|
|
165
|
-
// isConnected = true
|
|
166
|
-
|
|
167
134
|
constructor(configuration: HocuspocusProviderConfiguration) {
|
|
168
135
|
super()
|
|
169
136
|
this.setConfiguration(configuration)
|
|
@@ -183,24 +150,6 @@ export class HocuspocusProvider extends EventEmitter {
|
|
|
183
150
|
this.on('authenticated', this.configuration.onAuthenticated)
|
|
184
151
|
this.on('authenticationFailed', this.configuration.onAuthenticationFailed)
|
|
185
152
|
|
|
186
|
-
this.configuration.websocketProvider.on('connect', this.configuration.onConnect)
|
|
187
|
-
this.configuration.websocketProvider.on('connect', this.forwardConnect)
|
|
188
|
-
|
|
189
|
-
this.configuration.websocketProvider.on('open', this.boundOnOpen)
|
|
190
|
-
this.configuration.websocketProvider.on('open', this.forwardOpen)
|
|
191
|
-
|
|
192
|
-
this.configuration.websocketProvider.on('close', this.boundOnClose)
|
|
193
|
-
this.configuration.websocketProvider.on('close', this.configuration.onClose)
|
|
194
|
-
this.configuration.websocketProvider.on('close', this.forwardClose)
|
|
195
|
-
|
|
196
|
-
// this.configuration.websocketProvider.on('status', this.boundOnStatus)
|
|
197
|
-
|
|
198
|
-
this.configuration.websocketProvider.on('disconnect', this.configuration.onDisconnect)
|
|
199
|
-
this.configuration.websocketProvider.on('disconnect', this.forwardDisconnect)
|
|
200
|
-
|
|
201
|
-
this.configuration.websocketProvider.on('destroy', this.configuration.onDestroy)
|
|
202
|
-
this.configuration.websocketProvider.on('destroy', this.forwardDestroy)
|
|
203
|
-
|
|
204
153
|
this.awareness?.on('update', () => {
|
|
205
154
|
this.emit('awarenessUpdate', { states: awarenessStatesToArray(this.awareness!.getStates()) })
|
|
206
155
|
})
|
|
@@ -209,8 +158,9 @@ export class HocuspocusProvider extends EventEmitter {
|
|
|
209
158
|
this.emit('awarenessChange', { states: awarenessStatesToArray(this.awareness!.getStates()) })
|
|
210
159
|
})
|
|
211
160
|
|
|
212
|
-
this.document.on('update', this.
|
|
213
|
-
this.awareness?.on('update', this.
|
|
161
|
+
this.document.on('update', this.boundDocumentUpdateHandler)
|
|
162
|
+
this.awareness?.on('update', this.boundAwarenessUpdateHandler)
|
|
163
|
+
|
|
214
164
|
this.registerEventListeners()
|
|
215
165
|
|
|
216
166
|
if (
|
|
@@ -223,10 +173,14 @@ export class HocuspocusProvider extends EventEmitter {
|
|
|
223
173
|
)
|
|
224
174
|
}
|
|
225
175
|
|
|
226
|
-
this.
|
|
176
|
+
if( this.manageSocket ) {
|
|
177
|
+
this.attach()
|
|
178
|
+
}
|
|
227
179
|
}
|
|
228
180
|
|
|
229
|
-
|
|
181
|
+
boundDocumentUpdateHandler = this.documentUpdateHandler.bind(this)
|
|
182
|
+
|
|
183
|
+
boundAwarenessUpdateHandler = this.awarenessUpdateHandler.bind(this)
|
|
230
184
|
|
|
231
185
|
boundPageHide = this.pageHide.bind(this)
|
|
232
186
|
|
|
@@ -234,11 +188,9 @@ export class HocuspocusProvider extends EventEmitter {
|
|
|
234
188
|
|
|
235
189
|
boundOnClose = this.onClose.bind(this)
|
|
236
190
|
|
|
237
|
-
// boundOnStatus = this.onStatus.bind(this)
|
|
238
|
-
|
|
239
191
|
forwardConnect = (e: any) => this.emit('connect', e)
|
|
240
192
|
|
|
241
|
-
forwardOpen = (e: any) => this.emit('open', e)
|
|
193
|
+
// forwardOpen = (e: any) => this.emit('open', e)
|
|
242
194
|
|
|
243
195
|
forwardClose = (e: any) => this.emit('close', e)
|
|
244
196
|
|
|
@@ -246,21 +198,12 @@ export class HocuspocusProvider extends EventEmitter {
|
|
|
246
198
|
|
|
247
199
|
forwardDestroy = (e: any) => this.emit('destroy', e)
|
|
248
200
|
|
|
249
|
-
// public onStatus({ status } : onStatusParameters) {
|
|
250
|
-
// this.status = status
|
|
251
|
-
//
|
|
252
|
-
// this.configuration.onStatus({ status })
|
|
253
|
-
// this.emit('status', { status })
|
|
254
|
-
// }
|
|
255
|
-
|
|
256
201
|
public setConfiguration(configuration: Partial<HocuspocusProviderConfiguration> = {}): void {
|
|
257
|
-
if (!configuration.websocketProvider
|
|
202
|
+
if (!configuration.websocketProvider) {
|
|
258
203
|
const websocketProviderConfig = configuration as CompleteHocuspocusProviderWebsocketConfiguration
|
|
259
|
-
|
|
204
|
+
this.manageSocket = true
|
|
260
205
|
this.configuration.websocketProvider = new HocuspocusProviderWebsocket({
|
|
261
206
|
url: websocketProviderConfig.url,
|
|
262
|
-
connect: websocketProviderConfig.connect,
|
|
263
|
-
parameters: websocketProviderConfig.parameters,
|
|
264
207
|
})
|
|
265
208
|
}
|
|
266
209
|
|
|
@@ -290,10 +233,14 @@ export class HocuspocusProvider extends EventEmitter {
|
|
|
290
233
|
}
|
|
291
234
|
|
|
292
235
|
decrementUnsyncedChanges() {
|
|
293
|
-
this.unsyncedChanges
|
|
236
|
+
if( this.unsyncedChanges > 0 ) {
|
|
237
|
+
this.unsyncedChanges -= 1
|
|
238
|
+
}
|
|
239
|
+
|
|
294
240
|
if (this.unsyncedChanges === 0) {
|
|
295
241
|
this.synced = true
|
|
296
242
|
}
|
|
243
|
+
|
|
297
244
|
this.emit('unsyncedChanges', this.unsyncedChanges)
|
|
298
245
|
}
|
|
299
246
|
|
|
@@ -327,7 +274,7 @@ export class HocuspocusProvider extends EventEmitter {
|
|
|
327
274
|
}
|
|
328
275
|
|
|
329
276
|
this.incrementUnsyncedChanges()
|
|
330
|
-
this.send(UpdateMessage, { update, documentName: this.configuration.name }
|
|
277
|
+
this.send(UpdateMessage, { update, documentName: this.configuration.name })
|
|
331
278
|
}
|
|
332
279
|
|
|
333
280
|
awarenessUpdateHandler({ added, updated, removed }: any, origin: any) {
|
|
@@ -337,7 +284,7 @@ export class HocuspocusProvider extends EventEmitter {
|
|
|
337
284
|
awareness: this.awareness,
|
|
338
285
|
clients: changedClients,
|
|
339
286
|
documentName: this.configuration.name,
|
|
340
|
-
}
|
|
287
|
+
})
|
|
341
288
|
}
|
|
342
289
|
|
|
343
290
|
/**
|
|
@@ -356,45 +303,27 @@ export class HocuspocusProvider extends EventEmitter {
|
|
|
356
303
|
}
|
|
357
304
|
|
|
358
305
|
this.isSynced = state
|
|
359
|
-
|
|
360
|
-
|
|
306
|
+
|
|
307
|
+
if( state ) {
|
|
308
|
+
this.emit('synced', { state })
|
|
309
|
+
}
|
|
361
310
|
}
|
|
362
311
|
|
|
363
312
|
receiveStateless(payload: string) {
|
|
364
313
|
this.emit('stateless', { payload })
|
|
365
314
|
}
|
|
366
315
|
|
|
367
|
-
get isAuthenticationRequired(): boolean {
|
|
368
|
-
return !!this.configuration.token && !this.isAuthenticated
|
|
369
|
-
}
|
|
370
|
-
|
|
371
316
|
// not needed, but provides backward compatibility with e.g. lexical/yjs
|
|
372
317
|
async connect() {
|
|
373
318
|
console.warn('HocuspocusProvider::connect() is deprecated and does not do anything. Please connect/disconnect on the websocketProvider, or attach/deattach providers.')
|
|
374
|
-
|
|
375
|
-
// if (this.configuration.broadcast) {
|
|
376
|
-
// this.subscribeToBroadcastChannel()
|
|
377
|
-
// }
|
|
378
|
-
//
|
|
379
|
-
// return this.configuration.websocketProvider.connect()
|
|
380
319
|
}
|
|
381
320
|
|
|
382
321
|
disconnect() {
|
|
383
322
|
console.warn('HocuspocusProvider::disconnect() is deprecated and does not do anything. Please connect/disconnect on the websocketProvider, or attach/deattach providers.')
|
|
384
|
-
|
|
385
|
-
// this.disconnectBroadcastChannel()
|
|
386
|
-
// this.configuration.websocketProvider.detach(this)
|
|
387
|
-
// this.isConnected = false
|
|
388
|
-
//
|
|
389
|
-
// if (!this.configuration.preserveConnection) {
|
|
390
|
-
// this.configuration.websocketProvider.disconnect()
|
|
391
|
-
// }
|
|
392
|
-
//
|
|
393
323
|
}
|
|
394
324
|
|
|
395
325
|
async onOpen(event: Event) {
|
|
396
326
|
this.isAuthenticated = false
|
|
397
|
-
// this.isConnected = true
|
|
398
327
|
|
|
399
328
|
this.emit('open', { event })
|
|
400
329
|
|
|
@@ -406,12 +335,10 @@ export class HocuspocusProvider extends EventEmitter {
|
|
|
406
335
|
return
|
|
407
336
|
}
|
|
408
337
|
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
})
|
|
414
|
-
}
|
|
338
|
+
this.send(AuthenticationMessage, {
|
|
339
|
+
token: token ?? '',
|
|
340
|
+
documentName: this.configuration.name,
|
|
341
|
+
})
|
|
415
342
|
|
|
416
343
|
this.startSync()
|
|
417
344
|
}
|
|
@@ -439,15 +366,7 @@ export class HocuspocusProvider extends EventEmitter {
|
|
|
439
366
|
}
|
|
440
367
|
}
|
|
441
368
|
|
|
442
|
-
send(message: ConstructableOutgoingMessage, args: any
|
|
443
|
-
// if (!this.isConnected) {
|
|
444
|
-
// return
|
|
445
|
-
// }
|
|
446
|
-
|
|
447
|
-
// if (broadcast) {
|
|
448
|
-
// this.mux(() => { this.broadcast(message, args) })
|
|
449
|
-
// }
|
|
450
|
-
|
|
369
|
+
send(message: ConstructableOutgoingMessage, args: any) {
|
|
451
370
|
const messageSender = new MessageSender(message, args)
|
|
452
371
|
|
|
453
372
|
this.emit('outgoingMessage', { message: messageSender.message })
|
|
@@ -466,7 +385,7 @@ export class HocuspocusProvider extends EventEmitter {
|
|
|
466
385
|
new MessageReceiver(message).apply(this, true)
|
|
467
386
|
}
|
|
468
387
|
|
|
469
|
-
onClose(
|
|
388
|
+
onClose() {
|
|
470
389
|
this.isAuthenticated = false
|
|
471
390
|
this.synced = false
|
|
472
391
|
|
|
@@ -489,43 +408,70 @@ export class HocuspocusProvider extends EventEmitter {
|
|
|
489
408
|
|
|
490
409
|
if (this.awareness) {
|
|
491
410
|
removeAwarenessStates(this.awareness, [this.document.clientID], 'provider destroy')
|
|
492
|
-
this.awareness.off('update', this.
|
|
411
|
+
this.awareness.off('update', this.boundAwarenessUpdateHandler)
|
|
493
412
|
this.awareness.destroy()
|
|
494
413
|
}
|
|
495
414
|
|
|
496
|
-
this.document.off('update', this.
|
|
415
|
+
this.document.off('update', this.boundDocumentUpdateHandler)
|
|
497
416
|
|
|
498
417
|
this.removeAllListeners()
|
|
499
418
|
|
|
419
|
+
this.detach()
|
|
420
|
+
|
|
421
|
+
if( this.manageSocket ) {
|
|
422
|
+
this.configuration.websocketProvider.destroy()
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
if (typeof window === 'undefined' || !('removeEventListener' in window)) {
|
|
426
|
+
return
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
window.removeEventListener('pagehide', this.boundPageHide)
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
detach() {
|
|
500
433
|
this.configuration.websocketProvider.off('connect', this.configuration.onConnect)
|
|
501
434
|
this.configuration.websocketProvider.off('connect', this.forwardConnect)
|
|
502
435
|
this.configuration.websocketProvider.off('open', this.boundOnOpen)
|
|
503
|
-
this.configuration.websocketProvider.off('open', this.forwardOpen)
|
|
504
436
|
this.configuration.websocketProvider.off('close', this.boundOnClose)
|
|
505
437
|
this.configuration.websocketProvider.off('close', this.configuration.onClose)
|
|
506
438
|
this.configuration.websocketProvider.off('close', this.forwardClose)
|
|
507
|
-
// this.configuration.websocketProvider.off('status', this.boundOnStatus)
|
|
508
439
|
this.configuration.websocketProvider.off('disconnect', this.configuration.onDisconnect)
|
|
509
440
|
this.configuration.websocketProvider.off('disconnect', this.forwardDisconnect)
|
|
510
441
|
this.configuration.websocketProvider.off('destroy', this.configuration.onDestroy)
|
|
511
442
|
this.configuration.websocketProvider.off('destroy', this.forwardDestroy)
|
|
512
443
|
|
|
513
|
-
this.send(CloseMessage, { documentName: this.configuration.name })
|
|
514
444
|
this.configuration.websocketProvider.detach(this)
|
|
515
|
-
this.disconnect()
|
|
516
445
|
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
}
|
|
446
|
+
this.isAttached = false
|
|
447
|
+
}
|
|
520
448
|
|
|
521
|
-
|
|
449
|
+
attach() {
|
|
450
|
+
if( this.isAttached ) return
|
|
451
|
+
|
|
452
|
+
this.configuration.websocketProvider.on('connect', this.configuration.onConnect)
|
|
453
|
+
this.configuration.websocketProvider.on('connect', this.forwardConnect)
|
|
454
|
+
|
|
455
|
+
this.configuration.websocketProvider.on('open', this.boundOnOpen)
|
|
456
|
+
|
|
457
|
+
this.configuration.websocketProvider.on('close', this.boundOnClose)
|
|
458
|
+
this.configuration.websocketProvider.on('close', this.configuration.onClose)
|
|
459
|
+
this.configuration.websocketProvider.on('close', this.forwardClose)
|
|
460
|
+
|
|
461
|
+
this.configuration.websocketProvider.on('disconnect', this.configuration.onDisconnect)
|
|
462
|
+
this.configuration.websocketProvider.on('disconnect', this.forwardDisconnect)
|
|
463
|
+
|
|
464
|
+
this.configuration.websocketProvider.on('destroy', this.configuration.onDestroy)
|
|
465
|
+
this.configuration.websocketProvider.on('destroy', this.forwardDestroy)
|
|
466
|
+
|
|
467
|
+
this.configuration.websocketProvider.attach(this)
|
|
468
|
+
|
|
469
|
+
this.isAttached = true
|
|
522
470
|
}
|
|
523
471
|
|
|
524
472
|
permissionDeniedHandler(reason: string) {
|
|
525
473
|
this.emit('authenticationFailed', { reason })
|
|
526
474
|
this.isAuthenticated = false
|
|
527
|
-
// this.disconnect()
|
|
528
|
-
// this.status = WebSocketStatus.Disconnected
|
|
529
475
|
}
|
|
530
476
|
|
|
531
477
|
authenticatedHandler(scope: string) {
|
|
@@ -535,74 +481,6 @@ export class HocuspocusProvider extends EventEmitter {
|
|
|
535
481
|
this.emit('authenticated')
|
|
536
482
|
}
|
|
537
483
|
|
|
538
|
-
// get broadcastChannel() {
|
|
539
|
-
// return `${this.configuration.name}`
|
|
540
|
-
// }
|
|
541
|
-
//
|
|
542
|
-
// broadcastChannelSubscriber(data: ArrayBuffer) {
|
|
543
|
-
// this.mux(() => {
|
|
544
|
-
// const message = new IncomingMessage(data)
|
|
545
|
-
//
|
|
546
|
-
// const documentName = message.readVarString()
|
|
547
|
-
//
|
|
548
|
-
// message.writeVarString(documentName)
|
|
549
|
-
//
|
|
550
|
-
// new MessageReceiver(message)
|
|
551
|
-
// .setBroadcasted(true)
|
|
552
|
-
// .apply(this, false)
|
|
553
|
-
// })
|
|
554
|
-
// }
|
|
555
|
-
|
|
556
|
-
// subscribeToBroadcastChannel() {
|
|
557
|
-
// if (!this.subscribedToBroadcastChannel) {
|
|
558
|
-
// bc.subscribe(this.broadcastChannel, this.boundBroadcastChannelSubscriber)
|
|
559
|
-
// this.subscribedToBroadcastChannel = true
|
|
560
|
-
// }
|
|
561
|
-
//
|
|
562
|
-
// this.mux(() => {
|
|
563
|
-
// this.broadcast(SyncStepOneMessage, { document: this.document, documentName: this.configuration.name })
|
|
564
|
-
// this.broadcast(SyncStepTwoMessage, { document: this.document, documentName: this.configuration.name })
|
|
565
|
-
// this.broadcast(QueryAwarenessMessage, { document: this.document, documentName: this.configuration.name })
|
|
566
|
-
// if (this.awareness) {
|
|
567
|
-
// this.broadcast(AwarenessMessage, {
|
|
568
|
-
// awareness: this.awareness,
|
|
569
|
-
// clients: [this.document.clientID],
|
|
570
|
-
// document: this.document,
|
|
571
|
-
// documentName: this.configuration.name,
|
|
572
|
-
// })
|
|
573
|
-
// }
|
|
574
|
-
// })
|
|
575
|
-
// }
|
|
576
|
-
//
|
|
577
|
-
// disconnectBroadcastChannel() {
|
|
578
|
-
// // broadcast message with local awareness state set to null (indicating disconnect)
|
|
579
|
-
// if (this.awareness) {
|
|
580
|
-
// this.send(AwarenessMessage, {
|
|
581
|
-
// awareness: this.awareness,
|
|
582
|
-
// clients: [this.document.clientID],
|
|
583
|
-
// states: new Map(),
|
|
584
|
-
// documentName: this.configuration.name,
|
|
585
|
-
// }, true)
|
|
586
|
-
// }
|
|
587
|
-
//
|
|
588
|
-
// if (this.subscribedToBroadcastChannel) {
|
|
589
|
-
// bc.unsubscribe(this.broadcastChannel, this.boundBroadcastChannelSubscriber)
|
|
590
|
-
// this.subscribedToBroadcastChannel = false
|
|
591
|
-
// }
|
|
592
|
-
// }
|
|
593
|
-
|
|
594
|
-
// broadcast(Message: ConstructableOutgoingMessage, args?: any) {
|
|
595
|
-
// if (!this.configuration.broadcast) {
|
|
596
|
-
// return
|
|
597
|
-
// }
|
|
598
|
-
//
|
|
599
|
-
// if (!this.subscribedToBroadcastChannel) {
|
|
600
|
-
// return
|
|
601
|
-
// }
|
|
602
|
-
//
|
|
603
|
-
// new MessageSender(Message, args).broadcast(this.broadcastChannel)
|
|
604
|
-
// }
|
|
605
|
-
|
|
606
484
|
setAwarenessField(key: string, value: any) {
|
|
607
485
|
if (!this.awareness) {
|
|
608
486
|
throw new AwarenessError(`Cannot set awareness field "${key}" to ${JSON.stringify(value)}. You have disabled Awareness for this provider by explicitly passing awareness: null in the provider configuration.`)
|