1
- const Y = require ( 'yjs' )
2
- const syncProtocol = require ( 'y-protocols/sync' )
3
- const awarenessProtocol = require ( 'y-protocols/awareness' )
1
+ import * as Y from 'yjs'
2
+ import * as syncProtocol from 'y-protocols/sync'
3
+ import * as awarenessProtocol from 'y-protocols/awareness'
4
4
5
- const encoding = require ( 'lib0/encoding' )
6
- const decoding = require ( 'lib0/decoding' )
7
- const map = require ( 'lib0/map' )
5
+ import * as encoding from 'lib0/encoding'
6
+ import * as decoding from 'lib0/decoding'
7
+ import * as map from 'lib0/map'
8
8
9
- const debounce = require ( 'lodash.debounce' )
9
+ import debounce from 'lodash.debounce'
10
10
11
- const callbackHandler = require ( './callback.cjs' ) . callbackHandler
12
- const isCallbackSet = require ( './callback.cjs' ) . isCallbackSet
11
+ import { callbackHandler , isCallbackSet } from './callback.js'
13
12
14
13
const CALLBACK_DEBOUNCE_WAIT = parseInt ( process . env . CALLBACK_DEBOUNCE_WAIT || '2000' )
15
14
const CALLBACK_DEBOUNCE_MAXWAIT = parseInt ( process . env . CALLBACK_DEBOUNCE_MAXWAIT || '10000' )
@@ -50,22 +49,20 @@ if (typeof persistenceDir === 'string') {
50
49
* @param {{bindState: function(string,WSSharedDoc):void,
51
50
* writeState:function(string,WSSharedDoc):Promise<any>,provider:any}|null } persistence_
52
51
*/
53
- exports . setPersistence = persistence_ => {
52
+ export const setPersistence = persistence_ => {
54
53
persistence = persistence_
55
54
}
56
55
57
56
/**
58
57
* @return {null|{bindState: function(string,WSSharedDoc):void,
59
58
* writeState:function(string,WSSharedDoc):Promise<any>}|null} used persistence layer
60
59
*/
61
- exports . getPersistence = ( ) => persistence
60
+ export const getPersistence = ( ) => persistence
62
61
63
62
/**
64
63
* @type {Map<string,WSSharedDoc> }
65
64
*/
66
- const docs = new Map ( )
67
- // exporting docs so that others can use it
68
- exports . docs = docs
65
+ export const docs = new Map ( )
69
66
70
67
const messageSync = 0
71
68
const messageAwareness = 1
@@ -96,11 +93,11 @@ let contentInitializor = _ydoc => Promise.resolve()
96
93
*
97
94
* @param {(ydoc: Y.Doc) => Promise<void> } f
98
95
*/
99
- exports . setContentInitializor = ( f ) => {
96
+ export const setContentInitializor = ( f ) => {
100
97
contentInitializor = f
101
98
}
102
99
103
- class WSSharedDoc extends Y . Doc {
100
+ export class WSSharedDoc extends Y . Doc {
104
101
/**
105
102
* @param {string } name
106
103
*/
@@ -152,16 +149,14 @@ class WSSharedDoc extends Y.Doc {
152
149
}
153
150
}
154
151
155
- exports . WSSharedDoc = WSSharedDoc
156
-
157
152
/**
158
153
* Gets a Y.Doc by name, whether in memory or on disk
159
154
*
160
155
* @param {string } docname - the name of the Y.Doc to find or create
161
156
* @param {boolean } gc - whether to allow gc on the doc (applies only when created)
162
157
* @return {WSSharedDoc }
163
158
*/
164
- const getYDoc = ( docname , gc = true ) => map . setIfUndefined ( docs , docname , ( ) => {
159
+ export const getYDoc = ( docname , gc = true ) => map . setIfUndefined ( docs , docname , ( ) => {
165
160
const doc = new WSSharedDoc ( docname )
166
161
doc . gc = gc
167
162
if ( persistence !== null ) {
@@ -171,8 +166,6 @@ const getYDoc = (docname, gc = true) => map.setIfUndefined(docs, docname, () =>
171
166
return doc
172
167
} )
173
168
174
- exports . getYDoc = getYDoc
175
-
176
169
/**
177
170
* @param {any } conn
178
171
* @param {WSSharedDoc } doc
@@ -254,7 +247,7 @@ const pingTimeout = 30000
254
247
* @param {import('http').IncomingMessage } req
255
248
* @param {any } opts
256
249
*/
257
- exports . setupWSConnection = ( conn , req , { docName = ( req . url || '' ) . slice ( 1 ) . split ( '?' ) [ 0 ] , gc = true } = { } ) => {
250
+ export const setupWSConnection = ( conn , req , { docName = ( req . url || '' ) . slice ( 1 ) . split ( '?' ) [ 0 ] , gc = true } = { } ) => {
258
251
conn . binaryType = 'arraybuffer'
259
252
// get doc, initialize if it does not exist yet
260
253
const doc = getYDoc ( docName , gc )
0 commit comments