跳到主要内容

Signal

A reactive signal system for dependency tracking and cache invalidation.

Overview

Signal is the reactive core of Locus, providing:

  • Dependency tracking
  • Automatic cache invalidation
  • Chainable animation API
  • Type-safe accessors

Core Types

SignalValue<T>

type SignalValue<T> = T | (() => T);

A signal value type that can be a static value or a function returning a value.

SignalGetter<T>

interface SignalGetter<T> {
(): T;
}

A signal getter interface.

SignalSetter<T, TOwner>

interface SignalSetter<T, TOwner = void> {
(value: SignalValue<T> | typeof DEFAULT): TOwner;
}

A signal setter interface that returns the owner object to support chaining.

Factory Functions

createSignal

function createSignal<T>(initial?: SignalValue<T>, owner?: object): Signal<T>;

Creates a basic signal.

const count = createSignal(0);
count(); // 0
count(1); // Set to 1

createComputed

function createComputed<T, TOwner = void>(
computation: () => T,
owner?: TOwner,
): Computed<T>;

Creates a computed signal that automatically tracks dependencies.

const double = createComputed(() => count() * 2);

createEffect

function createEffect(computation: () => void | (() => void)): void;

Creates a side effect that executes when dependencies change.

Signal Generators

Signals support a chainable animation API:

signal
.to(100, 1) // Tween to 100, duration 1 second
.back(1) // Return, duration 1 second
.wait(0.5) // Wait 0.5 seconds
.to(0, 1); // Tween to 0, duration 1 second

Generator Methods

MethodParametersDescription
to(value, duration, timing?, interpolation?)Tween to target value
back(duration, timing?, interpolation?)Return to initial value
wait(duration)Wait for specified time
run(task)Run thread task
do(callback)Execute callback function

Vector2 Signals

const position = createSignal(new Vector2(0, 0));
position.x(); // 0
position.y(); // 0

// Chainable animation
position().to(new Vector2(100, 100), 1, easings.easeInOutQuad);