closeStream method

Future<void> closeStream({
  1. required String endpoint,
  2. required String method,
  3. String? parameter,
  4. required UuidValue methodStreamId,
  5. required CloseReason reason,
})

Implementation

Future<void> closeStream({
  required String endpoint,
  required String method,
  String? parameter,
  required UuidValue methodStreamId,
  required CloseReason reason,
}) async {
  var streamKey = _buildStreamKey(
    endpoint: endpoint,
    method: method,
    parameter: parameter,
    methodStreamId: methodStreamId,
  );
  if (parameter == null) {
    var context = _outputStreamContexts[streamKey];

    if (context == null) {
      return;
    }

    // Immediate close of the stream
    _updateCloseReason(streamKey, reason);
    await context.controller.onCancel?.call();
    unawaited(context.subscription.cancel());
  } else {
    var context = _inputStreamContexts.remove(streamKey);

    if (context == null) {
      return;
    }

    if (reason == CloseReason.error) {
      context.controller.addError(
        const StreamClosedWithErrorException(),
      );
    }

    return _closeControllers([context.controller]);
  }
}