@@ -148,7 +148,6 @@ export class Replicache<MD extends MutatorDefs = {}> {
148
148
private _online = true ;
149
149
private readonly _logger : Logger ;
150
150
private readonly _ready : Promise < void > ;
151
- private readonly _resolveReady : ( ) => void ;
152
151
private readonly _clientIDPromise : Promise < string > ;
153
152
private _root : Promise < string | undefined > = Promise . resolve ( undefined ) ;
154
153
private readonly _mutatorRegistry = new Map <
@@ -295,9 +294,8 @@ export class Replicache<MD extends MutatorDefs = {}> {
295
294
296
295
// Use a promise-resolve pair so that we have a promise to use even before
297
296
// we call the Open RPC.
298
- const { promise, resolve} = resolver < void > ( ) ;
299
- this . _ready = promise ;
300
- this . _resolveReady = resolve ;
297
+ const readyResolver = resolver < void > ( ) ;
298
+ this . _ready = readyResolver . promise ;
301
299
302
300
const { minDelayMs = MIN_DELAY_MS , maxDelayMs = MAX_DELAY_MS } =
303
301
requestOptions ;
@@ -331,23 +329,31 @@ export class Replicache<MD extends MutatorDefs = {}> {
331
329
332
330
this . _lc = new LogContext ( logLevel ) . addContext ( 'db' , name ) ;
333
331
334
- this . _clientIDPromise = this . _open ( ) ;
332
+ const clientIDResolver = resolver < string > ( ) ;
333
+ this . _clientIDPromise = clientIDResolver . promise ;
334
+
335
+ void this . _open ( clientIDResolver . resolve , readyResolver . resolve ) ;
335
336
}
336
337
337
- private async _open ( ) : Promise < string > {
338
+ private async _open (
339
+ resolveClientID : ( clientID : string ) => void ,
340
+ resolveReady : ( ) => void ,
341
+ ) : Promise < void > {
338
342
// If we are currently closing a Replicache instance with the same name,
339
343
// wait for it to finish closing.
340
344
await closingInstances . get ( this . name ) ;
341
345
342
346
await Promise . all ( [ initHasher ( ) , migrate ( this . _kvStore , this . _lc ) ] ) ;
343
347
344
- const [ clientID ] = await Promise . all ( [
345
- sync . initClientID ( this . _kvStore ) ,
348
+ await Promise . all ( [
349
+ sync . initClientID ( this . _kvStore ) . then ( clientID => {
350
+ resolveClientID ( clientID ) ;
351
+ } ) ,
346
352
db . maybeInitDefaultDB ( this . _dagStore ) ,
347
353
] ) ;
348
354
349
355
// Now we have both a clientID and DB!
350
- this . _resolveReady ( ) ;
356
+ resolveReady ( ) ;
351
357
352
358
if ( hasBroadcastChannel ) {
353
359
this . _broadcastChannel = new BroadcastChannel ( storageKeyName ( this . name ) ) ;
@@ -361,7 +367,6 @@ export class Replicache<MD extends MutatorDefs = {}> {
361
367
362
368
this . pull ( ) ;
363
369
this . _push ( ) ;
364
- return clientID ;
365
370
}
366
371
367
372
/**
@@ -585,13 +590,15 @@ export class Replicache<MD extends MutatorDefs = {}> {
585
590
f : ( tx : IndexTransactionImpl ) => Promise < void > ,
586
591
) : Promise < void > {
587
592
await this . _ready ;
593
+ // clientID must be awaited ouside dag transaction to avoid a premature
594
+ // auto-commit of the idb transaction.
595
+ const clientID = await this . _clientIDPromise ;
588
596
await this . _dagStore . withWrite ( async dagWrite => {
589
597
const dbWrite = await db . Write . newIndexChange (
590
598
db . whenceHead ( db . DEFAULT_HEAD_NAME ) ,
591
599
dagWrite ,
592
600
) ;
593
-
594
- const tx = new IndexTransactionImpl ( dbWrite , this . _lc ) ;
601
+ const tx = new IndexTransactionImpl ( clientID , dbWrite , this . _lc ) ;
595
602
await f ( tx ) ;
596
603
await tx . commit ( ) ;
597
604
} ) ;
@@ -994,9 +1001,12 @@ export class Replicache<MD extends MutatorDefs = {}> {
994
1001
*/
995
1002
async query < R > ( body : ( tx : ReadTransaction ) => Promise < R > | R ) : Promise < R > {
996
1003
await this . _ready ;
1004
+ // clientID must be awaited ouside dag transaction to avoid a premature
1005
+ // auto-commit of the idb transaction.
1006
+ const clientID = await this . _clientIDPromise ;
997
1007
return await this . _dagStore . withRead ( async dagRead => {
998
1008
const dbRead = await db . readFromDefaultHead ( dagRead ) ;
999
- const tx = new ReadTransactionImpl ( dbRead , this . _lc ) ;
1009
+ const tx = new ReadTransactionImpl ( clientID , dbRead , this . _lc ) ;
1000
1010
return await body ( tx ) ;
1001
1011
} ) ;
1002
1012
}
@@ -1079,6 +1089,9 @@ export class Replicache<MD extends MutatorDefs = {}> {
1079
1089
}
1080
1090
1081
1091
await this . _ready ;
1092
+ // clientID must be awaited ouside dag transaction to avoid a premature
1093
+ // auto-commit of the idb transaction.
1094
+ const clientID = await this . _clientIDPromise ;
1082
1095
return await this . _dagStore . withWrite ( async dagWrite => {
1083
1096
let whence : db . Whence | undefined ;
1084
1097
let originalHash : string | null = null ;
@@ -1098,7 +1111,7 @@ export class Replicache<MD extends MutatorDefs = {}> {
1098
1111
dagWrite ,
1099
1112
) ;
1100
1113
1101
- const tx = new WriteTransactionImpl ( dbWrite , this . _lc ) ;
1114
+ const tx = new WriteTransactionImpl ( clientID , dbWrite , this . _lc ) ;
1102
1115
const result : R = await mutatorImpl ( tx , args ) ;
1103
1116
1104
1117
const [ ref , changedKeys ] = await tx . commit ( ! isReplay ) ;
0 commit comments