persistEvent method
Persist an event to the event store Events are immutable and stored permanently
Implementation
@protected
Future<void> persistEvent(Event event) async {
if (_isRecovering || !_isRecovered) {
throw StateError(
'Cannot persist events during recovery or before recovery is complete');
}
try {
// Persist to event store with expected version for optimistic concurrency
await _eventStore.persistEvent(_persistenceId, event, _sequenceNumber);
// Update sequence number
_sequenceNumber++;
// Apply event to update state
eventHandler(event);
// Notify snapshot manager of event persistence
_snapshotManager?.onEventPersisted(_persistenceId);
// Check if automatic snapshot should be created
await _checkAndCreateSnapshot();
// Call persistence lifecycle hook
await onPersist(event);
} catch (e) {
// Handle persistence failures
await onPersistFailure(event, e);
rethrow;
}
}