@@ -42,6 +42,7 @@ export class MySharedDrive extends Drive implements ICollaborativeDrive {
4242 // pass through any events from the Drive superclass
4343 this . _ydriveFileChanged . emit ( change ) ;
4444 } ) ;
45+ this . _saveLock = new AsyncLock ( ) ;
4546 }
4647
4748 /**
@@ -103,11 +104,11 @@ export class MySharedDrive extends Drive implements ICollaborativeDrive {
103104 }
104105
105106 async listCheckpoints ( path : string ) : Promise < Contents . ICheckpointModel [ ] > {
106- return [ { id : "checkpoint" , last_modified : "2025-01-30T16:33:19.393756Z" } ] ;
107+ return [ ] ;
107108 }
108109
109110 async createCheckpoint ( path : string ) : Promise < Contents . ICheckpointModel > {
110- return { id : "checkpoint" , last_modified : "2025-01-30T16:33:19.393756Z" } ;
111+ return { id : '' , last_modified : '' } ;
111112 }
112113
113114 /**
@@ -154,7 +155,6 @@ export class MySharedDrive extends Drive implements ICollaborativeDrive {
154155 options : Contents . ISharedFactoryOptions ,
155156 sharedModel : YDocument < DocumentChange >
156157 ) => {
157- console . log ( '_onCreate' , options ) ;
158158 if ( typeof options . format !== 'string' ) {
159159 return ;
160160 }
@@ -167,17 +167,35 @@ export class MySharedDrive extends Drive implements ICollaborativeDrive {
167167 user : this . _user ,
168168 translator : this . _trans
169169 } ) ;
170- console . log ( 'provider' , provider ) ;
171170
172171 this . _app . serviceManager . contents . get ( options . path , { content : true } ) . then ( model => {
173- console . log ( 'set model source:' , model ) ;
174172 const content = model . format === 'base64' ? atob ( model . content ) : model . content ;
175173 provider . setSource ( content ) ;
176174 } ) ;
177175
178176 const key = `${ options . format } :${ options . contentType } :${ options . path } ` ;
179177 this . _providers . set ( key , provider ) ;
180178
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+
181199 sharedModel . changed . connect ( async ( _ , change ) => {
182200 if ( ! change . stateChange ) {
183201 return ;
@@ -231,6 +249,7 @@ export class MySharedDrive extends Drive implements ICollaborativeDrive {
231249 private _trans : TranslationBundle ;
232250 private _providers : Map < string , MyProvider > ;
233251 private _ydriveFileChanged = new Signal < this, Contents . IChangedArgs > ( this ) ;
252+ private _saveLock : AsyncLock ;
234253}
235254
236255/**
@@ -282,7 +301,6 @@ class SharedModelFactory implements ISharedModelFactory {
282301 createNew (
283302 options : Contents . ISharedFactoryOptions
284303 ) : ISharedDocument | undefined {
285- console . log ( "createNew" , options ) ;
286304 if ( typeof options . format !== 'string' ) {
287305 console . warn ( `Only defined format are supported; got ${ options . format } .` ) ;
288306 return ;
@@ -299,8 +317,22 @@ class SharedModelFactory implements ISharedModelFactory {
299317 this . _onCreate ( options , sharedModel ) ;
300318 return sharedModel ;
301319 }
302- console . log ( "no document factory" ) ;
303320
304321 return ;
305322 }
306323}
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