mirror of
https://github.com/fluencelabs/aqua-playground
synced 2025-04-25 19:22:20 +00:00
155 lines
5.0 KiB
TypeScript
155 lines
5.0 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.3.1-231
|
||
|
*
|
||
|
*/
|
||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||
|
import {
|
||
|
ResultCodes,
|
||
|
RequestFlow,
|
||
|
RequestFlowBuilder,
|
||
|
CallParams,
|
||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||
|
|
||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||
|
return fields.filter((f) => !(f in obj));
|
||
|
}
|
||
|
|
||
|
// Services
|
||
|
|
||
|
export interface PrintlnDef {
|
||
|
print: (arg0: string, callParams: CallParams<'arg0'>) => void;
|
||
|
}
|
||
|
|
||
|
export function registerPrintln(service: PrintlnDef): void;
|
||
|
export function registerPrintln(serviceId: string, service: PrintlnDef): void;
|
||
|
export function registerPrintln(peer: FluencePeer, service: PrintlnDef): void;
|
||
|
export function registerPrintln(peer: FluencePeer, serviceId: string, service: PrintlnDef): void;
|
||
|
export function registerPrintln(...args: any) {
|
||
|
let peer: FluencePeer;
|
||
|
let serviceId: any;
|
||
|
let service: any;
|
||
|
if (FluencePeer.isInstance(args[0])) {
|
||
|
peer = args[0];
|
||
|
} else {
|
||
|
peer = Fluence.getPeer();
|
||
|
}
|
||
|
|
||
|
if (typeof args[0] === 'string') {
|
||
|
serviceId = args[0];
|
||
|
} else if (typeof args[1] === 'string') {
|
||
|
serviceId = args[1];
|
||
|
} else {
|
||
|
serviceId = 'println-service-id';
|
||
|
}
|
||
|
|
||
|
// Figuring out which overload is the service.
|
||
|
// If the first argument is not Fluence Peer and it is an object, then it can only be the service def
|
||
|
// If the first argument is peer, we are checking further. The second argument might either be
|
||
|
// an object, that it must be the service object
|
||
|
// or a string, which is the service id. In that case the service is the third argument
|
||
|
if (!FluencePeer.isInstance(args[0]) && typeof args[0] === 'object') {
|
||
|
service = args[0];
|
||
|
} else if (typeof args[1] === 'object') {
|
||
|
service = args[1];
|
||
|
} else {
|
||
|
service = args[2];
|
||
|
}
|
||
|
|
||
|
const incorrectServiceDefinitions = missingFields(service, ['print']);
|
||
|
if (!!incorrectServiceDefinitions.length) {
|
||
|
throw new Error(
|
||
|
'Error registering service Println: missing functions: ' +
|
||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||
|
);
|
||
|
}
|
||
|
|
||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||
|
if (req.serviceId !== serviceId) {
|
||
|
next();
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (req.fnName === 'print') {
|
||
|
const callParams = {
|
||
|
...req.particleContext,
|
||
|
tetraplets: {
|
||
|
arg0: req.tetraplets[0],
|
||
|
},
|
||
|
};
|
||
|
resp.retCode = ResultCodes.success;
|
||
|
service.print(req.args[0], callParams);
|
||
|
resp.result = {};
|
||
|
}
|
||
|
|
||
|
next();
|
||
|
});
|
||
|
}
|
||
|
|
||
|
// Functions
|
||
|
|
||
|
export function print(str: string, config?: { ttl?: number }): Promise<void>;
|
||
|
export function print(peer: FluencePeer, str: string, config?: { ttl?: number }): Promise<void>;
|
||
|
export function print(...args: any) {
|
||
|
let peer: FluencePeer;
|
||
|
let str: any;
|
||
|
let config: any;
|
||
|
if (FluencePeer.isInstance(args[0])) {
|
||
|
peer = args[0];
|
||
|
str = args[1];
|
||
|
config = args[2];
|
||
|
} else {
|
||
|
peer = Fluence.getPeer();
|
||
|
str = args[0];
|
||
|
config = args[1];
|
||
|
}
|
||
|
|
||
|
let request: RequestFlow;
|
||
|
const promise = new Promise<void>((resolve, reject) => {
|
||
|
const r = new RequestFlowBuilder()
|
||
|
.disableInjections()
|
||
|
.withRawScript(
|
||
|
`
|
||
|
(xor
|
||
|
(seq
|
||
|
(seq
|
||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||
|
(call %init_peer_id% ("getDataSrv" "str") [] str)
|
||
|
)
|
||
|
(call %init_peer_id% ("println-service-id" "print") [str])
|
||
|
)
|
||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||
|
)
|
||
|
`,
|
||
|
)
|
||
|
.configHandler((h) => {
|
||
|
h.on('getDataSrv', '-relay-', () => {
|
||
|
return peer.getStatus().relayPeerId;
|
||
|
});
|
||
|
h.on('getDataSrv', 'str', () => {
|
||
|
return str;
|
||
|
});
|
||
|
h.onEvent('callbackSrv', 'response', (args) => {});
|
||
|
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||
|
const [err] = args;
|
||
|
reject(err);
|
||
|
});
|
||
|
})
|
||
|
.handleScriptError(reject)
|
||
|
.handleTimeout(() => {
|
||
|
reject('Request timed out for print');
|
||
|
});
|
||
|
|
||
|
if (config && config.ttl) {
|
||
|
r.withTTL(config.ttl);
|
||
|
}
|
||
|
|
||
|
request = r.build();
|
||
|
});
|
||
|
peer.internals.initiateFlow(request!);
|
||
|
return Promise.race([promise, Promise.resolve()]);
|
||
|
}
|