@@ -16,6 +16,7 @@ import toobusy from 'toobusy-js'
16
16
17
17
const logSocketIO = createModuleLogger ( '@y/socket-io/server' )
18
18
const PERSIST_INTERVAL = number . parseInt ( env . getConf ( 'y-socket-io-server-persist-interval' ) || '3000' )
19
+ const MAX_PERSIST_INTERVAL = number . parseInt ( env . getConf ( 'y-socket-io-server-max-persist-interval' ) || '30000' )
19
20
const REVALIDATE_TIMEOUT = number . parseInt ( env . getConf ( 'y-socket-io-server-revalidate-timeout' ) || '60000' )
20
21
const WORKER_DISABLED = env . getConf ( 'y-worker-disabled' ) === 'true'
21
22
@@ -117,6 +118,24 @@ export class YSocketIO {
117
118
* @readonly
118
119
*/
119
120
socketUserCache = new Map ( )
121
+ /**
122
+ * @type {Map<string, NodeJS.Timeout | null> }
123
+ * @private
124
+ * @readonly
125
+ */
126
+ debouncedPersistMap = new Map ( )
127
+ /**
128
+ * @type {Map<string, Y.Doc> }
129
+ * @private
130
+ * @readonly
131
+ */
132
+ debouncedPersistDocMap = new Map ( )
133
+ /**
134
+ * @type {Map<string, number> }
135
+ * @private
136
+ * @readonly
137
+ */
138
+ namespacePersistentMap = new Map ( )
120
139
121
140
/**
122
141
* YSocketIO constructor.
@@ -323,6 +342,7 @@ export class YSocketIO {
323
342
this . namespaceMap . delete ( ns )
324
343
this . namespaceDocMap . get ( ns ) ?. ydoc . destroy ( )
325
344
this . namespaceDocMap . delete ( ns )
345
+ this . namespacePersistentMap . delete ( ns )
326
346
}
327
347
}
328
348
} )
@@ -379,6 +399,7 @@ export class YSocketIO {
379
399
this . namespaceMap . delete ( namespace )
380
400
this . namespaceDocMap . get ( namespace ) ?. ydoc . destroy ( )
381
401
this . namespaceDocMap . delete ( namespace )
402
+ this . namespacePersistentMap . delete ( namespace )
382
403
}
383
404
384
405
/** @type {Uint8Array[] } */
@@ -429,20 +450,17 @@ export class YSocketIO {
429
450
changed = getDoc . changed
430
451
}
431
452
assert ( doc )
432
- if ( changed ) this . debouncedPersist ( namespace , doc . ydoc )
453
+ const lastPersistCalledAt = this . namespacePersistentMap . get ( namespace ) ?? 0
454
+ const now = Date . now ( )
455
+ const shouldPersist = now - lastPersistCalledAt > MAX_PERSIST_INTERVAL
456
+ if ( changed || shouldPersist ) {
457
+ this . namespacePersistentMap . set ( namespace , now )
458
+ this . debouncedPersist ( namespace , doc . ydoc )
459
+ }
433
460
this . namespaceDocMap . get ( namespace ) ?. ydoc . destroy ( )
434
461
this . namespaceDocMap . set ( namespace , doc )
435
462
}
436
463
437
- /**
438
- * @type {Map<string, NodeJS.Timeout | null> }
439
- */
440
- debouncedPersistMap = new Map ( )
441
- /**
442
- * @type {Map<string, Y.Doc> }
443
- */
444
- debouncedPersistDocMap = new Map ( )
445
-
446
464
/**
447
465
* @param {string } namespace
448
466
* @param {Y.Doc } doc
0 commit comments