createWatchedStreamController<T> static method

StreamController<T> createWatchedStreamController<T>(
  1. Readable<T> readable, {
  2. bool sync = false,
})

Implementation

static StreamController<T> createWatchedStreamController<T>(
    Readable<T> readable,
    {bool sync = false}) {
  Watcher? watcher;
  late final StreamController<T> controller;

  void disposer() {
    watcher?.dispose();
    watcher = null;
  }

  controller = StreamController<T>.broadcast(
    sync: sync,
    onListen: () {
      watcher = Watcher(() => readable.value, (newValue, __) {
        controller.add(newValue);
      },
          when: IMutableCollection.skipNode(readable),
          debug: const JoltDebugOption.type('Watcher<ToStream>'));
    },
    onCancel: disposer,
  );

  JFinalizer.attachToJoltAttachments(readable, disposer);

  return controller;
}