diff options
author | Aaron O'Mullan <aaron.omullan@gmail.com> | 2021-08-31 13:08:16 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-31 13:08:16 +0200 |
commit | b518f5e1ba4c3281d4aee4c637f410db382ce751 (patch) | |
tree | a10f196368db50473a99a50822fb4d8513468734 /core/ops_json.rs | |
parent | cee5be45394c77863dacbf8f1d0f0b90188d2cca (diff) |
feat(core): facilitate op-disabling middleware (#11858)
* feat(core): facilitate op-disabling middleware
By providing `void_op_sync()` and `void_op_async() as well as `core/examples/disable_ops.rs`
Diffstat (limited to 'core/ops_json.rs')
-rw-r--r-- | core/ops_json.rs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/core/ops_json.rs b/core/ops_json.rs index 25752322a..22a84154d 100644 --- a/core/ops_json.rs +++ b/core/ops_json.rs @@ -11,6 +11,33 @@ use std::cell::RefCell; use std::future::Future; use std::rc::Rc; +/// A helper function that returns a sync NOP OpFn +/// +/// It's mainly intended for embedders who want to disable ops, see ./examples/disable_ops.rs +pub fn void_op_sync() -> Box<OpFn> { + // TODO(@AaronO): use this simpler implementation after changing serde_v8 to allow all values + // to deserialize to the unit type instead of failing with `ExpectedNull` + // op_sync(|_, _: (), _: ()| Ok(())) + Box::new(move |state, _| -> Op { + let op_result = serialize_op_result(Ok(()), state); + Op::Sync(op_result) + }) +} + +/// A helper function that returns an async NOP OpFn +/// +/// It's mainly intended for embedders who want to disable ops, see ./examples/disable_ops.rs +pub fn void_op_async() -> Box<OpFn> { + // TODO(@AaronO): use this simpler implementation after changing serde_v8 to allow all values + // to deserialize to the unit type instead of failing with `ExpectedNull` + // op_async(|_, _: (), _: ()| futures::future::ok(())) + Box::new(move |state, payload| -> Op { + let pid = payload.promise_id; + let op_result = serialize_op_result(Ok(()), state); + Op::Async(Box::pin(futures::future::ready((pid, op_result)))) + }) +} + /// Creates an op that passes data synchronously using JSON. /// /// The provided function `op_fn` has the following parameters: |