conditionallyRunInAction method

void conditionallyRunInAction(
  1. void fn(),
  2. Atom atom, {
  3. String? name,
  4. ActionController? actionController,
})

Only run within an action if outside a batch fn is the function to execute. Optionally provide a debug-name.

Implementation

void conditionallyRunInAction(void Function() fn, Atom atom,
    {String? name, ActionController? actionController}) {
  if (isWithinBatch) {
    enforceWritePolicy(atom);
    fn();
  } else {
    final controller = actionController ??
        ActionController(
            context: this, name: name ?? nameFor('conditionallyRunInAction'));
    final runInfo = controller.startAction();

    try {
      enforceWritePolicy(atom);
      fn();
    } finally {
      controller.endAction(runInfo);
    }
  }
}