listen method

  1. @UseResult('Listener will leak in the container if it is not disposed')
void Function() listen(
  1. CapsuleListener listener
)

Temporarily listens to changes in a given set of Capsules.

Calls the supplied listener immediately, and then after any capsules it's listening to change. You must call the returned dispose Function when you no longer need the listener in order to prevent leaks.

If you want to listen to capsule(s) not temporarily, instead just make a non-idempotent capsule and read it once to initialize it. (See the documentation for more on this topic.)

Implementation

@UseResult('Listener will leak in the container if it is not disposed')
void Function() listen(CapsuleListener listener) {
  // Create a temporary *non-idempotent* capsule so that it doesn't get
  // idempotent garbage collected
  void capsule(CapsuleHandle use) {
    use.asListener();
    listener(use);
  }

  // Put the temporary capsule into the container so it gets data updates
  final manager = _managerOf(capsule);

  return manager.dispose;
}