Skip to content

Commit

Permalink
Fix scheduler main queue delay (yewstack#1953) (yewstack#1954)
Browse files Browse the repository at this point in the history
* Fix scheduler main queue delay (yewstack#1953)

Fixes an issue where a runnable added from outside the component
lifecyle (i.e. agent bridge message from inside a callback) would not be
executed until the next component lifecycle event.

* Add test for `scheduler::run`

Ensures `scheduler::run` executes runnables immediately.
  • Loading branch information
Noah Corona authored Jul 16, 2021
1 parent 94b4752 commit 91ab14b
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions packages/yew/src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ fn with(f: impl FnOnce(&mut Scheduler)) {
#[inline]
pub(crate) fn push(runnable: Box<dyn Runnable>) {
with(|s| s.main.push_back(runnable));
// Execute pending immediately. Necessary for runnables added outside the component lifecycle,
// which would otherwise be delayed.
start();
}

/// Push a component creation Runnable to be executed
Expand Down Expand Up @@ -113,3 +116,27 @@ impl Scheduler {
.or_else(|| self.main.pop_front())
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn push_executes_runnables_immediately() {
use std::cell::Cell;

thread_local! {
static FLAG: Cell<bool> = Default::default();
}

struct Test;
impl Runnable for Test {
fn run(self: Box<Self>) {
FLAG.with(|v| v.set(true));
}
}

push(Box::new(Test));
FLAG.with(|v| assert!(v.get()));
}
}

0 comments on commit 91ab14b

Please sign in to comment.