2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-05-22 12:08:24 -04:00
|
|
|
|
2021-05-22 12:08:24 -04:00
|
|
|
mod in_memory_broadcast_channel;
|
|
|
|
|
|
|
|
pub use in_memory_broadcast_channel::InMemoryBroadcastChannel;
|
2021-12-29 20:05:26 -05:00
|
|
|
pub use in_memory_broadcast_channel::InMemoryBroadcastChannelResource;
|
2021-05-22 12:08:24 -04:00
|
|
|
|
2023-01-14 23:06:46 -05:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
2021-05-22 12:08:24 -04:00
|
|
|
use async_trait::async_trait;
|
2021-05-22 12:08:24 -04:00
|
|
|
use deno_core::error::AnyError;
|
2023-09-19 22:39:27 -04:00
|
|
|
use deno_core::op2;
|
2023-06-22 17:37:56 -04:00
|
|
|
use deno_core::JsBuffer;
|
2021-05-22 12:08:24 -04:00
|
|
|
use deno_core::OpState;
|
|
|
|
use deno_core::Resource;
|
|
|
|
use deno_core::ResourceId;
|
|
|
|
|
2023-10-12 11:55:50 -04:00
|
|
|
pub const UNSTABLE_FEATURE_NAME: &str = "broadcast-channel";
|
|
|
|
|
2021-05-22 12:08:24 -04:00
|
|
|
#[async_trait]
|
|
|
|
pub trait BroadcastChannel: Clone {
|
|
|
|
type Resource: Resource;
|
2021-05-22 12:08:24 -04:00
|
|
|
|
2021-05-22 12:08:24 -04:00
|
|
|
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>;
|
2021-05-22 12:08:24 -04:00
|
|
|
}
|
|
|
|
|
2021-05-22 12:08:24 -04:00
|
|
|
pub type Message = (String, Vec<u8>);
|
|
|
|
|
2023-09-19 22:39:27 -04:00
|
|
|
#[op2(fast)]
|
|
|
|
#[smi]
|
2022-03-14 13:44:15 -04:00
|
|
|
pub fn op_broadcast_subscribe<BC>(
|
2021-05-22 12:08:24 -04:00
|
|
|
state: &mut OpState,
|
2022-03-14 13:44:15 -04:00
|
|
|
) -> Result<ResourceId, AnyError>
|
|
|
|
where
|
|
|
|
BC: BroadcastChannel + 'static,
|
|
|
|
{
|
2023-10-12 11:55:50 -04:00
|
|
|
// TODO(bartlomieju): replace with `state.feature_checker.check_or_exit`
|
|
|
|
// once we phase out `check_or_exit_with_legacy_fallback`
|
|
|
|
state.feature_checker.check_or_exit_with_legacy_fallback(
|
|
|
|
UNSTABLE_FEATURE_NAME,
|
|
|
|
"BroadcastChannel",
|
|
|
|
);
|
2021-05-22 12:08:24 -04:00
|
|
|
let bc = state.borrow::<BC>();
|
|
|
|
let resource = bc.subscribe()?;
|
|
|
|
Ok(state.resource_table.add(resource))
|
2021-05-22 12:08:24 -04:00
|
|
|
}
|
|
|
|
|
2023-09-19 22:39:27 -04:00
|
|
|
#[op2(fast)]
|
2022-03-14 13:44:15 -04:00
|
|
|
pub fn op_broadcast_unsubscribe<BC>(
|
2021-05-22 12:08:24 -04:00
|
|
|
state: &mut OpState,
|
2023-09-19 22:39:27 -04:00
|
|
|
#[smi] rid: ResourceId,
|
2022-03-14 13:44:15 -04:00
|
|
|
) -> Result<(), AnyError>
|
|
|
|
where
|
|
|
|
BC: BroadcastChannel + 'static,
|
|
|
|
{
|
2021-08-15 07:29:19 -04:00
|
|
|
let resource = state.resource_table.get::<BC::Resource>(rid)?;
|
2021-05-22 12:08:24 -04:00
|
|
|
let bc = state.borrow::<BC>();
|
|
|
|
bc.unsubscribe(&resource)
|
|
|
|
}
|
2021-05-22 12:08:24 -04:00
|
|
|
|
2023-09-19 22:39:27 -04:00
|
|
|
#[op2(async)]
|
2022-03-14 13:44:15 -04:00
|
|
|
pub async fn op_broadcast_send<BC>(
|
2021-05-22 12:08:24 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2023-09-19 22:39:27 -04:00
|
|
|
#[smi] rid: ResourceId,
|
|
|
|
#[string] name: String,
|
|
|
|
#[buffer] buf: JsBuffer,
|
2022-03-14 13:44:15 -04:00
|
|
|
) -> Result<(), AnyError>
|
|
|
|
where
|
|
|
|
BC: BroadcastChannel + 'static,
|
|
|
|
{
|
2021-08-15 07:29:19 -04:00
|
|
|
let resource = state.borrow().resource_table.get::<BC::Resource>(rid)?;
|
2021-05-22 12:08:24 -04:00
|
|
|
let bc = state.borrow().borrow::<BC>().clone();
|
|
|
|
bc.send(&resource, name, buf.to_vec()).await
|
2021-05-22 12:08:24 -04:00
|
|
|
}
|
|
|
|
|
2023-09-19 22:39:27 -04:00
|
|
|
#[op2(async)]
|
|
|
|
#[serde]
|
2022-03-14 13:44:15 -04:00
|
|
|
pub async fn op_broadcast_recv<BC>(
|
2021-05-22 12:08:24 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2023-09-19 22:39:27 -04:00
|
|
|
#[smi] rid: ResourceId,
|
2022-03-14 13:44:15 -04:00
|
|
|
) -> Result<Option<Message>, AnyError>
|
|
|
|
where
|
|
|
|
BC: BroadcastChannel + 'static,
|
|
|
|
{
|
2021-08-15 07:29:19 -04:00
|
|
|
let resource = state.borrow().resource_table.get::<BC::Resource>(rid)?;
|
2021-05-22 12:08:24 -04:00
|
|
|
let bc = state.borrow().borrow::<BC>().clone();
|
|
|
|
bc.recv(&resource).await
|
2021-05-22 12:08:24 -04:00
|
|
|
}
|
|
|
|
|
2023-03-17 14:22:15 -04:00
|
|
|
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" ],
|
2023-03-17 18:15:27 -04:00
|
|
|
options = {
|
2023-03-17 14:22:15 -04:00
|
|
|
bc: BC,
|
|
|
|
},
|
2023-03-17 18:15:27 -04:00
|
|
|
state = |state, options| {
|
|
|
|
state.put(options.bc);
|
2023-03-17 14:22:15 -04:00
|
|
|
},
|
|
|
|
);
|
2022-03-16 20:25:44 -04:00
|
|
|
|
|
|
|
pub fn get_declaration() -> PathBuf {
|
|
|
|
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.join("lib.deno_broadcast_channel.d.ts")
|
|
|
|
}
|