Generate optional ttl argument in typescript functions (#164)

This commit is contained in:
Dima 2021-06-15 13:02:46 +03:00 committed by GitHub
parent ee23ffeb9e
commit 221cc5ca33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,6 +14,15 @@ case class TypescriptFunc(func: FuncCallable) {
def argsTypescript: String = def argsTypescript: String =
func.args.args.map(ad => s"${ad.name}: " + typeToTs(ad.`type`)).mkString(", ") 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 = { def generateTypescript(conf: BodyConfig = BodyConfig()): String = {
val tsAir = FuncAirGen(func).generateClientAir(conf) val tsAir = FuncAirGen(func).generateClientAir(conf)
@ -24,7 +33,6 @@ case class TypescriptFunc(func: FuncCallable) {
| resolve(res); | resolve(res);
|}); |});
|""".stripMargin |""".stripMargin
} }
val setCallbacks = func.args.args.map { val setCallbacks = func.args.args.map {
@ -47,13 +55,20 @@ case class TypescriptFunc(func: FuncCallable) {
val returnVal = val returnVal =
func.ret.fold("Promise.race([promise, Promise.resolve()])")(_ => "promise") 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""" s"""
|export async function ${func.funcName}(client: FluenceClient${if (func.args.isEmpty) "" |export async function ${func.funcName}($clientArgName: FluenceClient${if (func.args.isEmpty)
else ", "}${argsTypescript}): Promise<$retType> { ""
else ", "}${argsTypescript}, $configArgName?: $configType): Promise<$retType> {
| let request: RequestFlow; | let request: RequestFlow;
| const promise = new Promise<$retType>((resolve, reject) => { | const promise = new Promise<$retType>((resolve, reject) => {
| request = new RequestFlowBuilder() | request = new RequestFlowBuilder()
| .disableInjections() | .disableInjections()
| .withTTL($configArgName?.ttl || 5000)
| .withRawScript( | .withRawScript(
| ` | `
|${tsAir.show} |${tsAir.show}
@ -62,7 +77,7 @@ case class TypescriptFunc(func: FuncCallable) {
| .configHandler((h) => { | .configHandler((h) => {
| ${conf.relayVarName.fold("") { r => | ${conf.relayVarName.fold("") { r =>
s"""h.on('${conf.getDataService}', '$r', () => { s"""h.on('${conf.getDataService}', '$r', () => {
| return client.relayPeerId!; | return $clientArgName.relayPeerId!;
| });""".stripMargin | });""".stripMargin
}} }}
| $setCallbacks | $setCallbacks
@ -79,7 +94,7 @@ case class TypescriptFunc(func: FuncCallable) {
| }) | })
| .build(); | .build();
| }); | });
| await client.initiateFlow(request!); | await $clientArgName.initiateFlow(request!);
| return ${returnVal}; | return ${returnVal};
|} |}
""".stripMargin """.stripMargin