isEmbeddedAvailableStream method

Stream<bool> isEmbeddedAvailableStream({
  1. required String embeddedId,
})

Returns a stream that emits whether embedded content is available for the given ID. The stream immediately emits the current availability state upon subscription, then emits updates whenever the embedded info changes.

Implementation

Stream<bool> isEmbeddedAvailableStream({required String embeddedId}) {
  late StreamController<bool> controller;
  StreamSubscription<List<EmbeddedInfo>>? subscription;

  controller = StreamController<bool>(
    onListen: () {
      // Immediately emit current cached state
      final isAvailable = _embeddedInfos.any((info) => info.embeddedId == embeddedId);
      controller.add(isAvailable);

      // Subscribe to future updates from the broadcast stream
      subscription = _embeddedInfoUpdatedController.stream.listen((embeddedInfos) {
        if (!controller.isClosed) {
          final isAvailable = embeddedInfos.any((info) => info.embeddedId == embeddedId);
          controller.add(isAvailable);
        }
      });

      // Ask the native side to replay the current embedded state. Without this a
      // view that subscribes after the last update never learns about content
      // that already exists, because the cached list is only filled by events.
      _resendLastEmbeddedEvent();
    },
    onCancel: () {
      subscription?.cancel();
    },
  );

  return controller.stream.distinct();
}