@blac/core
⚠️ Warning: This project is currently under active development. The API may change in future releases. Use with caution in production environments.
Core state management library implementing the BloC pattern for TypeScript applications.
Installation
npm install @blac/core
pnpm add @blac/core
yarn add @blac/core
Core Concepts
Cubit
Simple state container with direct state emission. Use when you need straightforward state updates.
import { Cubit } from '@blac/core';
class CounterCubit extends Cubit<{ count: number }> {
constructor() {
super({ count: 0 });
}
increment() {
this.emit({ count: this.state.count + 1 });
}
decrement() {
this.update((state) => ({ count: state.count - 1 }));
}
reset() {
this.patch({ count: 0 });
}
}
Registry API
Manage state container instances with the registry functions:
import { acquire, release, borrow, hasInstance, clear } from '@blac/core';
const counter = acquire(CounterCubit);
release(CounterCubit);
const instance = borrow(CounterCubit);
if (hasInstance(CounterCubit)) {
}
clear(CounterCubit);
clearAll();
Decorators
Use the @blac decorator to configure container behavior:
import { Cubit, blac } from '@blac/core';
@blac({ isolated: true })
class FormCubit extends Cubit<FormState> {}
@blac({ keepAlive: true })
class AuthCubit extends Cubit<AuthState> {}
@blac({ excludeFromDevTools: true })
class InternalCubit extends Cubit<State> {}
Utilities
waitUntil
Wait for a specific state condition:
import { waitUntil } from '@blac/core';
const counter = acquire(CounterCubit);
await waitUntil(counter, (state) => state.count >= 10);
await waitUntil(counter, (state) => state.count >= 10, {
timeout: 5000,
});
watch
Create computed values that react to state changes:
import { watch, instance } from '@blac/core';
class DashboardCubit extends Cubit<DashboardState> {
constructor() {
super({ items: [] });
watch(
instance(UserCubit),
(userState) => userState.preferences,
(preferences) => this.onPreferencesChanged(preferences),
);
}
}
Plugins
Extend functionality with plugins:
import { getPluginManager, type BlacPlugin } from '@blac/core';
const loggingPlugin: BlacPlugin = {
name: 'logging',
onStateChange: (container, prevState, newState) => {
console.log(`[${container.constructor.name}]`, prevState, '->', newState);
},
};
getPluginManager().register(loggingPlugin);
Configuration
Configure global behavior:
import { configureBlac } from '@blac/core';
configureBlac({
devMode: import.meta.env.DEV,
});
API Reference
State Containers
Cubit<S, P> | Simple state container with emit(), update(), patch() |
Registry Functions
acquire(Class, key?, options?) | Get or create instance, increment ref count |
release(Class, key?) | Decrement ref count, dispose when 0 |
borrow(Class, key?) | Get instance without affecting ref count |
borrowSafe(Class, key?) | Borrow or return undefined |
ensure(Class, key?, options?) | Acquire without incrementing ref count |
hasInstance(Class, key?) | Check if instance exists |
getRefCount(Class, key?) | Get current reference count |
clear(Class) | Remove all instances of a class |
clearAll() | Remove all instances |
Exports
export { Cubit } from '@blac/core';
export {
acquire,
release,
borrow,
ensure,
hasInstance,
clear,
clearAll,
} from '@blac/core';
export { waitUntil, watch, instance } from '@blac/core';
export { blac } from '@blac/core';
export { getPluginManager, type BlacPlugin } from '@blac/core';
export { configureBlac, getBlacConfig, isDevMode } from '@blac/core';
License
MIT