|
| 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 | +} |
0 commit comments