appendEvents method
Future<AppendEventsResult>
appendEvents(
- StreamName name,
- Iterable<
StreamEvent> events, - ExpectedStreamVersion expected
override
Append one or more events to a stream
Parameter name
identifies stream to append
events
to. Use parameter expected
to
indicate which stream version that was
last seen. Is used to perform consistency
checks based on optimistic locking.
Implementation
@override
Future<AppendEventsResult> appendEvents(
StreamName name,
Iterable<StreamEvent> events,
ExpectedStreamVersion expected,
) async {
try {
final request = expected == ExpectedStreamVersion.noStream
? client.append(
StreamState.noStream(name.value),
Stream.fromIterable(events.map(_toEventData)),
)
: _anyOrNot(
expected,
whenAny: () => client.append(
StreamState.any(name.value),
Stream.fromIterable(events.map(_toEventData)),
),
otherwise: () => client.append(
StreamState.exists(
name.value,
revision: expected.asStreamRevision(),
),
Stream.fromIterable(events.map(_toEventData)),
),
);
final result = await request;
return result is WrongExpectedVersionResult
? $e.WrongExpectedVersionResult(
name,
expected,
ExpectedStreamVersion(result.nextExpectedStreamRevision.toInt()),
result,
)
: AppendEventsResult.ok(
name,
result.actualPosition.commitPosition.toInt(),
result.nextExpectedStreamRevision.toInt(),
);
} on Exception catch (cause) {
return AppendEventsResult.error(
name,
expected.value,
cause,
);
}
}