2021-06-10 17:55:30 +03:00

128 lines
3.8 KiB
TypeScript

/**
*
* This file is auto-generated. Do not edit manually: changes may be erased.
* Generated by Aqua compiler: https://github.com/fluencelabs/aqua/.
* If you find any bugs, please write an issue on GitHub: https://github.com/fluencelabs/aqua/issues
* Aqua version: 0.1.6-143
*
*/
import { FluenceClient, PeerIdB58 } from '@fluencelabs/fluence';
import { RequestFlowBuilder } from '@fluencelabs/fluence/dist/api.unstable';
import { RequestFlow } from '@fluencelabs/fluence/dist/internal/RequestFlow';
export async function ifElseCall(client: FluenceClient, condition: boolean): Promise<void> {
let request: RequestFlow;
const promise = new Promise<void>((resolve, reject) => {
request = new RequestFlowBuilder()
.disableInjections()
.withRawScript(
`
(xor
(seq
(seq
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
(call %init_peer_id% ("getDataSrv" "condition") [] condition)
)
(xor
(match condition true
(xor
(call %init_peer_id% ("println-service-id" "print") ["it is true"])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
)
)
(seq
(seq
(call -relay- ("op" "identity") [])
(call %init_peer_id% ("println-service-id" "print") ["it is false"])
)
(call -relay- ("op" "identity") [])
)
)
)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
)
`,
)
.configHandler((h) => {
h.on('getDataSrv', '-relay-', () => {
return client.relayPeerId!;
});
h.on('getDataSrv', 'condition', () => {return condition;});
h.onEvent('errorHandlingSrv', 'error', (args) => {
// assuming error is the single argument
const [err] = args;
reject(err);
});
})
.handleScriptError(reject)
.handleTimeout(() => {
reject('Request timed out for ifElseCall');
})
.build();
});
await client.initiateFlow(request!);
return Promise.race([promise, Promise.resolve()]);
}
export async function ifElseNumCall(client: FluenceClient, condition: number): Promise<void> {
let request: RequestFlow;
const promise = new Promise<void>((resolve, reject) => {
request = new RequestFlowBuilder()
.disableInjections()
.withRawScript(
`
(xor
(seq
(seq
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
(call %init_peer_id% ("getDataSrv" "condition") [] condition)
)
(xor
(match condition 1
(xor
(call %init_peer_id% ("println-service-id" "print") ["it is 1"])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
)
)
(seq
(seq
(call -relay- ("op" "identity") [])
(call %init_peer_id% ("println-service-id" "print") ["it is not 1"])
)
(call -relay- ("op" "identity") [])
)
)
)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
)
`,
)
.configHandler((h) => {
h.on('getDataSrv', '-relay-', () => {
return client.relayPeerId!;
});
h.on('getDataSrv', 'condition', () => {return condition;});
h.onEvent('errorHandlingSrv', 'error', (args) => {
// assuming error is the single argument
const [err] = args;
reject(err);
});
})
.handleScriptError(reject)
.handleTimeout(() => {
reject('Request timed out for ifElseNumCall');
})
.build();
});
await client.initiateFlow(request!);
return Promise.race([promise, Promise.resolve()]);
}