Replies: 3 comments
-
Please see the example introduced by #371 |
Beta Was this translation helpful? Give feedback.
-
This method does not work for me. I can post a web_sys::File just fine, however when I change web_sys::File to a web_sys::OffscreenCanvas, post_message() fails and throws the following message: "failed to post message". I think this might because post_message_with_transfer() needs to be called inside of gloo instead of post_message() to correctly transfer Transferable Objects, but I haven't proved it. Testing was done on the latest version of Google Chrome. The stack trace of the failure looks as follows:
I've created an minimal example which is based on #371. lib.rs: use gloo_worker::Spawnable;
use gloo_worker::{HandlerId, Worker, WorkerScope};
use serde::{Deserialize, Serialize};
use wasm_bindgen::UnwrapThrowExt;
use web_sys::HtmlCanvasElement;
use yew::prelude::*;
use crate::codec::TransferrableCodec;
pub mod codec;
#[derive(Serialize, Deserialize)]
pub struct CanvasInput {
#[serde(with = "serde_wasm_bindgen::preserve")]
pub canvas: web_sys::OffscreenCanvas,
}
pub struct CanvasWorker {}
impl Worker for CanvasWorker {
type Input = CanvasInput;
type Output = ();
type Message = ();
fn create(_scope: &WorkerScope<Self>) -> Self {
Self {}
}
fn update(&mut self, _scope: &WorkerScope<Self>, _msg: Self::Message) {}
fn received(&mut self, _scope: &WorkerScope<Self>, _msg: Self::Input, _id: HandlerId) {}
}
#[function_component]
pub fn App() -> Html {
let worker = {
use_memo(
move |_| {
CanvasWorker::spawner()
.encoding::<TransferrableCodec>()
.spawn_with_loader("/example_worker_loader.js")
},
(),
)
};
let canvas_ref = use_node_ref();
let onclick = {
let canvas_ref = canvas_ref.clone();
Callback::from(move |_| {
let input = CanvasInput {
canvas: canvas_ref
.cast::<HtmlCanvasElement>()
.unwrap_throw()
.transfer_control_to_offscreen()
.unwrap_throw(),
};
worker.send(input);
})
};
html! {
<div>
<h1>{"To calculate file hash, please select a file below:"}</h1>
<div><canvas ref={canvas_ref} /></div>
<div><button {onclick} >{"Send Canvas"}</button></div>
</div>
}
} |
Beta Was this translation helpful? Give feedback.
-
I have also encountered the same issue, probably needing to implement a |
Beta Was this translation helpful? Give feedback.
-
Is it possible to transfer canvas from main thread to worker using WorkerBridge?
Beta Was this translation helpful? Give feedback.
All reactions