stream method

Stream<VoiceEvent> stream()

Open a new event subscription. The returned stream emits one VoiceEvent per agent event until cancelled or the agent ends. Each call produces a fresh single-subscription stream; multiple streams attached to the same native handle fan out from one C callback registration via _VoiceFanOutRegistry.

Implementation

Stream<VoiceEvent> stream() {
  final fanOut = _VoiceFanOutRegistry.fanOutFor(_handle);
  late StreamController<VoiceEvent> controller;

  controller = StreamController<VoiceEvent>(
    onListen: () {
      final attached = fanOut.attach(controller);
      if (!attached) {
        controller.addError(
          StateError(
            'rac_voice_agent_set_proto_callback failed '
            '(Protobuf may not be linked)',
          ),
        );
        unawaited(controller.close());
      }
    },
    onCancel: () => fanOut.detach(controller),
  );
  return controller.stream;
}