initScope method
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;
}
}
}