Skip to content

Commit

Permalink
Formatting changes and deps
Browse files Browse the repository at this point in the history
  • Loading branch information
rscarson committed Nov 4, 2024
1 parent 7a48e82 commit ac3d9d5
Show file tree
Hide file tree
Showing 8 changed files with 324 additions and 298 deletions.
34 changes: 17 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ worker = []

[dependencies]
thiserror = "1.0.65"
serde = "1.0.210"
serde = "1.0.214"

# Used to generate identifiers for callbacks
paste = "1.0.15"
Expand All @@ -93,10 +93,10 @@ paste = "1.0.15"
deno_core = "0.314.2"

# For transpiling typescript
deno_ast = { version = "0.42.2", features = ["transpiling"]}
deno_ast = { version = "0.43.3", features = ["transpiling"]}

# Runtime for async tasks
tokio = "=1.36.0"
tokio = "=1.41.0"
tokio-util = "0.7.12"

# For URL imports
Expand Down
25 changes: 18 additions & 7 deletions examples/background_tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ use std::time::Duration;

use deno_core::PollEventLoopOptions;
///
/// This example demonstrates a use for the websockets extension.
/// It will open a connection to the echo server at wss://echo.websocket.org
/// Send a message 'ping', wait for a response, and then close the connection.
/// This example the use of async module loading, and the handing of ongoing
/// background tasks.
///
use rustyscript::{Error, Module, ModuleHandle, Runtime, RuntimeOptions};

fn main() -> Result<(), Error> {
let module = Module::new(
"test.js",
"
// A basic messaging queue
const messages = [];
export function nextMessage() {
if (messages.length === 0) {
Expand All @@ -20,12 +20,11 @@ fn main() -> Result<(), Error> {
return messages.shift();
}
const socket = new WebSocket('wss://echo.websocket.org');
export function sendMessage(text) {
socket.send(text);
}
const socket = new WebSocket('wss://echo.websocket.org');
socket.addEventListener('error', (e) => {
clearInterval(t);
throw(e);
Expand All @@ -50,6 +49,7 @@ fn main() -> Result<(), Error> {
socket.close();
// Clear the interval, ending the event loop
console.log('Closing socket');
clearInterval(t);
}, 15000);
});
Expand All @@ -65,17 +65,28 @@ fn main() -> Result<(), Error> {
",
);

let mut runtime = Runtime::new(RuntimeOptions::default())?;
// Whitelist the echo server for certificate errors
let mut options = RuntimeOptions::default();
options
.extension_options
.web
.whitelist_certificate_for("echo.websocket.org");

let mut runtime = Runtime::new(options)?;
let tokio_runtime = runtime.tokio_runtime();

// Load the module
// This will run the event loop until the module is fully loaded, or an error occurs
let module_handle = tokio_runtime.block_on(runtime.load_module_async(&module))?;

// Run the event loop until it reports that it has finished
while runtime.advance_event_loop(PollEventLoopOptions::default())? {
// Check for messages every 50ms
// Check for messages from the module
if let Some(msg) = check_for_messages(&mut runtime, &module_handle)? {
println!("Received message: {}", msg);
}

// Run the event loop for 50ms
runtime.block_on_event_loop(
PollEventLoopOptions::default(),
Some(Duration::from_millis(50)),
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ mod test {
let e = runtime.eval::<Undefined>("1+1;\n1 + x").unwrap_err().as_highlighted(ErrorFormattingOptions::default());
assert_eq!(e, concat!(
"At 2:4:\n",
"= Uncaught ReferenceError: x is not defined"
"= Uncaught (in promise) ReferenceError: x is not defined"
));

let module = Module::new("test.js", "1+1;\n1 + x");
Expand Down
Loading

0 comments on commit ac3d9d5

Please sign in to comment.