Skip to content

Commit 03606cc

Browse files
committed
Introduced new interface to represent iterated elements
1 parent ebbccf8 commit 03606cc

File tree

6 files changed

+33
-8
lines changed

6 files changed

+33
-8
lines changed

common/api-review/database.api.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ export class DataSnapshot {
3333
child(path: string): DataSnapshot;
3434
exists(): boolean;
3535
exportVal(): any;
36-
forEach(action: (child: DataSnapshot & {
37-
key: string;
38-
}) => boolean | void): boolean;
36+
forEach(action: (child: IteratedDataSnapshot) => boolean | void): boolean;
3937
hasChild(path: string): boolean;
4038
hasChildren(): boolean;
4139
get key(): string | null;
@@ -87,6 +85,12 @@ export function goOnline(db: Database): void;
8785
// @public
8886
export function increment(delta: number): object;
8987

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

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ export class DataSnapshot implements Compat<ModularDataSnapshot> {
156156
return this._delegate.priority;
157157
}
158158

159+
160+
159161
/**
160162
* Iterates through child nodes and calls the specified action for each one.
161163
*
@@ -165,7 +167,7 @@ export class DataSnapshot implements Compat<ModularDataSnapshot> {
165167
* one of the child nodes.
166168
*/
167169
forEach(
168-
action: (snapshot: DataSnapshot & { key: string }) => boolean | void
170+
action: (snapshot: IteratedDataSnapshot) => boolean | void
169171
): boolean {
170172
validateArgCount('DataSnapshot.forEach', 1, 1, arguments.length);
171173
validateCallback('DataSnapshot.forEach', 'action', action, false);
@@ -210,6 +212,10 @@ export class DataSnapshot implements Compat<ModularDataSnapshot> {
210212
}
211213
}
212214

215+
export interface IteratedDataSnapshot extends DataSnapshot {
216+
key: string;
217+
}
218+
213219
export interface SnapshotCallback {
214220
(dataSnapshot: DataSnapshot, previousChildName?: string | null): unknown;
215221
}

packages/database-types/index.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,19 @@
1818
import { FirebaseApp } from '@firebase/app-types';
1919
import { EmulatorMockTokenOptions } from '@firebase/util';
2020

21+
/**
22+
* Represents a child snapshot of an iterated `Reference`. The key will never be undefined.
23+
*/
24+
export interface IteratedDataSnapshot extends DataSnapshot {
25+
key: string;
26+
}
27+
2128
export interface DataSnapshot {
2229
child(path: string): DataSnapshot;
2330
exists(): boolean;
2431
exportVal(): any;
2532
forEach(
26-
action: (a: DataSnapshot & { key: string }) => boolean | void
33+
action: (a: IteratedDataSnapshot) => boolean | void
2734
): boolean;
2835
getPriority(): string | number | null;
2936
hasChild(path: string): boolean;

packages/database/src/api.standalone.ts

Lines changed: 1 addition & 0 deletions
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

Lines changed: 9 additions & 2 deletions
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 `IteratorDataSnapshot`.
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
@@ -394,7 +394,7 @@ export class DataSnapshot {
394394
* true.
395395
*/
396396
forEach(
397-
action: (child: DataSnapshot & { key: string }) => boolean | void
397+
action: (child: IteratedDataSnapshot) => boolean | void
398398
): boolean {
399399
if (this._node.isLeafNode()) {
400400
return false;
@@ -465,6 +465,13 @@ export class DataSnapshot {
465465
}
466466
}
467467

468+
/**
469+
* Represents a child snapshot of an iterated `Reference`. The key will never be undefined.
470+
*/
471+
export interface IteratedDataSnapshot extends DataSnapshot {
472+
key: string;
473+
}
474+
468475
/**
469476
*
470477
* Returns a `Reference` representing the location in the Database

packages/firebase/compat/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5820,7 +5820,7 @@ declare namespace firebase.database {
58205820
*/
58215821
forEach(
58225822
action: (
5823-
a: firebase.database.DataSnapshot & { key: string }
5823+
a: firebase.database.IteratorDataSnapshot
58245824
) => boolean | void
58255825
): boolean;
58265826
/**

0 commit comments

Comments
 (0)