Skip to content

Commit 280374e

Browse files
Adding basic functionality for TabNotificationChannel
1 parent f724535 commit 280374e

14 files changed

+462
-449
lines changed

packages/firestore/src/core/firestore_client.ts

+3-35
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,6 @@ import { Query } from './query';
4545
import { Transaction } from './transaction';
4646
import { OnlineState } from './types';
4747
import { ViewSnapshot } from './view_snapshot';
48-
import {
49-
LocalStorageNotificationChannel,
50-
TabNotificationChannel
51-
} from '../local/tab_notification_channel';
52-
import { AutoId } from '../util/misc';
53-
import { WindowEventListener } from '../platform_browser/window_event_listener';
5448

5549
const LOG_TAG = 'FirestoreClient';
5650

@@ -71,9 +65,8 @@ export class FirestoreClient {
7165
private persistence: Persistence;
7266
private localStore: LocalStore;
7367
private remoteStore: RemoteStore;
74-
private notificationChannel?: TabNotificationChannel;
75-
private windowEventListener?: WindowEventListener;
7668
private syncEngine: SyncEngine;
69+
7770
constructor(
7871
private platform: Platform,
7972
private databaseInfo: DatabaseInfo,
@@ -238,8 +231,6 @@ export class FirestoreClient {
238231
* @returns A promise indicating success or failure.
239232
*/
240233
private startIndexedDbPersistence(): Promise<void> {
241-
const ownerId = AutoId.newId();
242-
243234
// TODO(http://b/33384523): For now we just disable garbage collection
244235
// when persistence is enabled.
245236
this.garbageCollector = new NoOpGarbageCollector();
@@ -250,26 +241,8 @@ export class FirestoreClient {
250241
const serializer = new JsonProtoSerializer(this.databaseInfo.databaseId, {
251242
useProto3Json: true
252243
});
253-
254-
this.persistence = new IndexedDbPersistence(
255-
storagePrefix,
256-
ownerId,
257-
serializer
258-
);
259-
260-
return this.persistence.start().then(() => {
261-
this.notificationChannel = new LocalStorageNotificationChannel(
262-
storagePrefix,
263-
ownerId,
264-
this.asyncQueue
265-
);
266-
this.notificationChannel.start();
267-
this.windowEventListener = new WindowEventListener(
268-
this.asyncQueue,
269-
this.notificationChannel
270-
);
271-
this.windowEventListener.start();
272-
});
244+
this.persistence = new IndexedDbPersistence(storagePrefix, serializer);
245+
return this.persistence.start();
273246
}
274247

275248
/**
@@ -363,11 +336,6 @@ export class FirestoreClient {
363336
.then(() => {
364337
// PORTING NOTE: LocalStore does not need an explicit shutdown on web.
365338
return this.persistence.shutdown();
366-
})
367-
.then(() => {
368-
if (this.notificationChannel) {
369-
this.notificationChannel.shutdown();
370-
}
371339
});
372340
}
373341

packages/firestore/src/core/types.ts

+3-25
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ export type BatchId = number;
2626
*/
2727
export type TargetId = number;
2828

29+
/**
30+
* A randomly-generated key assigned to each Firestore instance at startup.
31+
*/
2932
export type InstanceKey = string;
3033

3134
// TODO(b/35918695): In GRPC / node, tokens are Uint8Array. In WebChannel,
@@ -62,28 +65,3 @@ export enum OnlineState {
6265
*/
6366
Failed
6467
}
65-
66-
/**
67-
* Describes the foreground state of the tab that is hosting the Firestore
68-
* client. These values are determined on a best-effort basis and may (e.g. for
69-
* throttled tabs) not always reflect the tab's current visibility.
70-
*/
71-
export enum VisibilityState {
72-
/**
73-
* It is not known what state the tab is in. This is usually the state used
74-
* during the initial rendering phase.
75-
*/
76-
Unknown,
77-
78-
/**
79-
* The tab has received a notification from the browser that it is at least
80-
* partially visible (in a foreground tab that is non-minimized).
81-
*/
82-
Foreground,
83-
84-
/**
85-
* The tab has received a notification from the browser that it is not visible
86-
* (in a background tab or minimized).
87-
*/
88-
Background
89-
}

packages/firestore/src/local/indexeddb_persistence.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export class IndexedDbPersistence implements Persistence {
9494
private started: boolean;
9595
private dbName: string;
9696
private localStoragePrefix: string;
97+
private ownerId: string = this.generateOwnerId();
9798

9899
/**
99100
* Set to an Error object if we encounter an unrecoverable error. All further
@@ -108,11 +109,7 @@ export class IndexedDbPersistence implements Persistence {
108109

109110
private serializer: LocalSerializer;
110111

111-
constructor(
112-
prefix: string,
113-
private ownerId: string,
114-
serializer: JsonProtoSerializer
115-
) {
112+
constructor(prefix: string, serializer: JsonProtoSerializer) {
116113
this.dbName = prefix + IndexedDbPersistence.MAIN_DATABASE;
117114
this.serializer = new LocalSerializer(serializer);
118115
this.localStoragePrefix = prefix;
@@ -408,4 +405,9 @@ export class IndexedDbPersistence implements Persistence {
408405
private zombiedOwnerLocalStorageKey(): string {
409406
return this.localStoragePrefix + ZOMBIE_OWNER_LOCALSTORAGE_SUFFIX;
410407
}
408+
409+
private generateOwnerId(): string {
410+
// For convenience, just use an AutoId.
411+
return AutoId.newId();
412+
}
411413
}

0 commit comments

Comments
 (0)