Skip to content

Commit 4f904bf

Browse files
authored
Updated type of action parameter for DataSnapshot#forEach (#6541)
1 parent 6674fad commit 4f904bf

File tree

7 files changed

+41
-6
lines changed

7 files changed

+41
-6
lines changed

.changeset/silly-eagles-unite.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@firebase/database-compat": major
3+
"@firebase/database-types": major
4+
"@firebase/database": major
5+
---
6+
7+
Updated type of action parameter for DataSnapshot#forEach

common/api-review/database.api.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class DataSnapshot {
3333
child(path: string): DataSnapshot;
3434
exists(): boolean;
3535
exportVal(): any;
36-
forEach(action: (child: DataSnapshot) => boolean | void): boolean;
36+
forEach(action: (child: IteratedDataSnapshot) => boolean | void): boolean;
3737
hasChild(path: string): boolean;
3838
hasChildren(): boolean;
3939
get key(): string | null;
@@ -85,6 +85,12 @@ export function goOnline(db: Database): void;
8585
// @public
8686
export function increment(delta: number): object;
8787

88+
// @public
89+
export interface IteratedDataSnapshot extends DataSnapshot {
90+
// (undocumented)
91+
key: string;
92+
}
93+
8894
// @public
8995
export function limitToFirst(limit: number): QueryConstraint;
9096

packages/database-compat/src/api/Reference.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ export class DataSnapshot implements Compat<ModularDataSnapshot> {
164164
* @returns True if forEach was canceled by action returning true for
165165
* one of the child nodes.
166166
*/
167-
forEach(action: (snapshot: DataSnapshot) => boolean | void): boolean {
167+
forEach(action: (snapshot: IteratedDataSnapshot) => boolean | void): boolean {
168168
validateArgCount('DataSnapshot.forEach', 1, 1, arguments.length);
169169
validateCallback('DataSnapshot.forEach', 'action', action, false);
170170
return this._delegate.forEach(expDataSnapshot =>
@@ -208,6 +208,13 @@ export class DataSnapshot implements Compat<ModularDataSnapshot> {
208208
}
209209
}
210210

211+
/**
212+
* Represents a child snapshot of a `Reference` that is being iterated over. The key will never be undefined.
213+
*/
214+
export interface IteratedDataSnapshot extends DataSnapshot {
215+
key: string; // key of the location of this snapshot.
216+
}
217+
211218
export interface SnapshotCallback {
212219
(dataSnapshot: DataSnapshot, previousChildName?: string | null): unknown;
213220
}

packages/database-types/index.d.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,18 @@
1818
import { FirebaseApp } from '@firebase/app-types';
1919
import { EmulatorMockTokenOptions } from '@firebase/util';
2020

21+
/**
22+
* Represents a child snapshot of a `Reference` that is being iterated over. The key will never be undefined.
23+
*/
24+
export interface IteratedDataSnapshot extends DataSnapshot {
25+
key: string; // key of the location of this snapshot.
26+
}
27+
2128
export interface DataSnapshot {
2229
child(path: string): DataSnapshot;
2330
exists(): boolean;
2431
exportVal(): any;
25-
forEach(action: (a: DataSnapshot) => boolean | void): boolean;
32+
forEach(action: (a: IteratedDataSnapshot) => boolean | void): boolean;
2633
getPriority(): string | number | null;
2734
hasChild(path: string): boolean;
2835
hasChildren(): boolean;

packages/database/src/api.standalone.ts

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export { OnDisconnect } from './api/OnDisconnect';
3737
export {
3838
DataSnapshot,
3939
EventType,
40+
IteratedDataSnapshot,
4041
QueryConstraint,
4142
QueryConstraintType,
4243
endAt,

packages/database/src/api/Reference_impl.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ export class DataSnapshot {
376376
}
377377

378378
/**
379-
* Enumerates the top-level children in the `DataSnapshot`.
379+
* Enumerates the top-level children in the `IteratedDataSnapshot`.
380380
*
381381
* Because of the way JavaScript objects work, the ordering of data in the
382382
* JavaScript object returned by `val()` is not guaranteed to match the
@@ -393,7 +393,7 @@ export class DataSnapshot {
393393
* @returns true if enumeration was canceled due to your callback returning
394394
* true.
395395
*/
396-
forEach(action: (child: DataSnapshot) => boolean | void): boolean {
396+
forEach(action: (child: IteratedDataSnapshot) => boolean | void): boolean {
397397
if (this._node.isLeafNode()) {
398398
return false;
399399
}
@@ -463,6 +463,13 @@ export class DataSnapshot {
463463
}
464464
}
465465

466+
/**
467+
* Represents a child snapshot of a `Reference` that is being iterated over. The key will never be undefined.
468+
*/
469+
export interface IteratedDataSnapshot extends DataSnapshot {
470+
key: string; // key of the location of this snapshot.
471+
}
472+
466473
/**
467474
*
468475
* Returns a `Reference` representing the location in the Database

packages/firebase/compat/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5819,7 +5819,7 @@ declare namespace firebase.database {
58195819
* returning true.
58205820
*/
58215821
forEach(
5822-
action: (a: firebase.database.DataSnapshot) => boolean | void
5822+
action: (a: firebase.database.IteratorDataSnapshot) => boolean | void
58235823
): boolean;
58245824
/**
58255825
* Gets the priority value of the data in this `DataSnapshot`.

0 commit comments

Comments
 (0)