respondWith method

void respondWith({
  1. LazyMessagePayload? lazyPayload,
  2. List? arguments,
  3. Map<String, dynamic>? argumentsKeywords,
  4. bool isError = false,
  5. String? errorUri,
  6. YieldOptions? options,
})

Implementation

void respondWith({
  LazyMessagePayload? lazyPayload,
  List<dynamic>? arguments,
  Map<String, dynamic>? argumentsKeywords,
  bool isError = false,
  String? errorUri,
  YieldOptions? options,
}) {
  if (isError) {
    if (options != null) {
      assert(options.progress == false);
    }
    assert(UriPattern.match(errorUri!));
    final error = Error(
      MessageTypes.codeInvocation,
      requestId,
      HashMap(),
      errorUri,
      arguments: arguments,
      argumentsKeywords: argumentsKeywords,
    );
    _emitResponse(error);
  } else {
    var invokeArguments = arguments;
    var invokeArgumentsKeywords = argumentsKeywords;
    Uint8List? packedPayload;

    if (options?.pptScheme == 'wamp') {
      if (lazyPayload?.packedPayloadBytes != null &&
          _matchesPackedPayloadEncoding(lazyPayload!, options!)) {
        packedPayload = lazyPayload.packedPayloadBytes;
      }
    } else if (options?.pptScheme != null) {
      packedPayload = lazyPayload == null
          ? null
          : _packMatchingLazyPayload(lazyPayload, options!);
    }

    if (options?.pptScheme == 'wamp') {
      final runtimeContext = _responseRuntimeContext();
      invokeArguments = packedPayload == null
          ? E2EEPayload.packE2EEPayload(
              lazyPayload?.arguments ?? arguments,
              lazyPayload?.argumentsKeywords ?? argumentsKeywords,
              options!,
              provider: lazyPayload?.e2eeProvider ?? e2eeProvider,
              runtimeContext: runtimeContext,
            )
          : <dynamic>[packedPayload];
      invokeArgumentsKeywords = null;
    } else if (options?.pptScheme != null) {
      // It's some variation of PPT
      invokeArguments = packedPayload == null
          ? PPTPayload.packPPTPayload(arguments, argumentsKeywords, options!)
          : [packedPayload];
      invokeArgumentsKeywords = null;
    }

    final yield = Yield(
      requestId,
      options: options,
      arguments: invokeArguments,
      argumentsKeywords: invokeArgumentsKeywords,
    );
    yield.attachE2eeProvider(lazyPayload?.e2eeProvider ?? e2eeProvider);
    yield.attachE2eeRuntimeContext(_responseRuntimeContext());
    if (lazyPayload != null) {
      if (options?.pptScheme != null) {
        if (packedPayload != null) {
          yield.arguments = <dynamic>[packedPayload];
          yield.argumentsKeywords = null;
        } else {
          yield.arguments = invokeArguments;
          yield.argumentsKeywords = invokeArgumentsKeywords;
        }
      } else if (options?.pptScheme == null) {
        yield.setLazyPayload(
          argumentsBytes: lazyPayload.argumentsBytes,
          argumentsDecoder: lazyPayload.argumentsBytes == null
              ? null
              : (_) => lazyPayload.arguments ?? const <dynamic>[],
          argumentsKeywordsBytes: lazyPayload.argumentsKeywordsBytes,
          argumentsKeywordsDecoder: lazyPayload.argumentsKeywordsBytes == null
              ? null
              : (_) =>
                    lazyPayload.argumentsKeywords ??
                    const <String, dynamic>{},
          encoding: lazyPayload.encoding,
        );
        if (!lazyPayload.hasEncodedArguments) {
          yield.arguments = lazyPayload.arguments;
        }
        if (!lazyPayload.hasEncodedArgumentsKeywords) {
          yield.argumentsKeywords = lazyPayload.argumentsKeywords;
        }
      }
    }
    _emitResponse(yield);
  }
}