appCheckTokenChanges function

Stream<AppCheckToken> appCheckTokenChanges()

Tokens as they are issued, including the refreshes the SDK makes on its own.

Implementation

Stream<AppCheckToken> appCheckTokenChanges() {
  late RawReceivePort receive;
  late StreamController<AppCheckToken> controller;
  controller = StreamController<AppCheckToken>(
    onListen: () {
      receive = RawReceivePort();
      receive.handler = (Object? message) {
        try {
          controller.add(_readToken(message! as Uint8List));
        } on Object catch (e) {
          controller.addError(e);
        }
      };
      final rc = fdbAcAddListener(receive.sendPort.nativePort);
      if (rc != 0) {
        receive.close();
        controller.addError(
          StateError(
            rc == -3
                ? 'a token listener is already registered'
                : 'appCheckTokenChanges before initAppCheck ($rc)',
          ),
        );
      }
    },
    onCancel: () {
      fdbAcRemoveListener();
      receive.close();
    },
  );
  return controller.stream;
}