From b4e42953e1d243f2eda20e5be6b845d60b7bf688 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Mon, 14 Mar 2022 23:14:15 +0530 Subject: feat(core): codegen ops (#13861) Co-authored-by: Aaron O'Mullan --- ext/broadcast_channel/lib.rs | 49 ++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 20 deletions(-) (limited to 'ext/broadcast_channel') diff --git a/ext/broadcast_channel/lib.rs b/ext/broadcast_channel/lib.rs index 91ee67674..eff57c7df 100644 --- a/ext/broadcast_channel/lib.rs +++ b/ext/broadcast_channel/lib.rs @@ -8,8 +8,7 @@ pub use in_memory_broadcast_channel::InMemoryBroadcastChannelResource; use async_trait::async_trait; use deno_core::error::AnyError; use deno_core::include_js_files; -use deno_core::op_async; -use deno_core::op_sync; +use deno_core::op; use deno_core::Extension; use deno_core::OpState; use deno_core::Resource; @@ -43,11 +42,15 @@ pub type Message = (String, Vec); struct Unstable(bool); // --unstable -pub fn op_broadcast_subscribe( +#[op] +pub fn op_broadcast_subscribe( state: &mut OpState, _: (), _: (), -) -> Result { +) -> Result +where + BC: BroadcastChannel + 'static, +{ let unstable = state.borrow::().0; if !unstable { @@ -62,31 +65,43 @@ pub fn op_broadcast_subscribe( Ok(state.resource_table.add(resource)) } -pub fn op_broadcast_unsubscribe( +#[op] +pub fn op_broadcast_unsubscribe( state: &mut OpState, rid: ResourceId, _buf: (), -) -> Result<(), AnyError> { +) -> Result<(), AnyError> +where + BC: BroadcastChannel + 'static, +{ let resource = state.resource_table.get::(rid)?; let bc = state.borrow::(); bc.unsubscribe(&resource) } -pub async fn op_broadcast_send( +#[op] +pub async fn op_broadcast_send( state: Rc>, (rid, name): (ResourceId, String), buf: ZeroCopyBuf, -) -> Result<(), AnyError> { +) -> Result<(), AnyError> +where + BC: BroadcastChannel + 'static, +{ let resource = state.borrow().resource_table.get::(rid)?; let bc = state.borrow().borrow::().clone(); bc.send(&resource, name, buf.to_vec()).await } -pub async fn op_broadcast_recv( +#[op] +pub async fn op_broadcast_recv( state: Rc>, rid: ResourceId, _: (), -) -> Result, AnyError> { +) -> Result, AnyError> +where + BC: BroadcastChannel + 'static, +{ let resource = state.borrow().resource_table.get::(rid)?; let bc = state.borrow().borrow::().clone(); bc.recv(&resource).await @@ -102,16 +117,10 @@ pub fn init( "01_broadcast_channel.js", )) .ops(vec![ - ( - "op_broadcast_subscribe", - op_sync(op_broadcast_subscribe::), - ), - ( - "op_broadcast_unsubscribe", - op_sync(op_broadcast_unsubscribe::), - ), - ("op_broadcast_send", op_async(op_broadcast_send::)), - ("op_broadcast_recv", op_async(op_broadcast_recv::)), + op_broadcast_subscribe::decl::(), + op_broadcast_unsubscribe::decl::(), + op_broadcast_send::decl::(), + op_broadcast_recv::decl::(), ]) .state(move |state| { state.put(bc.clone()); -- cgit v1.2.3