33use super :: { Component , Scope } ;
44use crate :: scheduler:: { self , Runnable , Shared } ;
55use crate :: virtual_dom:: { VDiff , VNode } ;
6- use crate :: NodeRef ;
6+ use crate :: { Context , NodeRef } ;
77use std:: rc:: Rc ;
88use web_sys:: Element ;
99
1010pub ( crate ) struct ComponentState < COMP : Component > {
1111 pub ( crate ) component : Box < COMP > ,
1212 pub ( crate ) root_node : VNode ,
1313
14- scope : Scope < COMP > ,
14+ context : Context < COMP > ,
1515 parent : Element ,
1616 next_sibling : NodeRef ,
1717 node_ref : NodeRef ,
@@ -29,11 +29,13 @@ impl<COMP: Component> ComponentState<COMP> {
2929 scope : Scope < COMP > ,
3030 props : Rc < COMP :: Properties > ,
3131 ) -> Self {
32- let component = Box :: new ( COMP :: create ( Rc :: clone ( & props) , & scope) ) ;
32+ let context = Context { scope, props } ;
33+
34+ let component = Box :: new ( COMP :: create ( & context) ) ;
3335 Self {
3436 component,
3537 root_node,
36- scope ,
38+ context ,
3739 parent,
3840 next_sibling,
3941 node_ref,
@@ -127,25 +129,26 @@ impl<COMP: Component> Runnable for ComponentRunnable<COMP> {
127129 let should_render = match event {
128130 UpdateEvent :: First => true ,
129131 UpdateEvent :: Message ( message) => {
130- state. component . update ( & state. scope , message)
132+ state. component . update ( & state. context , message)
131133 }
132134 UpdateEvent :: MessageBatch ( messages) => {
133135 messages. into_iter ( ) . fold ( false , |acc, msg| {
134- state. component . update ( & state. scope , msg) || acc
136+ state. component . update ( & state. context , msg) || acc
135137 } )
136138 }
137139 UpdateEvent :: Properties ( props, node_ref, next_sibling) => {
138140 // When components are updated, a new node ref could have been passed in
139141 state. node_ref = node_ref;
140142 // When components are updated, their siblings were likely also updated
141143 state. next_sibling = next_sibling;
142- state. component . changed ( & state. scope , Rc :: clone ( & props) )
144+ state. context . props = Rc :: clone ( & props) ;
145+ state. component . changed ( & state. context )
143146 }
144147 } ;
145148
146149 if should_render {
147- state. pending_root = Some ( state. component . view ( & state. scope ) ) ;
148- state. scope . process ( ComponentLifecycleEvent :: Render ) ;
150+ state. pending_root = Some ( state. component . view ( & state. context ) ) ;
151+ state. context . scope . process ( ComponentLifecycleEvent :: Render ) ;
149152 } ;
150153 }
151154 }
@@ -155,25 +158,28 @@ impl<COMP: Component> Runnable for ComponentRunnable<COMP> {
155158 std:: mem:: swap ( & mut new_root, & mut state. root_node ) ;
156159 let ancestor = Some ( new_root) ;
157160 let new_root = & mut state. root_node ;
158- let scope = state. scope . clone ( ) . into ( ) ;
161+ let scope = state. context . scope . clone ( ) . into ( ) ;
159162 let next_sibling = state. next_sibling . clone ( ) ;
160163 let node = new_root. apply ( & scope, & state. parent , next_sibling, ancestor) ;
161164 state. node_ref . link ( node) ;
162- state. scope . process ( ComponentLifecycleEvent :: Rendered ) ;
165+ state
166+ . context
167+ . scope
168+ . process ( ComponentLifecycleEvent :: Rendered ) ;
163169 }
164170 }
165171 }
166172 ComponentLifecycleEvent :: Rendered => {
167173 if let Some ( mut state) = current_state. as_mut ( ) {
168174 let first_render = !state. has_rendered ;
169- state. component . rendered ( & state. scope , first_render) ;
175+ state. component . rendered ( & state. context , first_render) ;
170176 state. has_rendered = true ;
171177 state. drain_pending_updates ( & self . state ) ;
172178 }
173179 }
174180 ComponentLifecycleEvent :: Destroy => {
175181 if let Some ( mut state) = current_state. take ( ) {
176- state. component . destroy ( & state. scope ) ;
182+ state. component . destroy ( & state. context ) ;
177183 state. root_node . detach ( & state. parent ) ;
178184 state. node_ref . set ( None ) ;
179185 }
@@ -201,20 +207,18 @@ mod tests {
201207 lifecycle : Rc < RefCell < Vec < String > > > ,
202208 }
203209
204- struct Child {
205- props : Rc < ChildProps > ,
206- }
210+ struct Child { }
207211
208212 impl Component for Child {
209213 type Message = ( ) ;
210214 type Properties = ChildProps ;
211215
212- fn create ( props : Rc < Self :: Properties > , _ctx : & Context < Self > ) -> Self {
213- Child { props }
216+ fn create ( _ctx : & Context < Self > ) -> Self {
217+ Child { }
214218 }
215219
216- fn rendered ( & mut self , _ctx : & Context < Self > , _first_render : bool ) {
217- self . props
220+ fn rendered ( & mut self , ctx : & Context < Self > , _first_render : bool ) {
221+ ctx . props ( )
218222 . lifecycle
219223 . borrow_mut ( )
220224 . push ( "child rendered" . into ( ) ) ;
@@ -224,7 +228,7 @@ mod tests {
224228 false
225229 }
226230
227- fn changed ( & mut self , _ctx : & Context < Self > , _ : Rc < Self :: Properties > ) -> ShouldRender {
231+ fn changed ( & mut self , _ctx : & Context < Self > ) -> ShouldRender {
228232 false
229233 }
230234
@@ -245,60 +249,63 @@ mod tests {
245249 }
246250
247251 struct Comp {
248- props : Rc < Props > ,
252+ lifecycle : Rc < RefCell < Vec < String > > > ,
249253 }
250254
251255 impl Component for Comp {
252256 type Message = bool ;
253257 type Properties = Props ;
254258
255- fn create ( props : Rc < Self :: Properties > , ctx : & Context < Self > ) -> Self {
256- props. lifecycle . borrow_mut ( ) . push ( "create" . into ( ) ) ;
259+ fn create ( ctx : & Context < Self > ) -> Self {
260+ ctx . props ( ) . lifecycle . borrow_mut ( ) . push ( "create" . into ( ) ) ;
257261 #[ cfg( feature = "wasm_test" ) ]
258- if let Some ( msg) = props. create_message {
259- ctx. send_message ( msg) ;
262+ if let Some ( msg) = ctx. props ( ) . create_message {
263+ ctx. link ( ) . send_message ( msg) ;
264+ }
265+ Comp {
266+ lifecycle : Rc :: clone ( & ctx. props ( ) . lifecycle ) ,
260267 }
261- Comp { props }
262268 }
263269
264270 fn rendered ( & mut self , ctx : & Context < Self > , first_render : bool ) {
265- if let Some ( msg) = self . props . rendered_message . borrow_mut ( ) . take ( ) {
266- ctx. send_message ( msg) ;
271+ if let Some ( msg) = ctx . props ( ) . rendered_message . borrow_mut ( ) . take ( ) {
272+ ctx. link ( ) . send_message ( msg) ;
267273 }
268- self . props
274+ ctx . props ( )
269275 . lifecycle
270276 . borrow_mut ( )
271277 . push ( format ! ( "rendered({})" , first_render) ) ;
272278 }
273279
274280 fn update ( & mut self , ctx : & Context < Self > , msg : Self :: Message ) -> ShouldRender {
275- if let Some ( msg) = self . props . update_message . borrow_mut ( ) . take ( ) {
276- ctx. send_message ( msg) ;
281+ if let Some ( msg) = ctx . props ( ) . update_message . borrow_mut ( ) . take ( ) {
282+ ctx. link ( ) . send_message ( msg) ;
277283 }
278- self . props
284+ ctx . props ( )
279285 . lifecycle
280286 . borrow_mut ( )
281287 . push ( format ! ( "update({})" , msg) ) ;
282288 msg
283289 }
284290
285- fn changed ( & mut self , _ctx : & Context < Self > , _: Rc < Self :: Properties > ) -> ShouldRender {
286- self . props . lifecycle . borrow_mut ( ) . push ( "change" . into ( ) ) ;
291+ fn changed ( & mut self , ctx : & Context < Self > ) -> ShouldRender {
292+ self . lifecycle = Rc :: clone ( & ctx. props ( ) . lifecycle ) ;
293+ self . lifecycle . borrow_mut ( ) . push ( "change" . into ( ) ) ;
287294 false
288295 }
289296
290297 fn view ( & self , ctx : & Context < Self > ) -> Html {
291- if let Some ( msg) = self . props . view_message . borrow_mut ( ) . take ( ) {
292- ctx. send_message ( msg) ;
298+ if let Some ( msg) = ctx . props ( ) . view_message . borrow_mut ( ) . take ( ) {
299+ ctx. link ( ) . send_message ( msg) ;
293300 }
294- self . props . lifecycle . borrow_mut ( ) . push ( "view" . into ( ) ) ;
295- html ! { <Child lifecycle={ self . props . lifecycle. clone( ) } /> }
301+ self . lifecycle . borrow_mut ( ) . push ( "view" . into ( ) ) ;
302+ html ! { <Child lifecycle={ self . lifecycle. clone( ) } /> }
296303 }
297304 }
298305
299306 impl Drop for Comp {
300307 fn drop ( & mut self ) {
301- self . props . lifecycle . borrow_mut ( ) . push ( "drop" . into ( ) ) ;
308+ self . lifecycle . borrow_mut ( ) . push ( "drop" . into ( ) ) ;
302309 }
303310 }
304311
0 commit comments