11//! This module defines the `ContextProvider` component.
22
3- use crate :: html:: Context ;
43use crate :: html:: Scope ;
5- use crate :: { html, Callback , Children , Component , Html , Properties , ShouldRender } ;
4+ use crate :: { html, Callback , Children , Component , Context , Html , Properties } ;
65use slab:: Slab ;
76use std:: cell:: RefCell ;
87
@@ -23,6 +22,7 @@ pub struct ContextProviderProps<T: Clone + PartialEq> {
2322#[ derive( Debug ) ]
2423pub struct ContextProvider < T : Clone + PartialEq + ' static > {
2524 context : T ,
25+ children : Children ,
2626 consumers : RefCell < Slab < Callback < T > > > ,
2727}
2828
@@ -81,22 +81,32 @@ impl<T: Clone + PartialEq + 'static> Component for ContextProvider<T> {
8181 type Properties = ContextProviderProps < T > ;
8282
8383 fn create ( ctx : & Context < Self > ) -> Self {
84+ let props = ctx. props ( ) ;
8485 Self {
85- context : ctx. props ( ) . context . clone ( ) ,
86+ children : props. children . clone ( ) ,
87+ context : props. context . clone ( ) ,
8688 consumers : RefCell :: new ( Slab :: new ( ) ) ,
8789 }
8890 }
8991
90- fn update ( & mut self , _ctx : & Context < Self > , _msg : Self :: Message ) -> ShouldRender {
91- true
92- }
92+ fn changed ( & mut self , ctx : & Context < Self > ) -> bool {
93+ let props = ctx. props ( ) ;
94+ let should_render = if self . children == props. children {
95+ false
96+ } else {
97+ self . children = props. children . clone ( ) ;
98+ true
99+ } ;
100+
101+ if self . context != props. context {
102+ self . context = props. context . clone ( ) ;
103+ self . notify_consumers ( ) ;
104+ }
93105
94- fn changed ( & mut self , _ctx : & Context < Self > ) -> bool {
95- self . notify_consumers ( ) ;
96- true
106+ should_render
97107 }
98108
99- fn view ( & self , ctx : & Context < Self > ) -> Html {
100- html ! { <>{ ctx . props ( ) . children. clone( ) } </> }
109+ fn view ( & self , _ctx : & Context < Self > ) -> Html {
110+ html ! { <>{ self . children. clone( ) } </> }
101111 }
102112}
0 commit comments