1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 15:24:46 -05:00

cleanup(core): simplify op_async(), drop need for try_dispatch_op() (#10240)

This commit is contained in:
Aaron O'Mullan 2021-04-18 15:29:06 +02:00 committed by GitHub
parent 9b5d8a2f2c
commit 5b31d0f846
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,7 +4,6 @@ use crate::error::AnyError;
use crate::serialize_op_result;
use crate::Op;
use crate::OpFn;
use crate::OpPayload;
use crate::OpState;
use crate::ZeroCopyBuf;
use serde::de::DeserializeOwned;
@ -81,33 +80,21 @@ where
R: Future<Output = Result<RV, AnyError>> + 'static,
RV: Serialize + 'static,
{
let try_dispatch_op = move |state: Rc<RefCell<OpState>>,
p: OpPayload,
buf: Option<ZeroCopyBuf>|
-> Result<Op, AnyError> {
let pid = p.promise_id;
// Parse args
let args = p.deserialize()?;
Box::new(move |state, payload, buf| -> Op {
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::<(), AnyError>(err), state))
}
};
use crate::futures::FutureExt;
let fut = op_fn(state.clone(), args, buf)
.map(move |result| (pid, serialize_op_result(result, state)));
Ok(Op::Async(Box::pin(fut)))
};
Box::new(
move |state: Rc<RefCell<OpState>>,
p: OpPayload,
b: Option<ZeroCopyBuf>|
-> Op {
match try_dispatch_op(state.clone(), p, b) {
Ok(op) => op,
Err(err) => {
Op::Sync(serialize_op_result(Err::<(), AnyError>(err), state))
}
}
},
)
Op::Async(Box::pin(fut))
})
}
#[cfg(test)]