Skip to content

Commit

Permalink
feat: simplify rust future spawn
Browse files Browse the repository at this point in the history
  • Loading branch information
LeuisKen authored and andycall committed Feb 2, 2025
1 parent 0717ce1 commit 8f8b6e7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 31 deletions.
15 changes: 14 additions & 1 deletion bridge/rusty_webf_sys/src/webf_future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::future::Future;
use std::pin::Pin;
use std::rc::Rc;
use std::task::{Context, Poll, Waker};

use futures::task;
use crate::ExecutingContext;

type Task = Pin<Box<dyn Future<Output = ()>>>;

Expand Down Expand Up @@ -91,3 +91,16 @@ impl<T> Clone for WebFNativeFuture<T>
}
}
}

pub fn spawn<F>(context: ExecutingContext, future: F)
where
F: Future<Output = ()> + 'static,
{
let runtime = Rc::new(RefCell::new(FutureRuntime::new()));
runtime.borrow_mut().spawn(future);
let runtime_run_task_callback = Box::new(move || {
runtime.borrow_mut().run();
});
let exception_state = context.create_exception_state();
context.set_run_rust_future_tasks(runtime_run_task_callback, &exception_state).unwrap();
}
21 changes: 4 additions & 17 deletions integration_tests/rust_builder/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::cell::RefCell;
use std::ffi::c_void;
use std::rc::Rc;
use webf_sys::executing_context::ExecutingContextRustMethods;
use webf_sys::{initialize_webf_api, ExecutingContext, FutureRuntime, RustValue};
use webf_sys::{initialize_webf_api, ExecutingContext, RustValue};

pub mod async_storage;
pub mod navigator;
Expand All @@ -11,23 +9,12 @@ pub mod storage;
#[no_mangle]
pub extern "C" fn init_webf_test_app(handle: RustValue<ExecutingContextRustMethods>) -> *mut c_void {
let context: ExecutingContext = initialize_webf_api(handle);
let context_async = context.clone();

webf_test_utils::sync_runner::run_tests(context);
webf_test_utils::sync_runner::run_tests(context.clone());

let runtime = Rc::new(RefCell::new(FutureRuntime::new()));

let context_async_runtime = context_async.clone();
runtime.borrow_mut().spawn(async move {
webf_test_utils::async_runner::run_tests(context_async_runtime).await;
});

let runtime_run_task_callback = Box::new(move || {
runtime.borrow_mut().run();
webf_sys::webf_future::spawn(context.clone(), async move {
webf_test_utils::async_runner::run_tests(context.clone()).await;
});

let exception_state = context_async.create_exception_state();
context_async.set_run_rust_future_tasks(runtime_run_task_callback, &exception_state).unwrap();

std::ptr::null_mut()
}
15 changes: 2 additions & 13 deletions webf/example/rust_builder/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::cell::RefCell;
use std::ffi::c_void;
use std::rc::Rc;
use webf_sys::event::Event;
use webf_sys::executing_context::ExecutingContextRustMethods;
use webf_sys::webf_future::FutureRuntime;
use webf_sys::{initialize_webf_api, AddEventListenerOptions, EventTargetMethods, RustValue};
use webf_sys::element::Element;
use webf_sys::node::NodeMethods;
Expand All @@ -14,12 +11,9 @@ pub extern "C" fn init_webf_app(handle: RustValue<ExecutingContextRustMethods>)
println!("Context created");
let exception_state = context.create_exception_state();
let document = context.document();

let context2 = context.clone();

let runtime = Rc::new(RefCell::new(FutureRuntime::new()));

runtime.borrow_mut().spawn(async move {
webf_sys::webf_future::spawn(context.clone(), async move {
let context = context2.clone();
let exception_state = context.create_exception_state();
let async_storage_2 = context.async_storage();
Expand All @@ -39,12 +33,6 @@ pub extern "C" fn init_webf_app(handle: RustValue<ExecutingContextRustMethods>)
}
});

let runtime_run_task_callback = Box::new(move || {
runtime.borrow_mut().run();
});

context.set_run_rust_future_tasks(runtime_run_task_callback, &exception_state).unwrap();

let click_event = document.create_event("custom_click", &exception_state).unwrap();
document.dispatch_event(&click_event, &exception_state);

Expand Down Expand Up @@ -126,5 +114,6 @@ pub extern "C" fn init_webf_app(handle: RustValue<ExecutingContextRustMethods>)
event_cleaner_element.add_event_listener("click", event_cleaner_handler, &event_listener_options, &exception_state).unwrap();

document.body().append_child(&event_cleaner_element.as_node(), &exception_state).unwrap();

std::ptr::null_mut()
}

0 comments on commit 8f8b6e7

Please sign in to comment.