-
-
Couldn't load subscription status.
- Fork 618
signal-neon-futures: Poll a future once before queuing it #283
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,11 +20,7 @@ pub trait EventQueueEx { | |
| impl EventQueueEx for EventQueue { | ||
| fn send_future(&self, future: impl Future<Output = ()> + 'static + Send) { | ||
| self.send(move |mut cx| { | ||
| let task = Arc::new(FutureTask { | ||
| queue: cx.queue(), | ||
| future: Mutex::new(Some(Box::pin(future))), | ||
| }); | ||
| task.poll(); | ||
| cx.run_future(future); | ||
| Ok(()) | ||
| }) | ||
| } | ||
|
|
@@ -43,29 +39,44 @@ impl<T: Future> Future for AssertSendSafe<T> { | |
| } | ||
| } | ||
|
|
||
| /// Adds support for executing closures and futures on the JavaScript main thread's microtask queue. | ||
| /// Adds support for executing closures and futures on the JavaScript main thread's event queue. | ||
| pub trait ContextEx<'a>: Context<'a> { | ||
| /// Schedules `f` to run on the microtask queue. | ||
| /// Schedules `f` to run on the JavaScript thread's event queue. | ||
| /// | ||
| /// Equivalent to `cx.queue().send(f)` except that `f` doesn't need to be `Send`. | ||
| fn run_on_queue(&mut self, f: impl FnOnce(TaskContext<'_>) -> NeonResult<()> + 'static) { | ||
| fn queue_task(&mut self, f: impl FnOnce(TaskContext<'_>) -> NeonResult<()> + 'static) { | ||
| // Because we're currently in a JavaScript context, | ||
| // and `f` will run on the event queue associated with the current context, | ||
| // we can assert that it's safe to Send `f` to the queue. | ||
| let f = AssertSendSafe(f); | ||
| self.queue().send(move |cx| f.0(cx)); | ||
| } | ||
|
|
||
| /// Schedules `f` to run on the microtask queue. | ||
| /// Schedules `f` to run on the JavaScript thread's event queue. | ||
| /// | ||
| /// Equivalent to `cx.queue().send_future(f)` except that `f` doesn't need to be `Send`. | ||
| fn run_future_on_queue(&mut self, f: impl Future<Output = ()> + 'static) { | ||
| fn queue_future(&mut self, f: impl Future<Output = ()> + 'static) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No one calls There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right; they're there for "completeness" but I could just drop them. |
||
| // Because we're currently in a JavaScript context, | ||
| // and `f` will run on the event queue associated with the current context, | ||
| // we can assert that it's safe to Send `f` to the queue. | ||
| let f = AssertSendSafe(f); | ||
| self.queue().send_future(f); | ||
| } | ||
|
|
||
| /// Runs `f` on the JavaScript thread's event queue. | ||
| /// | ||
| /// Polls the future once synchronously, then schedules it to resume on the event queue. | ||
| fn run_future(&mut self, f: impl Future<Output = ()> + 'static) { | ||
| // Because we're currently in a JavaScript context, | ||
| // and `f` will run on the event queue associated with the current context, | ||
| // we can assert that it's safe to Send `f` to the queue. | ||
| let f = AssertSendSafe(f); | ||
| let task = Arc::new(FutureTask { | ||
| queue: self.queue(), | ||
| future: Mutex::new(Some(Box::pin(f))), | ||
| }); | ||
| task.poll(); | ||
| } | ||
| } | ||
|
|
||
| impl<'a, T: Context<'a>> ContextEx<'a> for T {} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was my mistake; we're using Node's event queue rather than JavaScript's microtask queue.