Skip to content

Commit f0aae0d

Browse files
Revert "Revert "Fix the type annotations in the RTDB Query.on/off/once API. (#1204)" (#1723)"
This reverts commit b6bfae0.
1 parent b6bfae0 commit f0aae0d

File tree

5 files changed

+39
-28
lines changed

5 files changed

+39
-28
lines changed

packages/database-types/index.d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,17 @@ export interface Query {
7171
eventType?: EventType,
7272
callback?: (a: DataSnapshot, b?: string | null) => any,
7373
context?: Object | null
74-
): any;
74+
): void;
7575
on(
7676
eventType: EventType,
77-
callback: (a: DataSnapshot | null, b?: string) => any,
78-
cancelCallbackOrContext?: Object | null,
77+
callback: (a: DataSnapshot, b?: string | null) => any,
78+
cancelCallbackOrContext?: ((a: Error) => any) | Object | null,
7979
context?: Object | null
80-
): (a: DataSnapshot | null, b?: string) => any;
80+
): (a: DataSnapshot, b?: string | null) => any;
8181
once(
8282
eventType: EventType,
83-
successCallback?: (a: DataSnapshot, b?: string) => any,
84-
failureCallbackOrContext?: Object | null,
83+
successCallback?: (a: DataSnapshot, b?: string | null) => any,
84+
failureCallbackOrContext?: ((a: Error) => void) | Object | null,
8585
context?: Object | null
8686
): Promise<DataSnapshot>;
8787
orderByChild(path: string): Query;

packages/database/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Unreleased
2+
- [changed] Improved consistency between the type annotations for `Query.on`/`Reference.on`,
3+
`Query.off`/`Reference.off` and `Query.once`/`Reference.once` (#1188, #1204).

packages/database/src/api/Query.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import { DataSnapshot } from './DataSnapshot';
4949
let __referenceConstructor: new (repo: Repo, path: Path) => Query;
5050

5151
export interface SnapshotCallback {
52-
(a: DataSnapshot, b?: string): any;
52+
(a: DataSnapshot, b?: string | null): any;
5353
}
5454

5555
/**
@@ -198,8 +198,8 @@ export class Query {
198198
on(
199199
eventType: string,
200200
callback: SnapshotCallback,
201-
cancelCallbackOrContext?: ((a: Error) => any) | Object,
202-
context?: Object
201+
cancelCallbackOrContext?: ((a: Error) => any) | Object | null,
202+
context?: Object | null
203203
): SnapshotCallback {
204204
validateArgCount('Query.on', 2, 4, arguments.length);
205205
validateEventType('Query.on', 1, eventType, false);
@@ -264,7 +264,11 @@ export class Query {
264264
* @param {(function(!DataSnapshot, ?string=))=} callback
265265
* @param {Object=} context
266266
*/
267-
off(eventType?: string, callback?: SnapshotCallback, context?: Object) {
267+
off(
268+
eventType?: string,
269+
callback?: SnapshotCallback,
270+
context?: Object | null
271+
): void {
268272
validateArgCount('Query.off', 0, 3, arguments.length);
269273
validateEventType('Query.off', 1, eventType, true);
270274
validateCallback('Query.off', 2, callback, true);
@@ -293,23 +297,23 @@ export class Query {
293297
* Attaches a listener, waits for the first event, and then removes the listener
294298
* @param {!string} eventType
295299
* @param {!function(!DataSnapshot, string=)} userCallback
296-
* @param cancelOrContext
300+
* @param failureCallbackOrContext
297301
* @param context
298302
* @return {!firebase.Promise}
299303
*/
300304
once(
301305
eventType: string,
302306
userCallback?: SnapshotCallback,
303-
cancelOrContext?: ((a: Error) => void) | Object,
304-
context?: Object
307+
failureCallbackOrContext?: ((a: Error) => void) | Object | null,
308+
context?: Object | null
305309
): Promise<DataSnapshot> {
306310
validateArgCount('Query.once', 1, 4, arguments.length);
307311
validateEventType('Query.once', 1, eventType, false);
308312
validateCallback('Query.once', 2, userCallback, true);
309313

310314
const ret = Query.getCancelAndContextArgs_(
311315
'Query.once',
312-
cancelOrContext,
316+
failureCallbackOrContext,
313317
context
314318
);
315319

@@ -631,8 +635,8 @@ export class Query {
631635
*/
632636
private static getCancelAndContextArgs_(
633637
fnName: string,
634-
cancelOrContext?: ((a: Error) => void) | Object,
635-
context?: Object
638+
cancelOrContext?: ((a: Error) => void) | Object | null,
639+
context?: Object | null
636640
): { cancel: ((a: Error) => void) | null; context: Object | null } {
637641
const ret: {
638642
cancel: ((a: Error) => void) | null;

packages/database/src/core/view/Change.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class Change {
3232
public snapshotNode: Node,
3333
public childName?: string,
3434
public oldSnap?: Node,
35-
public prevName?: string
35+
public prevName?: string | null
3636
) {}
3737

3838
/**

packages/firebase/index.d.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4388,8 +4388,8 @@ declare namespace firebase.database {
43884388
* must also be called on any child listeners to remove the callback.
43894389
*
43904390
* If a callback is not specified, all callbacks for the specified eventType
4391-
* will be removed. Similarly, if no eventType or callback is specified, all
4392-
* callbacks for the `Reference` will be removed.
4391+
* will be removed. Similarly, if no eventType is specified, all callbacks
4392+
* for the `Reference` will be removed.
43934393
*
43944394
* @example
43954395
* ```javascript
@@ -4414,16 +4414,17 @@ declare namespace firebase.database {
44144414
* ```
44154415
*
44164416
* @param eventType One of the following strings: "value",
4417-
* "child_added", "child_changed", "child_removed", or "child_moved."
4418-
* @param callback The
4419-
* callback function that was passed to `on()`.
4417+
* "child_added", "child_changed", "child_removed", or "child_moved." If
4418+
* omitted, all callbacks for the `Reference` will be removed.
4419+
* @param callback The callback function that was passed to `on()` or
4420+
* `undefined` to remove all callbacks.
44204421
* @param context The context that was passed to `on()`.
44214422
*/
44224423
off(
44234424
eventType?: EventType,
44244425
callback?: (a: firebase.database.DataSnapshot, b?: string | null) => any,
44254426
context?: Object | null
4426-
): any;
4427+
): void;
44274428

44284429
/**
44294430
* Listens for data changes at a particular location.
@@ -4539,10 +4540,10 @@ declare namespace firebase.database {
45394540
*/
45404541
on(
45414542
eventType: EventType,
4542-
callback: (a: firebase.database.DataSnapshot | null, b?: string) => any,
4543+
callback: (a: firebase.database.DataSnapshot, b?: string | null) => any,
45434544
cancelCallbackOrContext?: Object | null,
45444545
context?: Object | null
4545-
): (a: firebase.database.DataSnapshot | null, b?: string) => any;
4546+
): (a: firebase.database.DataSnapshot | null, b?: string | null) => any;
45464547

45474548
/**
45484549
* Listens for exactly one event of the specified event type, and then stops
@@ -4579,10 +4580,13 @@ declare namespace firebase.database {
45794580
*/
45804581
once(
45814582
eventType: EventType,
4582-
successCallback?: (a: firebase.database.DataSnapshot, b?: string) => any,
4583-
failureCallbackOrContext?: Object | null,
4583+
successCallback?: (
4584+
a: firebase.database.DataSnapshot,
4585+
b?: string | null
4586+
) => any,
4587+
failureCallbackOrContext?: ((a: Error) => void) | Object | null,
45844588
context?: Object | null
4585-
): Promise<DataSnapshot>;
4589+
): Promise<firebase.database.DataSnapshot>;
45864590
/**
45874591
* Generates a new `Query` object ordered by the specified child key.
45884592
*

0 commit comments

Comments
 (0)