add constant and stream examples

This commit is contained in:
DieMyst 2021-04-28 03:26:26 +03:00
parent 17495f9564
commit 9c84b765c0
8 changed files with 74 additions and 17 deletions

View File

@ -1,14 +1,18 @@
import "println.aqua"
const c = "hello"
service Getter("test"):
getNum: -> u32
const c = "non-default string"
-- a question mark means that this constant could be rewritten before this definition
const c ?= "default string"
const a = 1
func printConstant():
if a == 1:
print(c)
func callConstant(cb: string -> ()):
n <- Getter.getNum()
if n == a:
cb(c)
else:
print(c)
cb(c)

11
aqua/stream.aqua Normal file
View File

@ -0,0 +1,11 @@
service Stringer("stringer-id"):
returnString: string -> string
func checkStreams(ch: []string) -> []string:
stream: *string
stream <- Stringer.returnString("first")
stream <- Stringer.returnString("second")
for b <- ch:
stream <- Stringer.returnString(b)
<- stream

6
package-lock.json generated
View File

@ -5,9 +5,9 @@
"requires": true,
"dependencies": {
"@fluencelabs/aqua-cli": {
"version": "0.1.1-92",
"resolved": "https://registry.npmjs.org/@fluencelabs/aqua-cli/-/aqua-cli-0.1.1-92.tgz",
"integrity": "sha512-D5boZW20gqhi1ITIuJjWBikDZtBvCQr7V9U0qJu730UxOqUkUYSkv6SKW+rqyPddI7f6zzU8BMsPefLOpqj1yA==",
"version": "0.1.1-94",
"resolved": "https://registry.npmjs.org/@fluencelabs/aqua-cli/-/aqua-cli-0.1.1-94.tgz",
"integrity": "sha512-PPtFFrTOTKB8nwJ0hJX2MSRLCkM1CNi2/vEQ9yks/x2S9/knjNcq+t3pjrmRxH5YSye47GTlMMzVIKLfcRL1sg==",
"dev": true
},
"@fluencelabs/aquamarine-interpreter": {

View File

@ -22,7 +22,7 @@
"compile-aqua:air": "aqua-cli -i ./aqua/ -o ./compiled-air -a"
},
"devDependencies": {
"@fluencelabs/aqua-cli": "^0.1.1-92",
"@fluencelabs/aqua-cli": "^0.1.1-94",
"ts-node": "^9.1.1",
"typescript": "^4.2.4"
},

View File

@ -57,7 +57,7 @@ export async function print(client: FluenceClient, str: string): Promise<void> {
export async function printConstant(client: FluenceClient): Promise<void> {
export async function callConstant(client: FluenceClient, cb: (arg0: string) => void): Promise<void> {
let request;
const promise = new Promise<void>((resolve, reject) => {
request = new RequestFlowBuilder()
@ -67,11 +67,14 @@ export async function printConstant(client: FluenceClient): Promise<void> {
(xor
(seq
(call %init_peer_id% ("getDataSrv" "relay") [] relay)
(xor
(match 1 1
(call %init_peer_id% ("println-service-id" "print") ["hello"])
(seq
(call %init_peer_id% ("test" "getNum") [] n)
(xor
(match n 1
(call %init_peer_id% ("callbackSrv" "cb") ["non-default string"])
)
(call %init_peer_id% ("callbackSrv" "cb") ["non-default string"])
)
(call %init_peer_id% ("println-service-id" "print") ["hello"])
)
)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error%])
@ -86,7 +89,7 @@ export async function printConstant(client: FluenceClient): Promise<void> {
h.on('getRelayService', 'hasRelay', () => {// Not Used
return client.relayPeerId !== undefined;
});
h.on('callbackSrv', 'cb', (args) => {cb(args[0]); return {};});
h.onEvent('errorHandlingSrv', 'error', (args) => {
// assuming error is the single argument
@ -96,7 +99,7 @@ export async function printConstant(client: FluenceClient): Promise<void> {
})
.handleScriptError(reject)
.handleTimeout(() => {
reject('Request timed out for printConstant');
reject('Request timed out for callConstant');
})
.build();
});

14
src/constantsCall.ts Normal file
View File

@ -0,0 +1,14 @@
import {FluenceClient, registerServiceFunction} from "@fluencelabs/fluence";
import {callConstant} from "./compiled/constants";
export async function constantsCall(client: FluenceClient) {
registerServiceFunction(client, "test", "getNum", (args: any[], _) => {
return 1
})
return new Promise<string>(async (resolve, reject) => {
await callConstant(client, (a: string) => {
resolve(a)
});
})
}

View File

@ -11,6 +11,8 @@ import {foldCall} from "./foldCall";
import {ifCall} from "./if";
import {parCall} from "./parCall";
import {complexCall} from "./complex";
import {constantsCall} from "./constantsCall";
import {streamCall} from "./streamCall";
let deepEqual = require('deep-equal')
function checkCall(name: string, expected: any, actual: any, callBackOnError: () => void) {
@ -64,6 +66,12 @@ const main = async () => {
// complex.aqua
let complexCallResult = await complexCall(client)
// constants.aqua
let constantCallResult = await constantsCall(client)
// stream.aqua
let streamResult = await streamCall(client)
await client.disconnect();
let success = true;
@ -87,6 +95,10 @@ const main = async () => {
checkCall("complexCall", complexCallResult, "some str", cb)
checkCall("constantCall", constantCallResult, "non-default string", cb)
checkCall("streamCall", streamResult, ["first updated", "second updated", "third updated", "fourth updated"], cb)
if (success) {
process.exit(0)
} else {
@ -95,4 +107,7 @@ const main = async () => {
};
main();
main().catch((err) => {
console.log(err)
process.exit(1)
})

10
src/streamCall.ts Normal file
View File

@ -0,0 +1,10 @@
import {FluenceClient, registerServiceFunction} from "@fluencelabs/fluence";
import {checkStreams} from "./compiled/stream";
export async function streamCall(client: FluenceClient) {
registerServiceFunction(client, "stringer-id", "returnString", (args: any[], _) => {
return args[0] + " updated"
})
return checkStreams(client, ["third", "fourth"])
}