Skip to content

Commit 1128453

Browse files
Untangle Datastore
1 parent f23120e commit 1128453

File tree

8 files changed

+178
-214
lines changed

8 files changed

+178
-214
lines changed

packages/firestore/src/core/component_provider.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import { EventManager } from './event_manager';
2828
import { AsyncQueue } from '../util/async_queue';
2929
import { DatabaseInfo } from './database_info';
3030
import { Platform } from '../platform/platform';
31-
import { Datastore } from '../remote/datastore';
3231
import { User } from '../auth/user';
3332
import { PersistenceSettings } from './firestore_client';
3433
import { debugAssert } from '../util/assert';
@@ -42,6 +41,9 @@ import {
4241
MemoryEagerDelegate,
4342
MemoryPersistence
4443
} from '../local/memory_persistence';
44+
import { Connection } from '../remote/connection';
45+
import { CredentialsProvider } from '../api/credentials';
46+
import { JsonProtoSerializer } from '../remote/serializer';
4547

4648
const MEMORY_ONLY_PERSISTENCE_ERROR_MESSAGE =
4749
'You are using the memory-only build of Firestore. Persistence support is ' +
@@ -52,7 +54,9 @@ export interface ComponentConfiguration {
5254
asyncQueue: AsyncQueue;
5355
databaseInfo: DatabaseInfo;
5456
platform: Platform;
55-
datastore: Datastore;
57+
connection: Connection;
58+
credentials: CredentialsProvider;
59+
serializer: JsonProtoSerializer;
5660
clientId: ClientId;
5761
initialUser: User;
5862
maxConcurrentLimboResolutions: number;
@@ -143,7 +147,9 @@ export class MemoryComponentProvider implements ComponentProvider {
143147
createRemoteStore(cfg: ComponentConfiguration): RemoteStore {
144148
return new RemoteStore(
145149
this.localStore,
146-
cfg.datastore,
150+
cfg.connection,
151+
cfg.credentials,
152+
cfg.serializer,
147153
cfg.asyncQueue,
148154
onlineState =>
149155
this.syncEngine.applyOnlineStateChange(

packages/firestore/src/core/firestore_client.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import { Document, MaybeDocument, NoDocument } from '../model/document';
2323
import { DocumentKey } from '../model/document_key';
2424
import { Mutation } from '../model/mutation';
2525
import { Platform } from '../platform/platform';
26-
import { Datastore } from '../remote/datastore';
2726
import { RemoteStore } from '../remote/remote_store';
2827
import { AsyncQueue } from '../util/async_queue';
2928
import { Code, FirestoreError } from '../util/error';
@@ -238,18 +237,14 @@ export class FirestoreClient {
238237
const serializer = this.platform.newSerializer(
239238
this.databaseInfo.databaseId
240239
);
241-
const datastore = new Datastore(
242-
this.asyncQueue,
243-
connection,
244-
this.credentials,
245-
serializer
246-
);
247240

248241
await componentProvider.initialize({
249242
asyncQueue: this.asyncQueue,
250243
databaseInfo: this.databaseInfo,
251244
platform: this.platform,
252-
datastore,
245+
connection,
246+
credentials: this.credentials,
247+
serializer,
253248
clientId: this.clientId,
254249
initialUser: user,
255250
maxConcurrentLimboResolutions: MAX_CONCURRENT_LIMBO_RESOLUTIONS,

packages/firestore/src/remote/datastore.ts

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,13 @@
1616
*/
1717

1818
import { CredentialsProvider } from '../api/credentials';
19-
import { maybeDocumentMap } from '../model/collections';
2019
import { MaybeDocument } from '../model/document';
2120
import { DocumentKey } from '../model/document_key';
2221
import { Mutation, MutationResult } from '../model/mutation';
2322
import * as api from '../protos/firestore_proto_api';
2423
import { hardAssert } from '../util/assert';
25-
import { AsyncQueue } from '../util/async_queue';
2624
import { Code, FirestoreError } from '../util/error';
2725
import { Connection } from './connection';
28-
import {
29-
WatchStreamListener,
30-
WriteStreamListener,
31-
PersistentListenStream,
32-
PersistentWriteStream
33-
} from './persistent_stream';
3426

3527
import { JsonProtoSerializer } from './serializer';
3628

@@ -51,36 +43,11 @@ interface CommitRequest extends api.CommitRequest {
5143
*/
5244
export class Datastore {
5345
constructor(
54-
private queue: AsyncQueue,
5546
private connection: Connection,
5647
private credentials: CredentialsProvider,
5748
private serializer: JsonProtoSerializer
5849
) {}
5950

60-
newPersistentWriteStream(
61-
listener: WriteStreamListener
62-
): PersistentWriteStream {
63-
return new PersistentWriteStream(
64-
this.queue,
65-
this.connection,
66-
this.credentials,
67-
this.serializer,
68-
listener
69-
);
70-
}
71-
72-
newPersistentWatchStream(
73-
listener: WatchStreamListener
74-
): PersistentListenStream {
75-
return new PersistentListenStream(
76-
this.queue,
77-
this.connection,
78-
this.credentials,
79-
this.serializer,
80-
listener
81-
);
82-
}
83-
8451
commit(mutations: Mutation[]): Promise<MutationResult[]> {
8552
const params: CommitRequest = {
8653
database: this.serializer.encodedDatabaseId,
@@ -106,14 +73,14 @@ export class Datastore {
10673
BatchGetDocumentsRequest,
10774
api.BatchGetDocumentsResponse
10875
>('BatchGetDocuments', params).then(response => {
109-
let docs = maybeDocumentMap();
76+
const docs = new Map<string, MaybeDocument>();
11077
response.forEach(proto => {
11178
const doc = this.serializer.fromMaybeDocument(proto);
112-
docs = docs.insert(doc.key, doc);
79+
docs.set(doc.key.toString(), doc);
11380
});
11481
const result: MaybeDocument[] = [];
11582
keys.forEach(key => {
116-
const doc = docs.get(key);
83+
const doc = docs.get(key.toString());
11784
hardAssert(!!doc, 'Missing entity in write response for ' + key);
11885
result.push(doc);
11986
});

packages/firestore/src/remote/remote_store.ts

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ import {
5050
WatchTargetChangeState
5151
} from './watch_change';
5252
import { ByteString } from '../util/byte_string';
53+
import { Connection } from './connection';
54+
import { CredentialsProvider } from '../api/credentials';
55+
import { JsonProtoSerializer } from './serializer';
5356

5457
const LOG_TAG = 'RemoteStore';
5558

@@ -106,6 +109,7 @@ export class RemoteStore implements TargetMetadataProvider {
106109
*/
107110
private listenTargets = new Map<TargetId, TargetData>();
108111

112+
private datastore: Datastore;
109113
private connectivityMonitor: ConnectivityMonitor;
110114
private watchStream: PersistentListenStream;
111115
private writeStream: PersistentWriteStream;
@@ -122,12 +126,10 @@ export class RemoteStore implements TargetMetadataProvider {
122126
private onlineStateTracker: OnlineStateTracker;
123127

124128
constructor(
125-
/**
126-
* The local store, used to fill the write pipeline with outbound mutations.
127-
*/
128129
private localStore: LocalStore,
129-
/** The client-side proxy for interacting with the backend. */
130-
private datastore: Datastore,
130+
connection: Connection,
131+
credentials: CredentialsProvider,
132+
serializer: JsonProtoSerializer,
131133
asyncQueue: AsyncQueue,
132134
onlineStateHandler: (onlineState: OnlineState) => void,
133135
connectivityMonitor: ConnectivityMonitor
@@ -150,19 +152,33 @@ export class RemoteStore implements TargetMetadataProvider {
150152
onlineStateHandler
151153
);
152154

155+
this.datastore = new Datastore(connection, credentials, serializer);
156+
153157
// Create streams (but note they're not started yet).
154-
this.watchStream = this.datastore.newPersistentWatchStream({
155-
onOpen: this.onWatchStreamOpen.bind(this),
156-
onClose: this.onWatchStreamClose.bind(this),
157-
onWatchChange: this.onWatchStreamChange.bind(this)
158-
});
158+
this.watchStream = new PersistentListenStream(
159+
asyncQueue,
160+
connection,
161+
credentials,
162+
serializer,
163+
{
164+
onOpen: this.onWatchStreamOpen.bind(this),
165+
onClose: this.onWatchStreamClose.bind(this),
166+
onWatchChange: this.onWatchStreamChange.bind(this)
167+
}
168+
);
159169

160-
this.writeStream = this.datastore.newPersistentWriteStream({
161-
onOpen: this.onWriteStreamOpen.bind(this),
162-
onClose: this.onWriteStreamClose.bind(this),
163-
onHandshakeComplete: this.onWriteHandshakeComplete.bind(this),
164-
onMutationResult: this.onMutationResult.bind(this)
165-
});
170+
this.writeStream = new PersistentWriteStream(
171+
asyncQueue,
172+
connection,
173+
credentials,
174+
serializer,
175+
{
176+
onOpen: this.onWriteStreamOpen.bind(this),
177+
onClose: this.onWriteStreamClose.bind(this),
178+
onHandshakeComplete: this.onWriteHandshakeComplete.bind(this),
179+
onMutationResult: this.onMutationResult.bind(this)
180+
}
181+
);
166182
}
167183

168184
/**

0 commit comments

Comments
 (0)