diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/examples/schedule_task.rs | 3 | ||||
-rw-r--r-- | core/extensions.rs | 10 | ||||
-rw-r--r-- | core/runtime.rs | 2 |
3 files changed, 8 insertions, 7 deletions
diff --git a/core/examples/schedule_task.rs b/core/examples/schedule_task.rs index 7812dcb49..bd4bcb028 100644 --- a/core/examples/schedule_task.rs +++ b/core/examples/schedule_task.rs @@ -20,7 +20,8 @@ type Task = Box<dyn FnOnce()>; fn main() { let my_ext = Extension::builder() .ops(vec![op_schedule_task::decl()]) - .event_loop_middleware(|state, cx| { + .event_loop_middleware(|state_rc, cx| { + let mut state = state_rc.borrow_mut(); let recv = state.borrow_mut::<mpsc::UnboundedReceiver<Task>>(); let mut ref_loop = false; while let Poll::Ready(Some(call)) = recv.poll_next_unpin(cx) { diff --git a/core/extensions.rs b/core/extensions.rs index 682987124..ce6957875 100644 --- a/core/extensions.rs +++ b/core/extensions.rs @@ -1,13 +1,13 @@ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. use crate::OpState; use anyhow::Error; -use std::task::Context; +use std::{cell::RefCell, rc::Rc, task::Context}; pub type SourcePair = (&'static str, &'static str); pub type OpFnRef = v8::FunctionCallback; pub type OpMiddlewareFn = dyn Fn(OpDecl) -> OpDecl; pub type OpStateFn = dyn Fn(&mut OpState) -> Result<(), Error>; -pub type OpEventLoopFn = dyn Fn(&mut OpState, &mut Context) -> bool; +pub type OpEventLoopFn = dyn Fn(Rc<RefCell<OpState>>, &mut Context) -> bool; #[derive(Clone, Copy)] pub struct OpDecl { @@ -90,13 +90,13 @@ impl Extension { pub fn run_event_loop_middleware( &self, - op_state: &mut OpState, + op_state_rc: Rc<RefCell<OpState>>, cx: &mut Context, ) -> bool { self .event_loop_middleware .as_ref() - .map(|f| f(op_state, cx)) + .map(|f| f(op_state_rc, cx)) .unwrap_or(false) } @@ -148,7 +148,7 @@ impl ExtensionBuilder { pub fn event_loop_middleware<F>(&mut self, middleware_fn: F) -> &mut Self where - F: Fn(&mut OpState, &mut Context) -> bool + 'static, + F: Fn(Rc<RefCell<OpState>>, &mut Context) -> bool + 'static, { self.event_loop_middleware = Some(Box::new(middleware_fn)); self diff --git a/core/runtime.rs b/core/runtime.rs index 7cb556fd3..23fe73013 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -921,7 +921,7 @@ impl JsRuntime { let state = state_rc.borrow(); let op_state = state.op_state.clone(); for f in &self.event_loop_middlewares { - if f(&mut op_state.borrow_mut(), cx) { + if f(op_state.clone(), cx) { maybe_scheduling = true; } } |