Skip to content

@blac/core

Core state management primitives

API Sections

SectionDescription
RegistryInstance management and lifecycle
PluginsPlugin system for extending BlaC
Framework AdapterReact integration and dependency tracking
LoggingLogging utilities for debugging
UtilitiesHelper functions, ID generation, and type utilities

Classes

Cubit

Abstract class

Simple state container with direct state emission. Extends StateContainer with public methods for emitting and updating state.

typescript
export declare abstract class Cubit<S extends object = any, P = undefined> extends StateContainer<S, P>

Type Parameters:

  • S
  • P

Constructor:

typescript
constructor(initialState: S);
ParameterTypeDescription
initialStateS

Properties:

PropertyTypeDescription
patchS extends object ? (partial: Partial<S>) => void : neverMerge partial state changes into current state (only for object states)

Methods:

emit

Replace state with a new value and notify all listeners

typescript
emit(newState: S): void;
ParameterTypeDescription
newStateSThe new state value

update

Transform current state using an updater function and emit the new state

typescript
update(updater: (current: S) => S): void;
ParameterTypeDescription
updater(current: S) => SFunction that receives current state and returns new state

StateContainer

Abstract class

Abstract base class for all state containers in BlaC. Provides lifecycle management, subscription handling, ref counting, and integration with the global registry.

typescript
export declare abstract class StateContainer<S extends object = any, P = any>

Type Parameters:

  • S
  • P

Constructor:

typescript
constructor(initialState: S);
ParameterTypeDescription
initialStateS

Properties:

PropertyTypeDescription
__excludeFromDevTools (static)boolean
createdAtnumberTimestamp when this instance was created
debugbooleanWhether debug logging is enabled
enableStackTrace (static)boolean
instanceIdstringUnique identifier for this instance
isDisposed (readonly)booleanWhether this instance has been disposed
lastUpdateTimestampnumberTimestamp of the last state update
namestringDisplay name for this instance
onSystemEvent<E extends SystemEvent>(event: E, handler: SystemEventHandler<S, P, E>) => (() => void)Subscribe to system lifecycle events.
props (readonly)P | undefinedCurrent props value passed to this container
state (readonly)Readonly<S>Current state value

Methods:

dispose

Dispose this instance and clean up resources. Clears all listeners and emits the 'dispose' system event.

typescript
dispose(): void;

emit (protected)

Emit a new state value and notify all listeners.

typescript
protected emit(newState: S): void;
ParameterTypeDescription
newStateSThe new state value

initConfig

Initialize configuration for this instance. Called by the registry after construction.

typescript
initConfig(config: StateContainerConfig): void;
ParameterTypeDescription
configStateContainerConfigConfiguration options

subscribe

Subscribe to state changes

typescript
subscribe(listener: StateListener<S>): () => void;
ParameterTypeDescription
listenerStateListener<S>Function called when state changes

Returns: Unsubscribe function

update (protected)

Update state using a transform function.

typescript
protected update(updater: (current: S) => S): void;
ParameterTypeDescription
updater(current: S) => SFunction that receives current state and returns new state

updateProps

Update the props for this container. Emits the 'propsUpdated' system event.

typescript
updateProps(newProps: P): void;
ParameterTypeDescription
newPropsPThe new props value

Examples:

ts
class CounterBloc extends StateContainer<{ count: number }> {
  constructor() {
    super({ count: 0 });
  }
  increment() {
    this.emit({ count: this.state.count + 1 });
  }
}

Vertex

Abstract class

Event-driven state container using discriminated union events. Extends StateContainer with event-based state transitions and compile-time exhaustive checking for event handlers.

typescript
export declare abstract class Vertex<S extends object = object, E extends DiscriminatedEvent = DiscriminatedEvent, P = undefined> extends StateContainer<S, P>

Type Parameters:

  • S
  • E
  • P

Constructor:

typescript
constructor(initialState: S);
ParameterTypeDescription
initialStateS

Properties:

PropertyTypeDescription
add(event: E) => voidDispatch an event for processing. Events are processed synchronously; if called during processing, the event is queued and processed after the current event completes.

Methods:

createHandlers (protected)

Register all event handlers with exhaustive type checking. TypeScript will error if any event type from the union is missing.

typescript
protected createHandlers(handlers: EventHandlerMap<E, S>): void;
ParameterTypeDescription
handlersEventHandlerMap<E, S>Map of event type to handler function

onEventError (protected)

Called when an error occurs during event processing. Override to implement custom error handling.

typescript
protected onEventError(_event: EventWithMetadata<E>, _error: Error): void;
ParameterTypeDescription
_eventEventWithMetadata<E>The event that caused the error
_errorErrorThe error that occurred

Examples:

typescript
type CounterEvent =
  | { type: 'increment'; amount: number }
  | { type: 'decrement' }
  | { type: 'reset' };

class CounterVertex extends Vertex<{ count: number }, CounterEvent> {
  constructor() {
    super({ count: 0 });
    this.createHandlers({
      increment: (event, emit) => {
        emit({ count: this.state.count + event.amount });
      },
      decrement: (_, emit) => {
        emit({ count: this.state.count - 1 });
      },
      reset: (_, emit) => {
        emit({ count: 0 });
      },
    });
  }

  increment = (amount = 1) => this.add({ type: 'increment', amount });
}

Interfaces

StateContainerConfig

Configuration options for initializing a StateContainer instance

typescript
export interface StateContainerConfig
PropertyTypeDescription
debug (optional)booleanEnable debug logging for this instance
instanceId (optional)stringCustom instance identifier
name (optional)stringDisplay name for the instance (defaults to class name)

SystemEventPayloads

Payload types for each system event @template S - State type @template P - Props type

typescript
export interface SystemEventPayloads<S, P>
PropertyTypeDescription
disposevoidEmitted when the instance is disposed
propsUpdatedPEmitted when props are updated via updateProps()
stateChangedSEmitted when state changes via emit() or update()

Functions

blac

Decorator to configure StateContainer classes.

typescript
export declare function blac(options: BlacOptions): <T extends new (...args: any[]) => any>(target: T, _context?: ClassDecoratorContext) => T;
ParameterTypeDescription
optionsBlacOptions

Examples:

Decorator syntax (requires experimentalDecorators or TC39 decorators)

ts
@blac({ isolated: true })
class FormBloc extends Cubit<FormState> {}

@blac({ keepAlive: true })
class AuthBloc extends Cubit<AuthState> {}

@blac({ excludeFromDevTools: true })
class InternalBloc extends Cubit<InternalState> {}

Function syntax (no decorator support needed)

ts
const FormBloc = blac({ isolated: true })(
  class extends Cubit<FormState> {}
);

Types

ExtractConstructorArgs

Extract constructor argument types from a class @template T - The class type

typescript
export type ExtractConstructorArgs<T> = T extends new (...args: infer P) => any ? P : never[];

ExtractProps

Extract the props type from a StateContainer @template T - The StateContainer type

typescript
export type ExtractProps<T> = T extends StateContainerConstructor<any, infer P> ? P : undefined;

ExtractState

Extract the state type from a StateContainer @template T - The StateContainer type

typescript
export type ExtractState<T> = T extends StateContainerConstructor<infer S, any> ? Readonly<S> : never;

SystemEvent

System events emitted by StateContainer lifecycle

typescript
export type SystemEvent = 'propsUpdated' | 'stateChanged' | 'dispose';

Released under the MIT License.