subscribe method

void Function() subscribe(
  1. String topic,
  2. FutureOr<void> listener(
    1. Object? payload
    )
)

Calls listener with whatever is published to topic, and returns the function that stops it.

How one module tells the others something without importing them. A store module that confirms a payment publishes commerce.purchase, and whichever analytics modules the application mounted report it; mount none and the publish reaches nobody. A topic's name and the shape of its payload are the publishing module's to document, and a payload is plain data -- maps, lists, strings, numbers -- so neither side needs the other's types.

Implementation

void Function() subscribe(
  String topic,
  FutureOr<void> Function(Object? payload) listener,
) {
  (_subscribers[topic] ??= <FutureOr<void> Function(Object? payload)>[])
      .add(listener);
  return () => _subscribers[topic]?.remove(listener);
}