Skip to content

Commit aca9966

Browse files
schmidt-sebastianFeiyang1
authored andcommitted
Revert "Revert "Fix the type annotations in the RTDB Query.on/off/once API. (#1204)" (#1723)" (#1724)
This reverts commit b6bfae0.
1 parent a7bb0b5 commit aca9966

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
@@ -4539,8 +4539,8 @@ declare namespace firebase.database {
45394539
* must also be called on any child listeners to remove the callback.
45404540
*
45414541
* If a callback is not specified, all callbacks for the specified eventType
4542-
* will be removed. Similarly, if no eventType or callback is specified, all
4543-
* callbacks for the `Reference` will be removed.
4542+
* will be removed. Similarly, if no eventType is specified, all callbacks
4543+
* for the `Reference` will be removed.
45444544
*
45454545
* @example
45464546
* ```javascript
@@ -4565,16 +4565,17 @@ declare namespace firebase.database {
45654565
* ```
45664566
*
45674567
* @param eventType One of the following strings: "value",
4568-
* "child_added", "child_changed", "child_removed", or "child_moved."
4569-
* @param callback The
4570-
* callback function that was passed to `on()`.
4568+
* "child_added", "child_changed", "child_removed", or "child_moved." If
4569+
* omitted, all callbacks for the `Reference` will be removed.
4570+
* @param callback The callback function that was passed to `on()` or
4571+
* `undefined` to remove all callbacks.
45714572
* @param context The context that was passed to `on()`.
45724573
*/
45734574
off(
45744575
eventType?: EventType,
45754576
callback?: (a: firebase.database.DataSnapshot, b?: string | null) => any,
45764577
context?: Object | null
4577-
): any;
4578+
): void;
45784579

45794580
/**
45804581
* Listens for data changes at a particular location.
@@ -4690,10 +4691,10 @@ declare namespace firebase.database {
46904691
*/
46914692
on(
46924693
eventType: EventType,
4693-
callback: (a: firebase.database.DataSnapshot | null, b?: string) => any,
4694+
callback: (a: firebase.database.DataSnapshot, b?: string | null) => any,
46944695
cancelCallbackOrContext?: Object | null,
46954696
context?: Object | null
4696-
): (a: firebase.database.DataSnapshot | null, b?: string) => any;
4697+
): (a: firebase.database.DataSnapshot | null, b?: string | null) => any;
46974698

46984699
/**
46994700
* Listens for exactly one event of the specified event type, and then stops
@@ -4730,10 +4731,13 @@ declare namespace firebase.database {
47304731
*/
47314732
once(
47324733
eventType: EventType,
4733-
successCallback?: (a: firebase.database.DataSnapshot, b?: string) => any,
4734-
failureCallbackOrContext?: Object | null,
4734+
successCallback?: (
4735+
a: firebase.database.DataSnapshot,
4736+
b?: string | null
4737+
) => any,
4738+
failureCallbackOrContext?: ((a: Error) => void) | Object | null,
47354739
context?: Object | null
4736-
): Promise<DataSnapshot>;
4740+
): Promise<firebase.database.DataSnapshot>;
47374741
/**
47384742
* Generates a new `Query` object ordered by the specified child key.
47394743
*

0 commit comments

Comments
 (0)