diff options
author | Matt Mastracci <matthew@mastracci.com> | 2023-03-17 12:22:15 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-17 18:22:15 +0000 |
commit | e55b448730160a6e4df9815a268d4049ac89deab (patch) | |
tree | 35d80fd60f2f1d1d06903caff256484a7d703d76 /ext/broadcast_channel/lib.rs | |
parent | 0bc6bf5d33b8198253954d7f04558270de45c925 (diff) |
feat(core) deno_core::extension! macro to simplify extension registration (#18210)
This implements two macros to simplify extension registration and centralize a lot of the boilerplate as a base for future improvements:
* `deno_core::ops!` registers a block of `#[op]`s, optionally with type
parameters, useful for places where we share lists of ops
* `deno_core::extension!` is used to register an extension, and creates
two methods that can be used at runtime/snapshot generation time:
`init_ops` and `init_ops_and_esm`.
---------
Co-authored-by: Bartek IwaĆczuk <biwanczuk@gmail.com>
Diffstat (limited to 'ext/broadcast_channel/lib.rs')
-rw-r--r-- | ext/broadcast_channel/lib.rs | 62 |
1 files changed, 19 insertions, 43 deletions
diff --git a/ext/broadcast_channel/lib.rs b/ext/broadcast_channel/lib.rs index 14884e99c..3df11566f 100644 --- a/ext/broadcast_channel/lib.rs +++ b/ext/broadcast_channel/lib.rs @@ -2,7 +2,6 @@ mod in_memory_broadcast_channel; -use deno_core::ExtensionBuilder; pub use in_memory_broadcast_channel::InMemoryBroadcastChannel; pub use in_memory_broadcast_channel::InMemoryBroadcastChannelResource; @@ -12,9 +11,7 @@ use std::rc::Rc; use async_trait::async_trait; use deno_core::error::AnyError; -use deno_core::include_js_files; use deno_core::op; -use deno_core::Extension; use deno_core::OpState; use deno_core::Resource; use deno_core::ResourceId; @@ -107,46 +104,25 @@ where bc.recv(&resource).await } -fn ext() -> ExtensionBuilder { - Extension::builder_with_deps( - env!("CARGO_PKG_NAME"), - &["deno_webidl", "deno_web"], - ) -} - -fn ops<BC: BroadcastChannel + 'static>( - ext: &mut ExtensionBuilder, - bc: BC, - unstable: bool, -) -> &mut ExtensionBuilder { - ext - .ops(vec![ - op_broadcast_subscribe::decl::<BC>(), - op_broadcast_unsubscribe::decl::<BC>(), - op_broadcast_send::decl::<BC>(), - op_broadcast_recv::decl::<BC>(), - ]) - .state(move |state| { - state.put(bc.clone()); - state.put(Unstable(unstable)); - }) -} - -pub fn init_ops_and_esm<BC: BroadcastChannel + 'static>( - bc: BC, - unstable: bool, -) -> Extension { - ops::<BC>(&mut ext(), bc, unstable) - .esm(include_js_files!("01_broadcast_channel.js",)) - .build() -} - -pub fn init_ops<BC: BroadcastChannel + 'static>( - bc: BC, - unstable: bool, -) -> Extension { - ops::<BC>(&mut ext(), bc, unstable).build() -} +deno_core::extension!(deno_broadcast_channel, + deps = [ deno_webidl, deno_web ], + parameters = [BC: BroadcastChannel], + ops = [ + op_broadcast_subscribe<BC>, + op_broadcast_unsubscribe<BC>, + op_broadcast_send<BC>, + op_broadcast_recv<BC>, + ], + esm = [ "01_broadcast_channel.js" ], + config = { + bc: BC, + unstable: bool, + }, + state = |state, bc, unstable| { + state.put(bc); + state.put(Unstable(unstable)); + }, +); pub fn get_declaration() -> PathBuf { PathBuf::from(env!("CARGO_MANIFEST_DIR")) |