PersistentActor
Defined in: src/persistence/PersistentActor.ts:58
Classic-style event-sourced actor. Subclasses override onCommand
(which decides what to persist), onEvent (pure state update from the
event), and optionally onRecoveryComplete. Commands are automatically
stashed while persist(...) is pending, so user code can assume the
state is caught up by the time its callback fires.
class AccountActor extends PersistentActor<Cmd, Event, State> { readonly persistenceId = ‘account-42’; initialState(): State { return { balance: 0 }; } onEvent(state: State, e: Event): State { if (e.kind === ‘deposited’) return { balance: state.balance + e.amount }; return state; } onCommand(state: State, cmd: Cmd): void { if (cmd.kind === ‘deposit’) { this.persist({ kind: ‘deposited’, amount: cmd.amount }, (s) => { this.sender?.tell({ ok: s.balance }); }); } } }
Extends
Section titled “Extends”Actor<Cmd>
Extended by
Section titled “Extended by”Type Parameters
Section titled “Type Parameters”Cmd
Event
State
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new PersistentActor<
Cmd,Event,State>():PersistentActor<Cmd,Event,State>
Returns
Section titled “Returns”PersistentActor<Cmd, Event, State>
Inherited from
Section titled “Inherited from”Properties
Section titled “Properties”persistenceId
Section titled “persistenceId”
abstractreadonlypersistenceId:string
Defined in: src/persistence/PersistentActor.ts:59
Methods
Section titled “Methods”compression()
Section titled “compression()”compression():
CompressionConfig|undefined
Defined in: src/persistence/PersistentActor.ts:106
Per-actor compression — overrides the plugin default for THIS actor’s
snapshots. Stores that don’t compress (in-memory, SQLite, Cassandra)
ignore the value. Returning undefined (the default) defers to the
plugin’s resolver / configured default.
Returns
Section titled “Returns”CompressionConfig | undefined
encryption()
Section titled “encryption()”encryption():
EncryptionConfig|undefined
Defined in: src/persistence/PersistentActor.ts:114
Per-actor encryption — overrides the plugin default for THIS actor’s snapshots. Honoured by stores that encrypt at rest (object-storage); other stores ignore it. Used on both the write path (encrypt) and the read path (derive subkey from master to decrypt).
Returns
Section titled “Returns”EncryptionConfig | undefined
eventAdapter()
Section titled “eventAdapter()”eventAdapter():
EventAdapter<Event,Event> |undefined
Defined in: src/persistence/PersistentActor.ts:90
Optional event adapter for schema evolution. When defined, every
persisted event is wrapped into a { _v, _t, _e } envelope on the
write path and unwrapped (with up-casting through the adapter) on
the read path. Recovery is strict when an adapter is set: a
raw, non-envelope event in the journal will throw MigrationError.
See src/persistence/migration/.
Returns
Section titled “Returns”EventAdapter<Event, Event> | undefined
initialState()
Section titled “initialState()”
abstractinitialState():State
Defined in: src/persistence/PersistentActor.ts:62
Default initial state when no snapshot and no events exist.
Returns
Section titled “Returns”State
onCommand()
Section titled “onCommand()”
abstractonCommand(state,cmd):void|Promise<void>
Defined in: src/persistence/PersistentActor.ts:68
Handle an incoming command — typically calls persist(event, cb).
Parameters
Section titled “Parameters”State
Cmd
Returns
Section titled “Returns”void | Promise<void>
onEvent()
Section titled “onEvent()”
abstractonEvent(state,event):State
Defined in: src/persistence/PersistentActor.ts:65
Pure state-update function — MUST be deterministic.
Parameters
Section titled “Parameters”State
Event
Returns
Section titled “Returns”State
onReceive()
Section titled “onReceive()”onReceive(
message):Promise<void>
Defined in: src/persistence/PersistentActor.ts:209
Main message handler. Receives each envelope dequeued from the mailbox. A thrown error (sync or async) is caught by the supervisor.
Parameters
Section titled “Parameters”message
Section titled “message”Cmd
Returns
Section titled “Returns”Promise<void>
Overrides
Section titled “Overrides”onRecoveryComplete()
Section titled “onRecoveryComplete()”onRecoveryComplete(
_state):void|Promise<void>
Defined in: src/persistence/PersistentActor.ts:71
Called once recovery finishes, with the final replayed state.
Parameters
Section titled “Parameters”_state
Section titled “_state”State
Returns
Section titled “Returns”void | Promise<void>
onRecoveryFailure()
Section titled “onRecoveryFailure()”onRecoveryFailure(
reason):void
Defined in: src/persistence/PersistentActor.ts:74
Called when recovery itself throws. Default = propagate to supervision.
Parameters
Section titled “Parameters”reason
Section titled “reason”Error
Returns
Section titled “Returns”void
postRestart()
Section titled “postRestart()”postRestart(
_reason):void|Promise<void>
Defined in: src/Actor.ts:55
Called on the fresh instance after a restart. Default: call preStart().
Parameters
Section titled “Parameters”_reason
Section titled “_reason”Error
Returns
Section titled “Returns”void | Promise<void>
Inherited from
Section titled “Inherited from”postStop()
Section titled “postStop()”postStop():
void|Promise<void>
Defined in: src/Actor.ts:44
Called after the actor has been terminated. Children are already stopped.
Returns
Section titled “Returns”void | Promise<void>
Inherited from
Section titled “Inherited from”preRestart()
Section titled “preRestart()”preRestart(
_reason,_message?):void|Promise<void>
Defined in: src/Actor.ts:50
Called before a restart, on the instance about to be thrown away. The default stops children and then calls postStop().
Parameters
Section titled “Parameters”_reason
Section titled “_reason”Error
_message?
Section titled “_message?”Cmd
Returns
Section titled “Returns”void | Promise<void>
Inherited from
Section titled “Inherited from”preStart()
Section titled “preStart()”preStart():
Promise<void>
Defined in: src/persistence/PersistentActor.ts:138
Called after construction and before the first message is processed.
Returns
Section titled “Returns”Promise<void>
Overrides
Section titled “Overrides”snapshotAdapter()
Section titled “snapshotAdapter()”snapshotAdapter():
SnapshotAdapter<State,State> |undefined
Defined in: src/persistence/PersistentActor.ts:98
Optional snapshot adapter — same semantics as eventAdapter, but
applied to the state blob persisted by the snapshot store. When
a snapshot adapter is set and a stored snapshot is not an envelope,
recovery throws.
Returns
Section titled “Returns”SnapshotAdapter<State, State> | undefined
snapshotPolicy()
Section titled “snapshotPolicy()”snapshotPolicy():
SnapshotPolicy<State,Event>
Defined in: src/persistence/PersistentActor.ts:77
Snapshot policy — return true to snapshot the current state.
Returns
Section titled “Returns”SnapshotPolicy<State, Event>
supervisorStrategy()
Section titled “supervisorStrategy()”supervisorStrategy():
SupervisorStrategy
Defined in: src/Actor.ts:63
Supervisor strategy for this actor’s children. Defaults to restart, up to 10 times per minute, then stop.
Returns
Section titled “Returns”Inherited from
Section titled “Inherited from”tagsFor()
Section titled “tagsFor()”tagsFor(
_event): readonlystring[] |undefined
Defined in: src/persistence/PersistentActor.ts:80
Optional tags attached to every persisted event (for Persistence Query).
Parameters
Section titled “Parameters”_event
Section titled “_event”Event
Returns
Section titled “Returns”readonly string[] | undefined