streamAtom<A> function

StreamAtom<A> streamAtom<A>(
  1. AtomReader<Stream<A>> create, {
  2. A? initialValue,
})

Implementation

StreamAtom<A> streamAtom<A>(
  AtomReader<Stream<A>> create, {
  A? initialValue,
}) =>
    AtomWithParent(
      atom((get) => create(get).asBroadcastStream()),
      (get, stream) {
        A? currentData;

        final subscription = get(stream).listen(
          (data) {
            currentData = data;
            get.setSelf(FutureValue.loading(data));
          },
          onError: (err, stackTrace) => get.setSelf(FutureValue.error(
            err,
            stackTrace,
          )),
          onDone: () {
            if (currentData != null) {
              // ignore: null_check_on_nullable_type_parameter
              get.setSelf(FutureValue.data(currentData!));
              currentData = null;
            }
          },
        );

        get.onDispose(subscription.cancel);

        return FutureValue.loading(
          get.self()?.dataOrNull ?? initialValue,
        );
      },
    );