Implementation
CubixState<TState> reduce({
TState Function(TState state)? state,
Dispatcher? remove,
Dispatcher? add,
List<Dispatcher>? dispatchers,
}) {
if (state == null && remove == null && add == null && dispatchers == null) {
return this;
}
dispatchers ??= this.dispatchers;
var nextDispatchers = dispatchers;
if (add != null) {
if (nextDispatchers == dispatchers) {
nextDispatchers = [...dispatchers];
}
nextDispatchers.add(add);
}
if (remove != null && nextDispatchers.contains(remove)) {
if (nextDispatchers == dispatchers) {
nextDispatchers = [...dispatchers];
}
nextDispatchers.remove(remove);
}
final nextState = state == null ? this.state : state(this.state);
if (nextState == this.state && nextDispatchers == dispatchers) {
return this;
}
return CubixState(
nextState,
nextDispatchers,
);
}