Skip to content

Commit

Permalink
fix context
Browse files Browse the repository at this point in the history
  • Loading branch information
ranile committed Aug 12, 2021
1 parent 712636b commit 5bf16f5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
32 changes: 21 additions & 11 deletions packages/yew/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! This module defines the `ContextProvider` component.

use crate::html::Context;
use crate::html::Scope;
use crate::{html, Callback, Children, Component, Html, Properties, ShouldRender};
use crate::{html, Callback, Children, Component, Context, Html, Properties};
use slab::Slab;
use std::cell::RefCell;

Expand All @@ -23,6 +22,7 @@ pub struct ContextProviderProps<T: Clone + PartialEq> {
#[derive(Debug)]
pub struct ContextProvider<T: Clone + PartialEq + 'static> {
context: T,
children: Children,
consumers: RefCell<Slab<Callback<T>>>,
}

Expand Down Expand Up @@ -81,22 +81,32 @@ impl<T: Clone + PartialEq + 'static> Component for ContextProvider<T> {
type Properties = ContextProviderProps<T>;

fn create(ctx: &Context<Self>) -> Self {
let props = ctx.props();
Self {
context: ctx.props().context.clone(),
children: props.children.clone(),
context: props.context.clone(),
consumers: RefCell::new(Slab::new()),
}
}

fn update(&mut self, _ctx: &Context<Self>, _msg: Self::Message) -> ShouldRender {
true
}
fn changed(&mut self, ctx: &Context<Self>) -> bool {
let props = ctx.props();
let should_render = if self.children == props.children {
false
} else {
self.children = props.children.clone();
true
};

if self.context != props.context {
self.context = props.context.clone();
self.notify_consumers();
}

fn changed(&mut self, _ctx: &Context<Self>) -> bool {
self.notify_consumers();
true
should_render
}

fn view(&self, ctx: &Context<Self>) -> Html {
html! { <>{ ctx.props().children.clone() }</> }
fn view(&self, _ctx: &Context<Self>) -> Html {
html! { <>{ self.children.clone() }</> }
}
}
17 changes: 8 additions & 9 deletions packages/yew/tests/use_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,15 +283,14 @@ fn use_context_update_works() {
assert_eq!(obtain_result_by_id("test-0"), "total: 4");

// 1 initial + 2 context update
// TODO Fix these
// assert_eq!(
// obtain_result_by_id("test-1"),
// "current: hello world!, total: 3"
// );
assert_eq!(
obtain_result_by_id("test-1"),
"current: hello world!, total: 3"
);

// 1 initial + 1 context update + 1 magic update + 1 context update
// assert_eq!(
// obtain_result_by_id("test-2"),
// "current: hello world!, total: 4"
// );
assert_eq!(
obtain_result_by_id("test-2"),
"current: hello world!, total: 4"
);
}

0 comments on commit 5bf16f5

Please sign in to comment.