Skip to content

Commit 5042f72

Browse files
Move ignoreIfPrimaryLeaseLoss to local_store.ts (#2544)
1 parent 38876ff commit 5042f72

File tree

7 files changed

+38
-38
lines changed

7 files changed

+38
-38
lines changed

packages/firestore/src/core/sync_engine.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
import { User } from '../auth/user';
19-
import { LocalStore } from '../local/local_store';
19+
import { ignoreIfPrimaryLeaseLoss, LocalStore } from '../local/local_store';
2020
import { LocalViewChanges } from '../local/local_view_changes';
2121
import { ReferenceSet } from '../local/reference_set';
2222
import { TargetData, TargetPurpose } from '../local/target_data';
@@ -40,7 +40,6 @@ import { ObjectMap } from '../util/obj_map';
4040
import { Deferred } from '../util/promise';
4141
import { SortedMap } from '../util/sorted_map';
4242

43-
import { ignoreIfPrimaryLeaseLoss } from '../local/indexeddb_persistence';
4443
import { ClientId, SharedClientState } from '../local/shared_client_state';
4544
import {
4645
QueryTargetState,

packages/firestore/src/local/indexeddb_persistence.ts

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import {
6363
Persistence,
6464
PersistenceTransaction,
6565
PersistenceTransactionMode,
66+
PRIMARY_LEASE_LOST_ERROR_MSG,
6667
PrimaryStateListener,
6768
ReferenceDelegate
6869
} from './persistence';
@@ -97,9 +98,6 @@ const MAX_PRIMARY_ELIGIBLE_AGE_MS = 5000;
9798
*/
9899
const CLIENT_METADATA_REFRESH_INTERVAL_MS = 4000;
99100
/** User-facing error when the primary lease is required but not available. */
100-
const PRIMARY_LEASE_LOST_ERROR_MSG =
101-
'The current tab is not in the required state to perform this operation. ' +
102-
'It might be necessary to refresh the browser tab.';
103101
const PRIMARY_LEASE_EXCLUSIVE_ERROR_MSG =
104102
'Another tab has exclusive access to the persistence layer. ' +
105103
'To allow shared access, make sure to invoke ' +
@@ -1052,33 +1050,6 @@ export class IndexedDbPersistence implements Persistence {
10521050
}
10531051
}
10541052

1055-
function isPrimaryLeaseLostError(err: FirestoreError): boolean {
1056-
return (
1057-
err.code === Code.FAILED_PRECONDITION &&
1058-
err.message === PRIMARY_LEASE_LOST_ERROR_MSG
1059-
);
1060-
}
1061-
1062-
/**
1063-
* Verifies the error thrown by a LocalStore operation. If a LocalStore
1064-
* operation fails because the primary lease has been taken by another client,
1065-
* we ignore the error (the persistence layer will immediately call
1066-
* `applyPrimaryLease` to propagate the primary state change). All other errors
1067-
* are re-thrown.
1068-
*
1069-
* @param err An error returned by a LocalStore operation.
1070-
* @return A Promise that resolves after we recovered, or the original error.
1071-
*/
1072-
export async function ignoreIfPrimaryLeaseLoss(
1073-
err: FirestoreError
1074-
): Promise<void> {
1075-
if (isPrimaryLeaseLostError(err)) {
1076-
log.debug(LOG_TAG, 'Unexpectedly lost primary lease');
1077-
} else {
1078-
throw err;
1079-
}
1080-
}
1081-
10821053
/**
10831054
* Helper to get a typed SimpleDbStore for the primary client object store.
10841055
*/

packages/firestore/src/local/local_store.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
} from '../model/mutation_batch';
3939
import { RemoteEvent, TargetChange } from '../remote/remote_event';
4040
import { assert } from '../util/assert';
41+
import { Code, FirestoreError } from '../util/error';
4142
import * as log from '../util/log';
4243
import { primitiveComparator } from '../util/misc';
4344
import * as objUtils from '../util/obj';
@@ -49,7 +50,11 @@ import { LocalViewChanges } from './local_view_changes';
4950
import { LruGarbageCollector, LruResults } from './lru_garbage_collector';
5051
import { IndexedDbRemoteDocumentCache } from './indexeddb_remote_document_cache';
5152
import { MutationQueue } from './mutation_queue';
52-
import { Persistence, PersistenceTransaction } from './persistence';
53+
import {
54+
Persistence,
55+
PersistenceTransaction,
56+
PRIMARY_LEASE_LOST_ERROR_MSG
57+
} from './persistence';
5358
import { PersistencePromise } from './persistence_promise';
5459
import { TargetCache } from './target_cache';
5560
import { QueryEngine } from './query_engine';
@@ -1121,3 +1126,26 @@ export class LocalStore {
11211126
}
11221127
}
11231128
}
1129+
1130+
/**
1131+
* Verifies the error thrown by a LocalStore operation. If a LocalStore
1132+
* operation fails because the primary lease has been taken by another client,
1133+
* we ignore the error (the persistence layer will immediately call
1134+
* `applyPrimaryLease` to propagate the primary state change). All other errors
1135+
* are re-thrown.
1136+
*
1137+
* @param err An error returned by a LocalStore operation.
1138+
* @return A Promise that resolves after we recovered, or the original error.
1139+
*/
1140+
export async function ignoreIfPrimaryLeaseLoss(
1141+
err: FirestoreError
1142+
): Promise<void> {
1143+
if (
1144+
err.code === Code.FAILED_PRECONDITION &&
1145+
err.message === PRIMARY_LEASE_LOST_ERROR_MSG
1146+
) {
1147+
log.debug(LOG_TAG, 'Unexpectedly lost primary lease');
1148+
} else {
1149+
throw err;
1150+
}
1151+
}

packages/firestore/src/local/lru_garbage_collector.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ import { primitiveComparator } from '../util/misc';
2424
import { CancelablePromise } from '../util/promise';
2525
import { SortedMap } from '../util/sorted_map';
2626
import { SortedSet } from '../util/sorted_set';
27-
import { ignoreIfPrimaryLeaseLoss } from './indexeddb_persistence';
28-
import { LocalStore } from './local_store';
27+
import { ignoreIfPrimaryLeaseLoss, LocalStore } from './local_store';
2928
import { PersistenceTransaction } from './persistence';
3029
import { PersistencePromise } from './persistence_promise';
3130
import { TargetData } from './target_data';

packages/firestore/src/local/persistence.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ import { RemoteDocumentCache } from './remote_document_cache';
2828
import { ClientId } from './shared_client_state';
2929
import { TargetData } from './target_data';
3030

31+
export const PRIMARY_LEASE_LOST_ERROR_MSG =
32+
'The current tab is not in the required state to perform this operation. ' +
33+
'It might be necessary to refresh the browser tab.';
34+
3135
/**
3236
* A base class representing a persistence transaction, encapsulating both the
3337
* transaction's sequence numbers as well as a list of onCommitted listeners.

packages/firestore/src/remote/remote_store.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import { SnapshotVersion } from '../core/snapshot_version';
1919
import { Transaction } from '../core/transaction';
2020
import { OnlineState, TargetId } from '../core/types';
21-
import { LocalStore } from '../local/local_store';
21+
import { ignoreIfPrimaryLeaseLoss, LocalStore } from '../local/local_store';
2222
import { TargetData, TargetPurpose } from '../local/target_data';
2323
import { MutationResult } from '../model/mutation';
2424
import {
@@ -32,7 +32,6 @@ import { FirestoreError } from '../util/error';
3232
import * as log from '../util/log';
3333
import * as objUtils from '../util/obj';
3434

35-
import { ignoreIfPrimaryLeaseLoss } from '../local/indexeddb_persistence';
3635
import { DocumentKeySet } from '../model/collections';
3736
import { AsyncQueue } from '../util/async_queue';
3837
import { ConnectivityMonitor, NetworkStatus } from './connectivity_monitor';

packages/firestore/test/integration/api/database.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ apiDescribe('Database', (persistence: boolean) => {
814814
// eslint-disable-next-line no-restricted-properties
815815
describe.skip('Listens are rejected remotely:', () => {
816816
//eslint-disable-next-line @typescript-eslint/no-explicit-any
817-
const queryForRejection : firestore.Query = null as any;
817+
const queryForRejection: firestore.Query = null as any;
818818

819819
it('will reject listens', () => {
820820
const deferred = new Deferred();

0 commit comments

Comments
 (0)