@@ -54,8 +54,10 @@ const MUTATION_BATCH_KEY_PREFIX = 'firestore_mutations';
54
54
// firestore_targets_<persistence_prefix>_<target_id>
55
55
const QUERY_TARGET_KEY_PREFIX = 'firestore_targets' ;
56
56
57
- // The LocalStorage key that stores the primary tab's online state.
58
- const ONLINE_STATE_KEY = 'firestore_online_state' ;
57
+ // The LocalStorage prefix that stores the primary tab's online state. The
58
+ // format of the key is:
59
+ // firestore_online_state_<persistence_prefix>
60
+ const ONLINE_STATE_KEY_PREFIX = 'firestore_online_state' ;
59
61
60
62
/**
61
63
* A randomly-generated key assigned to each Firestore instance at startup.
@@ -511,6 +513,8 @@ export class WebStorageSharedClientState implements SharedClientState {
511
513
private readonly localClientStorageKey : string ;
512
514
private readonly activeClients : { [ key : string ] : ClientState } = { } ;
513
515
private readonly storageListener = this . handleLocalStorageEvent . bind ( this ) ;
516
+ private readonly escapedPersistenceKey : string ;
517
+ private readonly onlineStateKey : string ;
514
518
private readonly clientStateKeyRe : RegExp ;
515
519
private readonly mutationBatchKeyRe : RegExp ;
516
520
private readonly queryTargetKeyRe : RegExp ;
@@ -526,7 +530,7 @@ export class WebStorageSharedClientState implements SharedClientState {
526
530
constructor (
527
531
private readonly queue : AsyncQueue ,
528
532
private readonly platform : Platform ,
529
- private readonly persistenceKey : string ,
533
+ persistenceKey : string ,
530
534
private readonly localClientId : ClientId ,
531
535
initialUser : User
532
536
) {
@@ -536,30 +540,36 @@ export class WebStorageSharedClientState implements SharedClientState {
536
540
'LocalStorage is not available on this platform.'
537
541
) ;
538
542
}
543
+ // Escape the special characters mentioned here:
544
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
545
+ this . escapedPersistenceKey = persistenceKey . replace (
546
+ / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g,
547
+ '\\$&'
548
+ ) ;
549
+
539
550
this . storage = this . platform . window ! . localStorage ;
540
551
this . currentUser = initialUser ;
541
552
this . localClientStorageKey = this . toLocalStorageClientStateKey (
542
553
this . localClientId
543
554
) ;
544
555
this . activeClients [ this . localClientId ] = new LocalClientState ( ) ;
545
556
546
- // Escape the special characters mentioned here:
547
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
548
- const escapedPersistenceKey = persistenceKey . replace (
549
- / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g,
550
- '\\$&'
551
- ) ;
552
-
553
557
this . clientStateKeyRe = new RegExp (
554
- `^${ CLIENT_STATE_KEY_PREFIX } _${ escapedPersistenceKey } _([^_]*)$`
558
+ `^${ CLIENT_STATE_KEY_PREFIX } _${ this . escapedPersistenceKey } _([^_]*)$`
555
559
) ;
556
560
this . mutationBatchKeyRe = new RegExp (
557
- `^${ MUTATION_BATCH_KEY_PREFIX } _${ escapedPersistenceKey } _(\\d+)(?:_(.*))?$`
561
+ `^${ MUTATION_BATCH_KEY_PREFIX } _${
562
+ this . escapedPersistenceKey
563
+ } _(\\d+)(?:_(.*))?$`
558
564
) ;
559
565
this . queryTargetKeyRe = new RegExp (
560
- `^${ QUERY_TARGET_KEY_PREFIX } _${ escapedPersistenceKey } _(\\d+)$`
566
+ `^${ QUERY_TARGET_KEY_PREFIX } _${ this . escapedPersistenceKey } _(\\d+)$`
561
567
) ;
562
568
569
+ this . onlineStateKey = `${ ONLINE_STATE_KEY_PREFIX } _${
570
+ this . escapedPersistenceKey
571
+ } `;
572
+
563
573
// Rather than adding the storage observer during start(), we add the
564
574
// storage observer during initialization. This ensures that we collect
565
575
// events before other components populate their initial state (during their
@@ -612,7 +622,7 @@ export class WebStorageSharedClientState implements SharedClientState {
612
622
613
623
// Check if there is an existing online state and call the callback handler
614
624
// if applicable.
615
- const onlineStateJSON = this . storage . getItem ( ONLINE_STATE_KEY ) ;
625
+ const onlineStateJSON = this . storage . getItem ( this . onlineStateKey ) ;
616
626
if ( onlineStateJSON ) {
617
627
const onlineState = this . fromLocalStorageOnlineState ( onlineStateJSON ) ;
618
628
if ( onlineState ) {
@@ -821,7 +831,7 @@ export class WebStorageSharedClientState implements SharedClientState {
821
831
return this . handleQueryTargetEvent ( queryTargetMetadata ) ;
822
832
}
823
833
}
824
- } else if ( event . key === ONLINE_STATE_KEY ) {
834
+ } else if ( event . key === this . onlineStateKey ) {
825
835
if ( event . newValue !== null ) {
826
836
const onlineState = this . fromLocalStorageOnlineState (
827
837
event . newValue
@@ -871,7 +881,7 @@ export class WebStorageSharedClientState implements SharedClientState {
871
881
clientId : this . localClientId ,
872
882
onlineState : OnlineState [ onlineState ]
873
883
} ;
874
- this . storage . setItem ( ONLINE_STATE_KEY , JSON . stringify ( entry ) ) ;
884
+ this . storage . setItem ( this . onlineStateKey , JSON . stringify ( entry ) ) ;
875
885
}
876
886
877
887
private persistQueryTargetState (
@@ -891,18 +901,22 @@ export class WebStorageSharedClientState implements SharedClientState {
891
901
`Client key cannot contain '_', but was '${ clientId } '`
892
902
) ;
893
903
894
- return `${ CLIENT_STATE_KEY_PREFIX } _${ this . persistenceKey } _${ clientId } ` ;
904
+ return `${ CLIENT_STATE_KEY_PREFIX } _${
905
+ this . escapedPersistenceKey
906
+ } _${ clientId } `;
895
907
}
896
908
897
909
/** Assembles the key for a query state in LocalStorage */
898
910
private toLocalStorageQueryTargetMetadataKey ( targetId : TargetId ) : string {
899
- return `${ QUERY_TARGET_KEY_PREFIX } _${ this . persistenceKey } _${ targetId } ` ;
911
+ return `${ QUERY_TARGET_KEY_PREFIX } _${
912
+ this . escapedPersistenceKey
913
+ } _${ targetId } `;
900
914
}
901
915
902
916
/** Assembles the key for a mutation batch in LocalStorage */
903
917
private toLocalStorageMutationBatchKey ( batchId : BatchId ) : string {
904
918
let mutationKey = `${ MUTATION_BATCH_KEY_PREFIX } _${
905
- this . persistenceKey
919
+ this . escapedPersistenceKey
906
920
} _${ batchId } `;
907
921
908
922
if ( this . currentUser . isAuthenticated ( ) ) {
0 commit comments