initScope method

Future<void> initScope(
  1. EventBus bus, {
  2. T? initialData,
  3. @Deprecated('Use initialData') T? initalData,
  4. bool initialDataNeedExecute = true,
  5. @Deprecated('Use initialDataNeedExecute') bool? initalDataNeedExecute,
  6. String? path,
  7. int maxLenForUndo = 10,
  8. 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,
    @Deprecated('Use initialData') T? initalData,
    bool initialDataNeedExecute = true,
    @Deprecated('Use initialDataNeedExecute') bool? initalDataNeedExecute,
    String? path,
    int maxLenForUndo = 10,
    void Function(T newData)? onUpdate}) async {
  var data = initialData ?? initalData;
  var needExecute = initalDataNeedExecute ?? initialDataNeedExecute;

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

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

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