enterActor method

  1. @override
void enterActor(
  1. ActorContext ctx
)
override

The default implementation does nothing.

Implementation

@override
void enterActor(ActorContext ctx) {
  final methTypes = ctx.actorType()?.methTypes() ?? [];
  final keys = StringBuffer();
  final idlMethods = StringBuffer();
  final reqMethods = StringBuffer();
  for (final md in methTypes) {
    final key = md.idType(0)!.text;
    final ccKey = key.toCamelCase();
    keys.writeln(
      "/// ${md.text}\nstatic const String $ccKey = '$key';",
    );
    final body = md.funcType();
    if (body != null) {
      final ann = body.funcAnn()?.text;
      final args = body.tupleType(0)!;
      final returns = body.tupleType(1)!;
      final argNodes = TypeNode(args);
      final returnNodes = TypeNode(returns);
      final argFields = _resolveTupleNode(argNodes);
      final returnFields = _resolveTupleNode(returnNodes);
      final idlValue = {
        'didText': md.text,
        'idlName': ccKey,
        'idlReq': argFields.map((e) => e.idl).join(','),
        'idlRep': returnFields.map((e) => e.idl).join(','),
        'funcAnno': ann.isNotBlank ? "'$ann'" : '',
      };
      String params;
      String idlParams;
      final argFieldsLen = argFields.length;
      if (argFields.isEmpty) {
        params = '';
        idlParams = 'const <dynamic>[]';
      } else if (argFieldsLen == 1) {
        final first = argFields.first;
        params = '${first.type.nullable(first.nullable)} arg,';
        if (first.ser != null) {
          idlParams =
              "<dynamic>[${first.ser!.replaceAll(SerField.ph, "arg")},]";
        } else {
          idlParams = '<dynamic>[arg]';
        }
      } else {
        params =
            'Tuple$argFieldsLen<${argFields.map((e) => e.type.nullable(e.nullable)).join(",")}> args,';
        if (argFields.any((e) => e.ser != null)) {
          final ser = StringBuffer();
          for (int i = 0; i < argFieldsLen; ++i) {
            final f = argFields.elementAt(i);
            final ind = i + 1;
            if (f.ser != null) {
              ser.write(f.ser!.replaceAll(SerField.ph, 'args.item$ind'));
            } else {
              ser.write('args.item$ind');
            }
            ser.write(',');
          }
          idlParams = '<dynamic>[$ser]';
        } else {
          idlParams = 'args.toList(growable: false)';
        }
      }
      idlMethods.writeln(Template(
        idlMethod,
        htmlEscapeValues: false,
      ).renderString(idlValue));
      final returnFieldsLen = returnFields.length;
      final noReturn = returnFieldsLen == 0;
      reqMethods.writeln(Template(
        idlReqMethod,
        htmlEscapeValues: false,
      ).renderString({
        ...idlValue,
        'methodName': ccKey,
        'renderParams': (_) => params,
        'renderParamsName': (_) => idlParams,
        'idlName': clazz,
        'hasReturn': !noReturn,
        'returnType': noReturn
            ? 'void'
            : returnFieldsLen == 1
                ? returnFields.first.type
                    .nullable(returnFields.first.nullable)
                : "Tuple$returnFieldsLen<${returnFields.map((e) => e.type.nullable(e.nullable)).join(",")}>",
        'renderReturn': (_) {
          if (noReturn) {
            return '';
          }
          if (returnFieldsLen == 1) {
            final deser = returnFields.first.deser;
            if (deser != null) {
              return "return ${deser.replaceAll(SerField.ph, "resp")};";
            }
          } else if (returnFieldsLen > 1) {
            final sb = StringBuffer();
            for (int i = 0; i < returnFieldsLen; ++i) {
              final field = returnFields[i];
              if (field.deser != null) {
                sb.write(field.deser!.replaceAll(SerField.ph, 'resp[$i]'));
              } else {
                sb.write('resp[$i]');
              }
              sb.write(',');
            }
            return 'return Tuple$returnFieldsLen($sb);';
          }
          return 'return resp;';
        },
      }));
    }
  }
  _idlCodes = Template(
    idlTpl,
    htmlEscapeValues: false,
  ).renderString({
    'clazz': clazz,
    'fields': keys.toString(),
    'pairs': idlMethods.toString(),
    'methods': reqMethods.toString(),
  });
}