@@ -987,15 +987,11 @@ export class SyncDoc extends EventEmitter {
987
987
this . assert_table_is_ready ( "syncstring" ) ;
988
988
this . dbg ( "set_initialized" ) ( { error, read_only, size } ) ;
989
989
const init = { time : this . client . server_time ( ) , size, error } ;
990
- this . syncstring_table . set ( {
991
- string_id : this . string_id ,
992
- project_id : this . project_id ,
993
- path : this . path ,
990
+ await this . set_syncstring_table ( {
994
991
init,
995
992
read_only,
996
993
last_active : this . client . server_time ( ) ,
997
994
} ) ;
998
- await this . syncstring_table . save ( ) ;
999
995
}
1000
996
1001
997
/* List of timestamps of the versions of this string in the sync
@@ -1287,13 +1283,9 @@ export class SyncDoc extends EventEmitter {
1287
1283
dbg ( "getting table..." ) ;
1288
1284
this . syncstring_table = await this . synctable ( query , [ ] ) ;
1289
1285
if ( this . ephemeral && this . client . is_project ( ) ) {
1290
- this . syncstring_table . set ( {
1291
- string_id : this . string_id ,
1292
- project_id : this . project_id ,
1293
- path : this . path ,
1286
+ await this . set_syncstring_table ( {
1294
1287
doctype : JSON . stringify ( this . doctype ) ,
1295
1288
} ) ;
1296
- await this . syncstring_table . save ( ) ;
1297
1289
} else {
1298
1290
dbg ( "waiting for, then handling the first update..." ) ;
1299
1291
await this . handle_syncstring_update ( ) ;
@@ -1934,13 +1926,9 @@ export class SyncDoc extends EventEmitter {
1934
1926
*/
1935
1927
public set_settings = async ( obj ) : Promise < void > => {
1936
1928
this . assert_is_ready ( "set_settings" ) ;
1937
- this . syncstring_table . set ( {
1938
- string_id : this . string_id ,
1939
- project_id : this . project_id ,
1940
- path : this . path ,
1929
+ await this . set_syncstring_table ( {
1941
1930
settings : obj ,
1942
1931
} ) ;
1943
- await this . syncstring_table . save ( ) ;
1944
1932
} ;
1945
1933
1946
1934
client_id = ( ) => {
@@ -2179,13 +2167,9 @@ export class SyncDoc extends EventEmitter {
2179
2167
}
2180
2168
2181
2169
if ( this . state != "ready" ) return ;
2182
- this . syncstring_table . set ( {
2183
- string_id : this . string_id ,
2184
- project_id : this . project_id ,
2185
- path : this . path ,
2170
+ await this . set_syncstring_table ( {
2186
2171
last_snapshot : time ,
2187
2172
} ) ;
2188
- await this . syncstring_table . save ( ) ;
2189
2173
this . last_snapshot = time ;
2190
2174
}
2191
2175
@@ -2351,10 +2335,7 @@ export class SyncDoc extends EventEmitter {
2351
2335
} ;
2352
2336
2353
2337
public set_snapshot_interval = async ( n : number ) : Promise < void > => {
2354
- this . syncstring_table . set ( {
2355
- string_id : this . string_id ,
2356
- project_id : this . project_id ,
2357
- path : this . path ,
2338
+ await this . set_syncstring_table ( {
2358
2339
snapshot_interval : n ,
2359
2340
} ) ;
2360
2341
await this . syncstring_table . save ( ) ;
@@ -2558,13 +2539,9 @@ export class SyncDoc extends EventEmitter {
2558
2539
if ( this . my_user_id === - 1 ) {
2559
2540
this . my_user_id = this . users . length ;
2560
2541
this . users . push ( client_id ) ;
2561
- this . syncstring_table . set ( {
2562
- string_id : this . string_id ,
2563
- project_id : this . project_id ,
2564
- path : this . path ,
2542
+ await this . set_syncstring_table ( {
2565
2543
users : this . users ,
2566
2544
} ) ;
2567
- await this . syncstring_table . save ( ) ;
2568
2545
}
2569
2546
2570
2547
this . emit ( "metadata-change" ) ;
@@ -2743,24 +2720,15 @@ export class SyncDoc extends EventEmitter {
2743
2720
this . assert_table_is_ready ( "syncstring" ) ;
2744
2721
// set timestamp of when the save happened; this can be useful
2745
2722
// for coordinating running code, etc.... and is just generally useful.
2746
- this . syncstring_table . set ( {
2747
- string_id : this . string_id ,
2748
- project_id : this . project_id ,
2749
- path : this . path ,
2750
- save,
2751
- } ) ;
2752
- await this . syncstring_table . save ( ) ;
2723
+ if ( ! save . time ) {
2724
+ save . time = Date . now ( ) ;
2725
+ }
2726
+ await this . set_syncstring_table ( { save } ) ;
2753
2727
} ;
2754
2728
2755
2729
private set_read_only = async ( read_only : boolean ) : Promise < void > => {
2756
2730
this . assert_table_is_ready ( "syncstring" ) ;
2757
- this . syncstring_table . set ( {
2758
- string_id : this . string_id ,
2759
- project_id : this . project_id ,
2760
- path : this . path ,
2761
- read_only,
2762
- } ) ;
2763
- await this . syncstring_table . save ( ) ;
2731
+ await this . set_syncstring_table ( { read_only } ) ;
2764
2732
} ;
2765
2733
2766
2734
public is_read_only = ( ) : boolean => {
@@ -3323,4 +3291,19 @@ export class SyncDoc extends EventEmitter {
3323
3291
// with a nested for loop of sets. Doing it this way, massively
3324
3292
// simplifies client code.
3325
3293
emit_change_debounced = debounce ( this . emit_change . bind ( this ) , 0 ) ;
3294
+
3295
+ private set_syncstring_table = async ( obj , save = true ) => {
3296
+ let value = this . syncstring_table_get_one ( ) ;
3297
+ const value0 = value ;
3298
+ for ( const key in obj ) {
3299
+ value = value . set ( key , obj [ key ] ) ;
3300
+ }
3301
+ if ( value0 . equals ( value ) ) {
3302
+ return ;
3303
+ }
3304
+ this . syncstring_table . set ( value ) ;
3305
+ if ( save ) {
3306
+ await this . syncstring_table . save ( ) ;
3307
+ }
3308
+ } ;
3326
3309
}
0 commit comments