blac

    Core

    watch

    Observe one or more blocs outside React and run a callback whenever their state changes — the escape hatch for loggers, analytics, localStorage sync, imperative UI, and test assertions.

    useBloc connects a bloc to a React component. But state often needs to reach code that has no component to render: a logger, an analytics pipeline, a localStorage sync, a <canvas> driven by an imperative library, or a test assertion. watch is the escape hatch for those cases — it observes one or more blocs outside of React and runs a callback whenever their state changes.

    watch(bloc, callback)

    Observe one or more blocs outside React and run a callback on every state change.

    // Single bloc
    function watch<T extends StateContainerConstructor>(
    bloc: T | BlocRef<T>,
    callback: (bloc: InstanceType<T>) => void | typeof watch.STOP,
    ): () => void;
    // Multiple blocs
    function watch<T extends readonly BlocInput[]>(
    blocs: T,
    callback: (blocs: ExtractInstances<T>) => void | typeof watch.STOP,
    ): () => void;
    ParameterTypeRequiredDescription
    bloc / blocsT | BlocRef<T> or readonly BlocInput[]yesA bloc class (resolves to the default instance), an instance(Class, id) reference, or a readonly array of either.
    callback(bloc: InstanceType<T>) => void | typeof watch.STOPyesRuns once immediately with the current instance(s), then on every subsequent state change. Return watch.STOP to tear down the subscription from inside the callback.

    Returns: a stop() function. Calling it unsubscribes and is idempotent — calling it more than once is safe.

    Behavior. watch resolves each input from the registry via ensure — creating the instance if it does not exist yet. It does not hold a ref: watch will not keep an otherwise-unused bloc alive, and a bloc it created can be disposed if nothing else references it. Subscriptions are coalesced per microtask flush — several synchronous mutations produce a single callback run — so callbacks land asynchronously after emit(). The callback fires once immediately on setup with the current state, then again after every subsequent change.

    import {
    const watch: WatchFn
    watch
    } from '@blac/core';
    import {
    class Cubit<S extends object = any, Args = void, Deps extends object = Record<string, never>>

    Cubit<S> is a StateContainer<S> that exposes mutation publicly.

    That is the whole difference, and it is a real one: on StateContainer emit / patch / update are protected, so a container mutates itself from its own methods and callers go through the API it chooses to publish. Cubit re-declares the three as public for the cases where a caller legitimately drives state from outside — test helpers, devtools time-travel, benchmarks.

    Reach for StateContainer when business logic should live in the class, and Cubit when the caller owns the transitions.

    Cubit
    } from '@blac/core';
    class
    class UserCubit
    UserCubit
    extends
    class Cubit<S extends object = any, Args = void, Deps extends object = Record<string, never>>

    Cubit<S> is a StateContainer<S> that exposes mutation publicly.

    That is the whole difference, and it is a real one: on StateContainer emit / patch / update are protected, so a container mutates itself from its own methods and callers go through the API it chooses to publish. Cubit re-declares the three as public for the cases where a caller legitimately drives state from outside — test helpers, devtools time-travel, benchmarks.

    Reach for StateContainer when business logic should live in the class, and Cubit when the caller owns the transitions.

    Cubit
    <{
    name: string
    name
    : string }> {
    constructor() {
    super({
    name: string
    name
    : 'Alice' });
    }
    }
    const
    const stop: () => void
    stop
    =
    watch<typeof UserCubit>(bloc: typeof UserCubit | BlocRef<typeof UserCubit>, callback: (bloc: UserCubit) => void | unique symbol, options?: WatchOptions): () => void (+1 overload)
    watch
    (
    class UserCubit
    UserCubit
    , (
    user: UserCubit
    user
    ) => {
    var console: Console
    console
    .
    Console.log(...data: any[]): void

    The console.log() static method outputs a message to the console.

    MDN Reference

    log
    ('User state changed:',
    user: UserCubit
    user
    .
    StateContainer<{ name: string; }, void, Record<string, never>>.state: {
    name: string;
    }
    state
    .
    name: string
    name
    );
    });
    // later — e.g. in a cleanup or on teardown of your module:
    const stop: () => void
    stop
    ();

    instance(BlocClass, args?)

    Create a reference to a specific keyed bloc instance for use with watch.

    function instance<T extends StateContainerConstructor>(
    BlocClass: T,
    args?: ExtractArgs<T>,
    ): BlocRef<T>;
    ParameterTypeRequiredDescription
    BlocClassT extends StateContainerConstructoryesThe bloc class to reference.
    argsExtractArgs<T>noThe args identifying the instance — the same args you pass to useBloc. Resolved to a key via static key/structural hash.

    Returns: a BlocRef<T> — a lightweight reference object that tells watch which keyed instance to resolve.

    Behavior. By default watch(SomeCubit, ...) resolves the default instance. Wrap with instance(Class, args) to target the instance keyed by those args.

    import {
    const watch: WatchFn
    watch
    ,
    function instance<T extends StateContainerConstructor>(BlocClass: T, args?: ExtractArgs<T>): BlocRef<T>

    Create a reference to a specific bloc instance, identified by its args. The key is derived the same way useBloc/acquire derive it.

    @example

    watch(instance(UserBloc, { userId: 'user-123' }), (userBloc) => {
    console.log(userBloc.state.name);
    });

    instance
    } from '@blac/core';
    import {
    class Cubit<S extends object = any, Args = void, Deps extends object = Record<string, never>>

    Cubit<S> is a StateContainer<S> that exposes mutation publicly.

    That is the whole difference, and it is a real one: on StateContainer emit / patch / update are protected, so a container mutates itself from its own methods and callers go through the API it chooses to publish. Cubit re-declares the three as public for the cases where a caller legitimately drives state from outside — test helpers, devtools time-travel, benchmarks.

    Reach for StateContainer when business logic should live in the class, and Cubit when the caller owns the transitions.

    Cubit
    } from '@blac/core';
    class
    class UserCubit
    UserCubit
    extends
    class Cubit<S extends object = any, Args = void, Deps extends object = Record<string, never>>

    Cubit<S> is a StateContainer<S> that exposes mutation publicly.

    That is the whole difference, and it is a real one: on StateContainer emit / patch / update are protected, so a container mutates itself from its own methods and callers go through the API it chooses to publish. Cubit re-declares the three as public for the cases where a caller legitimately drives state from outside — test helpers, devtools time-travel, benchmarks.

    Reach for StateContainer when business logic should live in the class, and Cubit when the caller owns the transitions.

    Cubit
    <{
    name: string
    name
    : string }, {
    userId: string
    userId
    : string }> {
    static
    UserCubit.key: (a: {
    userId: string;
    }) => string
    key
    = (
    a: {
    userId: string;
    }
    a
    : {
    userId: string
    userId
    : string }) =>
    a: {
    userId: string;
    }
    a
    .
    userId: string
    userId
    ;
    constructor() {
    super({
    name: string
    name
    : '' });
    }
    }
    const
    const stop: () => void
    stop
    =
    watch<typeof UserCubit>(bloc: typeof UserCubit | BlocRef<typeof UserCubit>, callback: (bloc: UserCubit) => void | unique symbol, options?: WatchOptions): () => void (+1 overload)
    watch
    (
    instance<typeof UserCubit>(BlocClass: typeof UserCubit, args?: {
    userId: string;
    } | undefined): BlocRef<typeof UserCubit>

    Create a reference to a specific bloc instance, identified by its args. The key is derived the same way useBloc/acquire derive it.

    @example

    watch(instance(UserBloc, { userId: 'user-123' }), (userBloc) => {
    console.log(userBloc.state.name);
    });

    instance
    (
    class UserCubit
    UserCubit
    , {
    userId: string
    userId
    : 'user-123' }), (
    user: UserCubit
    user
    ) => {
    var console: Console
    console
    .
    Console.log(...data: any[]): void

    The console.log() static method outputs a message to the console.

    MDN Reference

    log
    (
    user: UserCubit
    user
    .
    StateContainer<{ name: string; }, { userId: string; }, Record<string, never>>.state: {
    name: string;
    }
    state
    .
    name: string
    name
    );
    });

    Stopping a watch

    A watch lives until you stop it. There are two ways, and forgetting both is the most common watch bug.

    Call the returned stop function

    import {
    const watch: WatchFn
    watch
    } from '@blac/core';
    import {
    class Cubit<S extends object = any, Args = void, Deps extends object = Record<string, never>>

    Cubit<S> is a StateContainer<S> that exposes mutation publicly.

    That is the whole difference, and it is a real one: on StateContainer emit / patch / update are protected, so a container mutates itself from its own methods and callers go through the API it chooses to publish. Cubit re-declares the three as public for the cases where a caller legitimately drives state from outside — test helpers, devtools time-travel, benchmarks.

    Reach for StateContainer when business logic should live in the class, and Cubit when the caller owns the transitions.

    Cubit
    } from '@blac/core';
    class
    class UserCubit
    UserCubit
    extends
    class Cubit<S extends object = any, Args = void, Deps extends object = Record<string, never>>

    Cubit<S> is a StateContainer<S> that exposes mutation publicly.

    That is the whole difference, and it is a real one: on StateContainer emit / patch / update are protected, so a container mutates itself from its own methods and callers go through the API it chooses to publish. Cubit re-declares the three as public for the cases where a caller legitimately drives state from outside — test helpers, devtools time-travel, benchmarks.

    Reach for StateContainer when business logic should live in the class, and Cubit when the caller owns the transitions.

    Cubit
    <{
    name: string
    name
    : string }> {
    constructor() {
    super({
    name: string
    name
    : '' });
    }
    }
    const
    const stop: () => void
    stop
    =
    watch<typeof UserCubit>(bloc: typeof UserCubit | BlocRef<typeof UserCubit>, callback: (bloc: UserCubit) => void | unique symbol, options?: WatchOptions): () => void (+1 overload)
    watch
    (
    class UserCubit
    UserCubit
    , (
    user: UserCubit
    user
    ) => {
    var console: Console
    console
    .
    Console.log(...data: any[]): void

    The console.log() static method outputs a message to the console.

    MDN Reference

    log
    (
    user: UserCubit
    user
    .
    StateContainer<{ name: string; }, void, Record<string, never>>.state: {
    name: string;
    }
    state
    .
    name: string
    name
    );
    });
    // later — e.g. in a cleanup, useEffect teardown, or on teardown of your module:
    const stop: () => void
    stop
    ();

    Return watch.STOP from the callback

    For one-shot or self-terminating watches, return the watch.STOP sentinel and the subscription tears itself down:

    import {
    const watch: WatchFn
    watch
    } from '@blac/core';
    import {
    class Cubit<S extends object = any, Args = void, Deps extends object = Record<string, never>>

    Cubit<S> is a StateContainer<S> that exposes mutation publicly.

    That is the whole difference, and it is a real one: on StateContainer emit / patch / update are protected, so a container mutates itself from its own methods and callers go through the API it chooses to publish. Cubit re-declares the three as public for the cases where a caller legitimately drives state from outside — test helpers, devtools time-travel, benchmarks.

    Reach for StateContainer when business logic should live in the class, and Cubit when the caller owns the transitions.

    Cubit
    } from '@blac/core';
    class
    class UserCubit
    UserCubit
    extends
    class Cubit<S extends object = any, Args = void, Deps extends object = Record<string, never>>

    Cubit<S> is a StateContainer<S> that exposes mutation publicly.

    That is the whole difference, and it is a real one: on StateContainer emit / patch / update are protected, so a container mutates itself from its own methods and callers go through the API it chooses to publish. Cubit re-declares the three as public for the cases where a caller legitimately drives state from outside — test helpers, devtools time-travel, benchmarks.

    Reach for StateContainer when business logic should live in the class, and Cubit when the caller owns the transitions.

    Cubit
    <{
    onboardingComplete: boolean
    onboardingComplete
    : boolean }> {
    constructor() {
    super({
    onboardingComplete: boolean
    onboardingComplete
    : false });
    }
    }
    declare function
    function runOnce(): void
    runOnce
    (): void;
    watch<typeof UserCubit>(bloc: typeof UserCubit | BlocRef<typeof UserCubit>, callback: (bloc: UserCubit) => void | unique symbol, options?: WatchOptions): () => void (+1 overload)
    watch
    (
    class UserCubit
    UserCubit
    , (
    user: UserCubit
    user
    ) => {
    if (
    user: UserCubit
    user
    .
    StateContainer<{ onboardingComplete: boolean; }, void, Record<string, never>>.state: {
    onboardingComplete: boolean;
    }
    state
    .
    onboardingComplete: boolean
    onboardingComplete
    ) {
    function runOnce(): void
    runOnce
    ();
    return
    const watch: WatchFn
    watch
    .
    WatchSingleFn.STOP: unique symbol
    STOP
    ; // stop after the condition is met
    }
    });

    Pass an AbortSignal

    Pass { signal } to stop the watch when the signal aborts, which ties several watches (or fetches, timers, listeners) to one AbortController:

    const controller = new AbortController();
    watch(UserCubit, syncToStorage, { signal: controller.signal });
    watch(CartCubit, syncToStorage, { signal: controller.signal });
    controller.abort(); // stops both

    Aborting is equivalent to calling the returned stop(). A signal that is already aborted returns a no-op stop() without creating or subscribing to anything.

    Watch in a scoped registry

    watch uses the global registry by default. Pass { registry } to watch inside another one, for example the registry from useRegistry() under a RegistryProvider.

    Inside a container, combine it with onActivated to scope a watch to the container’s activation. This replaces the hand-written bridge, where an unsubscribe function is stored in a field and must be called on every teardown path:

    Before — a manual bridge
    class SidebarCubit extends Cubit<SidebarState> {
    private unsubscribe?: () => void;
    constructor() {
    super({ open: false });
    this.unsubscribe = getBloc(CartCubit).subscribe((cart) => {
    if (cart.items.length === 0) this.patch({ open: false });
    });
    }
    dispose() {
    this.unsubscribe?.(); // easy to miss on a second teardown path
    super.dispose();
    }
    }
    After — scoped to activation
    class SidebarCubit extends Cubit<SidebarState> {
    constructor() {
    super({ open: false });
    onActivated(this, (signal) => {
    watch(
    CartCubit,
    (cart) => {
    if (cart.state.items.length === 0) this.patch({ open: false });
    },
    { signal },
    );
    });
    }
    }

    Differences from the manual bridge:

    • Activation, not construction. The watch lives while the container has owners and is recreated on reactivation (relevant for keepAlive containers).
    • Immediate first call. watch calls back once with the current instance, then on every change. Ignore the first call if only changes matter.
    • No ref held. watch does not keep the watched container alive.

    Watching multiple blocs

    Pass a readonly array to observe several blocs at once — the callback fires when any of them changes.

    import {
    const watch: WatchFn
    watch
    } from '@blac/core';
    import {
    class Cubit<S extends object = any, Args = void, Deps extends object = Record<string, never>>

    Cubit<S> is a StateContainer<S> that exposes mutation publicly.

    That is the whole difference, and it is a real one: on StateContainer emit / patch / update are protected, so a container mutates itself from its own methods and callers go through the API it chooses to publish. Cubit re-declares the three as public for the cases where a caller legitimately drives state from outside — test helpers, devtools time-travel, benchmarks.

    Reach for StateContainer when business logic should live in the class, and Cubit when the caller owns the transitions.

    Cubit
    } from '@blac/core';
    class
    class UserCubit
    UserCubit
    extends
    class Cubit<S extends object = any, Args = void, Deps extends object = Record<string, never>>

    Cubit<S> is a StateContainer<S> that exposes mutation publicly.

    That is the whole difference, and it is a real one: on StateContainer emit / patch / update are protected, so a container mutates itself from its own methods and callers go through the API it chooses to publish. Cubit re-declares the three as public for the cases where a caller legitimately drives state from outside — test helpers, devtools time-travel, benchmarks.

    Reach for StateContainer when business logic should live in the class, and Cubit when the caller owns the transitions.

    Cubit
    <{
    name: string
    name
    : string }> {
    constructor() {
    super({
    name: string
    name
    : '' });
    }
    }
    class
    class SettingsCubit
    SettingsCubit
    extends
    class Cubit<S extends object = any, Args = void, Deps extends object = Record<string, never>>

    Cubit<S> is a StateContainer<S> that exposes mutation publicly.

    That is the whole difference, and it is a real one: on StateContainer emit / patch / update are protected, so a container mutates itself from its own methods and callers go through the API it chooses to publish. Cubit re-declares the three as public for the cases where a caller legitimately drives state from outside — test helpers, devtools time-travel, benchmarks.

    Reach for StateContainer when business logic should live in the class, and Cubit when the caller owns the transitions.

    Cubit
    <{
    theme: string
    theme
    : string }> {
    constructor() {
    super({
    theme: string
    theme
    : 'light' });
    }
    }
    declare function
    function syncToServer(name: string, theme: string): void
    syncToServer
    (
    name: string
    name
    : string,
    theme: string
    theme
    : string): void;
    // Watch several blocs at once; the callback fires when any of them changes.
    const
    const stop: () => void
    stop
    =
    watch<readonly [typeof UserCubit, typeof SettingsCubit]>(blocs: readonly [typeof UserCubit, typeof SettingsCubit], callback: (blocs: readonly [UserCubit, SettingsCubit]) => void | unique symbol, options?: WatchOptions): () => void (+1 overload)
    watch
    ([
    class UserCubit
    UserCubit
    ,
    class SettingsCubit
    SettingsCubit
    ] as
    type const = readonly [typeof UserCubit, typeof SettingsCubit]
    const
    , ([
    user: UserCubit
    user
    ,
    settings: SettingsCubit
    settings
    ]) => {
    function syncToServer(name: string, theme: string): void
    syncToServer
    (
    user: UserCubit
    user
    .
    StateContainer<{ name: string; }, void, Record<string, never>>.state: {
    name: string;
    }
    state
    .
    name: string
    name
    ,
    settings: SettingsCubit
    settings
    .
    StateContainer<{ theme: string; }, void, Record<string, never>>.state: {
    theme: string;
    }
    state
    .
    theme: string
    theme
    );
    });

    When to use watch

    ScenarioUse
    A React component needs stateuseBloc
    Non-React side effects (logging, analytics, syncing)watch
    Bridging a bloc to imperative / non-React UIwatch
    Test assertions on state over timewatch
    You need selective, path-scoped observationcontainer.consume(cb) + setPaths

    Examples

    Logging state changes

    import {
    const watch: WatchFn
    watch
    } from '@blac/core';
    import {
    class Cubit<S extends object = any, Args = void, Deps extends object = Record<string, never>>

    Cubit<S> is a StateContainer<S> that exposes mutation publicly.

    That is the whole difference, and it is a real one: on StateContainer emit / patch / update are protected, so a container mutates itself from its own methods and callers go through the API it chooses to publish. Cubit re-declares the three as public for the cases where a caller legitimately drives state from outside — test helpers, devtools time-travel, benchmarks.

    Reach for StateContainer when business logic should live in the class, and Cubit when the caller owns the transitions.

    Cubit
    } from '@blac/core';
    interface
    interface CartItem
    CartItem
    {
    CartItem.price: number
    price
    : number;
    }
    class
    class CartCubit
    CartCubit
    extends
    class Cubit<S extends object = any, Args = void, Deps extends object = Record<string, never>>

    Cubit<S> is a StateContainer<S> that exposes mutation publicly.

    That is the whole difference, and it is a real one: on StateContainer emit / patch / update are protected, so a container mutates itself from its own methods and callers go through the API it chooses to publish. Cubit re-declares the three as public for the cases where a caller legitimately drives state from outside — test helpers, devtools time-travel, benchmarks.

    Reach for StateContainer when business logic should live in the class, and Cubit when the caller owns the transitions.

    Cubit
    <{
    items: CartItem[]
    items
    :
    interface CartItem
    CartItem
    [] }> {
    constructor() {
    super({
    items: CartItem[]
    items
    : [] });
    }
    get
    CartCubit.total: number
    total
    () {
    return this.
    StateContainer<{ items: CartItem[]; }, void, Record<string, never>>.state: {
    items: CartItem[];
    }
    state
    .
    items: CartItem[]
    items
    .
    Array<CartItem>.reduce<number>(callbackfn: (previousValue: number, currentValue: CartItem, currentIndex: number, array: CartItem[]) => number, initialValue: number): number (+2 overloads)

    Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

    @param ― callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.

    @param ― initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.

    reduce
    ((
    sum: number
    sum
    ,
    i: CartItem
    i
    ) =>
    sum: number
    sum
    +
    i: CartItem
    i
    .
    CartItem.price: number
    price
    , 0);
    }
    }
    declare const
    const logger: {
    info(msg: string, data: unknown): void;
    }
    logger
    : {
    function info(msg: string, data: unknown): void
    info
    (
    msg: string
    msg
    : string,
    data: unknown
    data
    : unknown): void };
    const
    const stop: () => void
    stop
    =
    watch<typeof CartCubit>(bloc: typeof CartCubit | BlocRef<typeof CartCubit>, callback: (bloc: CartCubit) => void | unique symbol, options?: WatchOptions): () => void (+1 overload)
    watch
    (
    class CartCubit
    CartCubit
    , (
    cart: CartCubit
    cart
    ) => {
    const logger: {
    info(msg: string, data: unknown): void;
    }
    logger
    .
    function info(msg: string, data: unknown): void
    info
    ('cart changed', {
    itemCount: number
    itemCount
    :
    cart: CartCubit
    cart
    .
    StateContainer<{ items: CartItem[]; }, void, Record<string, never>>.state: {
    items: CartItem[];
    }
    state
    .
    items: CartItem[]
    items
    .
    Array<CartItem>.length: number

    Gets or sets the length of the array. This is a number one higher than the highest index in the array.

    length
    ,
    total: number
    total
    :
    cart: CartCubit
    cart
    .
    CartCubit.total: number
    total
    , // getters work here too
    });
    });

    Bridging to non-React code

    Drive an imperative library (a chart, a map, a canvas) from bloc state without a React wrapper:

    import {
    const watch: WatchFn
    watch
    } from '@blac/core';
    import {
    class Cubit<S extends object = any, Args = void, Deps extends object = Record<string, never>>

    Cubit<S> is a StateContainer<S> that exposes mutation publicly.

    That is the whole difference, and it is a real one: on StateContainer emit / patch / update are protected, so a container mutates itself from its own methods and callers go through the API it chooses to publish. Cubit re-declares the three as public for the cases where a caller legitimately drives state from outside — test helpers, devtools time-travel, benchmarks.

    Reach for StateContainer when business logic should live in the class, and Cubit when the caller owns the transitions.

    Cubit
    } from '@blac/core';
    class
    class MetricsCubit
    MetricsCubit
    extends
    class Cubit<S extends object = any, Args = void, Deps extends object = Record<string, never>>

    Cubit<S> is a StateContainer<S> that exposes mutation publicly.

    That is the whole difference, and it is a real one: on StateContainer emit / patch / update are protected, so a container mutates itself from its own methods and callers go through the API it chooses to publish. Cubit re-declares the three as public for the cases where a caller legitimately drives state from outside — test helpers, devtools time-travel, benchmarks.

    Reach for StateContainer when business logic should live in the class, and Cubit when the caller owns the transitions.

    Cubit
    <{
    series: number[]
    series
    : number[] }> {
    constructor() {
    super({
    series: number[]
    series
    : [] });
    }
    }
    declare function
    function createChart(el: HTMLElement): {
    setData(series: number[]): void;
    destroy(): void;
    }
    createChart
    (
    el: HTMLElement
    el
    :
    interface HTMLElement

    The HTMLElement interface represents any HTML element. Some elements directly implement this interface, while others implement it via an interface that inherits it.

    MDN Reference

    HTMLElement
    ): {
    function setData(series: number[]): void
    setData
    (
    series: number[]
    series
    : number[]): void;
    function destroy(): void
    destroy
    (): void;
    };
    function
    function attachChart(el: HTMLElement): () => void
    attachChart
    (
    el: HTMLElement
    el
    :
    interface HTMLElement

    The HTMLElement interface represents any HTML element. Some elements directly implement this interface, while others implement it via an interface that inherits it.

    MDN Reference

    HTMLElement
    ) {
    const
    const chart: {
    setData(series: number[]): void;
    destroy(): void;
    }
    chart
    =
    function createChart(el: HTMLElement): {
    setData(series: number[]): void;
    destroy(): void;
    }
    createChart
    (
    el: HTMLElement
    el
    );
    const
    const stop: () => void
    stop
    =
    watch<typeof MetricsCubit>(bloc: typeof MetricsCubit | BlocRef<typeof MetricsCubit>, callback: (bloc: MetricsCubit) => void | unique symbol, options?: WatchOptions): () => void (+1 overload)
    watch
    (
    class MetricsCubit
    MetricsCubit
    , (
    metrics: MetricsCubit
    metrics
    ) => {
    const chart: {
    setData(series: number[]): void;
    destroy(): void;
    }
    chart
    .
    function setData(series: number[]): void
    setData
    (
    metrics: MetricsCubit
    metrics
    .
    StateContainer<{ series: number[]; }, void, Record<string, never>>.state: {
    series: number[];
    }
    state
    .
    series: number[]
    series
    );
    });
    // hand the caller a teardown that stops the watch and the chart
    return () => {
    const stop: () => void
    stop
    ();
    const chart: {
    setData(series: number[]): void;
    destroy(): void;
    }
    chart
    .
    function destroy(): void
    destroy
    ();
    };
    }

    watch vs subscribe and consume

    Both observe a single container outside React; both fire once per microtask flush. The differences:

    • watch gives you the instance (so getters and methods are available), accepts multiple blocs, supports instance() references and the watch.STOP sentinel, and fires once immediately on setup.
    • subscribe / consume are the lower-level container methods. subscribe(cb) fires on every change; consume(cb) returns a consumer you scope with setPaths(paths), so the callback fires only when one of those paths changes. Neither fires on setup, and both give you the changed path set rather than the instance — read state off the container yourself. Prefer watch for whole-state observation; reach for consume when you need path-scoped interest. See subscribe and consume.
    import {
    const watch: WatchFn
    watch
    ,
    function ensure<T extends StateContainerConstructor>(BlocClass: T, opts?: {
    args?: ExtractArgs<T>;
    }): InstanceType<T>

    Ensure an instance exists without taking ownership (no ref added). Instance identity is derived purely from args, matching acquire.

    ensure
    } from '@blac/core';
    import {
    class Cubit<S extends object = any, Args = void, Deps extends object = Record<string, never>>

    Cubit<S> is a StateContainer<S> that exposes mutation publicly.

    That is the whole difference, and it is a real one: on StateContainer emit / patch / update are protected, so a container mutates itself from its own methods and callers go through the API it chooses to publish. Cubit re-declares the three as public for the cases where a caller legitimately drives state from outside — test helpers, devtools time-travel, benchmarks.

    Reach for StateContainer when business logic should live in the class, and Cubit when the caller owns the transitions.

    Cubit
    } from '@blac/core';
    class
    class UserCubit
    UserCubit
    extends
    class Cubit<S extends object = any, Args = void, Deps extends object = Record<string, never>>

    Cubit<S> is a StateContainer<S> that exposes mutation publicly.

    That is the whole difference, and it is a real one: on StateContainer emit / patch / update are protected, so a container mutates itself from its own methods and callers go through the API it chooses to publish. Cubit re-declares the three as public for the cases where a caller legitimately drives state from outside — test helpers, devtools time-travel, benchmarks.

    Reach for StateContainer when business logic should live in the class, and Cubit when the caller owns the transitions.

    Cubit
    <{
    name: string
    name
    : string }> {
    constructor() {
    super({
    name: string
    name
    : '' });
    }
    }
    const
    const user: UserCubit
    user
    =
    ensure<typeof UserCubit>(BlocClass: typeof UserCubit, opts?: {
    args?: void | undefined;
    } | undefined): UserCubit

    Ensure an instance exists without taking ownership (no ref added). Instance identity is derived purely from args, matching acquire.

    ensure
    (
    class UserCubit
    UserCubit
    );
    // consume: path-scoped, fires on change (not on setup)
    const
    const consumer: Consumer
    consumer
    =
    const user: UserCubit
    user
    .
    StructuralContainer<{ name: string; }>.consume(cb: (dirty: PathSet) => void): Consumer

    Register a reader. It watches nothing until setPaths is called; after that cb runs, once per flush, when a watched path changed value. cb receives the changed path ids among those any consumer watches.

    consume
    (() => {
    var console: Console
    console
    .
    Console.log(...data: any[]): void

    The console.log() static method outputs a message to the console.

    MDN Reference

    log
    (
    const user: UserCubit
    user
    .
    StateContainer<{ name: string; }, void, Record<string, never>>.state: {
    name: string;
    }
    state
    .
    name: string
    name
    );
    });
    const consumer: Consumer
    consumer
    .
    Consumer.setPaths(paths: PathSet): void

    Replace the watched paths. The set is indexed as passed and must not be mutated afterwards; passing the current set again is a no-op. ALL_PATHS wakes on every change.

    setPaths
    (new
    var Set: SetConstructor
    new <number>(iterable?: Iterable<number> | null | undefined) => Set<number> (+1 overload)
    Set
    ([
    const user: UserCubit
    user
    .
    StructuralContainer<{ name: string; }>.interner: PathInterner
    interner
    .
    PathInterner.intern(path: string): PathId
    intern
    ('name')]));
    // watch: the instance, fires immediately, supports STOP and multiple blocs
    const
    const stop: () => void
    stop
    =
    watch<typeof UserCubit>(bloc: typeof UserCubit | BlocRef<typeof UserCubit>, callback: (bloc: UserCubit) => void | unique symbol, options?: WatchOptions): () => void (+1 overload)
    watch
    (
    class UserCubit
    UserCubit
    , (
    u: UserCubit
    u
    ) => {
    var console: Console
    console
    .
    Console.log(...data: any[]): void

    The console.log() static method outputs a message to the console.

    MDN Reference

    log
    (
    u: UserCubit
    u
    .
    StateContainer<{ name: string; }, void, Record<string, never>>.state: {
    name: string;
    }
    state
    .
    name: string
    name
    );
    });

    See also

    • System Events — the microtask flush and coalescing model behind watch
    • Tracking — why watch does not auto-track, and how useBloc does
    • Dependency Tracking — the React side of selective re-renders