@smoothbricks/statebus-core
v0.1.1
Published
Platform-agnostic core types and runtime for StateBus.
Downloads
198
Readme
StateBus Core
Platform-agnostic core types and runtime for StateBus.
Use this package for:
- module augmentation of
StatesandEvents - reducers, listeners, and typed state access
ManualStateBusin tests and non-DOM environmentscomputed()helpers that do not depend on React
If you are building a React app, import from @smoothbricks/statebus-react for the app-facing package surface.
Install
bun add @smoothbricks/statebus-coreDefine Your App Types
Declare your application States and Events by augmenting @smoothbricks/statebus-core:
declare module '@smoothbricks/statebus-core' {
interface States {
counter: number;
post: ByID<{ title: string; body: string }>;
}
interface Events {
count: {
increment: number;
decrement: number;
};
}
}Make sure the declaration file is included by TypeScript.
Basic Usage
import { ManualStateBus } from '@smoothbricks/statebus-core';
const bus = new ManualStateBus({
initialState: {
counter: 0,
},
reducers: {
count: {
increment: (state, payload) => {
state.counter.update((value) => value + payload);
},
decrement: (state, payload) => {
state.counter.update((value) => value - payload);
},
},
},
});
bus.publish({ topic: 'count', type: 'increment', payload: 1 });
bus.dispatchEvents();Derived State
import { computed, ManualStateBus } from '@smoothbricks/statebus-core';
const bus = new ManualStateBus({
initialState: { counter: 0 },
reducers: {},
});
const doubledCounter = computed(bus, 'doubledCounter', (state) => state.counter.get() * 2, undefined);Public Exports
@smoothbricks/statebus-core exports the platform-agnostic surface, including:
StateBusManualStateBuscomputedsortedKeyValuePairs- bus, event, state, and reducer types
Computed,Atom, and related signal-adjacent types needed by consumers
Migration From @smoothbricks/statebus
The legacy monolithic package has been removed.
declare module '@smoothbricks/statebus'->declare module '@smoothbricks/statebus-core'StateBusfrom the old package split into:ManualStateBusin@smoothbricks/statebus-coreStateBus(RAF-backed browser implementation) in@smoothbricks/statebus-react
