mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-29 09:46:19 +01:00
On the web, a plug-in being installed ran its start function and vc_manifest on the page, and a filter ran on the page for its whole budget, so a slow or looping module froze the tab for up to a minute (five for a heavy filter), and Cancel could not stop it. - A second wasm, apps/vectorcraft-plugin-worker, is built by Trunk as a worker (data-type="worker" with data-loader-shim in index.html). The page starts one worker for each check and each run, and terminates it on Cancel, when the document or the plug-in changes, and 5 s past the run's time budget, also while a dialog is open. Each worker's inbox holds its latest progress report and at most 8 other messages. - crates/plugins/src/worker.rs holds the messages and the worker's side. Runs can pause at each vc_call (Plugin::begin_run and resume_run), which the desktop's synchronous calls now use too. - Install Plug-in... and File > Open check a module in a worker first; the page then loads it with Plugin::load_checked, which runs none of its code. - A filter that calls commands sends each call to the page. The session holds the run's undo step open and refuses other edits with "a plug-in is running: cancel it or wait for it to finish" until the run ends; the progress window shows at once with Cancel only. - On the web every filter and plug-in command started from the menus or their dialogs runs in a worker, and the dialogs have no Preview. A plug-in command applies as itself, one undo step named after it. Commands that automation runs directly still run where they are called and return their result. - packaging/web: the worker files carry no hash, so the sample cache rules make only the hashed app files immutable.
38 lines
1.6 KiB
Rust
38 lines
1.6 KiB
Rust
//! The web build's plug-in worker ([`vectorcraft_plugins::worker`]). Trunk builds it beside the
|
|
//! app (`data-type="worker"` in `apps/vectorcraft-web/index.html`), and the page starts one for each
|
|
//! module it checks and each filter it runs, through the loader Trunk writes for it
|
|
//! (`vectorcraft-plugin-worker_loader.js`). Each message, both ways, is one `Uint8Array`.
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
fn main() {
|
|
use std::cell::RefCell;
|
|
use std::rc::Rc;
|
|
|
|
use wasm_bindgen::JsCast as _;
|
|
use wasm_bindgen::prelude::Closure;
|
|
use web_sys::{DedicatedWorkerGlobalScope, MessageEvent};
|
|
|
|
let scope: DedicatedWorkerGlobalScope = js_sys::global().unchecked_into();
|
|
let poster = scope.clone();
|
|
let post = Rc::new(move |msg: Vec<u8>| {
|
|
let bytes = js_sys::Uint8Array::from(msg.as_slice());
|
|
// A message that can't be posted leaves the page waiting, and its time budget ends the run.
|
|
let _ = poster.post_message_with_transfer(&bytes, &js_sys::Array::of1(&bytes.buffer()));
|
|
});
|
|
let worker = Rc::new(RefCell::new(vectorcraft_plugins::worker::Worker::new(post)));
|
|
let handler = Rc::clone(&worker);
|
|
let on_message = Closure::<dyn FnMut(MessageEvent)>::new(move |e: MessageEvent| {
|
|
let msg = js_sys::Uint8Array::new(&e.data()).to_vec();
|
|
handler.borrow_mut().handle(&msg);
|
|
});
|
|
scope.set_onmessage(Some(on_message.as_ref().unchecked_ref()));
|
|
// The handler lives as long as the worker.
|
|
on_message.forget();
|
|
worker.borrow().ready();
|
|
}
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
fn main() {
|
|
eprintln!("vectorcraft-plugin-worker only runs in the browser, as the web build's plug-in worker");
|
|
}
|