watchLocalWithRealtime<TResult> method

  1. @protected
Stream<TResult> watchLocalWithRealtime<TResult>(
  1. Stream<TResult> localStream, {
  2. required String scope,
  3. String? id,
  4. QueryParams? queryParams,
  5. Map<String, String>? headers,
  6. Map<String, dynamic>? extra,
  7. bool retryOnFail = true,
})
inherited

Wraps a local watch stream and ties a remote realtime subscription to the returned stream's lifecycle.

Implementation

@protected
Stream<TResult> watchLocalWithRealtime<TResult>(
  Stream<TResult> localStream, {
  required String scope,
  String? id,
  QueryParams? queryParams,
  Map<String, String>? headers,
  Map<String, dynamic>? extra,
  bool retryOnFail = true,
}) {
  _realtimeAdapterOrThrow();

  late final StreamController<TResult> controller;
  StreamSubscription<TResult>? localSubscription;
  _RealtimeSubscriptionKey? key;

  controller = StreamController<TResult>(
    onListen: () {
      key = _retainRealtimeSubscription(
        scope: scope,
        id: id,
        queryParams: queryParams,
        headers: headers,
        extra: extra,
        retryOnFail: retryOnFail,
      );

      localSubscription = localStream.listen(
        controller.add,
        onError: controller.addError,
        onDone: controller.close,
      );
    },
    onPause: () {
      localSubscription?.pause();
    },
    onResume: () {
      localSubscription?.resume();
    },
    onCancel: () async {
      await localSubscription?.cancel();
      final activeKey = key;
      if (activeKey != null) {
        await _releaseRealtimeSubscription(activeKey,
            retryOnFail: retryOnFail);
      }
    },
  );

  return controller.stream;
}