dartora

ModificationListener

ModificationListener helps track nested modifications on an object and count how many steps occurred during those modifications. It is useful for signalling to observers when an object’s state has changed and at what severity level.

Overview

The listener maintains a stack of _ModCatch objects representing each active modification scope. When you call start(), it pushes a new _ModCatch onto the stack with a level equal to the current stack length (so the first level is ModLevel.zero, the second is ModLevel.one, etc.). When you call step(action), the listener invokes the provided callback with a ModificationAction containing itself and the current level; if the callback returns true, the listener increments the step count for that level. When you call end(), it pops the top _ModCatch, adds its points to the listener’s total points and increments a modification counter. Finally, calling reset() triggers a user‑supplied callback with a ModResetType indicating whether any steps occurred.

Fields

Methods

Examples

final ml = ModificationListener(onReset: (l, type) {
  print('Reset: ${type.name}, steps: ${l._points}, scopes: ${l.modCount}');
});
ml.start();
ml.step((action) {
  // do something that modifies state
  return true; // record a modification step
});
ml.end();
ml.reset(); // prints: Reset: modified, steps: 1, scopes: 1

ModificationListener does not enforce any particular semantics; it simply counts steps and scopes. Users of the listener decide what constitutes a modification and when to call start/end/step/reset.