diff --git a/Cargo.lock b/Cargo.lock index 322a778..930f3cf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6115,7 +6115,7 @@ dependencies = [ [[package]] name = "rustyscript" -version = "0.12.2" +version = "0.12.3" dependencies = [ "async-trait", "base64-simd", diff --git a/Cargo.toml b/Cargo.toml index 0f18c21..0aea2a8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ authors = ["@rscarson"] description = "Effortless JS Integration for Rust" edition = "2021" license = "MIT OR Apache-2.0" -version = "0.12.2" +version = "0.12.3" repository = "https://github.com/rscarson/rustyscript" keywords = ["rust", "javascript", "deno", "runtime", "embedding"] diff --git a/src/ext/broadcast_channel/mod.rs b/src/ext/broadcast_channel/mod.rs index 7339405..b9e84f4 100644 --- a/src/ext/broadcast_channel/mod.rs +++ b/src/ext/broadcast_channel/mod.rs @@ -1,31 +1,79 @@ -use deno_broadcast_channel::InMemoryBroadcastChannel; -use deno_core::{extension, Extension}; - -use super::ExtensionTrait; - -mod wrapper; -pub use wrapper::BroadcastChannelWrapper; - -extension!( - init_broadcast_channel, - deps = [rustyscript], - esm_entry_point = "ext:init_broadcast_channel/init_broadcast_channel.js", - esm = [ dir "src/ext/broadcast_channel", "init_broadcast_channel.js" ], -); -impl ExtensionTrait<()> for init_broadcast_channel { - fn init((): ()) -> Extension { - init_broadcast_channel::init() - } -} -impl ExtensionTrait for deno_broadcast_channel::deno_broadcast_channel { - fn init(channel: InMemoryBroadcastChannel) -> Extension { - deno_broadcast_channel::deno_broadcast_channel::init(channel) - } -} - -pub fn extensions(channel: InMemoryBroadcastChannel, is_snapshot: bool) -> Vec { - vec![ - deno_broadcast_channel::deno_broadcast_channel::build(channel, is_snapshot), - init_broadcast_channel::build((), is_snapshot), - ] -} +use deno_broadcast_channel::InMemoryBroadcastChannel; +use deno_core::{extension, Extension}; + +use super::ExtensionTrait; + +mod wrapper; +pub use wrapper::BroadcastChannelWrapper; + +extension!( + init_broadcast_channel, + deps = [rustyscript], + esm_entry_point = "ext:init_broadcast_channel/init_broadcast_channel.js", + esm = [ dir "src/ext/broadcast_channel", "init_broadcast_channel.js" ], +); +impl ExtensionTrait<()> for init_broadcast_channel { + fn init((): ()) -> Extension { + init_broadcast_channel::init() + } +} +impl ExtensionTrait for deno_broadcast_channel::deno_broadcast_channel { + fn init(channel: InMemoryBroadcastChannel) -> Extension { + deno_broadcast_channel::deno_broadcast_channel::init(channel) + } +} + +pub fn extensions(channel: InMemoryBroadcastChannel, is_snapshot: bool) -> Vec { + vec![ + deno_broadcast_channel::deno_broadcast_channel::build(channel, is_snapshot), + init_broadcast_channel::build((), is_snapshot), + ] +} + +#[cfg(test)] +mod test { + use deno_core::PollEventLoopOptions; + + use crate::{module, BroadcastChannelWrapper, Module, Runtime, RuntimeOptions}; + + static TEST_MOD: Module = module!( + "test.js", + " + const channel = new BroadcastChannel('my_channel'); + channel.onmessage = (event) => { + channel.postMessage('Received: ' + event.data); + }; + " + ); + + #[test] + fn test_broadcast_channel() { + let options = RuntimeOptions::default(); + let channel = options.extension_options.broadcast_channel.clone(); + + let mut runtime = Runtime::new(options).unwrap(); + let tokio_runtime = runtime.tokio_runtime(); + + let channel = BroadcastChannelWrapper::new(&channel, "my_channel").unwrap(); + + tokio_runtime + .block_on(runtime.load_module_async(&TEST_MOD)) + .unwrap(); + + channel.send_sync(&mut runtime, "foo").unwrap(); + + runtime + .block_on_event_loop( + PollEventLoopOptions::default(), + Some(std::time::Duration::from_secs(1)), + ) + .unwrap(); + + let value = channel + .recv_sync::(&mut runtime, Some(std::time::Duration::from_secs(1))) + .unwrap() + .unwrap(); + + assert_eq!(value, "Received: foo"); + } +} diff --git a/src/inner_runtime.rs b/src/inner_runtime.rs index ac72d5c..0da2093 100644 --- a/src/inner_runtime.rs +++ b/src/inner_runtime.rs @@ -3,6 +3,7 @@ use std::{ path::{Path, PathBuf}, pin::Pin, rc::Rc, + sync::Arc, task::Poll, time::Duration, }; @@ -265,7 +266,7 @@ impl InnerRuntime { .rt_mut() .op_state() .borrow_mut() - .put(feature_checker); + .put(Arc::new(feature_checker)); // Add a callback to terminate the runtime if the max_heap_size limit is approached if options.max_heap_size.is_some() {