| 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383 |
12x
12x
12x
12x
12x
12x
12x
12x
12x
12x
12x
12x
12x
2870x
2870x
2870x
12x
831x
831x
830x
12x
1689x
1689x
1689x
1668x
822x
846x
1689x
12x
278x
278x
278x
12x
37x
37x
37x
37x
37x
12x
25x
12x
302x
302x
302x
302x
302x
302x
302x
12x
11x
11x
11x
11x
11x
11x
11x
11x
12x
72x
72x
72x
72x
72x
72x
72x
72x
72x
12x
17x
17x
17x
17x
12x
78x
78x
78x
78x
78x
78x
78x
78x
78x
60x
78x
78x
6x
72x
78x
60x
78x
78x
12x
26x
26x
26x
26x
26x
26x
26x
12x
342x
342x
342x
342x
342x
342x
342x
342x
342x
8x
334x
342x
342x
342x
1x
342x
12x
12x
25x
12x
253x
12x
1x
12x
37x
12x
12x
12x
| /**
* 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 { OnDisconnect } from './onDisconnect';
import { TransactionResult } from './TransactionResult';
import { warn } from '../core/util/util';
import { nextPushId } from '../core/util/NextPushId';
import { Query } from './Query';
import { Repo } from '../core/Repo';
import { Path } from '../core/util/Path';
import { QueryParams } from '../core/view/QueryParams';
import {
validateRootPathString,
validatePathString,
validateFirebaseMergeDataArg,
validateBoolean,
validatePriority,
validateFirebaseDataArg,
validateWritablePath
} from '../core/util/validation';
import { validateArgCount, validateCallback } from '@firebase/util';
import { Deferred } from '@firebase/util';
import { SyncPoint } from '../core/SyncPoint';
import { Database } from './Database';
import { DataSnapshot } from './DataSnapshot';
export interface ReferenceConstructor {
new (repo: Repo, path: Path): Reference;
}
export class Reference extends Query {
public then: (a?: any) => Promise<any>;
public catch: (a?: Error) => Promise<any>;
/**
* Call options:
* new Reference(Repo, Path) or
* new Reference(url: string, string|RepoManager)
*
* Externally - this is the firebase.database.Reference type.
*
* @param {!Repo} repo
* @param {(!Path)} path
* @extends {Query}
*/
constructor(repo: Repo, path: Path) {
Iif (!(repo instanceof Repo)) {
throw new Error(
'new Reference() no longer supported - use app.database().'
);
}
// call Query's constructor, passing in the repo and path.
super(repo, path, QueryParams.DEFAULT, false);
}
/** @return {?string} */
getKey(): string | null {
validateArgCount('Reference.key', 0, 0, arguments.length);
if (this.path.isEmpty()) return null;
else return this.path.getBack();
}
/**
* @param {!(string|Path)} pathString
* @return {!Reference}
*/
child(pathString: string | Path): Reference {
validateArgCount('Reference.child', 1, 1, arguments.length);
Iif (typeof pathString === 'number') {
pathString = String(pathString);
} else if (!(pathString instanceof Path)) {
if (this.path.getFront() === null)
validateRootPathString('Reference.child', 1, pathString, false);
else validatePathString('Reference.child', 1, pathString, false);
}
return new Reference(this.repo, this.path.child(pathString));
}
/** @return {?Reference} */
getParent(): Reference | null {
validateArgCount('Reference.parent', 0, 0, arguments.length);
const parentPath = this.path.parent();
return parentPath === null ? null : new Reference(this.repo, parentPath);
}
/** @return {!Reference} */
getRoot(): Reference {
validateArgCount('Reference.root', 0, 0, arguments.length);
let ref = this as any;
while (ref.getParent() !== null) {
ref = ref.getParent();
}
return ref;
}
/** @return {!Database} */
databaseProp(): Database {
return this.repo.database;
}
/**
* @param {*} newVal
* @param {function(?Error)=} onComplete
* @return {!Promise}
*/
set(newVal: any, onComplete?: (a: Error | null) => void): Promise<any> {
validateArgCount('Reference.set', 1, 2, arguments.length);
validateWritablePath('Reference.set', this.path);
validateFirebaseDataArg('Reference.set', 1, newVal, this.path, false);
validateCallback('Reference.set', 2, onComplete, true);
const deferred = new Deferred();
this.repo.setWithPriority(
this.path,
newVal,
/*priority=*/ null,
deferred.wrapCallback(onComplete)
);
return deferred.promise;
}
/**
* @param {!Object} objectToMerge
* @param {function(?Error)=} onComplete
* @return {!Promise}
*/
update(
objectToMerge: Object,
onComplete?: (a: Error | null) => void
): Promise<any> {
validateArgCount('Reference.update', 1, 2, arguments.length);
validateWritablePath('Reference.update', this.path);
Iif (Array.isArray(objectToMerge)) {
const newObjectToMerge: { [k: string]: any } = {};
for (let i = 0; i < objectToMerge.length; ++i) {
newObjectToMerge['' + i] = objectToMerge[i];
}
objectToMerge = newObjectToMerge;
warn(
'Passing an Array to Firebase.update() is deprecated. ' +
'Use set() if you want to overwrite the existing data, or ' +
'an Object with integer keys if you really do want to ' +
'only update some of the children.'
);
}
validateFirebaseMergeDataArg(
'Reference.update',
1,
objectToMerge,
this.path,
false
);
validateCallback('Reference.update', 2, onComplete, true);
const deferred = new Deferred();
this.repo.update(
this.path,
objectToMerge,
deferred.wrapCallback(onComplete)
);
return deferred.promise;
}
/**
* @param {*} newVal
* @param {string|number|null} newPriority
* @param {function(?Error)=} onComplete
* @return {!Promise}
*/
setWithPriority(
newVal: any,
newPriority: string | number | null,
onComplete?: (a: Error | null) => void
): Promise<any> {
validateArgCount('Reference.setWithPriority', 2, 3, arguments.length);
validateWritablePath('Reference.setWithPriority', this.path);
validateFirebaseDataArg(
'Reference.setWithPriority',
1,
newVal,
this.path,
false
);
validatePriority('Reference.setWithPriority', 2, newPriority, false);
validateCallback('Reference.setWithPriority', 3, onComplete, true);
Iif (this.getKey() === '.length' || this.getKey() === '.keys')
throw 'Reference.setWithPriority failed: ' +
this.getKey() +
' is a read-only object.';
const deferred = new Deferred();
this.repo.setWithPriority(
this.path,
newVal,
newPriority,
deferred.wrapCallback(onComplete)
);
return deferred.promise;
}
/**
* @param {function(?Error)=} onComplete
* @return {!Promise}
*/
remove(onComplete?: (a: Error | null) => void): Promise<any> {
validateArgCount('Reference.remove', 0, 1, arguments.length);
validateWritablePath('Reference.remove', this.path);
validateCallback('Reference.remove', 1, onComplete, true);
return this.set(null, onComplete);
}
/**
* @param {function(*):*} transactionUpdate
* @param {(function(?Error, boolean, ?DataSnapshot))=} onComplete
* @param {boolean=} applyLocally
* @return {!Promise}
*/
transaction(
transactionUpdate: (a: any) => any,
onComplete?: (a: Error | null, b: boolean, c: DataSnapshot | null) => void,
applyLocally?: boolean
): Promise<TransactionResult> {
validateArgCount('Reference.transaction', 1, 3, arguments.length);
validateWritablePath('Reference.transaction', this.path);
validateCallback('Reference.transaction', 1, transactionUpdate, false);
validateCallback('Reference.transaction', 2, onComplete, true);
// NOTE: applyLocally is an internal-only option for now. We need to decide if we want to keep it and how
// to expose it.
validateBoolean('Reference.transaction', 3, applyLocally, true);
Iif (this.getKey() === '.length' || this.getKey() === '.keys')
throw 'Reference.transaction failed: ' +
this.getKey() +
' is a read-only object.';
if (applyLocally === undefined) applyLocally = true;
const deferred = new Deferred<TransactionResult>();
if (typeof onComplete === 'function') {
deferred.promise.catch(() => {});
}
const promiseComplete = function(
error: Error,
committed: boolean,
snapshot: DataSnapshot
) {
if (error) {
deferred.reject(error);
} else {
deferred.resolve(new TransactionResult(committed, snapshot));
}
if (typeof onComplete === 'function') {
onComplete(error, committed, snapshot);
}
};
this.repo.startTransaction(
this.path,
transactionUpdate,
promiseComplete,
applyLocally
);
return deferred.promise;
}
/**
* @param {string|number|null} priority
* @param {function(?Error)=} onComplete
* @return {!Promise}
*/
setPriority(
priority: string | number | null,
onComplete?: (a: Error | null) => void
): Promise<any> {
validateArgCount('Reference.setPriority', 1, 2, arguments.length);
validateWritablePath('Reference.setPriority', this.path);
validatePriority('Reference.setPriority', 1, priority, false);
validateCallback('Reference.setPriority', 2, onComplete, true);
const deferred = new Deferred();
this.repo.setWithPriority(
this.path.child('.priority'),
priority,
null,
deferred.wrapCallback(onComplete)
);
return deferred.promise;
}
/**
* @param {*=} value
* @param {function(?Error)=} onComplete
* @return {!Reference}
*/
push(value?: any, onComplete?: (a: Error | null) => void): Reference {
validateArgCount('Reference.push', 0, 2, arguments.length);
validateWritablePath('Reference.push', this.path);
validateFirebaseDataArg('Reference.push', 1, value, this.path, true);
validateCallback('Reference.push', 2, onComplete, true);
const now = this.repo.serverTime();
const name = nextPushId(now);
// push() returns a ThennableReference whose promise is fulfilled with a regular Reference.
// We use child() to create handles to two different references. The first is turned into a
// ThennableReference below by adding then() and catch() methods and is used as the
// return value of push(). The second remains a regular Reference and is used as the fulfilled
// value of the first ThennableReference.
const thennablePushRef = this.child(name);
const pushRef = this.child(name);
let promise;
if (value != null) {
promise = thennablePushRef.set(value, onComplete).then(() => pushRef);
} else {
promise = Promise.resolve(pushRef);
}
thennablePushRef.then = promise.then.bind(promise);
thennablePushRef.catch = promise.then.bind(promise, undefined);
if (typeof onComplete === 'function') {
promise.catch(() => {});
}
return thennablePushRef;
}
/**
* @return {!OnDisconnect}
*/
onDisconnect(): OnDisconnect {
validateWritablePath('Reference.onDisconnect', this.path);
return new OnDisconnect(this.repo, this.path);
}
get database(): Database {
return this.databaseProp();
}
get key(): string | null {
return this.getKey();
}
get parent(): Reference | null {
return this.getParent();
}
get root(): Reference {
return this.getRoot();
}
}
/**
* Define reference constructor in various modules
*
* We are doing this here to avoid several circular
* dependency issues
*/
Query.__referenceConstructor = Reference;
SyncPoint.__referenceConstructor = Reference;
|