Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gloo-timers: don't consume callback in Interval closure #57

Merged
merged 2 commits into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions crates/timers/src/callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,7 @@ impl Interval {
where
F: 'static + FnMut(),
{
let mut callback = Some(callback);
let closure = Closure::wrap(Box::new(move || {
let mut callback = callback.take().unwrap_throw();
callback();
}) as Box<FnMut()>);
let closure = Closure::wrap(Box::new(callback) as Box<FnMut()>);

let id = window()
.set_interval_with_callback_and_timeout_and_arguments_0(
Expand Down
50 changes: 27 additions & 23 deletions crates/timers/tests/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,53 +91,57 @@ fn timeout_future_cancel() -> impl Future<Item = (), Error = wasm_bindgen::JsVal
#[wasm_bindgen_test(async)]
fn interval() -> impl Future<Item = (), Error = wasm_bindgen::JsValue> {
let (mut sender, receiver) = mpsc::channel(1);
Interval::new(1, move || sender.try_send(()).unwrap()).forget();
let i = Interval::new(1, move || {
if !sender.is_closed() {
sender.try_send(()).unwrap()
}
});

receiver
.take(5)
.map_err(|_| "impossible".into())
.for_each(|_| Ok(()))
.collect()
.and_then(|results| {
drop(i);
assert_eq!(results.len(), 5);
Ok(())
})
}

#[wasm_bindgen_test(async)]
fn interval_cancel() -> impl Future<Item = (), Error = wasm_bindgen::JsValue> {
let cell = Rc::new(Cell::new(false));
let (mut i_sender, i_receiver) = mpsc::channel(1);

let i = Interval::new(1, {
let cell = cell.clone();
move || {
cell.set(true);
panic!("should have been cancelled");
}
});
let i = Interval::new(1, move || i_sender.try_send(()).unwrap());
i.cancel();

// This keeps us live for long enough that if any erroneous Interval callbacks fired, we'll have seen them.
let (mut sender, receiver) = mpsc::channel(1);
Interval::new(2, move || {
Timeout::new(50, move || {
sender.try_send(()).unwrap();
assert_eq!(cell.get(), false);
})
.forget();

receiver
.merge(i_receiver)
.take(2)
.map_err(|_| "impossible".into())
.for_each(|_| Ok(()))
.collect()
.and_then(|results| {
// Should only be 1 item - and that's from the timeout. Anything more means interval spuriously fired.
assert_eq!(results.len(), 1);
Ok(())
})
}

#[wasm_bindgen_test(async)]
fn interval_stream() -> impl Future<Item = (), Error = wasm_bindgen::JsValue> {
let cell = Rc::new(Cell::new(0));
IntervalStream::new(1)
.take(5)
.map_err(|_| "impossible".into())
.for_each({
let cell = cell.clone();
move |_| {
cell.set(cell.get() + 1);
Ok(())
}
})
.and_then(move |_| {
assert_eq!(cell.get(), 5);
.collect()
.and_then(|results| {
assert_eq!(results.len(), 5);
Ok(())
})
}