Skip to content

Commit 1738c15

Browse files
committed
watch integration works.
1 parent ee64690 commit 1738c15

File tree

13 files changed

+138
-61
lines changed

13 files changed

+138
-61
lines changed

packages/firestore/src/api/pipeline.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
import { Pipeline as LitePipeline } from '../lite-api/pipeline';
66
import { PipelineResult } from '../lite-api/pipeline-result';
77
import { DocumentData, DocumentReference } from '../lite-api/reference';
8-
import {AddFields, Sort, Stage, Where} from '../lite-api/stage';
8+
import { AddFields, Sort, Stage, Where } from '../lite-api/stage';
99
import { UserDataReader } from '../lite-api/user_data_reader';
1010
import { AbstractUserDataWriter } from '../lite-api/user_data_writer';
1111
import { DocumentKey } from '../model/document_key';
@@ -15,8 +15,8 @@ import { DocumentSnapshot, PipelineSnapshot } from './snapshot';
1515
import { FirestoreError } from '../util/error';
1616
import { Unsubscribe } from './reference_impl';
1717
import { cast } from '../util/input_validation';
18-
import {Field, FilterCondition} from '../api';
19-
import {Expr} from '../lite-api/expressions';
18+
import { Field, FilterCondition } from '../api';
19+
import { Expr } from '../lite-api/expressions';
2020

2121
export class Pipeline<
2222
AppModelType = DocumentData
@@ -32,7 +32,7 @@ export class Pipeline<
3232
* @param converter
3333
*/
3434
constructor(
35-
private db: Firestore,
35+
readonly db: Firestore,
3636
userDataReader: UserDataReader,
3737
userDataWriter: AbstractUserDataWriter,
3838
documentReferenceFactory: (id: DocumentKey) => DocumentReference,
@@ -137,15 +137,10 @@ export class Pipeline<
137137
// )
138138
// );
139139

140-
this.stages.push(
141-
new Sort([
142-
Field.of('__name__').ascending()
143-
]
144-
)
145-
);
140+
this.stages.push(new Sort([Field.of('__name__').ascending()]));
146141

147142
const client = ensureFirestoreConfigured(this.db);
148-
firestoreClientListenPipeline(client, this, {next, error, complete});
143+
firestoreClientListenPipeline(client, this, { next, error, complete });
149144

150145
return () => {};
151146
}

packages/firestore/src/api/pipeline_source.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ import {
2323
DatabaseSource,
2424
DocumentsSource
2525
} from '../lite-api/stage';
26-
import {PipelineSource as LitePipelineSource} from '../lite-api/pipeline-source';
26+
import { PipelineSource as LitePipelineSource } from '../lite-api/pipeline-source';
2727
import { UserDataReader } from '../lite-api/user_data_reader';
2828
import { AbstractUserDataWriter } from '../lite-api/user_data_writer';
2929

3030
/**
3131
* Represents the source of a Firestore {@link Pipeline}.
3232
* @beta
3333
*/
34-
export class PipelineSource extends LitePipelineSource{
34+
export class PipelineSource extends LitePipelineSource {
3535
/**
3636
* @internal
3737
* @private

packages/firestore/src/api/snapshot.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -794,12 +794,6 @@ export function snapshotEqual<AppModelType, DbModelType extends DocumentData>(
794794
}
795795

796796
export class PipelineSnapshot<AppModelType = DocumentData> {
797-
/**
798-
* Metadata about this snapshot, concerning its source and if it has local
799-
* modifications.
800-
*/
801-
readonly metadata: SnapshotMetadata;
802-
803797
/**
804798
* The query on which you called `get` or `onSnapshot` in order to get this
805799
* `QuerySnapshot`.
@@ -808,21 +802,14 @@ export class PipelineSnapshot<AppModelType = DocumentData> {
808802

809803
/** @hideconstructor */
810804
constructor(
811-
readonly _firestore: Firestore,
812-
readonly _userDataWriter: AbstractUserDataWriter,
813805
pipeline: Pipeline<AppModelType>,
814-
readonly _snapshot: ViewSnapshot
806+
readonly _snapshot: PipelineResult<AppModelType>[]
815807
) {
816-
this.metadata = new SnapshotMetadata(
817-
_snapshot.hasPendingWrites,
818-
_snapshot.fromCache
819-
);
820808
this.pipeline = pipeline;
821809
}
822810

823811
/** An array of all the documents in the `QuerySnapshot`. */
824812
get results(): Array<PipelineResult<AppModelType>> {
825-
const result: Array<PipelineResult<AppModelType>> = [];
826-
return result;
813+
return this._snapshot;
827814
}
828815
}

packages/firestore/src/core/event_manager.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -602,15 +602,16 @@ export class QueryListener {
602602
}
603603

604604
export class PipelineListener {
605-
private snap: PipelineResultView | null = null;
605+
private view: PipelineResultView | null = null;
606606

607607
constructor(
608608
readonly pipeline: Pipeline,
609609
private queryObserver: Observer<PipelineSnapshot>
610610
) {}
611611

612-
onViewSnapshot(snap: PipelineResultView): boolean {
613-
this.snap = snap;
612+
onViewSnapshot(view: PipelineResultView): boolean {
613+
this.view = view;
614+
this.queryObserver.next(view.toPipelineSnapshot());
614615
return true;
615616
}
616617

packages/firestore/src/core/sync_engine_impl.ts

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ import { ViewSnapshot } from './view_snapshot';
121121
import { Pipeline } from '../api/pipeline';
122122
import { PipelineSnapshot } from '../api/snapshot';
123123
import { PipelineResult } from '../lite-api/pipeline-result';
124+
import { doc } from '../lite-api/reference';
124125

125126
const LOG_TAG = 'SyncEngine';
126127

@@ -150,9 +151,12 @@ class QueryView {
150151
}
151152

152153
export class PipelineResultView {
153-
private keyToIndexMap: Map<DocumentKey, number>;
154+
private keyToIndexMap: ObjectMap<DocumentKey, number>;
154155
constructor(public pipeline: Pipeline, public view: Array<MutableDocument>) {
155-
this.keyToIndexMap = new Map<DocumentKey, number>();
156+
this.keyToIndexMap = new ObjectMap<DocumentKey, number>(
157+
key => key.toString(),
158+
(a, b) => a.isEqual(b)
159+
);
156160
this.buildKeyToIndexMap();
157161
}
158162

@@ -197,6 +201,23 @@ export class PipelineResultView {
197201
}
198202
this.view[index] = doc;
199203
}
204+
205+
toPipelineSnapshot(): PipelineSnapshot {
206+
return new PipelineSnapshot(
207+
this.pipeline,
208+
this.view.map(
209+
d =>
210+
new PipelineResult(
211+
this.pipeline.userDataWriter,
212+
doc(this.pipeline.db, d.key.toString()),
213+
d.data,
214+
d.readTime.toTimestamp(),
215+
d.createTime.toTimestamp(),
216+
d.version.toTimestamp()
217+
)
218+
)
219+
);
220+
}
200221
}
201222

202223
/** Tracks a limbo resolution. */
@@ -1000,7 +1021,10 @@ function removeAndCleanupTarget(
10001021
syncEngineImpl.sharedClientState.removeLocalQueryTarget(targetId);
10011022

10021023
// TODO(pipeline): REMOVE this hack.
1003-
if(!syncEngineImpl.queriesByTarget.has(targetId)||syncEngineImpl.queriesByTarget.get(targetId)!.length !== 0){
1024+
if (
1025+
!syncEngineImpl.queriesByTarget.has(targetId) ||
1026+
syncEngineImpl.queriesByTarget.get(targetId)!.length !== 0
1027+
) {
10041028
return;
10051029
}
10061030

@@ -1174,7 +1198,10 @@ export async function syncEngineEmitNewSnapsAndNotifyLocalStore(
11741198
const change = remoteEvent?.targetChanges.get(targetId);
11751199
if (!!change) {
11761200
change.modifiedDocuments.forEach(key => {
1177-
results.updateResult(key, remoteEvent?.augmentedDocumentUpdates.get(key)!);
1201+
results.updateResult(
1202+
key,
1203+
remoteEvent?.augmentedDocumentUpdates.get(key)!
1204+
);
11781205
});
11791206
change.addedDocuments.forEach(key => {
11801207
results.addResult(key, remoteEvent?.augmentedDocumentUpdates.get(key)!);
@@ -1319,17 +1346,21 @@ export function syncEngineGetRemoteKeysForTarget(
13191346
} else {
13201347
let keySet = documentKeySet();
13211348
const queries = syncEngineImpl.queriesByTarget.get(targetId);
1322-
if (!queries) {
1349+
const pipelineView = syncEngineImpl.pipelineViewByTarget.get(targetId);
1350+
if (!queries && !pipelineView) {
13231351
return keySet;
13241352
}
1325-
for (const query of queries) {
1353+
for (const query of queries ?? []) {
13261354
const queryView = syncEngineImpl.queryViewsByQuery.get(query);
13271355
debugAssert(
13281356
!!queryView,
13291357
`No query view found for ${stringifyQuery(query)}`
13301358
);
13311359
keySet = keySet.unionWith(queryView.view.syncedDocuments);
13321360
}
1361+
for (const doc of pipelineView?.view ?? []) {
1362+
keySet = keySet.add(doc.key);
1363+
}
13331364
return keySet;
13341365
}
13351366
}

packages/firestore/src/lite-api/expressions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2336,7 +2336,7 @@ export class Mod extends FirestoreFunction {
23362336
*/
23372337
export class Eq extends FirestoreFunction implements FilterCondition {
23382338
constructor(private left: Expr, private right: Expr) {
2339-
super('eq', [left, right]);
2339+
super('equals', [left, right]);
23402340
}
23412341
filterable = true as const;
23422342
}

packages/firestore/src/lite-api/pipeline.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ import {
2424
import { invokeExecutePipeline } from '../remote/datastore';
2525
import {
2626
getEncodedDatabaseId,
27-
JsonProtoSerializer, ProtoSerializable
27+
JsonProtoSerializer,
28+
ProtoSerializable
2829
} from '../remote/serializer';
2930

3031
import { getDatastore } from './components';
@@ -117,7 +118,9 @@ function isReadableUserData(value: any): value is ReadableUserData {
117118
/**
118119
* Base-class implementation
119120
*/
120-
export class Pipeline<AppModelType = DocumentData> implements ProtoSerializable<ExecutePipelineRequest>{
121+
export class Pipeline<AppModelType = DocumentData>
122+
implements ProtoSerializable<ExecutePipelineRequest>
123+
{
121124
/**
122125
* @internal
123126
* @private
@@ -135,7 +138,7 @@ export class Pipeline<AppModelType = DocumentData> implements ProtoSerializable<
135138
* @internal
136139
* @private
137140
*/
138-
protected userDataWriter: AbstractUserDataWriter,
141+
readonly userDataWriter: AbstractUserDataWriter,
139142
/**
140143
* @internal
141144
* @private

packages/firestore/src/lite-api/stage.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import {
1919
} from '../protos/firestore_proto_api';
2020
import { toNumber } from '../remote/number_serializer';
2121
import {
22-
JsonProtoSerializer, ProtoSerializable,
22+
JsonProtoSerializer,
23+
ProtoSerializable,
2324
toMapValue,
2425
toStringValue
2526
} from '../remote/serializer';
@@ -37,7 +38,7 @@ import { VectorValue } from './vector_value';
3738
/**
3839
* @beta
3940
*/
40-
export interface Stage extends ProtoSerializable<ProtoStage>{
41+
export interface Stage extends ProtoSerializable<ProtoStage> {
4142
name: string;
4243
}
4344

packages/firestore/src/local/local_store_impl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,7 @@ export async function localStoreReleaseTarget(
10531053

10541054
// TODO(pipeline): this is a hack that only works because pipelines are the only ones returning nulls here.
10551055
// REMOVE ASAP.
1056-
if(targetData === null) {
1056+
if (targetData === null) {
10571057
return;
10581058
}
10591059

packages/firestore/src/protos/protos.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2363,7 +2363,7 @@
23632363
"type": "DocumentsTarget",
23642364
"id": 3
23652365
},
2366-
"pipeline_query": {
2366+
"pipelineQuery": {
23672367
"type": "PipelineQueryTarget",
23682368
"id": 13
23692369
},

0 commit comments

Comments
 (0)