initScope method

Future<void> initScope(
  1. EventBus bus, {
  2. T? initialData,
  3. bool initialDataNeedExecute = true,
  4. List<String>? paths,
  5. int maxLenForUndo = 10,
  6. void onUpdate(
    1. T newData
    )?,
})

Initializes the scope: subscribes to the bus and creates the call history.

The returned future is worth awaiting: it completes once the initial value has gone through the bus (or with a handler error). maxLenForUndo is the history depth, at least 2 (otherwise there is nothing to roll back to).

Implementation

Future<void> initScope(EventBus bus,
    {T? initialData,
    bool initialDataNeedExecute = true,
    List<String>? paths,
    int maxLenForUndo = 10,
    void Function(T newData)? onUpdate}) async {
  var data = initialData;
  var needExecute = initialDataNeedExecute;

  if (_scopeCommand != null) {
    // re-initialization: drop the previous subscription and handler
    disposeScope();
  }

  this.onUpdate = onUpdate;
  _scopeCommand = Command(bus, paths: paths, maxLen: maxLenForUndo);

  _scopeStreamSubscription = bus.listen<T>(paths: paths).listen((event) {
    lastData = event;
    this.onUpdate?.call(event);
  });
  if (data != null) {
    if (needExecute) {
      await _scopeCommand!.execute(data);
    } else {
      lastData = data;
    }
  }
}