append method

FakeEvent append(
  1. String type, {
  2. required JsonMap resource,
  3. required JsonMap payload,
  4. JsonMap scope = const {},
  5. String? actor,
  6. String? audience,
  7. String? clientMsgId,
})

Appends a durable event of type about resource in scope with payload, raising seq, and calls the eventHooks; returns it.

audience makes it personal (audience: self); actor names who triggered it.

Implementation

FakeEvent append(
  String type, {
  required JsonMap resource,
  required JsonMap payload,
  JsonMap scope = const {},
  String? actor,
  String? audience,
  String? clientMsgId,
}) {
  // 04 9.3: `resource.version` is present exactly for the types the catalogue marks, so the
  // fake cannot report a foreign version that would make a client drop later changes.
  final info = EventCatalog.lookup(type);
  if (info == null) {
    throw ArgumentError.value(type, 'type', 'not in the event catalogue');
  }
  if (info.carriesResourceVersion && !resource.containsKey('version')) {
    throw ArgumentError.value(type, 'type', 'needs resource.version');
  }
  final res = info.carriesResourceVersion
      ? resource
      : ({...resource}..remove('version'));
  final envelope = <String, Object?>{
    'id': nextId(),
    'seq': ++headSeq,
    'type': type,
    'schema_version': 1,
    'occurred_at': stamp(),
    if (actor != null)
      'actor': {'id': actor, 'kind': people[actor]?.kind ?? 'user'},
    'scope': scope,
    'resource': res,
    if (audience != null) 'audience': 'self',
    'payload': payload,
    'client_msg_id': ?clientMsgId,
    'permission_version': permissionVersion,
  };
  // A copy: the world keeps changing its objects, a logged event never changes.
  final event = FakeEvent(_copy(envelope), audience: audience);
  log.add(event);
  for (final hook in List.of(eventHooks)) {
    hook(event);
  }
  return event;
}