diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2021-11-25 19:49:09 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-25 19:49:09 +0100 |
commit | f3c0f0565bbf43b4cc31979b05e729d4f4a1538f (patch) | |
tree | 6554dfdecd89e076166531b91824d6750d5b76e0 /core/ops_json.rs | |
parent | 2853e3760471560c1812b46007e1a525966b1365 (diff) |
feat(core): Add ability to "ref" and "unref" pending ops (#12889)
This commit adds an ability to "ref" or "unref" pending ops.
Up to this point Deno had a notion of "async ops" and "unref async ops";
the former keep event loop alive, while the latter do not block event loop
from finishing. It was not possible to change between op types after
dispatching, one had to decide which type to use before dispatch.
Instead of storing ops in two separate "FuturesUnordered" collections,
now ops are stored in a single collection, with supplemental "HashSet"
storing ids of promises that were "unrefed".
Two APIs were added to "Deno.core":
"Deno.core.refOp(promiseId)" which allows to mark promise id
to be "refed" and keep event loop alive (the default behavior)
"Deno.core.unrefOp(promiseId)" which allows to mark promise
id as "unrefed" which won't block event loop from exiting
Diffstat (limited to 'core/ops_json.rs')
-rw-r--r-- | core/ops_json.rs | 33 |
1 files changed, 0 insertions, 33 deletions
diff --git a/core/ops_json.rs b/core/ops_json.rs index b3153763e..ad4aeeb47 100644 --- a/core/ops_json.rs +++ b/core/ops_json.rs @@ -67,7 +67,6 @@ where /// Creates an op that passes data asynchronously using JSON. /// /// When this op is dispatched, the runtime doesn't exit while processing it. -/// Use op_async_unref instead if you want to make the runtime exit while processing it. /// /// The provided function `op_fn` has the following parameters: /// * `Rc<RefCell<OpState>`: the op state, can be used to read/write resources in the runtime from an op. @@ -118,38 +117,6 @@ where }) } -/// Creates an op that passes data asynchronously using JSON. -/// -/// When this op is dispatched, the runtime still can exit while processing it. -/// -/// The other usages are the same as `op_async`. -pub fn op_async_unref<F, A, B, R, RV>(op_fn: F) -> Box<OpFn> -where - F: Fn(Rc<RefCell<OpState>>, A, B) -> R + 'static, - A: DeserializeOwned, - B: DeserializeOwned, - R: Future<Output = Result<RV, Error>> + 'static, - RV: Serialize + 'static, -{ - Box::new(move |state, payload| -> Op { - let op_id = payload.op_id; - let pid = payload.promise_id; - // Deserialize args, sync error on failure - let args = match payload.deserialize() { - Ok(args) => args, - Err(err) => { - return Op::Sync(serialize_op_result(Err::<(), Error>(err), state)) - } - }; - let (a, b) = args; - - use crate::futures::FutureExt; - let fut = op_fn(state.clone(), a, b) - .map(move |result| (pid, op_id, serialize_op_result(result, state))); - Op::AsyncUnref(OpCall::eager(fut)) - }) -} - #[cfg(test)] mod tests { use super::*; |