mizchi/luna/js/resource does not have a README file

    Context

    using @mizchi/signals { type Context }

    Context type with typed getter The current_getter is managed per-context and tracks the current value chain

    Lens

    using @mizchi/signals { type Lens }

    A lens focuses on a specific part of a larger structure

    LensStore

    using @mizchi/signals { type LensStore }

    A store with lens-based field access Each focused lens creates a derived signal that only updates when that field changes

    Owner

    using @mizchi/signals { type Owner }

    Owner - manages lifecycle of reactive computations (Solid.js style)

    Signal

    using @mizchi/signals { type Signal }

    Signal type - holds a value and notifies subscribers on change. A reactive primitive that stores a value and automatically tracks dependencies.

    Snapshot

    using @mizchi/signals { trait Snapshot }

    Trait for signal records that can produce a snapshot

    SplitStore2

    A record where each field is an independent Signal This is the most performant approach - updates only affect specific fields

    SplitStore3

    3-field version

    Resource

    pub struct Resource[T] {
    state :
    Signal
    [
    AsyncState
    [T]]
    // private fields
    }

    Resource - Async value container with Signal integration Environment-agnostic: uses callbacks instead of Promise/async directly

    Resource::error

    fn[T] Resource::error(self : Resource[T]) -> String?

    Get error if failure

    Resource::get

    Get current async state (reactive)

    Resource::is_failure

    fn[T] Resource::is_failure(self : Resource[T]) -> Bool

    Check if failed

    Resource::is_pending

    fn[T] Resource::is_pending(self : Resource[T]) -> Bool

    Check if currently pending

    Resource::is_success

    fn[T] Resource::is_success(self : Resource[T]) -> Bool

    Check if successfully loaded

    Resource::peek

    Peek current state without tracking

    Resource::pending

    fn[T] Resource::pending(self : Resource[T]) -> (() -> Bool)

    Returns a reactive getter that tracks whether this resource is pending. Unlike is_pending() which uses peek(), this uses get() for dependency tracking.

    Resource::refetch

    fn[T] Resource::refetch(self : Resource[T]) -> Unit

    Trigger refetch

    Resource::value

    fn[T] Resource::value(self : Resource[T]) -> T?

    Get value if success

    all

    fn all(signals : Array[
    Signal
    [Bool]]) -> (() -> Bool)

    Create a signal that is true when all input signals are true.

    any

    fn any(signals : Array[
    Signal
    [Bool]]) -> (() -> Bool)

    Create a signal that is true when any input signal is true.

    batch

    fn[T] batch(f : () -> T) -> T

    Run a function in a batch - all signal updates are batched. Effects only run once after all updates complete.

    batch_end

    fn batch_end() -> Unit

    End a batch update - run all pending effects

    batch_start

    fn batch_start() -> Unit

    Start a batch update - effects won't run until batch ends

    combine2

    fn[A, B, R] combine2(a :
    Signal
    [A], b :
    Signal
    [B], f : (A, B) -> R) -> (() -> R)

    Combine two signals into one. The getter recomputes when either signal changes.

    combine3

    fn[A, B, C, R] combine3(a :
    Signal
    [A], b :
    Signal
    [B], c :
    Signal
    [C], f : (A, B, C) -> R) -> (() -> R)

    Combine three signals into one

    combine4

    fn[A, B, C, D, R] combine4(a :
    Signal
    [A], b :
    Signal
    [B], c :
    Signal
    [C], d :
    Signal
    [D], f : (A, B, C, D) -> R) -> (() -> R)

    Combine four signals into one

    computed

    fn[T] computed(compute : () -> T) -> (() -> T)

    Create a computed signal (alias for memo with Signal-like API). Same as memo, but named to be more familiar for those coming from other reactive frameworks.

    create_context

    fn[T] create_context(default_value : T) ->
    Context
    [T]

    Create a new context with a default value Similar to Solid.js createContext

    create_root

    fn[T] create_root(f : (() -> Unit) -> T) -> T

    Create a new reactive root scope. The function receives a dispose callback that cleans up all effects. Returns the result of the function.

    create_root_with_dispose

    fn[T] create_root_with_dispose(f : () -> T) -> (T, () -> Unit)

    Create a reactive root and return both result and dispose function

    deferred

    fn[T] deferred() -> (Resource[T], (T) -> Unit, (String) -> Unit)

    Create a deferred Resource (starts pending, resolve/reject manually)

    effect

    fn effect(fn_ : () -> Unit) -> (() -> Unit)

    Create an effect that is deferred until after rendering completes. (Solid.js style createEffect - deferred execution via microtask) Returns a dispose function to stop the effect. Unlike render_effect, this runs asynchronously via microtask queue.

    effect_once

    fn effect_once(fn_ : () -> Unit) -> Unit

    Create a one-time effect that disposes itself after first run. Useful for initialization logic that should only run once.

    effect_when

    fn effect_when(condition : () -> Bool, fn_ : () -> Unit) -> (() -> Unit)

    Create an effect that only runs when condition is true. Uses render_effect for synchronous execution.

    flatten

    fn[T] flatten(outer :
    Signal
    [
    Signal
    [T]]) -> (() -> T)

    Flatten a signal of signals. Unwraps nested Signal[Signal[T]] to a getter for T.

    get_owner

    fn get_owner() ->
    Owner
    ?

    Get the current owner (if any)

    has_owner

    fn has_owner() -> Bool

    Check if currently inside an owner scope

    is_batching

    fn is_batching() -> Bool

    Check if we're currently inside a batch

    memo

    fn[T] memo(compute : () -> T) -> (() -> T)

    Create a memoized computation. Returns a getter function that caches the result and recomputes only when dependencies change.

    memo_eq

    fn[T : Eq] memo_eq(compute : () -> T) -> (() -> T)

    A memo whose cutoff compares values with Eq rather than object identity.

    Reach for this when compute returns a freshly allocated value that is often equal to the previous one — a formatted String, a small struct, a bucketed number. memo republishes each of those because every result is a new object; memo_eq republishes only when the value actually moved.

    test "memo_eq stops an effect that memo would wake" {
    let n = @resource.signal(0)
    // A fresh String every time, but the same text until `n` reaches 100.
    let label = @resource.memo_eq(() => "page \{n.get() / 100}")
    let runs = Ref(0)
    let _ = @resource.render_effect(() => {
    let _ = label()
    runs.val 1
    })
    for i in 1...=5 {
    n.set(i)
    }
    // Five source changes, one effect run: the label never moved.
    inspect(runs.val, content="1")
    inspect(label(), content="page 0")
    }

    Two differences from memo worth knowing:

    • It is eager. memo is lazy — compute first runs on the first read. memo_eq runs it once at construction and again on every source change, whether or not anything reads the result. Per source change the two cost the same single compute call.
    • It is owned. It installs an effect, registered with the current owner, so it stops recomputing once that owner is disposed; a disposed memo_eq keeps returning the last value it published. Created outside any owner it lives as long as the program, like a bare render_effect.

    Reads are always current: a read straight after set, with no flush in between, sees the new value.

    new_effect_id

    fn new_effect_id() -> Int

    Generate a new unique effect ID
    fn[T] on(sig :
    Signal
    [T], callback : (T) -> Unit) -> (() -> Unit)

    Explicitly subscribe to a signal with a callback. Returns an unsubscribe function. Unlike effect(), this doesn't auto-track and only listens to this one signal. Callback is NOT called on initial subscription, only on subsequent changes.

    on_cleanup

    fn on_cleanup(cleanup : () -> Unit) -> Unit

    Register a cleanup function to run when the current scope disposes. Works in:
    • Effects: cleanup runs before effect re-runs or when effect disposes
    • Components: cleanup runs when component unmounts (Solid.js style - can be called directly in component body)

    on_immediate

    fn[T] on_immediate(sig :
    Signal
    [T], callback : (T) -> Unit) -> (() -> Unit)

    Subscribe and run immediately with current value. Like on, but also invokes the callback with the current value right away.

    on_mount

    fn on_mount(fn_ : () -> Unit) -> Unit

    Run a function once after mount (Solid.js style onMount). The function runs without tracking dependencies. Cleanup registered via on_cleanup inside will run when owner is disposed.
    fn[T] previous(sig :
    Signal
    [T]) -> (() -> T?)

    Create a signal that holds the previous value of another signal. Returns a getter function for the previous value (None on first read).

    previous_with_initial

    fn[T] previous_with_initial(sig :
    Signal
    [T], initial : T) -> (() -> T)

    Create a signal that holds the previous value with initial value. Unlike previous, this returns a plain T instead of T?.

    provide

    fn[T, R] provide(ctx :
    Context
    [T], value : T, f : () -> R) -> R

    Provide a context value for the current Owner and its descendants The value is associated with the current Owner and will be available to all effects and components created within this scope.

    register_disposer

    fn register_disposer(disposer : () -> Unit) -> Unit

    Register a disposer with the current owner Called internally by effect() to register its dispose function

    register_owner_cleanup

    fn register_owner_cleanup(cleanup : () -> Unit) -> Unit

    Register a cleanup with the current owner (alternative to onCleanup in effect)

    render_effect

    fn render_effect(fn_ : () -> Unit) -> (() -> Unit)

    Create an effect that runs immediately and re-runs when dependencies change. (Solid.js style createRenderEffect - synchronous execution) Returns a dispose function to stop the effect. Supports on_cleanup() calls inside the effect.

    resource

    fn[T] resource(fetcher : ((T) -> Unit, (String) -> Unit) -> Unit) -> Resource[T]

    Create a Resource from a callback-based fetcher The fetcher receives (resolve, reject) callbacks This is environment-agnostic - the actual async execution is handled by the caller

    resource_rejected

    fn[T] resource_rejected(error : String) -> Resource[T]

    Create a Resource with initial error (already rejected)

    resource_resolved

    fn[T] resource_resolved(value : T) -> Resource[T]

    Create a Resource with initial value (already resolved)

    run_with_cleanup_tracking

    fn[T] run_with_cleanup_tracking(cleanups : Array[() -> Unit], f : () -> T) -> T

    Run a function with cleanup tracking enabled

    run_with_owner

    fn[T] run_with_owner(owner :
    Owner
    , f : () -> T) -> T

    Run a function with a specific owner as current

    select

    fn[T] select(items :
    Signal
    [Array[T]], index :
    Signal
    [Int]) -> (() -> T?)

    Select from an array signal by index signal. Returns None if index is out of bounds.

    set_current_cleanups

    fn set_current_cleanups(cleanups : Array[() -> Unit]?) -> Array[() -> Unit]?

    Set the current cleanup array (used internally by effect)

    sig_filter

    fn[T] sig_filter(sig :
    Signal
    [T], predicate : (T) -> Bool) ->
    Signal
    [T?]

    Filter signal updates - only updates when predicate is true. Returns a signal that only updates when predicate passes.

    sig_filter_map

    fn[T, U] sig_filter_map(sig :
    Signal
    [T], f : (T) -> U?) ->
    Signal
    [U?]

    Filter and map signal updates Uses render_effect for synchronous execution

    sig_map

    fn[T, U] sig_map(sig :
    Signal
    [T], f : (T) -> U) -> (() -> U)

    Map a signal's value through a function. Returns a getter function (like memo) that recomputes when the signal changes.

    signal

    fn[T] signal(initial : T) ->
    Signal
    [T]

    Convenience function to create a signal. Shorthand for Signal::new.

    switch_

    fn[T] switch_(condition :
    Signal
    [Bool], on_true :
    Signal
    [T], on_false :
    Signal
    [T]) -> (() -> T)

    Switch between signals based on a boolean signal. Returns value from on_true when condition is true, otherwise on_false.

    to_getter

    fn[T] to_getter(sig :
    Signal
    [T]) -> (() -> T)

    Create a read-only getter for a signal

    untracked

    fn[T] untracked(f : () -> T) -> T

    Run a function without tracking (useful for avoiding circular deps). Signal reads inside this function won't create subscriptions.

    use_context

    fn[T] use_context(ctx :
    Context
    [T]) -> T

    Use a context value - returns the current provided value or default Walks up the Owner chain to find the nearest provided value. Similar to Solid.js useContext

    watch

    fn[T : Eq] watch(source : () -> T, callback : (T, T) -> Unit) -> (() -> Unit)

    Watch a computed expression and run callback when it changes. Returns a dispose function. The callback receives (newValue, oldValue).

    watch_immediate

    fn[T : Eq] watch_immediate(source : () -> T, callback : (T, T?) -> Unit) -> (() -> Unit)

    Watch with immediate execution (runs callback on first value too). The callback receives (newValue, oldValue?) where oldValue is None on first run.

    with_parent_owner

    fn[T] with_parent_owner(captured_owner :
    Owner
    ?, f : () -> T) -> T

    Run a function with a captured parent owner context (if any) This is useful for rendering child components that need to inherit the parent's owner. If captured_owner is Some, runs with that owner as context. If captured_owner is None, runs directly without owner context.