An event sourcing framework implementation for Swift distributed Cluster System.
- Currently there is no default store providers, so you need to create one and conform to
EventStore
protocol. This could be any class, actor and etc. - Install plugins.
ClusterJournalPlugin
wraps store into singleton, so singleton plugin also should be added (and order is important!).
let node = await ClusterSystem("simple-node") {
$0.plugins.install(plugin: ClusterSingletonPlugin())
$0.plugins.install(
plugin: ClusterJournalPlugin { _ in
SomeStore()
}
)
}
- Make distributed actor
EventSourced
. ProvidepersistenceID
and define thehandleEvent(_:)
function:
distributed actor SomeActor: EventSourced {
// Some custom events for actor
enum Event {
case doSomething
}
// This is important to provide, events are stored per actor using persistenceID
@ActorID.Metadata(\.persistenceID)
var persistenceID: PersistenceID
distributed func doSomething() async throws {
try await self.emit(event: .doSomething)
}
distributed func handleEvent(_ event: Event) {
switch event {
case .doSomething:
// update state
}
}
init(actorSystem: ClusterSystem) {
self.actorSystem = actorSystem
self.persistenceID = "some-actor"
}
}