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

Fix scheduler main queue delay (#1953) #1954

Merged
merged 2 commits into from
Jul 16, 2021
Merged
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
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()));
}
}