Skip to main content

euv_engine/asset/
type.rs

1use super::*;
2
3/// A reference-counted, shared-mutable store of asset load callbacks.
4///
5/// Holds the `Closure` objects that keep `onload` / `onerror` handlers alive
6/// for as long as their load is in flight.
7///
8/// ## Why the store is indexed rather than a plain `Vec`
9///
10/// A `wasm_bindgen::Closure` must not be dropped while JavaScript is
11/// executing it — dropping frees the boxed Rust closure whose trampoline is
12/// currently on the stack. A load callback therefore cannot free itself. The
13/// callbacks instead mark their own slot `settled` in the parallel
14/// `settled` vector, and [`AssetLoader::collect`] drops the settled slots
15/// later, from a context that is provably not inside a callback.
16///
17/// ## Why the callbacks hold a `Weak` reference
18///
19/// The callbacks need to reach this store to mark their slot settled. A
20/// strong `Rc` would make the store own the closures while the closures own
21/// the store — a reference cycle leaking both halves. Holding `Weak` breaks
22/// the cycle: once the owning [`AssetLoader`] is dropped the store is freed
23/// and a still-pending callback's `Weak` upgrade simply returns `None`.
24///
25/// Held behind `EngineCell` (an `UnsafeCell`-backed `Sync` newtype) rather
26/// than `RefCell` so the storage matches the rest of the engine's
27/// `static mut` convention. Access via [`EngineCell::get_mut`] to obtain the
28/// underlying store for push / take / drain operations.
29pub type AssetClosures = Rc<EngineCell<AssetClosureStore>>;
30
31/// A shared counter of loads that have been requested but not yet settled.
32///
33/// Held behind `EngineCell` so the `onload` / `onerror` closures — which own
34/// only a `Weak` back-reference to the loader's state — can decrement it
35/// without borrowing the loader. Making the counter itself the single source
36/// of truth is what keeps `is_all_loaded` from sticking at `false`
37/// forever: there is no separate `pending_count` field that a callback
38/// could forget to update.
39pub type AssetPending = Rc<EngineCell<u32>>;