Skip to content

[Multi-Tab] Master Election #501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Mar 1, 2018
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions packages/firestore/src/core/firestore_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,12 @@ export class FirestoreClient {
const serializer = new JsonProtoSerializer(this.databaseInfo.databaseId, {
useProto3Json: true
});
this.persistence = new IndexedDbPersistence(storagePrefix, serializer);
this.persistence = new IndexedDbPersistence(
storagePrefix,
this.platform,
this.asyncQueue,
serializer
);
return this.persistence.start();
}

Expand All @@ -252,7 +257,7 @@ export class FirestoreClient {
*/
private startMemoryPersistence(): Promise<void> {
this.garbageCollector = new EagerGarbageCollector();
this.persistence = new MemoryPersistence();
this.persistence = new MemoryPersistence(this.asyncQueue);
return this.persistence.start();
}

Expand Down Expand Up @@ -309,6 +314,11 @@ export class FirestoreClient {
})
.then(() => {
return this.remoteStore.start();
})
.then(() => {
this.persistence.setPrimaryStateListener(isPrimary =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there's a non-obvious ordering dependency here, I think this warrants a comment:

// NOTE: This will immediately call the listener, so we make sure to set it after localStore / remoteStore are started.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doone.

this.syncEngine.applyPrimaryState(isPrimary)
);
});
}

Expand Down
11 changes: 11 additions & 0 deletions packages/firestore/src/core/sync_engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,19 @@ export class SyncEngine implements RemoteSyncer {
[uidKey: string]: SortedMap<BatchId, Deferred<void>>;
};
private targetIdGenerator = TargetIdGenerator.forSyncEngine();
private isPrimary = false;

constructor(
private localStore: LocalStore,
private remoteStore: RemoteStore,
private currentUser: User
) {}

// Only used for testing.
get isPrimaryClient() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently this is only used for testing, and I suspect this should remain the case. Add a comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I suspect you are right. Comment added.

return this.isPrimary;
}

/** Subscribes view and error handler. Can be called only once. */
subscribe(viewHandler: ViewHandler, errorHandler: ErrorHandler): void {
assert(
Expand Down Expand Up @@ -616,4 +622,9 @@ export class SyncEngine implements RemoteSyncer {
return this.remoteStore.handleUserChange(user);
});
}

applyPrimaryState(isPrimary: boolean): Promise<void> {
this.isPrimary = isPrimary;
return Promise.resolve();
}
}
Loading