notifierPod<N extends Notifier<T>, T> function

PodNotifier<Pod<N>, T> notifierPod<N extends Notifier<T>, T>(
  1. N create()
)

A pod which exposes a Notifier and listens to it.

This is equivalent to a pod that exposes ways to modify its state.

See also Notifier for more information.

Implementation

PodNotifier<Pod<N>, T> notifierPod<N extends Notifier<T>, T>(
  N Function() create,
) {
  var isPodDisposed = false;

  final podNotifier = pod<N>((ref) {
    isPodDisposed = false;
    final notifier = create().._ref = ref;
    notifier.state = notifier.build();

    return notifier;
  });

  return internalPodNotifier<Pod<N>, T>(podNotifier, (ref, pod) {
    final notifier = ref.watch(pod);

    if (isPodDisposed) {
      notifier.state = notifier.build();
    }

    final removeListener = notifier.addListener((state) {
      ref.setSelf(state);
    });

    ref.onDispose(() {
      isPodDisposed = true;
      removeListener();
    });

    return notifier.state;
  });
}