@d-buckner/ensemble-core
v0.2.0
Published
Type-safe actor model framework for predictable state management
Downloads
8
Maintainers
Readme
@d-buckner/ensemble-core
Type-safe actor model framework for predictable state management in TypeScript applications.
Features
- Actor Model: Isolated state management with clear boundaries
- TypeScript First: Full type safety with decorators and generics
- Effect System: Reactive communication between actors
- Dependency Injection: Type-safe actor dependencies
- Threading: Run actors in Web Workers for true parallelism
- Immutable Updates: Built on Mutative for performant immutable state
Installation
npm install @d-buckner/ensemble-coreQuick Start
import { Actor, action, createActorToken, ActorSystem } from '@d-buckner/ensemble-core';
// 1. Define your actor state and events
interface CounterState {
count: number;
}
interface CounterEvents {
incremented: number;
}
// 2. Create an actor class
class CounterActor extends Actor<CounterState, CounterEvents> {
static readonly initialState: CounterState = { count: 0 };
@action
increment(): void {
this.setState(draft => {
draft.count++;
});
this.emit('incremented', this.state.count);
}
@action
decrement(): void {
this.setState(draft => {
draft.count--;
});
}
}
// 3. Register and use the actor
const CounterToken = createActorToken<CounterActor>('counter');
const system = new ActorSystem();
system.register({ token: CounterToken, actor: CounterActor });
await system.start();
const counter = system.get(CounterToken);
counter.actions.increment();
console.log(counter.state.count); // 1Documentation
For comprehensive documentation, visit the Ensemble GitHub repository.
License
Apache-2.0 © Daniel Buckner
