mirror of
https://github.com/fluencelabs/examples
synced 2025-06-20 13:26:33 +00:00
Update all js-related examples to the latest version of JS SDK and Aqua compiler (#20)
This commit is contained in:
167
js-sdk-examples/node-example/src/_aqua/calc.ts
Normal file
167
js-sdk-examples/node-example/src/_aqua/calc.ts
Normal file
@ -0,0 +1,167 @@
|
||||
/**
|
||||
*
|
||||
* 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.0-226
|
||||
*
|
||||
*/
|
||||
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||
import {
|
||||
ResultCodes,
|
||||
RequestFlow,
|
||||
RequestFlowBuilder,
|
||||
CallParams,
|
||||
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||
|
||||
|
||||
// Services
|
||||
|
||||
export interface CalcDef {
|
||||
add: (n: number, callParams: CallParams<'n'>) => void;
|
||||
divide: (n: number, callParams: CallParams<'n'>) => void;
|
||||
getResult: (callParams: CallParams<null>) => number;
|
||||
multiply: (n: number, callParams: CallParams<'n'>) => void;
|
||||
reset: (callParams: CallParams<null>) => void;
|
||||
subtract: (n: number, callParams: CallParams<'n'>) => void;
|
||||
}
|
||||
|
||||
export function registerCalc(service: CalcDef): void;
|
||||
export function registerCalc(serviceId: string, service: CalcDef): void;
|
||||
export function registerCalc(peer: FluencePeer, service: CalcDef): void;
|
||||
export function registerCalc(peer: FluencePeer, serviceId: string, service: CalcDef): void;
|
||||
export function registerCalc(...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 = "calc"
|
||||
}
|
||||
|
||||
// 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];
|
||||
}
|
||||
|
||||
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||
if (req.serviceId !== serviceId) {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (req.fnName === 'add') {
|
||||
|
||||
const callParams = {
|
||||
...req.particleContext,
|
||||
tetraplets: {
|
||||
n: req.tetraplets[0]
|
||||
},
|
||||
};
|
||||
resp.retCode = ResultCodes.success;
|
||||
service.add(req.args[0], callParams); resp.result = {}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (req.fnName === 'divide') {
|
||||
|
||||
const callParams = {
|
||||
...req.particleContext,
|
||||
tetraplets: {
|
||||
n: req.tetraplets[0]
|
||||
},
|
||||
};
|
||||
resp.retCode = ResultCodes.success;
|
||||
service.divide(req.args[0], callParams); resp.result = {}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (req.fnName === 'getResult') {
|
||||
|
||||
const callParams = {
|
||||
...req.particleContext,
|
||||
tetraplets: {
|
||||
|
||||
},
|
||||
};
|
||||
resp.retCode = ResultCodes.success;
|
||||
resp.result = service.getResult(callParams)
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (req.fnName === 'multiply') {
|
||||
|
||||
const callParams = {
|
||||
...req.particleContext,
|
||||
tetraplets: {
|
||||
n: req.tetraplets[0]
|
||||
},
|
||||
};
|
||||
resp.retCode = ResultCodes.success;
|
||||
service.multiply(req.args[0], callParams); resp.result = {}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (req.fnName === 'reset') {
|
||||
|
||||
const callParams = {
|
||||
...req.particleContext,
|
||||
tetraplets: {
|
||||
|
||||
},
|
||||
};
|
||||
resp.retCode = ResultCodes.success;
|
||||
service.reset(callParams); resp.result = {}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (req.fnName === 'subtract') {
|
||||
|
||||
const callParams = {
|
||||
...req.particleContext,
|
||||
tetraplets: {
|
||||
n: req.tetraplets[0]
|
||||
},
|
||||
};
|
||||
resp.retCode = ResultCodes.success;
|
||||
service.subtract(req.args[0], callParams); resp.result = {}
|
||||
|
||||
}
|
||||
|
||||
|
||||
next();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// Functions
|
||||
|
122
js-sdk-examples/node-example/src/_aqua/demo-calculation.ts
Normal file
122
js-sdk-examples/node-example/src/_aqua/demo-calculation.ts
Normal file
@ -0,0 +1,122 @@
|
||||
/**
|
||||
*
|
||||
* 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.0-226
|
||||
*
|
||||
*/
|
||||
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||
import {
|
||||
ResultCodes,
|
||||
RequestFlow,
|
||||
RequestFlowBuilder,
|
||||
CallParams,
|
||||
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||
|
||||
|
||||
// Services
|
||||
|
||||
|
||||
// Functions
|
||||
|
||||
export function demoCalculation(config?: {ttl?: number}) : Promise<number>;
|
||||
export function demoCalculation(peer: FluencePeer, config?: {ttl?: number}) : Promise<number>;
|
||||
export function demoCalculation(...args: any) {
|
||||
let peer: FluencePeer;
|
||||
|
||||
let config: any;
|
||||
if (FluencePeer.isInstance(args[0])) {
|
||||
peer = args[0];
|
||||
config = args[1];
|
||||
} else {
|
||||
peer = Fluence.getPeer();
|
||||
config = args[0];
|
||||
}
|
||||
|
||||
let request: RequestFlow;
|
||||
const promise = new Promise<number>((resolve, reject) => {
|
||||
const r = new RequestFlowBuilder()
|
||||
.disableInjections()
|
||||
.withRawScript(
|
||||
`
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||
(call -relay- ("op" "noop") [])
|
||||
)
|
||||
(call "12D3KooWSD5PToNiLQwKDXsu8JSysCwUt8BVUJEqCHcDe7P5h45e" ("op" "noop") [])
|
||||
)
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call "12D3KooWLBkw4Tz8bRoSriy5WEpHyWfU11jEK3b5yCa7FBRDRWH3" ("calc" "add") [10])
|
||||
(call "12D3KooWLBkw4Tz8bRoSriy5WEpHyWfU11jEK3b5yCa7FBRDRWH3" ("calc" "multiply") [5])
|
||||
)
|
||||
(call "12D3KooWLBkw4Tz8bRoSriy5WEpHyWfU11jEK3b5yCa7FBRDRWH3" ("calc" "subtract") [8])
|
||||
)
|
||||
(call "12D3KooWLBkw4Tz8bRoSriy5WEpHyWfU11jEK3b5yCa7FBRDRWH3" ("calc" "divide") [6])
|
||||
)
|
||||
(call "12D3KooWLBkw4Tz8bRoSriy5WEpHyWfU11jEK3b5yCa7FBRDRWH3" ("calc" "getResult") [] res)
|
||||
)
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call "12D3KooWSD5PToNiLQwKDXsu8JSysCwUt8BVUJEqCHcDe7P5h45e" ("op" "noop") [])
|
||||
(call -relay- ("op" "noop") [])
|
||||
)
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||
)
|
||||
(call -relay- ("op" "noop") [])
|
||||
)
|
||||
)
|
||||
)
|
||||
(call "12D3KooWSD5PToNiLQwKDXsu8JSysCwUt8BVUJEqCHcDe7P5h45e" ("op" "noop") [])
|
||||
)
|
||||
(call -relay- ("op" "noop") [])
|
||||
)
|
||||
(xor
|
||||
(call %init_peer_id% ("callbackSrv" "response") [res])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||
)
|
||||
)
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||
)
|
||||
|
||||
`,
|
||||
)
|
||||
.configHandler((h) => {
|
||||
h.on('getDataSrv', '-relay-', () => {
|
||||
return peer.getStatus().relayPeerId;
|
||||
});
|
||||
|
||||
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 demoCalculation');
|
||||
})
|
||||
if(config && config.ttl) {
|
||||
r.withTTL(config.ttl)
|
||||
}
|
||||
request = r.build();
|
||||
});
|
||||
peer.internals.initiateFlow(request!);
|
||||
return promise;
|
||||
}
|
||||
|
59
js-sdk-examples/node-example/src/index.ts
Normal file
59
js-sdk-examples/node-example/src/index.ts
Normal file
@ -0,0 +1,59 @@
|
||||
import { Fluence } from "@fluencelabs/fluence";
|
||||
import { krasnodar } from "@fluencelabs/fluence-network-environment";
|
||||
import { registerCalc, CalcDef } from "./_aqua/calc";
|
||||
|
||||
class Calc implements CalcDef {
|
||||
private _state: number = 0;
|
||||
|
||||
add(n: number) {
|
||||
this._state += n;
|
||||
}
|
||||
|
||||
subtract(n: number) {
|
||||
this._state -= n;
|
||||
}
|
||||
|
||||
multiply(n: number) {
|
||||
this._state *= n;
|
||||
}
|
||||
|
||||
divide(n: number) {
|
||||
this._state /= n;
|
||||
}
|
||||
|
||||
reset() {
|
||||
this._state = 0;
|
||||
}
|
||||
|
||||
getResult() {
|
||||
return this._state;
|
||||
}
|
||||
}
|
||||
|
||||
const keypress = async () => {
|
||||
process.stdin.setRawMode(true);
|
||||
return new Promise<void>((resolve) =>
|
||||
process.stdin.once("data", () => {
|
||||
process.stdin.setRawMode(false);
|
||||
resolve();
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
async function main() {
|
||||
await Fluence.start({
|
||||
connectTo: krasnodar[0],
|
||||
});
|
||||
|
||||
registerCalc(new Calc());
|
||||
|
||||
console.log("application started");
|
||||
console.log("peer id is: ", Fluence.getStatus().peerId);
|
||||
console.log("relay is: ", Fluence.getStatus().relayPeerId);
|
||||
console.log("press any key to continue");
|
||||
await keypress();
|
||||
|
||||
await Fluence.stop();
|
||||
}
|
||||
|
||||
main();
|
Reference in New Issue
Block a user