createComponentMethods function

Iterable<Code> createComponentMethods(
  1. Iterable<ServerLifecycleComponentMethod> methods, {
  2. Map<String, Expression> inferredParams = const {},
})

Implementation

Iterable<Code> createComponentMethods(
  Iterable<ServerLifecycleComponentMethod> methods, {
  Map<String, Expression> inferredParams = const {},
}) sync* {
  for (final method in methods) {
    final params = getParams(
      method.parameters,
      inferredParams: inferredParams,
    );

    final usesBody = method.parameters.any((e) => e.annotations.body != null);

    final code = Method(
      (p) => p
        ..modifier = switch (usesBody) {
          true => MethodModifier.async,
          _ => null,
        }
        ..body = Block.of([
          if (usesBody)
            refer('context')
                .property('request')
                .property('resolvePayload')
                .call([])
                .awaited
                .statement,
          const Code('\n'),
          refer('component')
              .property(method.name)
              .call(params.positioned, params.named)
              .returned
              .statement,
        ]),
    );

    yield code.closure.code;
  }
}