brumate

Compile-time functions

The functions of brumate/signals describe state and behavior to the compiler. The compiler replaces every call and removes the import, so none of them ship.

import { $memo, $signal } from 'brumate/signals';

Summary

Function Returns Where Guide
$props<T>() T const in the component script Components
$signal<T>(value?) T let at the top level of a script State
$memo<T>(value) MemoValue<T> const at the top level of a script State, Async data
$debounced(value, ms) MemoValue<T> const at the top level of a script Async data
$throttled(value, ms) MemoValue<T> const at the top level of a script Async data
$isPending(memo) boolean Any expression Async data
$isPassed(prop) boolean const in the component script Components
$isBound(prop) boolean Any expression Components
$effect(callback) void Statement at the top level of a script Effects
$client(callback, trigger?) void Statement at the top level of a script Effects
$untrack(value) the value Any expression State
$produce(signal, recipe) void Where a signal can be written State
$setContext(context, value) void Statement at the top level of a script Context
$getContext(context) the value const at the top level of a script Context
$route(path?) { url, search, params } const at the top level of a script Navigation
$navigation() { pending, from, to } const at the top level of a script Navigation
$id() string const at the top level of a script Page metadata
$title(value) void Statement at the top level of a script. Replaced by <title> in #head Page metadata
$action(fn) the result, or undefined const at the top level of a script Forms
$optimistic(source) the value of source let at the top level of a script Forms
$handler(fn, ...args) a handler Any expression of a script or a module function Components

$props

function $props<T = Record<string, any>>(): Unbound<T>;
type Bindable<T> = T | BindableMark<T>;

Returns the props of the component.

$signal

function $signal<T>(value: T): T;
function $signal<T>(): T | undefined;

Declares writable state. Reading the variable reads the value, and assigning to it writes the value.

$memo

function $memo<T>(value: T): MemoValue<T>;
type MemoValue<T> = Awaited<T> extends AsyncIterable<infer U> ? U : Awaited<T>;

Declares a read-only value computed from other values.

$isPending

function $isPending(memo: unknown): boolean;

True while a new value of the memo loads in the browser, or a value that it reads loads.

$isPassed

function $isPassed(prop: unknown): boolean;

True when the caller passed the prop, also as undefined or null.

$isBound

function $isBound(prop: unknown): boolean;

True when the caller passed the prop with $bind, so a write of it changes the caller's value.

$effect

function $effect(callback: () => void | (() => void)): void;

Runs callback when the values that it reads change.

$client

function $client(callback: () => void | (() => void), trigger?: 'visible' | 'idle' | 'load'): void;

Runs callback in the browser once, when a trigger fires.

Trigger Runs when
'visible' The first element of the scope enters the viewport. This is the default.
'idle' The browser is idle after the page loads.
'load' The page loads. The compiler warns, because this runs before any event.

$untrack

function $untrack<T>(value: T): T;

Reads a value without depending on it. A memo or effect that reads $untrack(count) does not run again when count changes. A function called inside it, such as $untrack(save()), adds no dependencies either.

$produce

function $produce<T>(signal: T, recipe: (draft: T) => T | void): void;

Changes an object or array in a signal through a draft, and writes the signal with the result.

$setContext

function $setContext<T>(context: Context<T>, value: T): void;

Provides a value of a context to the components inside this one. A statement at the top level of a script. When value changes in the browser, every component that reads it updates. Create the context with createContext from brumate/context.

$getContext

function $getContext<T>(context: Context<T>): T;

Reads a context from the nearest component above that provides it, or its default value. Assign it to a const at the top level of a script.

$id

function $id(): string;

Returns an ID that is unique in the page, for attributes such as for and aria-describedby. Assign it to a const at the top level of a script. Server and client make the same ID. In the script of a block, such as the body of a #for, each instance gets its own. It has only letters, digits, - and _, so it works as a CSS selector too.

$title

function $title(value: unknown): void;

Sets the title of the page. A statement at the top level of a script, with one argument. It is replaced by a <title> in #head, the compiler warns about it, and it goes in a later version. See Page metadata.

$action

function $action<F extends (...args: never[]) => unknown>(fn: F): Awaited<ReturnType<F>> | undefined;

The latest result of a server function that a form of this component posts to.

$handler

function $handler<A extends unknown[], E = Event>(
  fn: (...args: [...A, E]) => unknown,
  ...args: A
): (event: E) => unknown;

Marks a function that a script makes as an event handler, so a spread can pass it on, as it passes on a handler that a caller writes in markup.

$bind

function $bind<T>(value: T, set?: (next: T) => unknown): BindableMark<T>;

Passes a value that the receiver writes back.

$optimistic

let shown = $optimistic(names);

$debounced and $throttled

function $debounced<T>(value: T, ms: number): MemoValue<T>;
function $throttled<T>(value: T, ms: number): MemoValue<T>;

Memos of value that the browser writes after a wait.

Not supported

These names are reserved, and calling one is a compile error that names the replacement:

Name Use instead
$ref A $signal with ref=$bind(name) on the element.
$watch $effect.
$settled $client.
$cleanup A function returned from $effect or $client.
$store A $signal with a new value, or $produce.
$get, $set Nothing. Signals cannot leave the file.