Skip to content

Commit c52451f

Browse files
Removing the last bit of dependency injection (#317)
1 parent 0e4d3c6 commit c52451f

File tree

9 files changed

+202
-314
lines changed

9 files changed

+202
-314
lines changed

src/document-change.js

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*!
2+
* Copyright 2018 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
import * as is from 'is';
20+
21+
/**
22+
* A DocumentChange represents a change to the documents matching a query.
23+
* It contains the document affected and the type of change that occurred.
24+
*
25+
* @class
26+
*/
27+
export class DocumentChange {
28+
/**
29+
* @private
30+
* @hideconstructor
31+
*
32+
* @param {string} type - 'added' | 'removed' | 'modified'.
33+
* @param {QueryDocumentSnapshot} document - The document.
34+
* @param {number} oldIndex - The index in the documents array prior to this
35+
* change.
36+
* @param {number} newIndex - The index in the documents array after this
37+
* change.
38+
*/
39+
constructor(type, document, oldIndex, newIndex) {
40+
this._type = type;
41+
this._document = document;
42+
this._oldIndex = oldIndex;
43+
this._newIndex = newIndex;
44+
}
45+
46+
/**
47+
* The type of change ('added', 'modified', or 'removed').
48+
*
49+
* @type {string}
50+
* @name DocumentChange#type
51+
* @readonly
52+
*
53+
* @example
54+
* let query = firestore.collection('col').where('foo', '==', 'bar');
55+
* let docsArray = [];
56+
*
57+
* let unsubscribe = query.onSnapshot(querySnapshot => {
58+
* for (let change of querySnapshot.docChanges) {
59+
* console.log(`Type of change is ${change.type}`);
60+
* }
61+
* });
62+
*
63+
* // Remove this listener.
64+
* unsubscribe();
65+
*/
66+
get type() {
67+
return this._type;
68+
}
69+
70+
/**
71+
* The document affected by this change.
72+
*
73+
* @type {QueryDocumentSnapshot}
74+
* @name DocumentChange#doc
75+
* @readonly
76+
*
77+
* @example
78+
* let query = firestore.collection('col').where('foo', '==', 'bar');
79+
*
80+
* let unsubscribe = query.onSnapshot(querySnapshot => {
81+
* for (let change of querySnapshot.docChanges) {
82+
* console.log(change.doc.data());
83+
* }
84+
* });
85+
*
86+
* // Remove this listener.
87+
* unsubscribe();
88+
*/
89+
get doc() {
90+
return this._document;
91+
}
92+
93+
/**
94+
* The index of the changed document in the result set immediately prior to
95+
* this DocumentChange (i.e. supposing that all prior DocumentChange objects
96+
* have been applied). Is -1 for 'added' events.
97+
*
98+
* @type {number}
99+
* @name DocumentChange#oldIndex
100+
* @readonly
101+
*
102+
* @example
103+
* let query = firestore.collection('col').where('foo', '==', 'bar');
104+
* let docsArray = [];
105+
*
106+
* let unsubscribe = query.onSnapshot(querySnapshot => {
107+
* for (let change of querySnapshot.docChanges) {
108+
* if (change.oldIndex !== -1) {
109+
* docsArray.splice(change.oldIndex, 1);
110+
* }
111+
* if (change.newIndex !== -1) {
112+
* docsArray.splice(change.newIndex, 0, change.doc);
113+
* }
114+
* }
115+
* });
116+
*
117+
* // Remove this listener.
118+
* unsubscribe();
119+
*/
120+
get oldIndex() {
121+
return this._oldIndex;
122+
}
123+
124+
/**
125+
* The index of the changed document in the result set immediately after
126+
* this DocumentChange (i.e. supposing that all prior DocumentChange
127+
* objects and the current DocumentChange object have been applied).
128+
* Is -1 for 'removed' events.
129+
*
130+
* @type {number}
131+
* @name DocumentChange#newIndex
132+
* @readonly
133+
*
134+
* @example
135+
* let query = firestore.collection('col').where('foo', '==', 'bar');
136+
* let docsArray = [];
137+
*
138+
* let unsubscribe = query.onSnapshot(querySnapshot => {
139+
* for (let change of querySnapshot.docChanges) {
140+
* if (change.oldIndex !== -1) {
141+
* docsArray.splice(change.oldIndex, 1);
142+
* }
143+
* if (change.newIndex !== -1) {
144+
* docsArray.splice(change.newIndex, 0, change.doc);
145+
* }
146+
* }
147+
* });
148+
*
149+
* // Remove this listener.
150+
* unsubscribe();
151+
*/
152+
get newIndex() {
153+
return this._newIndex;
154+
}
155+
156+
/**
157+
* Returns true if the data in this `DocumentChange` is equal to the provided
158+
* value.
159+
*
160+
* @param {*} other The value to compare against.
161+
* @return true if this `DocumentChange` is equal to the provided value.
162+
*/
163+
isEqual(other) {
164+
if (this === other) {
165+
return true;
166+
}
167+
168+
return (
169+
is.instanceof(other, DocumentChange) && this._type === other._type &&
170+
this._oldIndex === other._oldIndex &&
171+
this._newIndex === other._newIndex &&
172+
this._document.isEqual(other._document));
173+
}
174+
}

src/index.js

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@ import pkgUp from 'pkg-up';
2424
import through2 from 'through2';
2525
import {replaceProjectIdToken} from '@google-cloud/projectify';
2626

27-
import {referencePkg} from './reference';
28-
import {DocumentSnapshot, QueryDocumentSnapshot, validateDocumentData, validateFieldValue, validatePrecondition, validateSetOptions} from './document';
27+
import {CollectionReference, CollectionReference, DocumentReference, QuerySnapshot, Query, validateDocumentReference, validateComparisonOperator, validateFieldOrder} from './reference';
28+
import {DocumentSnapshot, QueryDocumentSnapshot, validatePrecondition, validateSetOptions} from './document';
2929
import {FieldTransform, FieldValue, DeleteTransform} from './field-value';
3030
import {Validator, customObjectError} from './validate';
3131
import {WriteBatch, WriteResult, validateUpdateMap} from './write-batch';
32-
import {transactionPkg} from './transaction';
32+
import {Transaction} from './transaction';
3333
import {Timestamp} from './timestamp';
3434
import {FieldPath, ResourcePath} from './path';
3535
import {ClientPool} from './pool';
36+
import {DocumentChange} from './document-change';
3637
import {isPlainObject, Serializer} from './serializer';
3738
import {GeoPoint} from './geo-point';
3839
import {logger, setLibVersion, setLogFunction} from './logger';
@@ -59,31 +60,11 @@ setLibVersion(libVersion);
5960
* @namespace google.firestore.v1beta1
6061
*/
6162

62-
/*!
63-
* @see CollectionReference
64-
*/
65-
let CollectionReference;
66-
67-
/*!
68-
* @see DocumentReference
69-
*/
70-
let DocumentReference;
71-
72-
/*!
73-
* @see Transaction
74-
*/
75-
let Transaction;
76-
7763
/*!
7864
* @see v1beta1
7965
*/
8066
let v1beta1; // Lazy-loaded in `_runRequest()`
8167

82-
// Injected custom validation functions.
83-
let validateDocumentReference;
84-
let validateComparisonOperator;
85-
let validateFieldOrder;
86-
8768
/*!
8869
* HTTP header for the resource prefix to improve routing and project isolation
8970
* by the backend.
@@ -1298,15 +1279,6 @@ function validateDocumentData(obj, options) {
12981279
*/
12991280
Firestore.setLogFunction = setLogFunction;
13001281

1301-
// Initializing dependencies that require that Firestore class type.
1302-
const reference = referencePkg(Firestore);
1303-
CollectionReference = reference.CollectionReference;
1304-
DocumentReference = reference.DocumentReference;
1305-
validateDocumentReference = reference.validateDocumentReference;
1306-
validateComparisonOperator = reference.validateComparisonOperator;
1307-
validateFieldOrder = reference.validateFieldOrder;
1308-
Transaction = transactionPkg(Firestore);
1309-
13101282
/**
13111283
* The default export of the `@google-cloud/firestore` package is the
13121284
* {@link Firestore} class.
@@ -1439,7 +1411,7 @@ module.exports.CollectionReference = CollectionReference;
14391411
* @see QuerySnapshot
14401412
* @type QuerySnapshot
14411413
*/
1442-
module.exports.QuerySnapshot = reference.QuerySnapshot;
1414+
module.exports.QuerySnapshot = QuerySnapshot;
14431415

14441416
/**
14451417
* {@link DocumentChange} class.
@@ -1448,7 +1420,7 @@ module.exports.QuerySnapshot = reference.QuerySnapshot;
14481420
* @see DocumentChange
14491421
* @type DocumentChange
14501422
*/
1451-
module.exports.DocumentChange = reference.DocumentChange;
1423+
module.exports.DocumentChange = DocumentChange;
14521424

14531425
/**
14541426
* {@link Query} class.
@@ -1457,7 +1429,7 @@ module.exports.DocumentChange = reference.DocumentChange;
14571429
* @see Query
14581430
* @type Query
14591431
*/
1460-
module.exports.Query = reference.Query;
1432+
module.exports.Query = Query;
14611433

14621434
/**
14631435
* {@link FieldValue} class.

0 commit comments

Comments
 (0)