@@ -17,7 +17,7 @@ export class SyncManager {
1717
1818 private isSending = false ;
1919
20- constructor ( private doc : Y . Doc , private context : {
20+ constructor ( private doc : Y . Doc , private context : {
2121 userId : string , workspaceId : string , objectId : string , collabType : Types
2222 } ) {
2323 this . versionVector = this . loadVersionVector ( ) ;
@@ -26,41 +26,41 @@ export class SyncManager {
2626 this . setupListener ( ) ;
2727 }
2828
29- private setupListener ( ) {
29+ private setupListener ( ) {
3030 this . doc . on ( 'update' , ( _update : Uint8Array , origin : CollabOrigin ) => {
31- if ( origin === CollabOrigin . Remote ) return ;
31+ if ( origin === CollabOrigin . Remote ) return ;
3232 console . log ( 'Local changes detected. Sending update...' , origin ) ;
3333 this . debouncedSendUpdate ( ) ;
3434 } ) ;
3535 }
3636
37- private getStorageKey ( baseKey : string ) : string {
37+ private getStorageKey ( baseKey : string ) : string {
3838 return `${ this . context . userId } _${ baseKey } _${ this . context . workspaceId } _${ this . context . objectId } ` ;
3939 }
4040
41- private loadVersionVector ( ) : number {
41+ private loadVersionVector ( ) : number {
4242 const storedVector = localStorage . getItem ( this . getStorageKey ( VERSION_VECTOR_KEY ) ) ;
4343
4444 return storedVector ? parseInt ( storedVector , 10 ) : 0 ;
4545 }
4646
47- private saveVersionVector ( ) {
47+ private saveVersionVector ( ) {
4848 localStorage . setItem ( this . getStorageKey ( VERSION_VECTOR_KEY ) , this . versionVector . toString ( ) ) ;
4949 }
5050
51- private loadUnsyncedFlag ( ) : boolean {
51+ private loadUnsyncedFlag ( ) : boolean {
5252 return localStorage . getItem ( this . getStorageKey ( UNSYNCED_FLAG_KEY ) ) === 'true' ;
5353 }
5454
55- private saveUnsyncedFlag ( ) {
55+ private saveUnsyncedFlag ( ) {
5656 localStorage . setItem ( this . getStorageKey ( UNSYNCED_FLAG_KEY ) , this . hasUnsyncedChanges . toString ( ) ) ;
5757 }
5858
59- private loadLastSyncedAt ( ) : string {
59+ private loadLastSyncedAt ( ) : string {
6060 return localStorage . getItem ( this . getStorageKey ( LAST_SYNCED_AT_KEY ) ) || '' ;
6161 }
6262
63- private saveLastSyncedAt ( ) {
63+ private saveLastSyncedAt ( ) {
6464 localStorage . setItem ( this . getStorageKey ( LAST_SYNCED_AT_KEY ) , this . lastSyncedAt ) ;
6565 }
6666
@@ -71,8 +71,8 @@ export class SyncManager {
7171 void this . sendUpdate ( ) ;
7272 } , 1000 ) ; // 1 second debounce
7373
74- private async sendUpdate ( ) {
75- if ( this . isSending ) return ;
74+ private async sendUpdate ( ) {
75+ if ( this . isSending ) return ;
7676 this . isSending = true ;
7777
7878 try {
@@ -85,14 +85,14 @@ export class SyncManager {
8585
8686 const response = await updateCollab ( this . context . workspaceId , this . context . objectId , this . context . collabType , update , context ) ;
8787
88- if ( response ) {
88+ if ( response ) {
8989 console . log ( `Update sent successfully. Server version: ${ response . version_vector } ` ) ;
9090
9191 // Update last synced time
9292 this . lastSyncedAt = String ( Date . now ( ) ) ;
9393 this . saveLastSyncedAt ( ) ;
9494
95- if ( response . version_vector === this . versionVector ) {
95+ if ( response . version_vector === this . versionVector ) {
9696 // Our update was the latest
9797 this . hasUnsyncedChanges = false ;
9898 this . saveUnsyncedFlag ( ) ;
@@ -106,7 +106,7 @@ export class SyncManager {
106106 } else {
107107 return Promise . reject ( response ) ;
108108 }
109- } catch ( error ) {
109+ } catch ( error ) {
110110 console . error ( 'Failed to send update:' , error ) ;
111111 // Keep the unsynced flag as true
112112 this . hasUnsyncedChanges = true ;
@@ -116,23 +116,23 @@ export class SyncManager {
116116 }
117117 }
118118
119- public initialize ( ) {
120- if ( this . hasUnsyncedChanges ) {
119+ public initialize ( ) {
120+ if ( this . hasUnsyncedChanges ) {
121121 console . log ( 'Unsynced changes found. Sending update...' ) ;
122122 // Send an update if there are unsynced changes
123123 this . debouncedSendUpdate ( ) ;
124124 }
125125 }
126126
127- public getUnsyncedStatus ( ) : boolean {
127+ public getUnsyncedStatus ( ) : boolean {
128128 return this . hasUnsyncedChanges ;
129129 }
130130
131- public getLastSyncedAt ( ) : string {
131+ public getLastSyncedAt ( ) : string {
132132 return this . lastSyncedAt ;
133133 }
134134
135- public getCurrentVersionVector ( ) : number {
135+ public getCurrentVersionVector ( ) : number {
136136 return this . versionVector ;
137137 }
138138}
0 commit comments