| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292 |
15x
15x
15x
15x
15x
15x
6446x
15x
3411x
3411x
6835x
3411x
3411x
15x
6539x
15x
16136x
15x
112x
15x
13x
13x
15x
2845x
2790x
55x
40x
15x
15x
4x
15x
15x
85x
69x
16x
2x
14x
15x
446x
446x
365x
81x
3x
78x
78x
15x
4606x
15x
15x
15x
4314x
206x
4108x
15x
302x
282x
282x
78x
282x
282x
282x
215x
67x
282x
302x
15x
6607x
15x
3213x
113x
3100x
219x
2881x
2881x
15x
2881x
2881x
2881x
2881x
2881x
2881x
2881x
2788x
2788x
888x
1900x
872x
1028x
93x
15x
373x
15x
573x
15x
1478x
696x
782x
711x
711x
71x
15x
| /**
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { assert } from '@firebase/util';
import { doubleToIEEE754String, sha1 } from '../util/util';
import { priorityHashText, validatePriorityNode } from './snap';
import { Node } from './Node';
import { Path } from '../util/Path';
import { Index } from './indexes/Index';
import { ChildrenNodeConstructor } from './ChildrenNode';
let __childrenNodeConstructor: ChildrenNodeConstructor;
/**
* LeafNode is a class for storing leaf nodes in a DataSnapshot. It
* implements Node and stores the value of the node (a string,
* number, or boolean) accessible via getValue().
*/
export class LeafNode implements Node {
static set __childrenNodeConstructor(val: ChildrenNodeConstructor) {
__childrenNodeConstructor = val;
}
static get __childrenNodeConstructor() {
return __childrenNodeConstructor;
}
/**
* The sort order for comparing leaf nodes of different types. If two leaf nodes have
* the same type, the comparison falls back to their value
* @type {Array.<!string>}
* @const
*/
static VALUE_TYPE_ORDER = ['object', 'boolean', 'number', 'string'];
private lazyHash_: string | null = null;
/**
* @implements {Node}
* @param {!(string|number|boolean|Object)} value_ The value to store in this leaf node.
* The object type is possible in the event of a deferred value
* @param {!Node=} priorityNode_ The priority of this node.
*/
constructor(
private readonly value_: string | number | boolean | object,
private priorityNode_: Node = LeafNode.__childrenNodeConstructor.EMPTY_NODE
) {
assert(
this.value_ !== undefined && this.value_ !== null,
"LeafNode shouldn't be created with null/undefined value."
);
validatePriorityNode(this.priorityNode_);
}
/** @inheritDoc */
isLeafNode(): boolean {
return true;
}
/** @inheritDoc */
getPriority(): Node {
return this.priorityNode_;
}
/** @inheritDoc */
updatePriority(newPriorityNode: Node): Node {
return new LeafNode(this.value_, newPriorityNode);
}
/** @inheritDoc */
getImmediateChild(childName: string): Node {
// Hack to treat priority as a regular child
Iif (childName === '.priority') {
return this.priorityNode_;
} else {
return LeafNode.__childrenNodeConstructor.EMPTY_NODE;
}
}
/** @inheritDoc */
getChild(path: Path): Node {
if (path.isEmpty()) {
return this;
} else if (path.getFront() === '.priority') {
return this.priorityNode_;
} else {
return LeafNode.__childrenNodeConstructor.EMPTY_NODE;
}
}
/**
* @inheritDoc
*/
hasChild(): boolean {
return false;
}
/** @inheritDoc */
getPredecessorChildName(childName: String, childNode: Node): null {
return null;
}
/** @inheritDoc */
updateImmediateChild(childName: string, newChildNode: Node): Node {
if (childName === '.priority') {
return this.updatePriority(newChildNode);
} else if (newChildNode.isEmpty() && childName !== '.priority') {
return this;
} else {
return LeafNode.__childrenNodeConstructor.EMPTY_NODE.updateImmediateChild(
childName,
newChildNode
).updatePriority(this.priorityNode_);
}
}
/** @inheritDoc */
updateChild(path: Path, newChildNode: Node): Node {
const front = path.getFront();
if (front === null) {
return newChildNode;
} else if (newChildNode.isEmpty() && front !== '.priority') {
return this;
} else {
assert(
front !== '.priority' || path.getLength() === 1,
'.priority must be the last token in a path'
);
return this.updateImmediateChild(
front,
LeafNode.__childrenNodeConstructor.EMPTY_NODE.updateChild(
path.popFront(),
newChildNode
)
);
}
}
/** @inheritDoc */
isEmpty(): boolean {
return false;
}
/** @inheritDoc */
numChildren(): number {
return 0;
}
/** @inheritDoc */
forEachChild(index: Index, action: (s: string, n: Node) => void): any {
return false;
}
/**
* @inheritDoc
*/
val(exportFormat?: boolean): Object {
if (exportFormat && !this.getPriority().isEmpty())
return {
'.value': this.getValue(),
'.priority': this.getPriority().val()
};
else return this.getValue();
}
/** @inheritDoc */
hash(): string {
if (this.lazyHash_ === null) {
let toHash = '';
if (!this.priorityNode_.isEmpty())
toHash +=
'priority:' +
priorityHashText(this.priorityNode_.val() as number | string) +
':';
const type = typeof this.value_;
toHash += type + ':';
if (type === 'number') {
toHash += doubleToIEEE754String(this.value_ as number);
} else {
toHash += this.value_;
}
this.lazyHash_ = sha1(toHash);
}
return this.lazyHash_;
}
/**
* Returns the value of the leaf node.
* @return {Object|string|number|boolean} The value of the node.
*/
getValue(): object | string | number | boolean {
return this.value_;
}
/**
* @inheritDoc
*/
compareTo(other: Node): number {
if (other === LeafNode.__childrenNodeConstructor.EMPTY_NODE) {
return 1;
} else if (other instanceof LeafNode.__childrenNodeConstructor) {
return -1;
} else {
assert(other.isLeafNode(), 'Unknown node type');
return this.compareToLeafNode_(other as LeafNode);
}
}
/**
* Comparison specifically for two leaf nodes
* @param {!LeafNode} otherLeaf
* @return {!number}
* @private
*/
private compareToLeafNode_(otherLeaf: LeafNode): number {
const otherLeafType = typeof otherLeaf.value_;
const thisLeafType = typeof this.value_;
const otherIndex = LeafNode.VALUE_TYPE_ORDER.indexOf(otherLeafType);
const thisIndex = LeafNode.VALUE_TYPE_ORDER.indexOf(thisLeafType);
assert(otherIndex >= 0, 'Unknown leaf type: ' + otherLeafType);
assert(thisIndex >= 0, 'Unknown leaf type: ' + thisLeafType);
if (otherIndex === thisIndex) {
// Same type, compare values
Iif (thisLeafType === 'object') {
// Deferred value nodes are all equal, but we should also never get to this point...
return 0;
} else {
// Note that this works because true > false, all others are number or string comparisons
if (this.value_ < otherLeaf.value_) {
return -1;
} else if (this.value_ === otherLeaf.value_) {
return 0;
} else {
return 1;
}
}
} else {
return thisIndex - otherIndex;
}
}
/**
* @inheritDoc
*/
withIndex(): Node {
return this;
}
/**
* @inheritDoc
*/
isIndexed(): boolean {
return true;
}
/**
* @inheritDoc
*/
equals(other: Node): boolean {
/**
* @inheritDoc
*/
if (other === this) {
return true;
} else if (other.isLeafNode()) {
const otherLeaf = other as LeafNode;
return (
this.value_ === otherLeaf.value_ &&
this.priorityNode_.equals(otherLeaf.priorityNode_)
);
} else {
return false;
}
}
}
|