2026-08-16 14:48:56 +02:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
|
|
/**
|
2026-08-21 23:05:45 +02:00
|
|
|
* The authoring hooks, as signatures. They read the host a project is mounted
|
2026-08-16 14:48:56 +02:00
|
|
|
* into, which only exists inside the editor: when a project is mounted, the
|
|
|
|
|
* renderer substitutes its own "@diffusionstudio/jsx" module (see
|
|
|
|
|
* @diffusionstudio/reconciler) and these implementations are replaced by ones
|
|
|
|
|
* bound to the live host. This package carries the declarations so project
|
|
|
|
|
* sources type-check and get IntelliSense; calling them outside a mount
|
|
|
|
|
* throws rather than silently returning a dead value.
|
|
|
|
|
*/
|
|
|
|
|
|
2026-08-21 23:05:45 +02:00
|
|
|
import type { Accessor } from "solid-js";
|
2026-08-16 14:48:56 +02:00
|
|
|
|
|
|
|
|
export type Ticker = {
|
|
|
|
|
time: Accessor<number>;
|
|
|
|
|
frame: Accessor<number>;
|
|
|
|
|
delta: Accessor<number>;
|
|
|
|
|
playing: Accessor<boolean>;
|
2026-08-25 16:31:49 +02:00
|
|
|
hold: (work: Promise<unknown>) => void;
|
2026-08-16 14:48:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function hostOnly(name: string): never {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`${name} is implemented by the editor's renderer and only works inside a mounted project.`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Subscribes to the project's timeline: the playhead of the scene the mount's
|
|
|
|
|
* root lives in (or is), pushed by the host once per playback tick. Each
|
|
|
|
|
* accessor only propagates when its value changes, so a paused scene re-runs
|
|
|
|
|
* nothing and `frame()` consumers update at most once per frame. Values move
|
|
|
|
|
* while the reactive graph is alive: a mount stays live, and export, capture,
|
|
|
|
|
* and reload each re-execute the module and drive the ticker themselves.
|
2026-08-25 16:31:49 +02:00
|
|
|
*
|
|
|
|
|
* `hold` is the other half of that: because a mount of its own is what an
|
|
|
|
|
* export or a capture draws, a project's own async work (a mesh, a texture,
|
|
|
|
|
* a fetch a layout is decided by) races the first frames, which are sampled
|
|
|
|
|
* as soon as the module has rendered. Handing the promise to `hold` keeps
|
|
|
|
|
* those frames waiting for it. Live playback collects nothing, so a held
|
|
|
|
|
* promise costs the editor nothing.
|
2026-08-16 14:48:56 +02:00
|
|
|
*/
|
|
|
|
|
export function useTicker(): Ticker {
|
|
|
|
|
return hostOnly("useTicker");
|
|
|
|
|
}
|