-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move async implementation into macro_internal
- Loading branch information
1 parent
7d97a58
commit a0fe39e
Showing
3 changed files
with
37 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use std::future::Future; | ||
|
||
use crate::{ | ||
context::{Context, TaskContext}, | ||
result::JsResult, | ||
types::JsValue, | ||
}; | ||
|
||
pub fn spawn<'cx, C, F, S>(cx: &mut C, fut: F, settle: S) -> JsResult<'cx, JsValue> | ||
where | ||
C: Context<'cx>, | ||
F: Future + Send + 'static, | ||
F::Output: Send, | ||
S: FnOnce(TaskContext, F::Output) -> JsResult<JsValue> + Send + 'static, | ||
{ | ||
let rt = match crate::RUNTIME.get(cx) { | ||
Some(rt) => rt, | ||
None => return cx.throw_error("neon::RUNTIME is not initialized"), | ||
}; | ||
|
||
let ch = cx.channel(); | ||
let (d, promise) = cx.promise(); | ||
|
||
rt.spawn(Box::pin(async move { | ||
let res = fut.await; | ||
let _ = d.try_settle_with(&ch, move |cx| settle(cx, res)); | ||
})); | ||
|
||
Ok(promise.upcast()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters