mirror of
https://github.com/fluencelabs/aqua-playground
synced 2025-06-22 16:51:43 +00:00
Update JS SDK version and migrate to the new JS API (#11)
This commit is contained in:
@ -1,36 +1,101 @@
|
||||
/**
|
||||
*
|
||||
* This file is auto-generated. Do not edit manually: changes may be erased.
|
||||
* Generated by Aqua compiler: https://github.com/fluencelabs/aqua/.
|
||||
* 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.0-SNAPSHOT
|
||||
*
|
||||
*/
|
||||
import { FluenceClient, PeerIdB58 } from '@fluencelabs/fluence';
|
||||
import { RequestFlowBuilder } from '@fluencelabs/fluence/dist/api.unstable';
|
||||
import { RequestFlow } from '@fluencelabs/fluence/dist/internal/RequestFlow';
|
||||
|
||||
import { FluencePeer } from '@fluencelabs/fluence';
|
||||
import {
|
||||
ResultCodes,
|
||||
RequestFlow,
|
||||
RequestFlowBuilder,
|
||||
CallParams,
|
||||
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||
|
||||
// Services
|
||||
|
||||
//Stringer
|
||||
//defaultId = "stringer-id"
|
||||
export interface StringerDef {
|
||||
returnString: (arg0: string, callParams: CallParams<'arg0'>) => string;
|
||||
}
|
||||
|
||||
//returnString: (arg0: string) => string
|
||||
//END Stringer
|
||||
export function registerStringer(service: StringerDef): void;
|
||||
export function registerStringer(serviceId: string, service: StringerDef): void;
|
||||
export function registerStringer(peer: FluencePeer, service: StringerDef): void;
|
||||
export function registerStringer(peer: FluencePeer, serviceId: string, service: StringerDef): void;
|
||||
export function registerStringer(...args) {
|
||||
let peer: FluencePeer;
|
||||
let serviceId;
|
||||
let service;
|
||||
if (args[0] instanceof FluencePeer) {
|
||||
peer = args[0];
|
||||
} else {
|
||||
peer = FluencePeer.default;
|
||||
}
|
||||
|
||||
if (typeof args[0] === 'string') {
|
||||
serviceId = args[0];
|
||||
} else if (typeof args[1] === 'string') {
|
||||
serviceId = args[1];
|
||||
} else {
|
||||
serviceId = 'stringer-id';
|
||||
}
|
||||
|
||||
if (!(args[0] instanceof FluencePeer) && typeof args[0] === 'object') {
|
||||
service = args[0];
|
||||
} else if (typeof args[1] === 'object') {
|
||||
service = args[1];
|
||||
} else {
|
||||
service = args[2];
|
||||
}
|
||||
|
||||
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||
if (req.serviceId !== serviceId) {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.fnName === 'returnString') {
|
||||
const callParams = {
|
||||
...req.particleContext,
|
||||
tetraplets: {
|
||||
arg0: req.tetraplets[0],
|
||||
},
|
||||
};
|
||||
resp.retCode = ResultCodes.success;
|
||||
resp.result = service.returnString(req.args[0], callParams);
|
||||
}
|
||||
|
||||
next();
|
||||
});
|
||||
}
|
||||
|
||||
// Functions
|
||||
|
||||
export async function checkStreams(client: FluenceClient, ch: string[], config?: {ttl?: number}): Promise<string[]> {
|
||||
export function checkStreams(ch: string[], config?: { ttl?: number }): Promise<string[]>;
|
||||
export function checkStreams(peer: FluencePeer, ch: string[], config?: { ttl?: number }): Promise<string[]>;
|
||||
export function checkStreams(...args) {
|
||||
let peer: FluencePeer;
|
||||
let ch;
|
||||
let config;
|
||||
if (args[0] instanceof FluencePeer) {
|
||||
peer = args[0];
|
||||
ch = args[1];
|
||||
config = args[2];
|
||||
} else {
|
||||
peer = FluencePeer.default;
|
||||
ch = args[0];
|
||||
config = args[1];
|
||||
}
|
||||
|
||||
let request: RequestFlow;
|
||||
const promise = new Promise<string[]>((resolve, reject) => {
|
||||
const r = new RequestFlowBuilder()
|
||||
.disableInjections()
|
||||
.withRawScript(
|
||||
`
|
||||
(xor
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
@ -58,20 +123,21 @@ export async function checkStreams(client: FluenceClient, ch: string[], config?:
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||
)
|
||||
|
||||
`,
|
||||
`,
|
||||
)
|
||||
.configHandler((h) => {
|
||||
h.on('getDataSrv', '-relay-', () => {
|
||||
return client.relayPeerId!;
|
||||
return peer.connectionInfo.connectedRelay;
|
||||
});
|
||||
h.on('getDataSrv', 'ch', () => {
|
||||
return ch;
|
||||
});
|
||||
h.on('getDataSrv', 'ch', () => {return ch;});
|
||||
h.onEvent('callbackSrv', 'response', (args) => {
|
||||
const [res] = args;
|
||||
resolve(res);
|
||||
});
|
||||
const [res] = args;
|
||||
resolve(res);
|
||||
});
|
||||
|
||||
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||
// assuming error is the single argument
|
||||
const [err] = args;
|
||||
reject(err);
|
||||
});
|
||||
@ -79,13 +145,12 @@ export async function checkStreams(client: FluenceClient, ch: string[], config?:
|
||||
.handleScriptError(reject)
|
||||
.handleTimeout(() => {
|
||||
reject('Request timed out for checkStreams');
|
||||
})
|
||||
if(config && config.ttl) {
|
||||
r.withTTL(config.ttl)
|
||||
});
|
||||
if (config && config.ttl) {
|
||||
r.withTTL(config.ttl);
|
||||
}
|
||||
request = r.build();
|
||||
});
|
||||
await client.initiateFlow(request!);
|
||||
peer.internals.initiateFlow(request!);
|
||||
return promise;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user