withRefresh method

Stream<LceState<DATA>> withRefresh(
  1. Stream refresh
)

Takes the LceUseCase.state of model that is being refreshed each time refresh emits a value Useful when you create a model as a result of mapping of some input (params for example) and the LceUseCase.refresh property becomes invisible for the outside world DATA Source model data type refresh Whenever this stream emits a value, the model is refreshed

Implementation

Stream<LceState<DATA>> withRefresh(Stream<dynamic> refresh) {
  final controller = StreamController<LceState<DATA>>(sync: true);
  late StreamSubscription<LceState<DATA>> stateSubscription;
  late StreamSubscription<dynamic> refreshSubscription;

  controller.onListen = () {
    stateSubscription = state.listen(controller.add, onError: controller.addError, onDone: controller.close);
    refreshSubscription = refresh.asyncMap((event) async => await this.refresh()).listen((_) { }, onError: (_) { });
  };
  controller.onPause = () {
    stateSubscription.pause();
    refreshSubscription.pause();
  };
  controller.onResume = () {
    stateSubscription.resume();
    refreshSubscription.resume();
  };
  controller.onCancel = () {
    stateSubscription.cancel();
    refreshSubscription.cancel();
  };

  return controller.stream;
}