@@ -21,35 +21,26 @@ import { Node } from './snap/Node';
21
21
22
22
/**
23
23
* Helper class to store a sparse set of snapshots.
24
- *
25
- * @constructor
26
24
*/
27
25
export class SparseSnapshotTree {
28
- /**
29
- * @private
30
- * @type {Node }
31
- */
32
- private value_ : Node | null = null ;
26
+ private value : Node | null = null ;
33
27
34
- /**
35
- * @private
36
- */
37
- private children_ : Map < string , SparseSnapshotTree > | null = null ;
28
+ private readonly children : Map < string , SparseSnapshotTree > = new Map ( ) ;
38
29
39
30
/**
40
31
* Gets the node stored at the given path if one exists.
41
32
*
42
- * @param { !Path } path Path to look up snapshot for.
43
- * @return { ?Node } The retrieved node, or null.
33
+ * @param path Path to look up snapshot for.
34
+ * @return The retrieved node, or null.
44
35
*/
45
36
find ( path : Path ) : Node | null {
46
- if ( this . value_ != null ) {
47
- return this . value_ . getChild ( path ) ;
48
- } else if ( ! path . isEmpty ( ) && this . children_ != null ) {
37
+ if ( this . value != null ) {
38
+ return this . value . getChild ( path ) ;
39
+ } else if ( ! path . isEmpty ( ) && this . children . size > 0 ) {
49
40
const childKey = path . getFront ( ) ;
50
41
path = path . popFront ( ) ;
51
- if ( this . children_ . has ( childKey ) ) {
52
- const childTree = this . children_ . get ( childKey ) ;
42
+ if ( this . children . has ( childKey ) ) {
43
+ const childTree = this . children . get ( childKey ) ;
53
44
return childTree . find ( path ) ;
54
45
} else {
55
46
return null ;
@@ -63,26 +54,22 @@ export class SparseSnapshotTree {
63
54
* Stores the given node at the specified path. If there is already a node
64
55
* at a shallower path, it merges the new data into that snapshot node.
65
56
*
66
- * @param { !Path } path Path to look up snapshot for.
67
- * @param { !Node } data The new data, or null.
57
+ * @param path Path to look up snapshot for.
58
+ * @param data The new data, or null.
68
59
*/
69
60
remember ( path : Path , data : Node ) {
70
61
if ( path . isEmpty ( ) ) {
71
- this . value_ = data ;
72
- this . children_ = null ;
73
- } else if ( this . value_ !== null ) {
74
- this . value_ = this . value_ . updateChild ( path , data ) ;
62
+ this . value = data ;
63
+ this . children . clear ( ) ;
64
+ } else if ( this . value !== null ) {
65
+ this . value = this . value . updateChild ( path , data ) ;
75
66
} else {
76
- if ( this . children_ == null ) {
77
- this . children_ = new Map < string , SparseSnapshotTree > ( ) ;
78
- }
79
-
80
67
const childKey = path . getFront ( ) ;
81
- if ( ! this . children_ . has ( childKey ) ) {
82
- this . children_ . set ( childKey , new SparseSnapshotTree ( ) ) ;
68
+ if ( ! this . children . has ( childKey ) ) {
69
+ this . children . set ( childKey , new SparseSnapshotTree ( ) ) ;
83
70
}
84
71
85
- const child = this . children_ . get ( childKey ) as SparseSnapshotTree ;
72
+ const child = this . children . get ( childKey ) ;
86
73
path = path . popFront ( ) ;
87
74
child . remember ( path , data ) ;
88
75
}
@@ -91,22 +78,22 @@ export class SparseSnapshotTree {
91
78
/**
92
79
* Purge the data at path from the cache.
93
80
*
94
- * @param { !Path } path Path to look up snapshot for.
95
- * @return { boolean } True if this node should now be removed.
81
+ * @param path Path to look up snapshot for.
82
+ * @return True if this node should now be removed.
96
83
*/
97
84
forget ( path : Path ) : boolean {
98
85
if ( path . isEmpty ( ) ) {
99
- this . value_ = null ;
100
- this . children_ = null ;
86
+ this . value = null ;
87
+ this . children . clear ( ) ;
101
88
return true ;
102
89
} else {
103
- if ( this . value_ !== null ) {
104
- if ( this . value_ . isLeafNode ( ) ) {
90
+ if ( this . value !== null ) {
91
+ if ( this . value . isLeafNode ( ) ) {
105
92
// We're trying to forget a node that doesn't exist
106
93
return false ;
107
94
} else {
108
- const value = this . value_ ;
109
- this . value_ = null ;
95
+ const value = this . value ;
96
+ this . value = null ;
110
97
111
98
const self = this ;
112
99
value . forEachChild ( PRIORITY_INDEX , function ( key , tree ) {
@@ -115,24 +102,17 @@ export class SparseSnapshotTree {
115
102
116
103
return this . forget ( path ) ;
117
104
}
118
- } else if ( this . children_ !== null ) {
105
+ } else if ( this . children . size > 0 ) {
119
106
const childKey = path . getFront ( ) ;
120
107
path = path . popFront ( ) ;
121
- if ( this . children_ . has ( childKey ) ) {
122
- const safeToRemove = ( this . children_ . get (
123
- childKey
124
- ) as SparseSnapshotTree ) . forget ( path ) ;
108
+ if ( this . children . has ( childKey ) ) {
109
+ const safeToRemove = this . children . get ( childKey ) . forget ( path ) ;
125
110
if ( safeToRemove ) {
126
- this . children_ . delete ( childKey ) ;
111
+ this . children . delete ( childKey ) ;
127
112
}
128
113
}
129
114
130
- if ( this . children_ . size === 0 ) {
131
- this . children_ = null ;
132
- return true ;
133
- } else {
134
- return false ;
135
- }
115
+ return this . children . size === 0 ;
136
116
} else {
137
117
return true ;
138
118
}
@@ -143,12 +123,12 @@ export class SparseSnapshotTree {
143
123
* Recursively iterates through all of the stored tree and calls the
144
124
* callback on each one.
145
125
*
146
- * @param { !Path } prefixPath Path to look up node for.
147
- * @param { !Function } func The function to invoke for each tree.
126
+ * @param prefixPath Path to look up node for.
127
+ * @param func The function to invoke for each tree.
148
128
*/
149
129
forEachTree ( prefixPath : Path , func : ( a : Path , b : Node ) => any ) {
150
- if ( this . value_ !== null ) {
151
- func ( prefixPath , this . value_ ) ;
130
+ if ( this . value !== null ) {
131
+ func ( prefixPath , this . value ) ;
152
132
} else {
153
133
this . forEachChild ( ( key , tree ) => {
154
134
const path = new Path ( prefixPath . toString ( ) + '/' + key ) ;
@@ -160,13 +140,11 @@ export class SparseSnapshotTree {
160
140
/**
161
141
* Iterates through each immediate child and triggers the callback.
162
142
*
163
- * @param { !Function } func The function to invoke for each child.
143
+ * @param func The function to invoke for each child.
164
144
*/
165
145
forEachChild ( func : ( a : string , b : SparseSnapshotTree ) => void ) {
166
- if ( this . children_ !== null ) {
167
- this . children_ . forEach ( ( tree , key ) => {
168
- func ( key , tree ) ;
169
- } ) ;
170
- }
146
+ this . children . forEach ( ( tree , key ) => {
147
+ func ( key , tree ) ;
148
+ } ) ;
171
149
}
172
150
}
0 commit comments