@@ -42,6 +42,7 @@ export class MySharedDrive extends Drive implements ICollaborativeDrive {
42
42
// pass through any events from the Drive superclass
43
43
this . _ydriveFileChanged . emit ( change ) ;
44
44
} ) ;
45
+ this . _saveLock = new AsyncLock ( ) ;
45
46
}
46
47
47
48
/**
@@ -103,11 +104,11 @@ export class MySharedDrive extends Drive implements ICollaborativeDrive {
103
104
}
104
105
105
106
async listCheckpoints ( path : string ) : Promise < Contents . ICheckpointModel [ ] > {
106
- return [ { id : "checkpoint" , last_modified : "2025-01-30T16:33:19.393756Z" } ] ;
107
+ return [ ] ;
107
108
}
108
109
109
110
async createCheckpoint ( path : string ) : Promise < Contents . ICheckpointModel > {
110
- return { id : "checkpoint" , last_modified : "2025-01-30T16:33:19.393756Z" } ;
111
+ return { id : '' , last_modified : '' } ;
111
112
}
112
113
113
114
/**
@@ -154,7 +155,6 @@ export class MySharedDrive extends Drive implements ICollaborativeDrive {
154
155
options : Contents . ISharedFactoryOptions ,
155
156
sharedModel : YDocument < DocumentChange >
156
157
) => {
157
- console . log ( '_onCreate' , options ) ;
158
158
if ( typeof options . format !== 'string' ) {
159
159
return ;
160
160
}
@@ -167,17 +167,35 @@ export class MySharedDrive extends Drive implements ICollaborativeDrive {
167
167
user : this . _user ,
168
168
translator : this . _trans
169
169
} ) ;
170
- console . log ( 'provider' , provider ) ;
171
170
172
171
this . _app . serviceManager . contents . get ( options . path , { content : true } ) . then ( model => {
173
- console . log ( 'set model source:' , model ) ;
174
172
const content = model . format === 'base64' ? atob ( model . content ) : model . content ;
175
173
provider . setSource ( content ) ;
176
174
} ) ;
177
175
178
176
const key = `${ options . format } :${ options . contentType } :${ options . path } ` ;
179
177
this . _providers . set ( key , provider ) ;
180
178
179
+ sharedModel . ydoc . on ( 'update' , ( update , origin ) => {
180
+ if ( origin === this ) {
181
+ return ;
182
+ }
183
+ this . _saveLock . promise . then ( ( ) => {
184
+ this . _saveLock . enable ( ) ;
185
+ let content = sharedModel . getSource ( ) ;
186
+ sharedModel . ydoc . transact ( ( ) => {
187
+ sharedModel . ystate . set ( 'dirty' , false ) ;
188
+ } , this ) ;
189
+ if ( options . format === 'text' && typeof content === 'object' ) {
190
+ content = JSON . stringify ( content ) ;
191
+ }
192
+ this . _app . serviceManager . contents . save ( options . path , { content, format : options . format , type : options . contentType } )
193
+ . then ( ( ) => {
194
+ this . _saveLock . disable ( ) ;
195
+ } ) ;
196
+ } ) ;
197
+ } ) ;
198
+
181
199
sharedModel . changed . connect ( async ( _ , change ) => {
182
200
if ( ! change . stateChange ) {
183
201
return ;
@@ -231,6 +249,7 @@ export class MySharedDrive extends Drive implements ICollaborativeDrive {
231
249
private _trans : TranslationBundle ;
232
250
private _providers : Map < string , MyProvider > ;
233
251
private _ydriveFileChanged = new Signal < this, Contents . IChangedArgs > ( this ) ;
252
+ private _saveLock : AsyncLock ;
234
253
}
235
254
236
255
/**
@@ -282,7 +301,6 @@ class SharedModelFactory implements ISharedModelFactory {
282
301
createNew (
283
302
options : Contents . ISharedFactoryOptions
284
303
) : ISharedDocument | undefined {
285
- console . log ( "createNew" , options ) ;
286
304
if ( typeof options . format !== 'string' ) {
287
305
console . warn ( `Only defined format are supported; got ${ options . format } .` ) ;
288
306
return ;
@@ -299,8 +317,22 @@ class SharedModelFactory implements ISharedModelFactory {
299
317
this . _onCreate ( options , sharedModel ) ;
300
318
return sharedModel ;
301
319
}
302
- console . log ( "no document factory" ) ;
303
320
304
321
return ;
305
322
}
306
323
}
324
+
325
+
326
+ class AsyncLock {
327
+ constructor ( ) {
328
+ this . disable = ( ) => { } ;
329
+ this . promise = Promise . resolve ( ) ;
330
+ }
331
+
332
+ enable ( ) {
333
+ this . promise = new Promise ( resolve => this . disable = resolve ) ;
334
+ }
335
+
336
+ disable : any ;
337
+ promise : Promise < void > ;
338
+ }
0 commit comments