diff options
author | Aaron O'Mullan <aaron.omullan@gmail.com> | 2021-04-28 18:41:50 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-28 18:41:50 +0200 |
commit | 0260b488fbba9a43c64641428d3603b8761067a4 (patch) | |
tree | 66ce487f9241a3b91942dd048c7e43cb192bf9e8 /op_crates/timers/lib.rs | |
parent | b28f9445aae85dbf86033300cfcb55e404529a23 (diff) |
core: introduce extensions (#9800)
Extensions allow declarative extensions to "JsRuntime" (ops, state, JS or middleware).
This allows for:
- `op_crates` to be plug-and-play & self-contained, reducing complexity leaked to consumers
- op middleware (like metrics_op) to be opt-in and for new middleware (unstable, tracing,...)
- `MainWorker` and `WebWorker` to be composable, allowing users to extend workers with their ops whilst benefiting from the other infrastructure (inspector, etc...)
In short extensions improve deno's modularity, reducing complexity and leaky abstractions for embedders and the internal codebase.
Diffstat (limited to 'op_crates/timers/lib.rs')
-rw-r--r-- | op_crates/timers/lib.rs | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/op_crates/timers/lib.rs b/op_crates/timers/lib.rs index 047b6923c..6359b20f0 100644 --- a/op_crates/timers/lib.rs +++ b/op_crates/timers/lib.rs @@ -13,6 +13,9 @@ use deno_core::futures; use deno_core::futures::channel::oneshot; use deno_core::futures::FutureExt; use deno_core::futures::TryFutureExt; +use deno_core::op_async; +use deno_core::op_sync; +use deno_core::Extension; use deno_core::OpState; use deno_core::ZeroCopyBuf; use std::cell::RefCell; @@ -28,12 +31,34 @@ pub trait TimersPermission { fn check_unstable(&self, state: &OpState, api_name: &'static str); } -pub fn init(rt: &mut deno_core::JsRuntime) { - rt.execute( - "deno:op_crates/timers/01_timers.js", - include_str!("01_timers.js"), +pub struct NoTimersPermission; + +impl TimersPermission for NoTimersPermission { + fn allow_hrtime(&mut self) -> bool { + false + } + fn check_unstable(&self, _: &OpState, _: &'static str) {} +} + +pub fn init<P: TimersPermission + 'static>() -> Extension { + Extension::with_ops( + vec![( + "deno:op_crates/timers/01_timers.js", + include_str!("01_timers.js"), + )], + vec![ + ("op_global_timer_stop", op_sync(op_global_timer_stop)), + ("op_global_timer_start", op_sync(op_global_timer_start)), + ("op_global_timer", op_async(op_global_timer)), + ("op_now", op_sync(op_now::<P>)), + ("op_sleep_sync", op_sync(op_sleep_sync::<P>)), + ], + Some(Box::new(|state| { + state.put(GlobalTimer::default()); + state.put(StartTime::now()); + Ok(()) + })), ) - .unwrap(); } pub type StartTime = Instant; |