mirror of
https://github.com/fluencelabs/aqua.git
synced 2024-12-12 17:55:33 +00:00
Generate optional ttl argument in typescript functions (#164)
This commit is contained in:
parent
ee23ffeb9e
commit
221cc5ca33
@ -14,6 +14,15 @@ case class TypescriptFunc(func: FuncCallable) {
|
||||
def argsTypescript: String =
|
||||
func.args.args.map(ad => s"${ad.name}: " + typeToTs(ad.`type`)).mkString(", ")
|
||||
|
||||
def generateUniqueArgName(args: List[String], basis: String, attempt: Int): String = {
|
||||
val name = if (attempt == 0) {
|
||||
basis
|
||||
} else {
|
||||
basis + attempt
|
||||
}
|
||||
args.find(_ == name).map(_ => generateUniqueArgName(args, basis, attempt + 1)).getOrElse(name)
|
||||
}
|
||||
|
||||
def generateTypescript(conf: BodyConfig = BodyConfig()): String = {
|
||||
|
||||
val tsAir = FuncAirGen(func).generateClientAir(conf)
|
||||
@ -24,7 +33,6 @@ case class TypescriptFunc(func: FuncCallable) {
|
||||
| resolve(res);
|
||||
|});
|
||||
|""".stripMargin
|
||||
|
||||
}
|
||||
|
||||
val setCallbacks = func.args.args.map {
|
||||
@ -47,13 +55,20 @@ case class TypescriptFunc(func: FuncCallable) {
|
||||
val returnVal =
|
||||
func.ret.fold("Promise.race([promise, Promise.resolve()])")(_ => "promise")
|
||||
|
||||
val clientArgName = generateUniqueArgName(func.args.args.map(_.name), "client", 0)
|
||||
val configArgName = generateUniqueArgName(func.args.args.map(_.name), "config", 0)
|
||||
|
||||
val configType = "{ttl?: number}"
|
||||
|
||||
s"""
|
||||
|export async function ${func.funcName}(client: FluenceClient${if (func.args.isEmpty) ""
|
||||
else ", "}${argsTypescript}): Promise<$retType> {
|
||||
|export async function ${func.funcName}($clientArgName: FluenceClient${if (func.args.isEmpty)
|
||||
""
|
||||
else ", "}${argsTypescript}, $configArgName?: $configType): Promise<$retType> {
|
||||
| let request: RequestFlow;
|
||||
| const promise = new Promise<$retType>((resolve, reject) => {
|
||||
| request = new RequestFlowBuilder()
|
||||
| .disableInjections()
|
||||
| .withTTL($configArgName?.ttl || 5000)
|
||||
| .withRawScript(
|
||||
| `
|
||||
|${tsAir.show}
|
||||
@ -62,7 +77,7 @@ case class TypescriptFunc(func: FuncCallable) {
|
||||
| .configHandler((h) => {
|
||||
| ${conf.relayVarName.fold("") { r =>
|
||||
s"""h.on('${conf.getDataService}', '$r', () => {
|
||||
| return client.relayPeerId!;
|
||||
| return $clientArgName.relayPeerId!;
|
||||
| });""".stripMargin
|
||||
}}
|
||||
| $setCallbacks
|
||||
@ -79,7 +94,7 @@ case class TypescriptFunc(func: FuncCallable) {
|
||||
| })
|
||||
| .build();
|
||||
| });
|
||||
| await client.initiateFlow(request!);
|
||||
| await $clientArgName.initiateFlow(request!);
|
||||
| return ${returnVal};
|
||||
|}
|
||||
""".stripMargin
|
||||
|
Loading…
Reference in New Issue
Block a user