Skip to content

Commit 4065718

Browse files
[AUTOMATED]: Prettier Code Styling
1 parent 82b3d27 commit 4065718

File tree

4 files changed

+197
-143
lines changed

4 files changed

+197
-143
lines changed

packages/firestore/src/local/instance_metadata_channel.ts

+76-45
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ import { assert } from '../util/assert';
2020
import { debug, error } from '../util/log';
2121
import { primitiveComparator } from '../util/misc';
2222
import { SortedSet } from '../util/sorted_set';
23-
import {isSafeInteger} from '../util/types';
23+
import { isSafeInteger } from '../util/types';
2424

2525
const LOG_TAG = 'InstanceMetadataChannel';
2626

27-
2827
const FIRESTORE_PREFIX = 'fs';
2928

3029
// The format of an instance key if:
@@ -96,7 +95,8 @@ export interface InstanceMetadataChannel {
9695
* LocalStorage serialization. The InstanceKey is omitted here here as it is
9796
* encoded as part of the key.
9897
*/
99-
export interface InstanceStateSchema { // Visible for testing.
98+
export interface InstanceStateSchema {
99+
// Visible for testing.
100100
lastUpdateTime: number;
101101
activeTargetIds: number[];
102102
minMutationBatchId?: number;
@@ -132,14 +132,18 @@ class InstanceState {
132132
* Parses an InstanceState from its JSON representation in LocalStorage.
133133
* Logs a warning and returns null if the data could not be parsed.
134134
*/
135-
static fromLocalStorageEntry(instanceKey: string, value: string): InstanceState | null {
135+
static fromLocalStorageEntry(
136+
instanceKey: string,
137+
value: string
138+
): InstanceState | null {
136139
const instanceData = JSON.parse(value) as InstanceStateSchema;
137140

138-
let validData = typeof instanceData === 'object'
139-
&& isSafeInteger(instanceData.lastUpdateTime)
140-
&& instanceData.activeTargetIds instanceof Array
141-
&& isSafeInteger (instanceData.minMutationBatchId)
142-
&& isSafeInteger(instanceData.maxMutationBatchId);
141+
let validData =
142+
typeof instanceData === 'object' &&
143+
isSafeInteger(instanceData.lastUpdateTime) &&
144+
instanceData.activeTargetIds instanceof Array &&
145+
isSafeInteger(instanceData.minMutationBatchId) &&
146+
isSafeInteger(instanceData.maxMutationBatchId);
143147

144148
if (validData) {
145149
let activeTargetIdsArray = instanceData.activeTargetIds;
@@ -152,18 +156,20 @@ class InstanceState {
152156

153157
if (validData) {
154158
return new InstanceState(
155-
instanceKey,
156-
new Date(instanceData.lastUpdateTime),
157-
activeTargetIdsSet,
158-
instanceData.minMutationBatchId,
159-
instanceData.maxMutationBatchId
159+
instanceKey,
160+
new Date(instanceData.lastUpdateTime),
161+
activeTargetIdsSet,
162+
instanceData.minMutationBatchId,
163+
instanceData.maxMutationBatchId
160164
);
161165
}
162166
}
163167

164-
error(LOG_TAG, `Failed to parse instance metadata for instance '${instanceKey}'`);
168+
error(
169+
LOG_TAG,
170+
`Failed to parse instance metadata for instance '${instanceKey}'`
171+
);
165172
return null;
166-
167173
}
168174
}
169175

@@ -181,7 +187,13 @@ class LocalInstanceState extends InstanceState {
181187
private pendingBatchIds = new SortedSet<BatchId>(primitiveComparator);
182188

183189
constructor(instanceKey: InstanceKey) {
184-
super(instanceKey, new Date(), new SortedSet<TargetId>(primitiveComparator), null, null);
190+
super(
191+
instanceKey,
192+
new Date(),
193+
new SortedSet<TargetId>(primitiveComparator),
194+
null,
195+
null
196+
);
185197
}
186198

187199
get minMutationBatchId(): BatchId | null {
@@ -268,12 +280,16 @@ export class WebStorageMetadataChannel implements InstanceMetadataChannel {
268280
constructor(private persistenceKey: string, private instanceKey: string) {
269281
this.localStorage = window ? window.localStorage : undefined;
270282
this.storageKey = toLocalStorageKey(
271-
INSTANCE_KEY_NAMESPACE,
272-
this.persistenceKey,
273-
this.instanceKey
283+
INSTANCE_KEY_NAMESPACE,
284+
this.persistenceKey,
285+
this.instanceKey
286+
);
287+
this.activeInstances[this.instanceKey] = new LocalInstanceState(
288+
this.instanceKey
289+
);
290+
this.instanceKeyRe = new RegExp(
291+
`^fs_instances_${persistenceKey}_[^_]{20}$`
274292
);
275-
this.activeInstances[this.instanceKey] = new LocalInstanceState(this.instanceKey);
276-
this.instanceKeyRe = new RegExp(`^fs_instances_${persistenceKey}_[^_]{20}$`);
277293
}
278294

279295
/** Returns 'true' if LocalStorage is available in the current environment. */
@@ -284,8 +300,8 @@ export class WebStorageMetadataChannel implements InstanceMetadataChannel {
284300
start(knownInstances: InstanceKey[]): void {
285301
if (!WebStorageMetadataChannel.isAvailable()) {
286302
throw new FirestoreError(
287-
Code.UNIMPLEMENTED,
288-
'LocalStorage is not available on this platform.'
303+
Code.UNIMPLEMENTED,
304+
'LocalStorage is not available on this platform.'
289305
);
290306
}
291307
assert(!this.started, 'LocalStorageMetadataNotifier already started');
@@ -294,11 +310,14 @@ export class WebStorageMetadataChannel implements InstanceMetadataChannel {
294310

295311
for (const instanceKey of knownInstances) {
296312
const storageKey = toLocalStorageKey(
297-
INSTANCE_KEY_NAMESPACE,
298-
this.persistenceKey,
299-
instanceKey
313+
INSTANCE_KEY_NAMESPACE,
314+
this.persistenceKey,
315+
instanceKey
316+
);
317+
const instanceState = InstanceState.fromLocalStorageEntry(
318+
instanceKey,
319+
this.localStorage.getItem(storageKey)
300320
);
301-
const instanceState = InstanceState.fromLocalStorageEntry(instanceKey, this.localStorage.getItem(storageKey));
302321
if (instanceState) {
303322
this.activeInstances[instanceState.instanceKey] = instanceState;
304323
}
@@ -315,7 +334,7 @@ export class WebStorageMetadataChannel implements InstanceMetadataChannel {
315334
minMutationBatch = this.activeInstances[key].minMutationBatchId;
316335
} else {
317336
minMutationBatch = Math.min(
318-
this.activeInstances[key].minMutationBatchId,
337+
this.activeInstances[key].minMutationBatchId,
319338
minMutationBatch
320339
);
321340
}
@@ -328,7 +347,9 @@ export class WebStorageMetadataChannel implements InstanceMetadataChannel {
328347
let activeTargets = new SortedSet<TargetId>(primitiveComparator);
329348

330349
Object.keys(this.activeInstances).forEach(key => {
331-
activeTargets = activeTargets.unionWith(this.activeInstances[key].activeTargetIds);
350+
activeTargets = activeTargets.unionWith(
351+
this.activeInstances[key].activeTargetIds
352+
);
332353
});
333354

334355
return activeTargets;
@@ -356,29 +377,35 @@ export class WebStorageMetadataChannel implements InstanceMetadataChannel {
356377

357378
shutdown(): void {
358379
assert(
359-
this.started,
360-
'LocalStorageMetadataNotifier.shutdown() called when not started'
380+
this.started,
381+
'LocalStorageMetadataNotifier.shutdown() called when not started'
361382
);
362383
window.removeEventListener('storage', this.storageListener);
363384
this.localStorage.removeItem(this.storageKey);
364385
this.started = false;
365386
}
366387

367-
private handleStorageEvent( event:StorageEvent) : void{
388+
private handleStorageEvent(event: StorageEvent): void {
368389
if (!this.started) {
369390
return;
370391
}
371392
if (event.storageArea === this.localStorage) {
372393
// TODO(multitab): This assert will likely become invalid as we add garbage
373394
// collection.
374-
assert(event.key !== this.storageKey, "Received LocalStorage notification for local change.");
395+
assert(
396+
event.key !== this.storageKey,
397+
'Received LocalStorage notification for local change.'
398+
);
375399

376400
const instanceKey = fromLocalStorageKey(event.key, this.instanceKeyRe, 3);
377401
if (instanceKey) {
378402
if (event.newValue == null) {
379403
delete this.activeInstances[instanceKey];
380404
} else {
381-
const newInstance = InstanceState.fromLocalStorageEntry(instanceKey, event.newValue);
405+
const newInstance = InstanceState.fromLocalStorageEntry(
406+
instanceKey,
407+
event.newValue
408+
);
382409
if (newInstance) {
383410
this.activeInstances[newInstance.instanceKey] = newInstance;
384411
}
@@ -392,22 +419,22 @@ export class WebStorageMetadataChannel implements InstanceMetadataChannel {
392419
}
393420

394421
private persistState(): void {
395-
assert(
396-
this.started,
397-
'LocalStorageMetadataNotifier used before started.'
398-
);
422+
assert(this.started, 'LocalStorageMetadataNotifier used before started.');
399423
debug(LOG_TAG, 'Persisting state in LocalStorage');
400424
this.localInstanceState.refreshLastUpdateTime();
401-
this.localStorage.setItem(this.storageKey, this.localInstanceState.toLocalStorageJSON());
425+
this.localStorage.setItem(
426+
this.storageKey,
427+
this.localInstanceState.toLocalStorageJSON()
428+
);
402429
}
403430
}
404431

405432
/** Assembles a key for LocalStorage */
406433
function toLocalStorageKey(...segments: string[]): string {
407434
segments.forEach(value => {
408435
assert(
409-
value.indexOf('_') === -1,
410-
`Key element cannot contain '_', but was '${value}'`
436+
value.indexOf('_') === -1,
437+
`Key element cannot contain '_', but was '${value}'`
411438
);
412439
});
413440

@@ -417,11 +444,15 @@ function toLocalStorageKey(...segments: string[]): string {
417444
/**
418445
* Parses the segments from a key in LocalStorage. Returns null if the key
419446
* doesn't match the provided regular expression. */
420-
function fromLocalStorageKey(key: string, expectedMatch: RegExp, dataSegement:number): string | null {
447+
function fromLocalStorageKey(
448+
key: string,
449+
expectedMatch: RegExp,
450+
dataSegement: number
451+
): string | null {
421452
if (!expectedMatch.test(key)) {
422453
console.log('key didnt match ' + key);
423454
return null;
424455
}
425456

426-
return key.split("_")[dataSegement];
427-
}
457+
return key.split('_')[dataSegement];
458+
}

0 commit comments

Comments
 (0)