0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-10-29 08:58:01 -04:00
denoland-deno/ext/broadcast_channel/lib.rs
Bartek Iwańczuk 8f207c0f3f
refactor: Split extension registration for runtime and snapshotting (#18095)
This commit splits "<ext_name>::init" functions into "init_ops" and
"init_ops_and_esm". That way we don't have to construct list of
ESM sources on each startup if we're running with a snapshot.

In a follow up commit "deno_core" will be changed to not have a split
between "extensions" and "extensions_with_js" - it will be embedders'
responsibility to pass appropriately configured extensions.

Prerequisite for https://github.com/denoland/deno/pull/18080
2023-03-09 14:56:19 +00:00

154 lines
3.5 KiB
Rust

// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
mod in_memory_broadcast_channel;
use deno_core::ExtensionBuilder;
pub use in_memory_broadcast_channel::InMemoryBroadcastChannel;
pub use in_memory_broadcast_channel::InMemoryBroadcastChannelResource;
use std::cell::RefCell;
use std::path::PathBuf;
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;
use deno_core::ZeroCopyBuf;
#[async_trait]
pub trait BroadcastChannel: Clone {
type Resource: Resource;
fn subscribe(&self) -> Result<Self::Resource, AnyError>;
fn unsubscribe(&self, resource: &Self::Resource) -> Result<(), AnyError>;
async fn send(
&self,
resource: &Self::Resource,
name: String,
data: Vec<u8>,
) -> Result<(), AnyError>;
async fn recv(
&self,
resource: &Self::Resource,
) -> Result<Option<Message>, AnyError>;
}
pub type Message = (String, Vec<u8>);
struct Unstable(bool); // --unstable
#[op]
pub fn op_broadcast_subscribe<BC>(
state: &mut OpState,
) -> Result<ResourceId, AnyError>
where
BC: BroadcastChannel + 'static,
{
let unstable = state.borrow::<Unstable>().0;
if !unstable {
eprintln!(
"Unstable API 'BroadcastChannel'. The --unstable flag must be provided.",
);
std::process::exit(70);
}
let bc = state.borrow::<BC>();
let resource = bc.subscribe()?;
Ok(state.resource_table.add(resource))
}
#[op]
pub fn op_broadcast_unsubscribe<BC>(
state: &mut OpState,
rid: ResourceId,
) -> Result<(), AnyError>
where
BC: BroadcastChannel + 'static,
{
let resource = state.resource_table.get::<BC::Resource>(rid)?;
let bc = state.borrow::<BC>();
bc.unsubscribe(&resource)
}
#[op]
pub async fn op_broadcast_send<BC>(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
name: String,
buf: ZeroCopyBuf,
) -> Result<(), AnyError>
where
BC: BroadcastChannel + 'static,
{
let resource = state.borrow().resource_table.get::<BC::Resource>(rid)?;
let bc = state.borrow().borrow::<BC>().clone();
bc.send(&resource, name, buf.to_vec()).await
}
#[op]
pub async fn op_broadcast_recv<BC>(
state: Rc<RefCell<OpState>>,
rid: ResourceId,
) -> Result<Option<Message>, AnyError>
where
BC: BroadcastChannel + 'static,
{
let resource = state.borrow().resource_table.get::<BC::Resource>(rid)?;
let bc = state.borrow().borrow::<BC>().clone();
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()
}
pub fn get_declaration() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("lib.deno_broadcast_channel.d.ts")
}