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'
44
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'
88
9- const debounce = require ( 'lodash.debounce' )
9+ import debounce from 'lodash.debounce'
1010
11- const callbackHandler = require ( './callback.cjs' ) . callbackHandler
12- const isCallbackSet = require ( './callback.cjs' ) . isCallbackSet
11+ import { callbackHandler , isCallbackSet } from './callback.js'
1312
1413const CALLBACK_DEBOUNCE_WAIT = parseInt ( process . env . CALLBACK_DEBOUNCE_WAIT || '2000' )
1514const CALLBACK_DEBOUNCE_MAXWAIT = parseInt ( process . env . CALLBACK_DEBOUNCE_MAXWAIT || '10000' )
@@ -50,22 +49,20 @@ if (typeof persistenceDir === 'string') {
5049 * @param {{bindState: function(string,WSSharedDoc):void,
5150 * writeState:function(string,WSSharedDoc):Promise<any>,provider:any}|null } persistence_
5251 */
53- exports . setPersistence = persistence_ => {
52+ export const setPersistence = persistence_ => {
5453 persistence = persistence_
5554}
5655
5756/**
5857 * @return {null|{bindState: function(string,WSSharedDoc):void,
5958 * writeState:function(string,WSSharedDoc):Promise<any>}|null} used persistence layer
6059 */
61- exports . getPersistence = ( ) => persistence
60+ export const getPersistence = ( ) => persistence
6261
6362/**
6463 * @type {Map<string,WSSharedDoc> }
6564 */
66- const docs = new Map ( )
67- // exporting docs so that others can use it
68- exports . docs = docs
65+ export const docs = new Map ( )
6966
7067const messageSync = 0
7168const messageAwareness = 1
@@ -96,11 +93,11 @@ let contentInitializor = _ydoc => Promise.resolve()
9693 *
9794 * @param {(ydoc: Y.Doc) => Promise<void> } f
9895 */
99- exports . setContentInitializor = ( f ) => {
96+ export const setContentInitializor = ( f ) => {
10097 contentInitializor = f
10198}
10299
103- class WSSharedDoc extends Y . Doc {
100+ export class WSSharedDoc extends Y . Doc {
104101 /**
105102 * @param {string } name
106103 */
@@ -152,16 +149,14 @@ class WSSharedDoc extends Y.Doc {
152149 }
153150}
154151
155- exports . WSSharedDoc = WSSharedDoc
156-
157152/**
158153 * Gets a Y.Doc by name, whether in memory or on disk
159154 *
160155 * @param {string } docname - the name of the Y.Doc to find or create
161156 * @param {boolean } gc - whether to allow gc on the doc (applies only when created)
162157 * @return {WSSharedDoc }
163158 */
164- const getYDoc = ( docname , gc = true ) => map . setIfUndefined ( docs , docname , ( ) => {
159+ export const getYDoc = ( docname , gc = true ) => map . setIfUndefined ( docs , docname , ( ) => {
165160 const doc = new WSSharedDoc ( docname )
166161 doc . gc = gc
167162 if ( persistence !== null ) {
@@ -171,8 +166,6 @@ const getYDoc = (docname, gc = true) => map.setIfUndefined(docs, docname, () =>
171166 return doc
172167} )
173168
174- exports . getYDoc = getYDoc
175-
176169/**
177170 * @param {any } conn
178171 * @param {WSSharedDoc } doc
@@ -254,7 +247,7 @@ const pingTimeout = 30000
254247 * @param {import('http').IncomingMessage } req
255248 * @param {any } opts
256249 */
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 } = { } ) => {
258251 conn . binaryType = 'arraybuffer'
259252 // get doc, initialize if it does not exist yet
260253 const doc = getYDoc ( docName , gc )
0 commit comments