@@ -7,6 +7,7 @@ import * as encoding from 'lib0/encoding'
7
7
import * as decoding from 'lib0/decoding'
8
8
import * as map from 'lib0/map'
9
9
import * as lodash from 'lodash'
10
+ import { getYDoc as getYstreamDoc } from '../ystream/adaptor.js'
10
11
11
12
const { debounce } = lodash
12
13
@@ -25,44 +26,8 @@ const wsReadyStateClosed = 3 // eslint-disable-line
25
26
26
27
// disable gc when using snapshots!
27
28
const gcEnabled = process . env . GC !== 'false' && process . env . GC !== '0'
28
- const persistenceDir = `./.db-${ process . env . INSTANCE_NAME || 'default' } `
29
- /**
30
- * @type {{bindState: function(string,WSSharedDoc):void, writeState:function(string,WSSharedDoc):Promise<any>, provider: any}|null }
31
- */
32
- let persistence = null
33
- if ( typeof persistenceDir === 'string' ) {
34
- console . info ( 'Persisting documents to "' + persistenceDir + '"' )
35
- // @ts -ignore
36
- const { LeveldbPersistence } = await import ( 'y-leveldb' )
37
- const ldb = new LeveldbPersistence ( persistenceDir )
38
- persistence = {
39
- provider : ldb ,
40
- bindState : async ( docName , ydoc ) => {
41
- const persistedYdoc = await ldb . getYDoc ( docName )
42
- const newUpdates = Y . encodeStateAsUpdate ( ydoc )
43
- ldb . storeUpdate ( docName , newUpdates )
44
- Y . applyUpdate ( ydoc , Y . encodeStateAsUpdate ( persistedYdoc ) )
45
- ydoc . on ( 'update' , update => {
46
- ldb . storeUpdate ( docName , update )
47
- } )
48
- } ,
49
- writeState : async ( _docName , _ydoc ) => { }
50
- }
51
- }
52
-
53
- /**
54
- * @param {{bindState: function(string,WSSharedDoc):void,
55
- * writeState:function(string,WSSharedDoc):Promise<any>,provider:any}|null } persistence_
56
- */
57
- export const setPersistence = persistence_ => {
58
- persistence = persistence_
59
- }
60
29
61
- /**
62
- * @return {null|{bindState: function(string,WSSharedDoc):void,
63
- * writeState:function(string,WSSharedDoc):Promise<any>}|null} used persistence layer
64
- */
65
- export const getPersistence = ( ) => persistence
30
+ let persistence = null
66
31
67
32
/**
68
33
* @type {Map<string,WSSharedDoc> }
@@ -167,10 +132,11 @@ export const getYDoc = async (docname, gc = true) => {
167
132
if ( ! doc ) {
168
133
doc = new WSSharedDoc ( docname )
169
134
doc . gc = gc
170
- if ( persistence !== null ) {
171
- await persistence . bindState ( docname , doc )
172
- }
173
135
docs . set ( docname , doc )
136
+
137
+ const peerDoc = await getYstreamDoc ( docname )
138
+ peerDoc . on ( 'update' , update => Y . applyUpdate ( doc , update ) )
139
+ doc . on ( 'update' , update => Y . applyUpdate ( peerDoc , update ) )
174
140
}
175
141
return doc ;
176
142
}
0 commit comments