@@ -255,25 +255,29 @@ export class FirebaseAdapter implements IDatabaseAdapter {
255255 */
256256 protected _monitorHistory ( ) : void {
257257 // Get the latest checkpoint as a starting point so we don't have to re-play entire history.
258- this . _databaseRef ! . child ( "checkpoint" ) . once ( "value" , ( snapshot ) => {
259- if ( this . _zombie ) {
260- // just in case we were cleaned up before we got the checkpoint data.
261- return ;
262- }
258+ this . _databaseRef ! . child ( "checkpoint" )
259+ . once ( "value" , ( snapshot ) => {
260+ if ( this . _zombie ) {
261+ // just in case we were cleaned up before we got the checkpoint data.
262+ return ;
263+ }
263264
264- const revisionId : string | null = snapshot . child ( "id" ) . val ( ) ;
265- const op : TextOperationType | null = snapshot . child ( "o" ) . val ( ) ;
266- const author : UserIDType | null = snapshot . child ( "a" ) . val ( ) ;
265+ const revisionId : string | null = snapshot . child ( "id" ) . val ( ) ;
266+ const op : TextOperationType | null = snapshot . child ( "o" ) . val ( ) ;
267+ const author : UserIDType | null = snapshot . child ( "a" ) . val ( ) ;
267268
268- if ( op != null && revisionId != null && author !== null ) {
269- this . _pendingReceivedRevisions [ revisionId ] = { o : op , a : author } ;
270- this . _checkpointRevision = this . _revisionFromId ( revisionId ) ;
271- this . _monitorHistoryStartingAt ( this . _checkpointRevision + 1 ) ;
272- } else {
273- this . _checkpointRevision = 0 ;
274- this . _monitorHistoryStartingAt ( this . _checkpointRevision ) ;
275- }
276- } ) ;
269+ if ( op != null && revisionId != null && author !== null ) {
270+ this . _pendingReceivedRevisions [ revisionId ] = { o : op , a : author } ;
271+ this . _checkpointRevision = this . _revisionFromId ( revisionId ) ;
272+ this . _monitorHistoryStartingAt ( this . _checkpointRevision + 1 ) ;
273+ } else {
274+ this . _checkpointRevision = 0 ;
275+ this . _monitorHistoryStartingAt ( this . _checkpointRevision ) ;
276+ }
277+ } )
278+ . catch ( ( err ) => {
279+ console . error ( "[firebase] Error getting checkpoint" , err ) ;
280+ } ) ;
277281 }
278282
279283 /**
@@ -305,9 +309,13 @@ export class FirebaseAdapter implements IDatabaseAdapter {
305309
306310 this . _firebaseOn ( historyRef , "child_added" , this . _historyChildAdded , this ) ;
307311
308- historyRef . once ( "value" , ( ) => {
309- this . _handleInitialRevisions ( ) ;
310- } ) ;
312+ historyRef
313+ . once ( "value" , ( ) => {
314+ this . _handleInitialRevisions ( ) ;
315+ } )
316+ . catch ( ( err ) => {
317+ console . error ( "[firebase] Error getting initial revisions" , err ) ;
318+ } ) ;
311319 }
312320
313321 /**
@@ -479,43 +487,52 @@ export class FirebaseAdapter implements IDatabaseAdapter {
479487 revisionData : FirebaseOperationDataType ,
480488 callback : SendOperationCallbackType
481489 ) : void {
482- this . _databaseRef ! . child ( "history" )
483- . child ( revisionId )
484- . transaction (
485- ( current ) => {
486- if ( current === null ) {
487- return revisionData ;
488- }
489- } ,
490- ( error , committed ) => {
491- if ( error ) {
492- if ( error . message === "disconnect" ) {
493- if ( this . _sent && this . _sent . id === revisionId ) {
494- // We haven't seen our transaction succeed or fail. Send it again.
495- setTimeout ( ( ) => {
496- this . _doTransaction ( revisionId , revisionData , callback ) ;
497- } ) ;
498- }
499-
500- return callback ( error , false ) ;
501- } else {
502- this . _trigger (
503- FirebaseAdapterEvent . Error ,
504- error ,
505- revisionData . o . toString ( ) ,
506- {
507- operation : revisionData . o . toString ( ) ,
508- document : this . _document ! . toString ( ) ,
490+ try {
491+ this . _databaseRef ! . child ( "history" )
492+ . child ( revisionId )
493+ . transaction (
494+ ( current ) => {
495+ if ( current === null ) {
496+ return revisionData ;
497+ }
498+ } ,
499+ ( error , committed ) => {
500+ console . error ( "[firebase] Transaction error - onComplete" , error ) ;
501+
502+ if ( error ) {
503+ if ( error . message === "disconnect" ) {
504+ if ( this . _sent && this . _sent . id === revisionId ) {
505+ // We haven't seen our transaction succeed or fail. Send it again.
506+ setTimeout ( ( ) => {
507+ this . _doTransaction ( revisionId , revisionData , callback ) ;
508+ } ) ;
509509 }
510- ) ;
511- Utils . onFailedDatabaseTransaction ( error . message ) ;
510+
511+ return callback ( error , false ) ;
512+ } else {
513+ this . _trigger (
514+ FirebaseAdapterEvent . Error ,
515+ error ,
516+ revisionData . o . toString ( ) ,
517+ {
518+ operation : revisionData . o . toString ( ) ,
519+ document : this . _document ! . toString ( ) ,
520+ }
521+ ) ;
522+ Utils . onFailedDatabaseTransaction ( error . message ) ;
523+ }
512524 }
513- }
514525
515- return callback ( null , committed ) ;
516- } ,
517- false
518- ) ;
526+ return callback ( null , committed ) ;
527+ } ,
528+ false
529+ )
530+ . catch ( ( error ) => {
531+ console . error ( "[firebase] Transaction error - catch()" , error ) ;
532+ } ) ;
533+ } catch ( error ) {
534+ console . error ( "[firebase] Transaction error - trycatch" , error ) ;
535+ }
519536 }
520537
521538 /**
@@ -550,12 +567,16 @@ export class FirebaseAdapter implements IDatabaseAdapter {
550567 * Updates current document state into `checkpoint` node in Firebase.
551568 */
552569 protected _saveCheckpoint ( ) : void {
553- this . _databaseRef ! . child ( "checkpoint" ) . set ( {
554- a : this . _userId ,
555- o : this . _document ! . toJSON ( ) ,
556- // use the id for the revision we just wrote.
557- id : this . _revisionToId ( this . _revision - 1 ) ,
558- } ) ;
570+ this . _databaseRef ! . child ( "checkpoint" )
571+ . set ( {
572+ a : this . _userId ,
573+ o : this . _document ! . toJSON ( ) ,
574+ // use the id for the revision we just wrote.
575+ id : this . _revisionToId ( this . _revision - 1 ) ,
576+ } )
577+ . catch ( ( err ) => {
578+ console . error ( "[firebase] Error saving checkpoint" , err ) ;
579+ } ) ;
559580 }
560581
561582 isHistoryEmpty ( ) : boolean {
0 commit comments