pub async fn new(
            canvas: web_sys::OffscreenCanvas,
        ) -> (Self, futures::channel::mpsc::UnboundedReceiver<WM>) {
            let mut options = web_sys::WorkerOptions::new();
            options.type_(web_sys::WorkerType::Module);
            let worker = Rc::new(RefCell::new(
                web_sys::Worker::new_with_options("./worker.js", &options).unwrap(),
            ));

            let (fs, fr) = futures::channel::oneshot::channel();
            let mut fs = Some(fs);

            let (ks, kr) = futures::channel::mpsc::unbounded();
            let _handle =
                gloo::events::EventListener::new(&worker.borrow(), "message", move |event| {
                    //log!("waaa");
                    let event = event.dyn_ref::<web_sys::MessageEvent>().unwrap_throw();
                    let data = event.data();

                    let data: js_sys::Array = data.dyn_into().unwrap_throw();
                    let m = data.get(0);
                    let k = data.get(1);

                    if !m.is_null() {
                        if let Some(s) = m.as_string() {
                            if s == "ready" {
                                if let Some(f) = fs.take() {
                                    f.send(()).unwrap_throw();
                                }
                            }
                        }
                    } else {
                        let a = k.into_serde().unwrap_throw();
                        ks.unbounded_send(a).unwrap_throw();
                    }
                });

            let _ = fr.await.unwrap_throw();

            let arr = js_sys::Array::new_with_length(1);
            arr.set(0, canvas.clone().into());

            let data = js_sys::Array::new();
            data.set(0, canvas.into());
            data.set(1, JsValue::null());

            worker
                .borrow()
                .post_message_with_transfer(&data, &arr)
                .unwrap_throw();

            (
                EngineMain {
                    worker,
                    _handle,
                    _p: PhantomData,
                },
                kr,
            )
        }
///
/// Create the engine. Blocks until the worker thread reports that
/// it is ready to receive the offscreen canvas.
///
Create the engine. Blocks until the worker thread reports that
it is ready to receive the offscreen canvas.
Create the engine . Blocks until the worker thread reports that it is ready to receive the offscreen canvas .
pub async fn new(
            canvas: web_sys::OffscreenCanvas,
        ) -> (Self, futures::channel::mpsc::UnboundedReceiver<WM>) {
            let mut options = web_sys::WorkerOptions::new();
            options.type_(web_sys::WorkerType::Module);
            let worker = Rc::new(RefCell::new(
                web_sys::Worker::new_with_options("./worker.js", &options).unwrap(),
            ));
            let (fs, fr) = futures::channel::oneshot::channel();
            let mut fs = Some(fs);
            let (ks, kr) = futures::channel::mpsc::unbounded();
            let _handle =
                gloo::events::EventListener::new(&worker.borrow(), "message", move |event| {
                    //log!("waaa");
                    let event = event.dyn_ref::<web_sys::MessageEvent>().unwrap_throw();
                    let data = event.data();
                    let data: js_sys::Array = data.dyn_into().unwrap_throw();
                    let m = data.get(0);
                    let k = data.get(1);
                    if !m.is_null() {
                        if let Some(s) = m.as_string() {
                            if s == "ready" {
                                if let Some(f) = fs.take() {
                                    f.send(()).unwrap_throw();
                                }
                            }
                        }
                    } else {
                        let a = k.into_serde().unwrap_throw();
                        ks.unbounded_send(a).unwrap_throw();
                    }
                });
            let _ = fr.await.unwrap_throw();
            let arr = js_sys::Array::new_with_length(1);
            arr.set(0, canvas.clone().into());
            let data = js_sys::Array::new();
            data.set(0, canvas.into());
            data.set(1, JsValue::null());
            worker
                .borrow()
                .post_message_with_transfer(&data, &arr)
                .unwrap_throw();
            (
                EngineMain {
                    worker,
                    _handle,
                    _p: PhantomData,
                },
                kr,
            )
        }
pub async fn new ( canvas : web_sys :: OffscreenCanvas , ) -> ( Self , futures :: channel :: mpsc :: UnboundedReceiver < WM > ) { let mut options = web_sys :: WorkerOptions :: new ( ) ; options . type_ ( web_sys :: WorkerType :: Module ) ; let worker = Rc :: new ( RefCell :: new ( web_sys :: Worker :: new_with_options ( " " , & options ) . unwrap ( ) , ) ) ; let ( fs , fr ) = futures :: channel :: oneshot :: channel ( ) ; let mut fs = Some ( fs ) ; let ( ks , kr ) = futures :: channel :: mpsc :: unbounded ( ) ; let _handle = gloo :: events :: EventListener :: new ( & worker . borrow ( ) , " " , move | event | { //log!("waaa"); let event = event . dyn_ref :: < web_sys :: MessageEvent > ( ) . unwrap_throw ( ) ; let data = event . data ( ) ; let data : js_sys :: Array = data . dyn_into ( ) . unwrap_throw ( ) ; let m = data . get ( 0 ) ; let k = data . get ( 1 ) ; if ! m . is_null ( ) { if let Some ( s ) = m . as_string ( ) { if s == " " { if let Some ( f ) = fs . take ( ) { f . send ( ( ) ) . unwrap_throw ( ) ; } } } } else { let a = k . into_serde ( ) . unwrap_throw ( ) ; ks . unbounded_send ( a ) . unwrap_throw ( ) ; } } ) ; let _ = fr . await . unwrap_throw ( ) ; let arr = js_sys :: Array :: new_with_length ( 1 ) ; arr . set ( 0 , canvas . clone ( ) . into ( ) ) ; let data = js_sys :: Array :: new ( ) ; data . set ( 0 , canvas . into ( ) ) ; data . set ( 1 , JsValue :: null ( ) ) ; worker . borrow ( ) . post_message_with_transfer ( & data , & arr ) . unwrap_throw ( ) ; ( EngineMain { worker , _handle , _p : PhantomData , } , kr , ) }
Create the engine.
Create the engine .
canvas
web_sys::OffscreenCanvas
canvas
web_sys::OffscreenCanvas