Skip to content

Commit

Permalink
Updated type of action parameter for DataSnapshot#forEach (#6541)
Browse files Browse the repository at this point in the history
  • Loading branch information
maneesht authored Jul 5, 2023
1 parent 6674fad commit 4f904bf
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .changeset/silly-eagles-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@firebase/database-compat": major
"@firebase/database-types": major
"@firebase/database": major
---

Updated type of action parameter for DataSnapshot#forEach
8 changes: 7 additions & 1 deletion common/api-review/database.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class DataSnapshot {
child(path: string): DataSnapshot;
exists(): boolean;
exportVal(): any;
forEach(action: (child: DataSnapshot) => boolean | void): boolean;
forEach(action: (child: IteratedDataSnapshot) => boolean | void): boolean;
hasChild(path: string): boolean;
hasChildren(): boolean;
get key(): string | null;
Expand Down Expand Up @@ -85,6 +85,12 @@ export function goOnline(db: Database): void;
// @public
export function increment(delta: number): object;

// @public
export interface IteratedDataSnapshot extends DataSnapshot {
// (undocumented)
key: string;
}

// @public
export function limitToFirst(limit: number): QueryConstraint;

Expand Down
9 changes: 8 additions & 1 deletion packages/database-compat/src/api/Reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export class DataSnapshot implements Compat<ModularDataSnapshot> {
* @returns True if forEach was canceled by action returning true for
* one of the child nodes.
*/
forEach(action: (snapshot: DataSnapshot) => boolean | void): boolean {
forEach(action: (snapshot: IteratedDataSnapshot) => boolean | void): boolean {
validateArgCount('DataSnapshot.forEach', 1, 1, arguments.length);
validateCallback('DataSnapshot.forEach', 'action', action, false);
return this._delegate.forEach(expDataSnapshot =>
Expand Down Expand Up @@ -208,6 +208,13 @@ export class DataSnapshot implements Compat<ModularDataSnapshot> {
}
}

/**
* Represents a child snapshot of a `Reference` that is being iterated over. The key will never be undefined.
*/
export interface IteratedDataSnapshot extends DataSnapshot {
key: string; // key of the location of this snapshot.
}

export interface SnapshotCallback {
(dataSnapshot: DataSnapshot, previousChildName?: string | null): unknown;
}
Expand Down
9 changes: 8 additions & 1 deletion packages/database-types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,18 @@
import { FirebaseApp } from '@firebase/app-types';
import { EmulatorMockTokenOptions } from '@firebase/util';

/**
* Represents a child snapshot of a `Reference` that is being iterated over. The key will never be undefined.
*/
export interface IteratedDataSnapshot extends DataSnapshot {
key: string; // key of the location of this snapshot.
}

export interface DataSnapshot {
child(path: string): DataSnapshot;
exists(): boolean;
exportVal(): any;
forEach(action: (a: DataSnapshot) => boolean | void): boolean;
forEach(action: (a: IteratedDataSnapshot) => boolean | void): boolean;
getPriority(): string | number | null;
hasChild(path: string): boolean;
hasChildren(): boolean;
Expand Down
1 change: 1 addition & 0 deletions packages/database/src/api.standalone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export { OnDisconnect } from './api/OnDisconnect';
export {
DataSnapshot,
EventType,
IteratedDataSnapshot,
QueryConstraint,
QueryConstraintType,
endAt,
Expand Down
11 changes: 9 additions & 2 deletions packages/database/src/api/Reference_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ export class DataSnapshot {
}

/**
* Enumerates the top-level children in the `DataSnapshot`.
* Enumerates the top-level children in the `IteratedDataSnapshot`.
*
* Because of the way JavaScript objects work, the ordering of data in the
* JavaScript object returned by `val()` is not guaranteed to match the
Expand All @@ -393,7 +393,7 @@ export class DataSnapshot {
* @returns true if enumeration was canceled due to your callback returning
* true.
*/
forEach(action: (child: DataSnapshot) => boolean | void): boolean {
forEach(action: (child: IteratedDataSnapshot) => boolean | void): boolean {
if (this._node.isLeafNode()) {
return false;
}
Expand Down Expand Up @@ -463,6 +463,13 @@ export class DataSnapshot {
}
}

/**
* Represents a child snapshot of a `Reference` that is being iterated over. The key will never be undefined.
*/
export interface IteratedDataSnapshot extends DataSnapshot {
key: string; // key of the location of this snapshot.
}

/**
*
* Returns a `Reference` representing the location in the Database
Expand Down
2 changes: 1 addition & 1 deletion packages/firebase/compat/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5819,7 +5819,7 @@ declare namespace firebase.database {
* returning true.
*/
forEach(
action: (a: firebase.database.DataSnapshot) => boolean | void
action: (a: firebase.database.IteratorDataSnapshot) => boolean | void
): boolean;
/**
* Gets the priority value of the data in this `DataSnapshot`.
Expand Down

0 comments on commit 4f904bf

Please sign in to comment.