Documentation Index
Fetch the complete documentation index at: https://mintlify.com/sanchedev/tiny-engine/llms.txt
Use this file to discover all available pages before exploring further.
Signal<T> is the reactive primitive that powers state management in tiny-engine. When a signal’s value changes, every registered subscriber is called synchronously with the new value. Hooks like useSignal are thin wrappers around Signal designed for JSX components; the raw Signal class is what you use inside TinyScript classes and standalone game logic.
new Signal<T>(initialValue)
Creates a signal with the given initial value. The type parameter T is inferred from the initial value.
The starting value of the signal. Determines the signal’s type.
.value
A getter/setter pair for reading and writing the signal’s current value.
val !== this.#value).
.sub(fn)
Registers a subscriber that is called every time the signal’s value changes.
The callback to invoke on each change. Receives the new value as its only argument.
useSignal, .sub() does not return an unsubscribe function — use .unsub(fn) to remove a specific listener, or .clearSubs() to remove all of them.
.unsub(fn)
Removes a previously registered subscriber. You must pass the same function reference that was passed to .sub().
The exact callback reference to remove.
.clearSubs()
Removes all subscribers from this signal at once. Useful during scene teardown to prevent stale callbacks from firing after a node is destroyed.
SignalGetter<T>
A type alias for a zero-argument function that returns the current signal value:
SignalGetter<T> is returned as the first element of the useSignal tuple and acts as both a read accessor and a dependency tracker for reactive JSX expressions. Any JSX attribute that is a function () => T is automatically treated as a reactive binding — it re-evaluates whenever the underlying signal changes.
SignalSetter<T>
A type alias for a function that updates the signal value:
SignalSetter<T> is the second element of the useSignal tuple. Calling it is equivalent to assigning signal.value = newValue.
Signal vs useSignal
| Scenario | Use |
|---|---|
Logic inside a TinyScript class | new Signal<T>(initial) |
| Shared state between multiple scripts | new Signal<T>(initial) (module-level) |
| Reactive state in a JSX component | useSignal(initial) |
| Exposing readable state to JSX | Return a SignalGetter from a script method |
useSignal is implemented on top of Signal and adds integration with the JSX component lifecycle. Outside of JSX components, prefer Signal directly.
Example: TinyScript with reactive health
Example: Reactive JSX attribute
Any JSX prop that is a function (
() => T) is treated as a reactive binding and re-evaluated whenever the signals it reads change. Static values (e.g., plain numbers or strings) are bound once and never update.