run<T> static method

T run<T>(
  1. String description,
  2. T fn()
)

Runs fn inside a new Zone that carries a CausalityContext.

If called inside an existing CausalityZone.run, the new context's CausalityContext.parentEventId automatically points to the outer context's CausalityContext.eventId, forming a chain.

Returns the result of fn. Works with both sync and async functions — async functions will carry the Zone context through all their awaits.

Implementation

static T run<T>(String description, T Function() fn) {
  final parentContext = currentContext();
  final context = CausalityContext(
    eventId: _uuid.v4(),
    parentEventId: parentContext?.eventId,
    originDescription: description,
    timestamp: DateTime.now(),
  );
  return Zone.current.fork(zoneValues: {_causalityKey: context}).run(fn);
}