blac

    Core

    Cubit

    A Cubit is the state container you subclass for almost everything in BlaC — typed state, emit/update/patch mutations, derived getters, and the args/deps lifecycle.

    A Cubit is a state container that holds a typed state value and exposes methods to change it. It is the class you subclass for almost everything in BlaC. Every signature on this page is quoted from the @blac/core source.

    class Cubit<
    S extends object = any,
    Args = void,
    Deps extends object = Record<string, never>,
    > extends StateContainer<S, Args, Deps> {
    emit(next: S): void;
    patch(partial: DeepPartial<S>): void;
    update(fn: (state: S) => S): void;
    }
    Type parameterDefaultDescription
    SanyThe state shape. Must be an object type (S extends object); primitives like number or string are not supported as state.
    ArgsvoidSerializable construction data delivered to init(args). See Args.
    DepsRecord<string, never>Non-serializable handles injected per consumer and read via this.deps. See Deps.

    Cubit re-declares emit, patch and update as public; on StateContainer the three are protected. That is the difference between them: a StateContainer owns its own transitions and callers go through the methods it exposes, while a Cubit can also be driven from outside. Extend Cubit unless you specifically want mutation sealed inside the class.

    Why a class? (and why “Cubit”, not “Bloc”)

    BlaC has exactly two base types: StateContainer (the abstract engine) and Cubit (the concrete class you extend). There is no Bloc class — if you are coming from flutter_bloc, the closest equivalent is Cubit. We use the name “bloc” colloquially to mean “any state-container instance,” but the only thing you ever extend is Cubit. See Core Concepts for the full StateContainer / Cubit / bloc / instance hierarchy.

    The state lives in a class for three concrete reasons:

    • State and the logic that changes it live together. Methods like addItem sit next to the state they mutate, so an action is a method call, not a reducer plus an action-type constant plus a dispatch.
    • Derived values are just getters. A get total() is computed on read and tracked automatically — no selector library, no memo wiring (see Getters).
    • Instances have identity and a lifecycle. The registry can create, key, ref-count, and dispose instances. That is what makes shared-vs-scoped state, args, and deps possible (see Instance Management).

    For the deeper rationale (why proxy tracking over selectors, why microtask batching, how this compares to Redux/Zustand/MobX), see the Mental Model.

    Constructor

    Define your state type as a generic parameter and pass the initial state to super().

    constructor(initialState: S, options?: StructuralContainerOptions)
    ParameterTypeRequiredDescription
    initialStateSyesThe starting state. Must be an object.
    optionsStructuralContainerOptionsnoLow-level container options (custom per-path equality, skeleton hints). Most subclasses omit it.

    Returns: a new instance. The registry always builds instances zero-arg, so your subclass constructor takes no arguments — it only calls super(initialState).

    Behavior. Runs before init(args). Use it solely to set the initial state; args-derived setup belongs in init.

    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 TodoState
    TodoState
    {
    TodoState.items: string[]
    items
    : string[];
    TodoState.filter: "all" | "active" | "done"
    filter
    : 'all' | 'active' | 'done';
    }
    class
    class TodoCubit
    TodoCubit
    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
    <
    interface TodoState
    TodoState
    > {
    constructor() {
    super({
    TodoState.items: string[]
    items
    : [],
    TodoState.filter: "all" | "active" | "done"
    filter
    : 'all' });
    }
    TodoCubit.addItem: (text: string) => void
    addItem
    = (
    text: string
    text
    : string) => {
    this.
    Cubit<TodoState, void, Record<string, never>>.update(fn: (state: TodoState) => TodoState): void
    update
    ((
    s: TodoState
    s
    ) => ({ ...
    s: TodoState
    s
    ,
    TodoState.items: string[]
    items
    : [...
    s: TodoState
    s
    .
    TodoState.items: string[]
    items
    ,
    text: string
    text
    ] }));
    };
    TodoCubit.setFilter: (filter: TodoState["filter"]) => void
    setFilter
    = (
    filter: "all" | "active" | "done"
    filter
    :
    interface TodoState
    TodoState
    ['filter']) => {
    this.
    Cubit<TodoState, void, Record<string, never>>.patch(partial: {
    items?: readonly string[] | undefined;
    filter?: "all" | "active" | "done" | undefined;
    }): void

    Shallow-or-deep patch: accepts a DeepPartial<S> so nested object branches type-check without casts. deepMerge walks plain-object branches and treats class instances, arrays, Date, Map, Set, etc. as atomic leaves.

    Dirty paths are derived from the patch's shape but value-filtered: a path is marked only if its value actually changed. Plain-object branches are handled precisely by changedPathsFromPatch. For atomic-leaf replacements (arrays, class instances, etc.) where changedPathsFromPatch would emit a coarse ancestor-watch mark, _refineAncestorMarks replaces it with precise skeleton-child diffs so per-index consumers only wake when their specific value changed.

    @see ― emit for the StateContainer concerns layered onto super.patch.

    patch
    ({
    filter?: "all" | "active" | "done" | undefined
    filter
    });
    };
    }

    Mutation methods

    State in BlaC is immutable from the outside: you never assign to this.state.x. Instead you hand the container a new state and it diffs the change, marks which paths moved, and wakes only the consumers that read those paths. The three methods below are three ways to produce that next state.

    emit(next)

    Replace the entire state. Use when you have the full new state ready.

    emit(next: S): void
    ParameterTypeRequiredDescription
    nextSyesThe complete new state. Replaces the current state wholesale — it does not merge.

    Returns: void.

    Behavior. emit is a no-op when the next state is equal to the current one: it short-circuits if prev === next by reference, or if the configured equality function (default: shallow per-key Object.is) reports them equal. So emitting an object that happens to match the current state will not wake any consumers. Equality is configurable per class via blac({ equality }) and globally via configureBlac — see Configuration.

    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 CounterState
    CounterState
    {
    CounterState.count: number
    count
    : number;
    CounterState.label: string
    label
    : string;
    }
    class
    class CounterCubit
    CounterCubit
    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
    <
    interface CounterState
    CounterState
    > {
    constructor() {
    super({
    CounterState.count: number
    count
    : 0,
    CounterState.label: string
    label
    : 'start' });
    }
    CounterCubit.reset: () => void
    reset
    = () => {
    this.
    Cubit<CounterState, void, Record<string, never>>.emit(next: CounterState): void
    emit
    ({
    CounterState.count: number
    count
    : 0,
    CounterState.label: string
    label
    : 'reset' }); // full replacement
    };
    }

    update(fn)

    Derive new state from the current state. Use when you need to read the current state to compute the next one.

    update(fn: (state: S) => S): void
    ParameterTypeRequiredDescription
    fn(state: S) => SyesA reducer receiving the current state and returning the complete next state.

    Returns: void.

    Behavior. update is sugar over emit: it calls this.emit(fn(currentState)), so it inherits emit’s equality short-circuit. Your fn must return the full next state — the same “no merge” rule as emit applies, so spread the existing state when you only change a few fields.

    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 CounterCubit
    CounterCubit
    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
    <{
    count: number
    count
    : number }> {
    constructor() {
    super({
    count: number
    count
    : 0 });
    }
    CounterCubit.increment: () => void
    increment
    = () => {
    this.
    Cubit<{ count: number; }, void, Record<string, never>>.update(fn: (state: {
    count: number;
    }) => {
    count: number;
    }): void
    update
    ((
    current: {
    count: number;
    }
    current
    ) => ({ ...
    current: {
    count: number;
    }
    current
    ,
    count: number
    count
    :
    current: {
    count: number;
    }
    current
    .
    count: number
    count
    + 1 }));
    };
    }

    patch(partial)

    Deep-merge partial changes into the current state. Use when you want to update some fields without touching others.

    patch(partial: DeepPartial<S>): void
    ParameterTypeRequiredDescription
    partialDeepPartial<S>yesThe subtree to merge. Nested objects can be patched without spreading the full structure; arrays, Date, Map, Set, and class instances are treated as atomic leaves.

    Returns: void.

    Behavior. patch deep-merges along plain-object branches and value-filters the result: a path is marked dirty only if its value actually changed. It skips the update entirely if all provided top-level values are already Object.is-equal to the current state (a shallow pre-spread no-op check), and deepMerge also returns the previous state by reference on a deep no-op.

    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 ProfileState
    ProfileState
    {
    ProfileState.loading: boolean
    loading
    : boolean;
    ProfileState.user: {
    profile: {
    name: string;
    age: number;
    };
    }
    user
    : {
    profile: {
    name: string;
    age: number;
    }
    profile
    : {
    name: string
    name
    : string;
    age: number
    age
    : number } };
    }
    class
    class ProfileCubit
    ProfileCubit
    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
    <
    interface ProfileState
    ProfileState
    > {
    constructor() {
    super({
    ProfileState.loading: boolean
    loading
    : false,
    ProfileState.user: {
    profile: {
    name: string;
    age: number;
    };
    }
    user
    : {
    profile: {
    name: string;
    age: number;
    }
    profile
    : {
    name: string
    name
    : '',
    age: number
    age
    : 0 } } });
    }
    ProfileCubit.startLoad: () => void
    startLoad
    = () => {
    this.
    Cubit<ProfileState, void, Record<string, never>>.patch(partial: {
    loading?: boolean | undefined;
    user?: {
    profile?: {
    name?: string | undefined;
    age?: number | undefined;
    } | undefined;
    } | undefined;
    }): void

    Shallow-or-deep patch: accepts a DeepPartial<S> so nested object branches type-check without casts. deepMerge walks plain-object branches and treats class instances, arrays, Date, Map, Set, etc. as atomic leaves.

    Dirty paths are derived from the patch's shape but value-filtered: a path is marked only if its value actually changed. Plain-object branches are handled precisely by changedPathsFromPatch. For atomic-leaf replacements (arrays, class instances, etc.) where changedPathsFromPatch would emit a coarse ancestor-watch mark, _refineAncestorMarks replaces it with precise skeleton-child diffs so per-index consumers only wake when their specific value changed.

    @see ― emit for the StateContainer concerns layered onto super.patch.

    patch
    ({
    loading?: boolean | undefined
    loading
    : true });
    };
    ProfileCubit.rename: (name: string) => void
    rename
    = (
    name: string
    name
    : string) => {
    // Only updates user.profile.name — age and other fields are preserved
    this.
    Cubit<ProfileState, void, Record<string, never>>.patch(partial: {
    loading?: boolean | undefined;
    user?: {
    profile?: {
    name?: string | undefined;
    age?: number | undefined;
    } | undefined;
    } | undefined;
    }): void

    Shallow-or-deep patch: accepts a DeepPartial<S> so nested object branches type-check without casts. deepMerge walks plain-object branches and treats class instances, arrays, Date, Map, Set, etc. as atomic leaves.

    Dirty paths are derived from the patch's shape but value-filtered: a path is marked only if its value actually changed. Plain-object branches are handled precisely by changedPathsFromPatch. For atomic-leaf replacements (arrays, class instances, etc.) where changedPathsFromPatch would emit a coarse ancestor-watch mark, _refineAncestorMarks replaces it with precise skeleton-child diffs so per-index consumers only wake when their specific value changed.

    @see ― emit for the StateContainer concerns layered onto super.patch.

    patch
    ({
    user?: {
    profile?: {
    name?: string | undefined;
    age?: number | undefined;
    } | undefined;
    } | undefined
    user
    : {
    profile?: {
    name?: string | undefined;
    age?: number | undefined;
    } | undefined
    profile
    : {
    name?: string | undefined
    name
    } } });
    };
    }

    Choosing a method

    ScenarioMethod
    Full state replacementemit
    Derived from current stateupdate
    Update a few fieldspatch
    Toggle a booleanupdate
    Reset to initial stateemit

    Getters: derived state

    Getters are how you model derived state — values computed from other state rather than stored. Prefer a getter over storing a computed field: a stored total can drift out of sync with items, but a get total() is recomputed on every read and can never be stale.

    A getter has no signature fence of its own — it is plain TypeScript on your subclass. In React render, it participates in auto-tracking:

    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
    ,
    item: CartItem
    item
    ) =>
    sum: number
    sum
    +
    item: CartItem
    item
    .
    CartItem.price: number
    price
    , 0);
    }
    get
    CartCubit.isEmpty: boolean
    isEmpty
    () {
    return this.
    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
    === 0;
    }
    CartCubit.addItem: (item: CartItem) => void
    addItem
    = (
    item: CartItem
    item
    :
    interface CartItem
    CartItem
    ) => {
    this.
    Cubit<{ items: CartItem[]; }, void, Record<string, never>>.update(fn: (state: {
    items: CartItem[];
    }) => {
    items: CartItem[];
    }): void
    update
    ((
    s: {
    items: CartItem[];
    }
    s
    ) => ({
    items: CartItem[]
    items
    : [...
    s: {
    items: CartItem[];
    }
    s
    .
    items: CartItem[]
    items
    ,
    item: CartItem
    item
    ] }));
    };
    }
    function CartSummary() {
    const [, cart] = useBloc(CartCubit);
    // re-renders when items change because the getter reads this.state.items
    return <span>Total: ${cart.total}</span>;
    }

    Lifecycle, args, and deps

    A Cubit instance moves through a fixed sequence, and the input hooks slot into specific points:

    1. Construct — new Type() runs your constructor, which calls super(initialState). The constructor takes no arguments; the registry always builds instances zero-arg.
    2. init(args) — called once, synchronously, after construction and before any consumer reads the first snapshot. This is where args-derived setup belongs.
    3. onDepsChanged(next, prev) — called whenever the merged per-consumer deps view changes (and once more on dispose, with everything cleared).
    4. Mutations — emit / update / patch run as your methods are called.
    5. Dispose — when the last consumer releases the instance (unless keepAlive); fires the dispose system event.

    Steps 2 and 3 are the two input lanes: args for serializable identity data, deps for live non-serializable handles. The rest of this section covers each.

    init(args)

    Seed args-derived state or kick off loads, once per instance, before the first snapshot.

    protected init(args: Args): void
    ParameterTypeRequiredDescription
    argsArgsyesThe args passed at acquire time (useBloc(Type, { args })). void when no Args generic is declared.

    Returns: void.

    Behavior. A protected lifecycle method — not callable from outside the class. The framework calls it once per instance, synchronously after new Type() and before the first state snapshot is read. It replaces the old setConfig/setProps patterns. When useBloc is called with { args }, those args are required at the call site (type error if omitted when Args != void), used to derive the instance identity (different args ⇒ different instance), and available synchronously in init before any consumer sees state. See Passing Inputs for the full args/identity model.

    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 UserCardState
    UserCardState
    {
    UserCardState.name: string
    name
    : string;
    UserCardState.loading: boolean
    loading
    : boolean;
    }
    class
    class UserCardCubit
    UserCardCubit
    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
    <
    interface UserCardState
    UserCardState
    , {
    userId: string
    userId
    : string }> {
    constructor() {
    super({
    UserCardState.name: string
    name
    : '',
    UserCardState.loading: boolean
    loading
    : false });
    }
    // Constructor stays zero-arg. The framework calls init(args) before first snapshot.
    protected
    UserCardCubit.init(args: {
    userId: string;
    }): void

    Called once after construction with the args passed at acquire time, before the first state snapshot is read by any consumer. Override to seed args-derived state (via this.emit(...)) or kick off loads.

    init
    (
    args: {
    userId: string;
    }
    args
    : {
    userId: string
    userId
    : string }) {
    this.
    Cubit<UserCardState, { userId: string; }, Record<string, never>>.patch(partial: {
    name?: string | undefined;
    loading?: boolean | undefined;
    }): void

    Shallow-or-deep patch: accepts a DeepPartial<S> so nested object branches type-check without casts. deepMerge walks plain-object branches and treats class instances, arrays, Date, Map, Set, etc. as atomic leaves.

    Dirty paths are derived from the patch's shape but value-filtered: a path is marked only if its value actually changed. Plain-object branches are handled precisely by changedPathsFromPatch. For atomic-leaf replacements (arrays, class instances, etc.) where changedPathsFromPatch would emit a coarse ancestor-watch mark, _refineAncestorMarks replaces it with precise skeleton-child diffs so per-index consumers only wake when their specific value changed.

    @see ― emit for the StateContainer concerns layered onto super.patch.

    patch
    ({
    loading?: boolean | undefined
    loading
    : true });
    }
    // Side effects (fetch/subscribe/timer) belong here, not in init — see below.
    protected
    UserCardCubit.onActivate(signal: AbortSignal): void

    Called when this instance goes from unowned to owned (first ref or first depend() dependent). Override to start work that should only run while something is actually using the instance — fetches, timers, listeners.

    signal aborts on the matching onDeactivate and on dispose, so async work started here can be cancelled without extra bookkeeping.

    Runs before the first useBlocDeps slice is applied, so this.deps is empty on first activation — use onDepsChanged for deps-driven work.

    onActivate
    (
    signal: AbortSignal
    signal
    :
    interface AbortSignal

    The AbortSignal interface represents a signal object that allows you to communicate with an asynchronous operation (such as a fetch request) and abort it if required via an AbortController object.

    MDN Reference

    AbortSignal
    ) {
    void this.
    UserCardCubit.loadUser(_id: string): Promise<void>
    loadUser
    (this.
    StateContainer<UserCardState, { userId: string; }, Record<string, never>>.args: {
    userId: string;
    } | undefined
    args
    !.
    userId: string
    userId
    );
    }
    private async
    UserCardCubit.loadUser(_id: string): Promise<void>
    loadUser
    (
    _id: string
    _id
    : string) {
    /* ...fetch... */
    }
    }

    onActivate / onDeactivate

    protected onActivate(signal: AbortSignal): void
    protected onDeactivate(): void

    Behavior. onActivate fires on the instance’s 0→1 ownership transition — the moment something first acquires it — and is the recommended place for side effects (fetch, subscribe, start a timer). It hands you an AbortSignal that is aborted when onDeactivate fires on the matching 1→0 transition, so cleanup is one line:

    class
    class UserCardCubit
    UserCardCubit
    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
    <
    interface UserCardState
    UserCardState
    , {
    userId: string
    userId
    : string }> {
    constructor() {
    super({
    UserCardState.name: string
    name
    : '' });
    }
    protected
    UserCardCubit.onActivate(signal: AbortSignal): void

    Called when this instance goes from unowned to owned (first ref or first depend() dependent). Override to start work that should only run while something is actually using the instance — fetches, timers, listeners.

    signal aborts on the matching onDeactivate and on dispose, so async work started here can be cancelled without extra bookkeeping.

    Runs before the first useBlocDeps slice is applied, so this.deps is empty on first activation — use onDepsChanged for deps-driven work.

    onActivate
    (
    signal: AbortSignal
    signal
    :
    interface AbortSignal

    The AbortSignal interface represents a signal object that allows you to communicate with an asynchronous operation (such as a fetch request) and abort it if required via an AbortController object.

    MDN Reference

    AbortSignal
    ) {
    function fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>
    fetch
    (`/api/users/${this.
    StateContainer<UserCardState, { userId: string; }, Record<string, never>>.args: {
    userId: string;
    } | undefined
    args
    !.
    userId: string
    userId
    }`, {
    RequestInit.signal?: AbortSignal | null | undefined

    An AbortSignal to set request's signal.

    signal
    })
    .
    Promise<Response>.then<any, never>(onfulfilled?: ((value: Response) => any) | null | undefined, onrejected?: ((reason: any) => PromiseLike<never>) | null | undefined): Promise<any>

    Attaches callbacks for the resolution and/or rejection of the Promise.

    @param ― onfulfilled The callback to execute when the Promise is resolved.

    @param ― onrejected The callback to execute when the Promise is rejected.

    @returns ― A Promise for the completion of which ever callback is executed.

    then
    ((
    r: Response
    r
    ) =>
    r: Response
    r
    .
    Body.json(): Promise<any>
    json
    ())
    .
    Promise<any>.then<void, never>(onfulfilled?: ((value: any) => void | PromiseLike<void>) | null | undefined, onrejected?: ((reason: any) => PromiseLike<never>) | null | undefined): Promise<void>

    Attaches callbacks for the resolution and/or rejection of the Promise.

    @param ― onfulfilled The callback to execute when the Promise is resolved.

    @param ― onrejected The callback to execute when the Promise is rejected.

    @returns ― A Promise for the completion of which ever callback is executed.

    then
    ((
    user: any
    user
    ) => this.
    Cubit<UserCardState, { userId: string; }, Record<string, never>>.patch(partial: {
    name?: string | undefined;
    }): void

    Shallow-or-deep patch: accepts a DeepPartial<S> so nested object branches type-check without casts. deepMerge walks plain-object branches and treats class instances, arrays, Date, Map, Set, etc. as atomic leaves.

    Dirty paths are derived from the patch's shape but value-filtered: a path is marked only if its value actually changed. Plain-object branches are handled precisely by changedPathsFromPatch. For atomic-leaf replacements (arrays, class instances, etc.) where changedPathsFromPatch would emit a coarse ancestor-watch mark, _refineAncestorMarks replaces it with precise skeleton-child diffs so per-index consumers only wake when their specific value changed.

    @see ― emit for the StateContainer concerns layered onto super.patch.

    patch
    (
    user: any
    user
    ));
    }
    }

    onActivated

    import { onActivated } from '@blac/core';
    onActivated(container: StateContainer, handler: (signal: AbortSignal) => void): () => void

    Behavior. Subscribes to the same 0→1 activation as onActivate, but from outside the class — a helper or a field initializer can hook a container’s lifecycle without subclassing. The handler runs on every activation with that span’s AbortSignal, and immediately if the container is already active. It returns an unsubscribe function, and handlers are dropped when the container is disposed. A throwing handler is logged and does not stop the others.

    function pollWhileActive(cubit: StateContainer) {
    return onActivated(cubit, (signal) => {
    const timer = setInterval(refresh, 30_000);
    signal.addEventListener('abort', () => clearInterval(timer));
    });
    }

    Called from a field initializer, the handler runs after args and the registry are set (registration itself happens earlier), so read this.args inside the handler.

    Async Resources is built on this hook.

    init is not deprecated and keeps working exactly as documented above — it’s still the right place to seed synchronous state from args. Only the recommendation for side effects moves: onActivate fires only when something actually owns the instance, so work started there is never wasted on a discarded or SSR-only render.

    static key: explicit identity declaration

    By default, instance identity is the structural hash of all args. Override with a static property on the class to control exactly which args distinguish one instance from another:

    static key: (args: Args) => string

    Behavior. A function (args: Args) => string declared once on the class, not at every call site. When absent, BlaC hashes the full args object. Args not referenced by key ride along as config but do not fork instances.

    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 DocState
    DocState
    {
    DocState.title: string
    title
    : string;
    }
    class
    class DocumentCubit
    DocumentCubit
    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
    <
    interface DocState
    DocState
    ,
    {
    docId: string
    docId
    : string;
    readonly: boolean
    readonly
    : boolean }
    > {
    constructor() {
    super({
    DocState.title: string
    title
    : '' });
    }
    static
    DocumentCubit.key: (args: DocumentCubit["args"]) => string
    key
    = (
    args: {
    docId: string;
    readonly: boolean;
    } | undefined
    args
    :
    class DocumentCubit
    DocumentCubit
    ['args']) =>
    args: {
    docId: string;
    readonly: boolean;
    } | undefined
    args
    ?.
    docId: string | undefined
    docId
    ?? '';
    // `readonly` rides along as config but does NOT fork instances
    }

    Deps: non-serializable handles

    Declare a Deps type as the third generic parameter to receive non-serializable values (refs, callbacks, controller instances) that can’t go in args. Deps are read lazily via this.deps.x and may be undefined — always guard.

    get deps(): Readonly<Deps>

    Returns: a Readonly<Deps> view — the union of every consumer’s contributed slice.

    Key properties of deps:

    • Never key identity — different refs don’t fork the instance.
    • Per-consumer merged — each useBloc call contributes its own slice; the bloc sees the union (last writer wins on key collisions).
    • Live — updated after each commit; may change over time. May legitimately be undefined, so always optional-chain.
    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 UploadState
    UploadState
    {
    UploadState.progress: number
    progress
    : number;
    }
    interface
    interface InputRef
    InputRef
    {
    InputRef.current?: {
    click?: () => void;
    } | undefined
    current
    ?: {
    click?: (() => void) | undefined
    click
    ?: () => void };
    }
    class
    class FileUploadCubit
    FileUploadCubit
    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
    <
    interface UploadState
    UploadState
    ,
    {
    endpoint: string
    endpoint
    : string }, // Args
    {
    inputRef?: InputRef | undefined
    inputRef
    ?:
    interface InputRef
    InputRef
    } // Deps
    > {
    private
    FileUploadCubit.endpoint: string
    endpoint
    = '';
    constructor() {
    super({
    UploadState.progress: number
    progress
    : 0 });
    }
    protected
    FileUploadCubit.init(args: {
    endpoint: string;
    }): void

    Called once after construction with the args passed at acquire time, before the first state snapshot is read by any consumer. Override to seed args-derived state (via this.emit(...)) or kick off loads.

    init
    (
    args: {
    endpoint: string;
    }
    args
    : {
    endpoint: string
    endpoint
    : string }) {
    this.
    FileUploadCubit.endpoint: string
    endpoint
    =
    args: {
    endpoint: string;
    }
    args
    .
    endpoint: string
    endpoint
    ;
    }
    FileUploadCubit.openPicker(): void
    openPicker
    () {
    this.
    StateContainer<UploadState, { endpoint: string; }, { inputRef?: InputRef; }>.deps: Readonly<{
    inputRef?: InputRef;
    }>
    deps
    .
    inputRef?: InputRef | undefined
    inputRef
    ?.
    InputRef.current?: {
    click?: () => void;
    } | undefined
    current
    ?.
    click?: (() => void) | undefined
    click
    ?.(); // lazy read, may be undefined
    }
    }

    onDepsChanged(next, prev)

    For handles that require initialization when they arrive (a canvas element, a rich-text-editor controller), implement onDepsChanged. It fires after each deps merge with the new and previous combined views.

    protected onDepsChanged(next: Readonly<Deps>, prev: Readonly<Deps>): void
    ParameterTypeRequiredDescription
    nextReadonly<Deps>yesThe merged deps view after this reconcile.
    prevReadonly<Deps>yesThe merged view before it. Compare the two to detect arrivals and departures.

    Returns: void.

    Behavior. Optional — blocs that don’t declare it just read this.deps.x lazily. When declared, it gives the bloc clean acquire/release edges without any consumer-side cleanup wiring. It also fires once on dispose with every key cleared (so next.x is undefined), giving you a teardown edge.

    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 RenderState
    RenderState
    {
    RenderState.ready: boolean
    ready
    : boolean;
    }
    interface
    interface RteController
    RteController
    {
    RteController.destroy(): void
    destroy
    (): void;
    }
    class
    class CanvasRendererCubit
    CanvasRendererCubit
    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
    <
    interface RenderState
    RenderState
    ,
    {
    sceneId: string
    sceneId
    : string },
    {
    canvas?: HTMLCanvasElement | undefined
    canvas
    ?:
    interface HTMLCanvasElement

    The HTMLCanvasElement interface provides properties and methods for manipulating the layout and presentation of elements. The HTMLCanvasElement interface also inherits the properties and methods of the HTMLElement interface.

    MDN Reference

    HTMLCanvasElement
    ;
    controller?: RteController | undefined
    controller
    ?:
    interface RteController
    RteController
    }
    > {
    constructor() {
    super({
    RenderState.ready: boolean
    ready
    : false });
    }
    CanvasRendererCubit.onDepsChanged(next: this["deps"], prev: this["deps"]): void
    onDepsChanged
    (
    next: this["deps"]
    next
    : this['deps'],
    prev: this["deps"]
    prev
    : this['deps']) {
    if (
    next: this["deps"]
    next
    .
    canvas?: HTMLCanvasElement | undefined
    canvas
    &&
    next: this["deps"]
    next
    .
    canvas?: HTMLCanvasElement
    canvas
    !==
    prev: this["deps"]
    prev
    .
    canvas?: HTMLCanvasElement | undefined
    canvas
    ) {
    this.
    CanvasRendererCubit.initRenderer(_c: HTMLCanvasElement): void
    initRenderer
    (
    next: this["deps"]
    next
    .
    canvas?: HTMLCanvasElement
    canvas
    );
    }
    if (!
    next: this["deps"]
    next
    .
    canvas?: HTMLCanvasElement | undefined
    canvas
    &&
    prev: this["deps"]
    prev
    .
    canvas?: HTMLCanvasElement | undefined
    canvas
    ) {
    this.
    CanvasRendererCubit.disposeRenderer(): void
    disposeRenderer
    ();
    }
    if (
    next: this["deps"]
    next
    .
    controller?: RteController | undefined
    controller
    !==
    prev: this["deps"]
    prev
    .
    controller?: RteController | undefined
    controller
    ) {
    this.
    CanvasRendererCubit.bindController(_c?: RteController): void
    bindController
    (
    next: this["deps"]
    next
    .
    controller?: RteController | undefined
    controller
    );
    }
    }
    private
    CanvasRendererCubit.initRenderer(_c: HTMLCanvasElement): void
    initRenderer
    (
    _c: HTMLCanvasElement
    _c
    :
    interface HTMLCanvasElement

    The HTMLCanvasElement interface provides properties and methods for manipulating the layout and presentation of elements. The HTMLCanvasElement interface also inherits the properties and methods of the HTMLElement interface.

    MDN Reference

    HTMLCanvasElement
    ) {}
    private
    CanvasRendererCubit.disposeRenderer(): void
    disposeRenderer
    () {}
    private
    CanvasRendererCubit.bindController(_c?: RteController): void
    bindController
    (
    _c: RteController | undefined
    _c
    ?:
    interface RteController
    RteController
    ) {}
    }

    depend(Type)

    Declare a cross-bloc dependency. See Bloc Communication for the full pattern.

    protected depend<T extends StateContainerConstructor>(
    Type: T,
    defaultArgs?: ExtractArgs<T>,
    ): DepHandle<T>
    ParameterTypeRequiredDescription
    TypeT extends StateContainerConstructoryesThe state-container class to depend on.
    defaultArgsExtractArgs<T>noArgs identifying which keyed instance to resolve when an accessor is called without its own args.

    Returns: a DepHandle<T> with two accessors — handle.untracked() resolves the dep against the registry lazily on each call (this.user.untracked()), and handle.track() does the same plus subscribes the reading React consumer. Both take an optional { args } to override defaultArgs per call. See Auto-tracking with .track().

    Behavior. Records the dependency, then returns the handle. Resolving on every call keeps the surface immune to dep-instance churn. Note: .untracked() does not auto-resubscribe to the dep’s channel — consumers that need reactive updates use .track() (or subscribe explicitly via useBloc’s tracker). A naive always-on auto-bridge would cycle on mutual deps, which is why tracking is opt-in.

    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 AuthCubit
    AuthCubit
    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
    <{
    userId: string | null
    userId
    : string | null }> {
    constructor() {
    super({
    userId: string | null
    userId
    : null });
    }
    }
    class
    class ProfileCubit
    ProfileCubit
    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 }> {
    private
    ProfileCubit.auth: DepHandle<typeof AuthCubit>
    auth
    = this.
    StateContainer<{ name: string; }, void, Record<string, never>>.depend<typeof AuthCubit>(Type: typeof AuthCubit, defaultArgs?: void | undefined): DepHandle<typeof AuthCubit>

    Declare a cross-bloc dependency. Returns a branded handle with two accessors — the dep instance is resolved against the registry on each call, which keeps the surface immune to dep-instance churn:

    • handle.track(options?) — reactive read. Returns [state, instance]. Inside a getter reached through the React proxy this subscribes the reading component to the dep's changes (base impl: live, no subscription).
    • handle.untracked(options?) — returns the live instance with no tracking, for imperative method calls and one-off reads.

    defaultArgs resolves the dep instance when an accessor is called without its own args; per-call options.args overrides it and can derive from current state. This does NOT auto-resubscribe outside the React proxy; non-React consumers needing updates should subscribe explicitly.

    depend
    (
    class AuthCubit
    AuthCubit
    );
    constructor() {
    super({
    name: string
    name
    : '' });
    }
    get
    ProfileCubit.currentUserId: string | null
    currentUserId
    () {
    return this.
    ProfileCubit.auth: DepHandle<typeof AuthCubit>
    auth
    .
    DepHandle<typeof AuthCubit>.untracked(options?: DepAccessOptions<typeof AuthCubit> | undefined): AuthCubit
    untracked
    ().
    StateContainer<{ userId: string | null; }, void, Record<string, never>>.state: {
    userId: string | null;
    }
    state
    .
    userId: string | null
    userId
    ; // lazy resolve
    }
    }

    Protected APIs

    These are available inside your Cubit class but not from the outside:

    Public properties

    PropertyTypeDescription
    stateReadonly<S>Current state value
    $blacBlacMeta<S>Identity and lifecycle metadata. See the table below.
    $blac.namestringDisplay name (defaults to class name)
    $blac.idstringUnique instance identifier
    $blac.createdAtnumberCreation timestamp
    $blac.disposedbooleanWhether the instance has been disposed
    $blac.registryStateContainerRegistryThe registry that owns this instance (the global one for a bare new)
    $blac.hydrationBlacHydration<S>Hydration lifecycle object. See below.
    $blac.hydration.statusHydrationStatusCurrent hydration phase ('idle' | 'hydrating' | 'hydrated' | 'error'); see Persistence
    $blac.hydration.isHydratedbooleanShorthand: status === 'hydrated'
    $blac.hydration.wait()Promise<void>Resolves once hydration settles; see Persistence

    Async methods

    Cubits handle async operations naturally. Model loading/error state explicitly and guard against stale responses:

    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 Article
    Article
    {
    Article.id: string
    id
    : string;
    }
    declare const
    const api: {
    fetchArticles(category: string): Promise<Article[]>;
    }
    api
    : {
    function fetchArticles(category: string): Promise<Article[]>
    fetchArticles
    (
    category: string
    category
    : string):
    interface Promise<T>

    Represents the completion of an asynchronous operation

    Promise
    <
    interface Article
    Article
    []> };
    interface
    interface ArticleState
    ArticleState
    {
    ArticleState.articles: Article[]
    articles
    :
    interface Article
    Article
    [];
    ArticleState.status: "idle" | "loading" | "error" | "success"
    status
    : 'idle' | 'loading' | 'error' | 'success';
    ArticleState.error: string | null
    error
    : string | null;
    }
    class
    class ArticleCubit
    ArticleCubit
    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
    <
    interface ArticleState
    ArticleState
    > {
    private
    ArticleCubit.requestId: number
    requestId
    = 0;
    constructor() {
    super({
    ArticleState.articles: Article[]
    articles
    : [],
    ArticleState.status: "idle" | "loading" | "error" | "success"
    status
    : 'idle',
    ArticleState.error: string | null
    error
    : null });
    }
    ArticleCubit.load: (category: string) => Promise<void>
    load
    = async (
    category: string
    category
    : string) => {
    const
    const id: number
    id
    = ++this.
    ArticleCubit.requestId: number
    requestId
    ;
    this.
    Cubit<ArticleState, void, Record<string, never>>.patch(partial: {
    articles?: readonly {
    id?: string | undefined;
    }[] | undefined;
    status?: "idle" | "loading" | "error" | "success" | undefined;
    error?: string | null | undefined;
    }): void

    Shallow-or-deep patch: accepts a DeepPartial<S> so nested object branches type-check without casts. deepMerge walks plain-object branches and treats class instances, arrays, Date, Map, Set, etc. as atomic leaves.

    Dirty paths are derived from the patch's shape but value-filtered: a path is marked only if its value actually changed. Plain-object branches are handled precisely by changedPathsFromPatch. For atomic-leaf replacements (arrays, class instances, etc.) where changedPathsFromPatch would emit a coarse ancestor-watch mark, _refineAncestorMarks replaces it with precise skeleton-child diffs so per-index consumers only wake when their specific value changed.

    @see ― emit for the StateContainer concerns layered onto super.patch.

    patch
    ({
    status?: "idle" | "loading" | "error" | "success" | undefined
    status
    : 'loading',
    error?: string | null | undefined
    error
    : null });
    try {
    const
    const articles: Article[]
    articles
    = await
    const api: {
    fetchArticles(category: string): Promise<Article[]>;
    }
    api
    .
    function fetchArticles(category: string): Promise<Article[]>
    fetchArticles
    (
    category: string
    category
    );
    if (
    const id: number
    id
    !== this.
    ArticleCubit.requestId: number
    requestId
    ) return; // stale response
    this.
    Cubit<ArticleState, void, Record<string, never>>.emit(next: ArticleState): void
    emit
    ({
    ArticleState.articles: Article[]
    articles
    ,
    ArticleState.status: "idle" | "loading" | "error" | "success"
    status
    : 'success',
    ArticleState.error: string | null
    error
    : null });
    } catch (
    function (local var) e: unknown
    e
    ) {
    if (
    const id: number
    id
    !== this.
    ArticleCubit.requestId: number
    requestId
    ) return;
    this.
    Cubit<ArticleState, void, Record<string, never>>.patch(partial: {
    articles?: readonly {
    id?: string | undefined;
    }[] | undefined;
    status?: "idle" | "loading" | "error" | "success" | undefined;
    error?: string | null | undefined;
    }): void

    Shallow-or-deep patch: accepts a DeepPartial<S> so nested object branches type-check without casts. deepMerge walks plain-object branches and treats class instances, arrays, Date, Map, Set, etc. as atomic leaves.

    Dirty paths are derived from the patch's shape but value-filtered: a path is marked only if its value actually changed. Plain-object branches are handled precisely by changedPathsFromPatch. For atomic-leaf replacements (arrays, class instances, etc.) where changedPathsFromPatch would emit a coarse ancestor-watch mark, _refineAncestorMarks replaces it with precise skeleton-child diffs so per-index consumers only wake when their specific value changed.

    @see ― emit for the StateContainer concerns layered onto super.patch.

    patch
    ({
    status?: "idle" | "loading" | "error" | "success" | undefined
    status
    : 'error',
    error?: string | null | undefined
    error
    :
    var String: StringConstructor
    (value?: any) => string

    Allows manipulation and formatting of text strings and determination and location of substrings within strings.

    String
    (
    function (local var) e: unknown
    e
    ) });
    }
    };
    }

    The requestId pattern discards responses from superseded requests. Each new load() call increments the ID, and callbacks from previous calls see a mismatch and bail out.

    See also

    • Mental Model — why class-based containers, proxy tracking, and microtask batching
    • Passing Inputs — the full args / deps identity model

    Troubleshooting

    For the full FAQ see Troubleshooting. Below are the Cubit-specific problems.

    emit silently drops fields

    Symptom: After calling emit, some state fields are undefined even though you didn’t intend to clear them.

    Cause: emit replaces the entire state — it does not merge. Passing a partial object drops every field you omit.

    Fix: Use patch for partial updates, or spread existing state in update:

    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 TodoCubit
    TodoCubit
    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: string[]
    items
    : string[];
    filter: "all" | "done"
    filter
    : 'all' | 'done';
    }> {
    constructor() {
    super({
    items: string[]
    items
    : [],
    filter: "all" | "done"
    filter
    : 'all' });
    }
    // state: { items: [...], filter: 'all' }
    TodoCubit.bad: () => void
    bad
    = () => {
    // @ts-expect-error — items is required; emit replaces the whole state
    this.
    Cubit<{ items: string[]; filter: "all" | "done"; }, void, Record<string, never>>.emit(next: {
    items: string[];
    filter: "all" | "done";
    }): void
    emit
    ({
    filter: "all" | "done"
    filter
    : 'done' }); // items would be undefined!
    };
    // Fix A — patch merges only the listed fields
    TodoCubit.fixA: () => void
    fixA
    = () => this.
    Cubit<{ items: string[]; filter: "all" | "done"; }, void, Record<string, never>>.patch(partial: {
    items?: readonly string[] | undefined;
    filter?: "all" | "done" | undefined;
    }): void

    Shallow-or-deep patch: accepts a DeepPartial<S> so nested object branches type-check without casts. deepMerge walks plain-object branches and treats class instances, arrays, Date, Map, Set, etc. as atomic leaves.

    Dirty paths are derived from the patch's shape but value-filtered: a path is marked only if its value actually changed. Plain-object branches are handled precisely by changedPathsFromPatch. For atomic-leaf replacements (arrays, class instances, etc.) where changedPathsFromPatch would emit a coarse ancestor-watch mark, _refineAncestorMarks replaces it with precise skeleton-child diffs so per-index consumers only wake when their specific value changed.

    @see ― emit for the StateContainer concerns layered onto super.patch.

    patch
    ({
    filter?: "all" | "done" | undefined
    filter
    : 'done' });
    // Fix B — update reads current state first
    TodoCubit.fixB: () => void
    fixB
    = () => this.
    Cubit<{ items: string[]; filter: "all" | "done"; }, void, Record<string, never>>.update(fn: (state: {
    items: string[];
    filter: "all" | "done";
    }) => {
    items: string[];
    filter: "all" | "done";
    }): void
    update
    ((
    s: {
    items: string[];
    filter: "all" | "done";
    }
    s
    ) => ({ ...
    s: {
    items: string[];
    filter: "all" | "done";
    }
    s
    ,
    filter: "all" | "done"
    filter
    : 'done' }));
    }

    See Mutation methods: choosing a method above.

    A new instance is created on every render

    Symptom: DevTools shows a new bloc instance being created and destroyed on every render, or the circuit-breaker throws “max instances exceeded.”

    Cause: A non-serializable value (function, ref, class instance) is in args. Because args must be JSON-serializable, a fresh object reference each render produces a different hash and therefore a different instance key.

    Fix: Move non-serializable values to the bloc’s Deps lane — they never affect instance identity:

    // Non-serializable in args → new instance every render (throws in dev)
    useBloc(UploadCubit, { args: { onComplete: () => {} } });
    // Serializable identity in args, handle in deps
    useBloc(UploadCubit, { args: { endpoint: '/upload' } });
    // (wire the callback via deps — see Cubit Deps and Passing Inputs)

    See Deps: non-serializable handles and Troubleshooting: instance identity.