Skip to content

Commit

Permalink
fix(core): 🐛 fix mismatch of providers
Browse files Browse the repository at this point in the history
  • Loading branch information
wjian23 committed Dec 13, 2024
1 parent 304debe commit 75df512
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ Please only add new entries below the [Unreleased](#unreleased---releasedate) he

## [@Unreleased] - @ReleaseDate

### Fixed
- **core**: fix mismatch of providers (#pr @wjian23)

## [0.4.0-alpha.18] - 2024-12-11

### Features
Expand Down
50 changes: 44 additions & 6 deletions core/src/builtin_widgets/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,17 @@ impl<'c> ComposeChild<'c> for Provider {
})
.provider;

fn_widget! {
// We push the provider into the build context to ensure that the widget build
// logic can access this provider.
let ctx = BuildCtx::get_mut();
ctx.current_providers.push(provider);
child.into_widget().on_build(|id| {
let provider = ctx.current_providers.pop().unwrap();
id.attach_data(provider, ctx.tree_mut());
})
let ctx = BuildCtx::get_mut();
ctx.current_providers.push(provider);
child.into_widget().on_build(move |id| {
let provider = ctx.current_providers.pop().unwrap();
id.attach_data(provider, ctx.tree_mut());
})
}
.into_widget()
}
}

Expand Down Expand Up @@ -260,6 +263,41 @@ mod tests {
assert_eq!(*value.read(), 1);
}

#[test]
fn with_multi_providers() {
reset_test_env!();

let (value1, w_value1) = split_value(0);
let (value2, w_value2) = split_value(0);
let w = fn_widget! {
let w_value1 = w_value1.clone_writer();
let w_value2 = w_value2.clone_writer();
@MockMulti {
@ {
Provider::new(Box::new(Queryable(1i32))).with_child(fn_widget! {
let v = Provider::of::<i32>(BuildCtx::get()).unwrap();
*$w_value1.write() = *v;
@ Void{}
})
}

@ {
Provider::new(Box::new(Queryable(2i32))).with_child(fn_widget! {
let v = Provider::of::<i32>(BuildCtx::get()).unwrap();
*$w_value2.write() = *v;
@ Void{}
})
}
}
};

let mut wnd = TestWindow::new(w);
wnd.draw_frame();

assert_eq!(*value1.read(), 1);
assert_eq!(*value2.read(), 2);
}

#[test]
fn provider_for_pipe() {
reset_test_env!();
Expand Down

0 comments on commit 75df512

Please sign in to comment.