17
17
18
18
import { validateArgCount , validateCallback } from '@firebase/util' ;
19
19
import { validatePathString } from '../core/util/validation' ;
20
- import { Path } from '../core/util/Path' ;
21
- import { PRIORITY_INDEX } from '../core/snap/indexes/PriorityIndex' ;
22
- import { Node } from '../core/snap/Node' ;
23
20
import { Reference } from './Reference' ;
24
- import { Index } from '../core/snap/indexes/Index' ;
25
- import { ChildrenNode } from '../core/snap/ChildrenNode' ;
21
+ import { DataSnapshot as ExpDataSnapshot } from '../exp/DataSnapshot' ;
22
+
23
+ // TODO(databaseexp): Import Compat from @firebase/util
24
+ export interface Compat < T > {
25
+ readonly _delegate : T ;
26
+ }
26
27
27
28
/**
28
29
* Class representing a firebase data snapshot. It wraps a SnapshotNode and
29
30
* surfaces the public methods (val, forEach, etc.) we want to expose.
30
31
*/
31
- export class DataSnapshot {
32
- /**
33
- * @param node_ A SnapshotNode to wrap.
34
- * @param ref_ The ref of the location this snapshot came from.
35
- * @param index_ The iteration order for this snapshot
36
- */
37
- constructor (
38
- private readonly node_ : Node ,
39
- private readonly ref_ : Reference ,
40
- private readonly index_ : Index
41
- ) { }
32
+ export class DataSnapshot implements Compat < ExpDataSnapshot > {
33
+ constructor ( readonly _delegate : ExpDataSnapshot ) { }
42
34
43
35
/**
44
36
* Retrieves the snapshot contents as JSON. Returns null if the snapshot is
@@ -48,7 +40,7 @@ export class DataSnapshot {
48
40
*/
49
41
val ( ) : unknown {
50
42
validateArgCount ( 'DataSnapshot.val' , 0 , 0 , arguments . length ) ;
51
- return this . node_ . val ( ) ;
43
+ return this . _delegate . val ( ) ;
52
44
}
53
45
54
46
/**
@@ -58,15 +50,15 @@ export class DataSnapshot {
58
50
*/
59
51
exportVal ( ) : unknown {
60
52
validateArgCount ( 'DataSnapshot.exportVal' , 0 , 0 , arguments . length ) ;
61
- return this . node_ . val ( true ) ;
53
+ return this . _delegate . exportVal ( ) ;
62
54
}
63
55
64
56
// Do not create public documentation. This is intended to make JSON serialization work but is otherwise unnecessary
65
57
// for end-users
66
58
toJSON ( ) : unknown {
67
59
// Optional spacer argument is unnecessary because we're depending on recursion rather than stringifying the content
68
60
validateArgCount ( 'DataSnapshot.toJSON' , 0 , 1 , arguments . length ) ;
69
- return this . exportVal ( ) ;
61
+ return this . _delegate . toJSON ( ) ;
70
62
}
71
63
72
64
/**
@@ -76,7 +68,7 @@ export class DataSnapshot {
76
68
*/
77
69
exists ( ) : boolean {
78
70
validateArgCount ( 'DataSnapshot.exists' , 0 , 0 , arguments . length ) ;
79
- return ! this . node_ . isEmpty ( ) ;
71
+ return this . _delegate . exists ( ) ;
80
72
}
81
73
82
74
/**
@@ -90,14 +82,7 @@ export class DataSnapshot {
90
82
// Ensure the childPath is a string (can be a number)
91
83
childPathString = String ( childPathString ) ;
92
84
validatePathString ( 'DataSnapshot.child' , 1 , childPathString , false ) ;
93
-
94
- const childPath = new Path ( childPathString ) ;
95
- const childRef = this . ref_ . child ( childPath ) ;
96
- return new DataSnapshot (
97
- this . node_ . getChild ( childPath ) ,
98
- childRef ,
99
- PRIORITY_INDEX
100
- ) ;
85
+ return new DataSnapshot ( this . _delegate . child ( childPathString ) ) ;
101
86
}
102
87
103
88
/**
@@ -109,9 +94,7 @@ export class DataSnapshot {
109
94
hasChild ( childPathString : string ) : boolean {
110
95
validateArgCount ( 'DataSnapshot.hasChild' , 1 , 1 , arguments . length ) ;
111
96
validatePathString ( 'DataSnapshot.hasChild' , 1 , childPathString , false ) ;
112
-
113
- const childPath = new Path ( childPathString ) ;
114
- return ! this . node_ . getChild ( childPath ) . isEmpty ( ) ;
97
+ return this . _delegate . hasChild ( childPathString ) ;
115
98
}
116
99
117
100
/**
@@ -121,9 +104,7 @@ export class DataSnapshot {
121
104
*/
122
105
getPriority ( ) : string | number | null {
123
106
validateArgCount ( 'DataSnapshot.getPriority' , 0 , 0 , arguments . length ) ;
124
-
125
- // typecast here because we never return deferred values or internal priorities (MAX_PRIORITY)
126
- return this . node_ . getPriority ( ) . val ( ) as string | number | null ;
107
+ return this . _delegate . priority ;
127
108
}
128
109
129
110
/**
@@ -134,21 +115,12 @@ export class DataSnapshot {
134
115
* @return True if forEach was canceled by action returning true for
135
116
* one of the child nodes.
136
117
*/
137
- forEach ( action : ( d : DataSnapshot ) => boolean | void ) : boolean {
118
+ forEach ( action : ( snapshot : DataSnapshot ) => boolean | void ) : boolean {
138
119
validateArgCount ( 'DataSnapshot.forEach' , 1 , 1 , arguments . length ) ;
139
120
validateCallback ( 'DataSnapshot.forEach' , 1 , action , false ) ;
140
-
141
- if ( this . node_ . isLeafNode ( ) ) {
142
- return false ;
143
- }
144
-
145
- const childrenNode = this . node_ as ChildrenNode ;
146
- // Sanitize the return value to a boolean. ChildrenNode.forEachChild has a weird return type...
147
- return ! ! childrenNode . forEachChild ( this . index_ , ( key , node ) => {
148
- return action (
149
- new DataSnapshot ( node , this . ref_ . child ( key ) , PRIORITY_INDEX )
150
- ) ;
151
- } ) ;
121
+ return this . _delegate . forEach ( expDataSnapshot =>
122
+ action ( new DataSnapshot ( expDataSnapshot ) )
123
+ ) ;
152
124
}
153
125
154
126
/**
@@ -157,16 +129,11 @@ export class DataSnapshot {
157
129
*/
158
130
hasChildren ( ) : boolean {
159
131
validateArgCount ( 'DataSnapshot.hasChildren' , 0 , 0 , arguments . length ) ;
160
-
161
- if ( this . node_ . isLeafNode ( ) ) {
162
- return false ;
163
- } else {
164
- return ! this . node_ . isEmpty ( ) ;
165
- }
132
+ return this . _delegate . hasChildren ( ) ;
166
133
}
167
134
168
135
get key ( ) {
169
- return this . ref_ . getKey ( ) ;
136
+ return this . _delegate . key ;
170
137
}
171
138
172
139
/**
@@ -175,8 +142,7 @@ export class DataSnapshot {
175
142
*/
176
143
numChildren ( ) : number {
177
144
validateArgCount ( 'DataSnapshot.numChildren' , 0 , 0 , arguments . length ) ;
178
-
179
- return this . node_ . numChildren ( ) ;
145
+ return this . _delegate . size ;
180
146
}
181
147
182
148
/**
@@ -185,8 +151,7 @@ export class DataSnapshot {
185
151
*/
186
152
getRef ( ) : Reference {
187
153
validateArgCount ( 'DataSnapshot.ref' , 0 , 0 , arguments . length ) ;
188
-
189
- return this . ref_ ;
154
+ return new Reference ( this . _delegate . ref . _repo , this . _delegate . ref . _path ) ;
190
155
}
191
156
192
157
get ref ( ) {
0 commit comments