mirror of
https://github.com/fluencelabs/aqua-playground
synced 2025-06-22 23:11:53 +00:00
update fluence-js version, update compiled
This commit is contained in:
@ -8,11 +8,10 @@
|
||||
*/
|
||||
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||
import {
|
||||
ResultCodes,
|
||||
RequestFlow,
|
||||
RequestFlowBuilder,
|
||||
CallParams
|
||||
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||
CallParams,
|
||||
callFunction,
|
||||
registerService,
|
||||
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v2';
|
||||
|
||||
|
||||
function missingFields(obj: any, fields: string[]): string[] {
|
||||
@ -31,60 +30,28 @@ export function registerStringExtra(peer: FluencePeer, serviceId: string, servic
|
||||
|
||||
|
||||
export function registerStringExtra(...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 = "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, ['addNameToHello']);
|
||||
if (!!incorrectServiceDefinitions.length) {
|
||||
throw new Error("Error registering service StringExtra: missing functions: " + incorrectServiceDefinitions.map((d) => "'" + d + "'").join(", "))
|
||||
}
|
||||
|
||||
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||
if (req.serviceId !== serviceId) {
|
||||
next();
|
||||
return;
|
||||
registerService(
|
||||
args,
|
||||
{
|
||||
"defaultServiceId" : "service-id",
|
||||
"functions" : [
|
||||
{
|
||||
"functionName" : "addNameToHello",
|
||||
"argDefs" : [
|
||||
{
|
||||
"name" : "arg0",
|
||||
"argType" : {
|
||||
"tag" : "primitive"
|
||||
}
|
||||
}
|
||||
],
|
||||
"returnType" : {
|
||||
"tag" : "primitive"
|
||||
}
|
||||
}
|
||||
|
||||
if (req.fnName === 'addNameToHello') {
|
||||
const callParams = {
|
||||
...req.particleContext,
|
||||
tetraplets: {
|
||||
arg0: req.tetraplets[0]
|
||||
},
|
||||
};
|
||||
resp.retCode = ResultCodes.success;
|
||||
resp.result = service.addNameToHello(req.args[0], callParams)
|
||||
}
|
||||
|
||||
next();
|
||||
});
|
||||
]
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// Functions
|
||||
@ -93,25 +60,9 @@ export function registerStringExtra(...args: any) {
|
||||
export function helloWorld(name: string, config?: {ttl?: number}): Promise<string>;
|
||||
export function helloWorld(peer: FluencePeer, name: string, config?: {ttl?: number}): Promise<string>;
|
||||
export function helloWorld(...args: any) {
|
||||
let peer: FluencePeer;
|
||||
let name: any;
|
||||
let config: any;
|
||||
if (FluencePeer.isInstance(args[0])) {
|
||||
peer = args[0];
|
||||
name = args[1];
|
||||
config = args[2];
|
||||
} else {
|
||||
peer = Fluence.getPeer();
|
||||
name = args[0];
|
||||
config = args[1];
|
||||
}
|
||||
|
||||
let request: RequestFlow;
|
||||
const promise = new Promise<string>((resolve, reject) => {
|
||||
const r = new RequestFlowBuilder()
|
||||
.disableInjections()
|
||||
.withRawScript(`
|
||||
(xor
|
||||
let script = `
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
@ -127,33 +78,32 @@ export function helloWorld(...args: any) {
|
||||
)
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||
)
|
||||
`,
|
||||
)
|
||||
.configHandler((h) => {
|
||||
h.on('getDataSrv', '-relay-', () => {
|
||||
return peer.getStatus().relayPeerId;
|
||||
});
|
||||
h.on('getDataSrv', 'name', () => {return name;});
|
||||
h.onEvent('callbackSrv', 'response', (args) => {
|
||||
const [res] = args;
|
||||
resolve(res);
|
||||
});
|
||||
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||
const [err] = args;
|
||||
reject(err);
|
||||
});
|
||||
})
|
||||
.handleScriptError(reject)
|
||||
.handleTimeout(() => {
|
||||
reject('Request timed out for helloWorld');
|
||||
})
|
||||
|
||||
if (config && config.ttl) {
|
||||
r.withTTL(config.ttl)
|
||||
}
|
||||
|
||||
request = r.build();
|
||||
});
|
||||
peer.internals.initiateFlow(request!);
|
||||
return promise;
|
||||
`
|
||||
return callFunction(
|
||||
args,
|
||||
{
|
||||
"functionName" : "helloWorld",
|
||||
"returnType" : {
|
||||
"tag" : "primitive"
|
||||
},
|
||||
"argDefs" : [
|
||||
{
|
||||
"name" : "name",
|
||||
"argType" : {
|
||||
"tag" : "primitive"
|
||||
}
|
||||
}
|
||||
],
|
||||
"names" : {
|
||||
"relay" : "-relay-",
|
||||
"getDataSrv" : "getDataSrv",
|
||||
"callbackSrv" : "callbackSrv",
|
||||
"responseSrv" : "callbackSrv",
|
||||
"responseFnName" : "response",
|
||||
"errorHandlingSrv" : "errorHandlingSrv",
|
||||
"errorFnName" : "error"
|
||||
}
|
||||
},
|
||||
script
|
||||
)
|
||||
}
|
||||
|
Reference in New Issue
Block a user