@@ -20,14 +20,6 @@ const isIntrinsic = <I>(cb: IKeys<I> | ((...args: unknown[]) => unknown)): cb is
20
20
return typeof cb === 'string' ;
21
21
} ;
22
22
23
- const addToDisposeQueue = ( node : BaseNode , ops : DisposeOp [ ] ) => {
24
- if ( ! node . _onDispose ) {
25
- node . _onDispose = ops ;
26
- } else {
27
- node . _onDispose = node . _onDispose . concat ( ops ) ;
28
- }
29
- } ;
30
-
31
23
let _NextUniqueNodeId = 0 ;
32
24
33
25
interface NodeQueue {
@@ -45,14 +37,14 @@ class Prober<I extends FuncMap> implements IProber {
45
37
private _intrinsics : Partial < I > ;
46
38
private _fallback ?: IntrinsicFallback < I > ;
47
39
private _stack : ProberStackFrame [ ] = [ ] ;
48
- private _current : ProberStackFrame = { _disposeOps : [ ] } ;
40
+ private _currentFrame : ProberStackFrame = { _disposeOps : [ ] } ;
49
41
50
42
_onDispose ( op : DisposeOp ) : void {
51
- this . _current . _disposeOps . push ( op ) ;
43
+ this . _currentFrame . _disposeOps . push ( op ) ;
52
44
}
53
45
54
- _getProbingContext ( ) : ProbingContext | undefined {
55
- return this . _current ! . _node ! . _buildData ! . _context ;
46
+ _getProbingContext ( ) : ProbingContext {
47
+ return this . _currentFrame ! . _node ! . _buildData ! . _context ;
56
48
}
57
49
58
50
constructor ( intrinsics : Partial < I > , fallback ?: IntrinsicFallback < I > ) {
@@ -79,14 +71,15 @@ class Prober<I extends FuncMap> implements IProber {
79
71
newNode . _uniqueNodeId = _NextUniqueNodeId ++ ;
80
72
}
81
73
82
- if ( ! this . _current . _announced ) {
83
- this . _current . _announced = { _head : newNode , _tail : newNode } ;
74
+ if ( ! this . _currentFrame . _announced ) {
75
+ this . _currentFrame . _announced = { _head : newNode , _tail : newNode } ;
84
76
} else {
85
- this . _current . _announced . _tail . _buildData ! . _next = newNode ;
86
- this . _current . _announced . _tail = newNode ;
77
+ this . _currentFrame . _announced . _tail . _buildData ! . _next = newNode ;
78
+ this . _currentFrame . _announced . _tail = newNode ;
87
79
}
88
80
89
81
newNode . _buildData = {
82
+ _resolveAs : newNode ,
90
83
_cb,
91
84
_prober : this ,
92
85
_args,
@@ -100,41 +93,33 @@ class Prober<I extends FuncMap> implements IProber {
100
93
101
94
_finalizeNode ( node : BaseNode ) {
102
95
// If a component returns a Node (as opposed to a value), then we short-circuit to the parent.
103
- let destinationNode = node ;
104
- while ( destinationNode . _buildData && destinationNode . _buildData . _resolveAs ) {
105
- destinationNode = destinationNode . _buildData ! . _resolveAs ;
106
- }
96
+ const bd = node . _buildData ! ;
97
+ const destinationNode = bd . _resolveAs ;
107
98
108
- this . _current . _node = node ;
109
- const { _cb, _args } = node . _buildData ! ;
110
- const cbResult = _cb ( ..._args , node . _buildData ! . _context ) ;
99
+ this . _currentFrame . _node = node ;
100
+ const cbResult = bd . _cb ( ...bd . _args , bd . _context ) ;
111
101
112
102
if ( isPNode ( cbResult ) ) {
113
103
if ( cbResult . finalized ) {
114
- // Post-ex-facto proxying.
115
- destinationNode . _result = cbResult . _result ;
116
- if ( cbResult . _onDispose ) {
117
- addToDisposeQueue ( destinationNode , cbResult . _onDispose ) ;
118
- cbResult . _onDispose = [ ] ;
119
- }
104
+ destinationNode . _assign ( cbResult ) ;
120
105
} else {
121
106
cbResult . _buildData ! . _resolveAs = destinationNode ;
122
107
}
123
108
} else {
124
109
destinationNode . _result = cbResult ;
125
110
}
126
111
127
- if ( this . _current . _disposeOps . length > 0 ) {
128
- addToDisposeQueue ( destinationNode , this . _current . _disposeOps ) ;
129
- this . _current . _disposeOps = [ ] ;
112
+ if ( this . _currentFrame . _disposeOps . length > 0 ) {
113
+ destinationNode . _addToDispose ( this . _currentFrame . _disposeOps ) ;
114
+ this . _currentFrame . _disposeOps = [ ] ;
130
115
}
131
116
}
132
117
133
118
_finalize ( target : IPNode ) : void {
134
119
if ( process . env . NODE_ENV === 'check' ) {
135
120
let lookup : BaseNode | undefined ;
136
- if ( this . _current . _announced ) {
137
- lookup = this . _current . _announced . _head ;
121
+ if ( this . _currentFrame . _announced ) {
122
+ lookup = this . _currentFrame . _announced . _head ;
138
123
}
139
124
while ( lookup && lookup !== target ) {
140
125
lookup = lookup . _buildData ! . _next ;
@@ -144,58 +129,51 @@ class Prober<I extends FuncMap> implements IProber {
144
129
}
145
130
}
146
131
147
- let node : BaseNode = this . _current . _announced ! . _head ! ;
148
- let end : BaseNode = target as BaseNode ;
132
+ let node = this . _currentFrame . _announced ! . _head ! ;
133
+ let end = target as BaseNode ;
149
134
150
135
if ( end . _buildData ! . _next ) {
151
- this . _current . _announced ! . _head = end . _buildData ! . _next ;
136
+ this . _currentFrame . _announced ! . _head = end . _buildData ! . _next ;
152
137
} else {
153
- this . _current . _announced = undefined ;
138
+ this . _currentFrame . _announced = undefined ;
154
139
}
155
140
end . _buildData ! . _next = undefined ;
156
141
157
- /*
158
- //These two steps are, I suspect, Technically unnecessary
159
-
160
- if (!this._current._announced._head) {
161
- this._current._announced = {};
162
- }
163
- */
164
142
pushEnv ( this ) ;
165
- this . _stack . push ( this . _current ) ;
166
- this . _current = { _node : node , _disposeOps : [ ] } ;
143
+ this . _stack . push ( this . _currentFrame ) ;
144
+ this . _currentFrame = { _node : node , _disposeOps : [ ] } ;
167
145
168
146
let done = false ;
169
147
while ( ! done ) {
170
148
this . _finalizeNode ( node ) ;
171
149
172
- // Queue up any work that was discovered in the process.
173
- if ( this . _current . _announced ) {
174
- end = this . _current . _announced . _tail ;
175
- node . _buildData ! . _next = this . _current . _announced . _head ;
176
- this . _current . _announced = undefined ;
150
+ // Queue up any work that was discovered in the process,
151
+ // and update our end target so that we complete it before
152
+ // returning.
153
+ if ( this . _currentFrame . _announced ) {
154
+ end = this . _currentFrame . _announced . _tail ;
155
+ node . _buildData ! . _next = this . _currentFrame . _announced . _head ;
156
+ this . _currentFrame . _announced = undefined ;
177
157
}
178
158
179
159
done = node === end ;
180
- const nextNode = node . _buildData ! . _next as BaseNode ;
160
+ const nextNode = node . _buildData ! . _next ;
181
161
node . _buildData = undefined ;
182
- node = nextNode ;
162
+ node = nextNode ! ;
183
163
}
184
164
185
- this . _current = this . _stack . pop ( ) ! ;
165
+ this . _currentFrame = this . _stack . pop ( ) ! ;
186
166
popEnv ( ) ;
187
167
}
188
168
189
169
private _getCb < T extends IKeys < I > | ComponentCb > ( what : T ) : { _cb : ComponentCb ; _name : string } {
190
170
if ( isIntrinsic < I > ( what ) ) {
191
171
let _cb : ComponentCb | undefined = this . _intrinsics [ what ] ;
192
- const _name = what . toString ( ) ;
193
172
if ( ! _cb ) {
194
- // This is safe, it's caught at the start of _announce()
195
173
_cb = this . _fallback ! ;
196
174
}
197
175
198
- return { _cb : _cb ! , _name } ;
176
+ return { _cb : _cb ! , _name : what . toString ( ) } ;
199
177
} else {
200
178
return { _cb : what as ComponentCb , _name : ( what as ComponentCb ) . name } ;
201
179
}
0 commit comments