From 9c84b765c0c968b58c183a24e1de9c0d3f29aa5e Mon Sep 17 00:00:00 2001 From: DieMyst Date: Wed, 28 Apr 2021 03:26:26 +0300 Subject: [PATCH] add constant and stream examples --- aqua/constants.aqua | 14 +++++++++----- aqua/stream.aqua | 11 +++++++++++ package-lock.json | 6 +++--- package.json | 2 +- src/compiled/constants.ts | 17 ++++++++++------- src/constantsCall.ts | 14 ++++++++++++++ src/index.ts | 17 ++++++++++++++++- src/streamCall.ts | 10 ++++++++++ 8 files changed, 74 insertions(+), 17 deletions(-) create mode 100644 aqua/stream.aqua create mode 100644 src/constantsCall.ts create mode 100644 src/streamCall.ts diff --git a/aqua/constants.aqua b/aqua/constants.aqua index ded939f..b1ee458 100644 --- a/aqua/constants.aqua +++ b/aqua/constants.aqua @@ -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) diff --git a/aqua/stream.aqua b/aqua/stream.aqua new file mode 100644 index 0000000..f7dcf2f --- /dev/null +++ b/aqua/stream.aqua @@ -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 + diff --git a/package-lock.json b/package-lock.json index 4ad3f2c..d11740f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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": { diff --git a/package.json b/package.json index 0a1c0d2..056ab80 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/compiled/constants.ts b/src/compiled/constants.ts index 6d1c2e0..b5cc64f 100644 --- a/src/compiled/constants.ts +++ b/src/compiled/constants.ts @@ -57,7 +57,7 @@ export async function print(client: FluenceClient, str: string): Promise { -export async function printConstant(client: FluenceClient): Promise { +export async function callConstant(client: FluenceClient, cb: (arg0: string) => void): Promise { let request; const promise = new Promise((resolve, reject) => { request = new RequestFlowBuilder() @@ -67,11 +67,14 @@ export async function printConstant(client: FluenceClient): Promise { (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 { 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 { }) .handleScriptError(reject) .handleTimeout(() => { - reject('Request timed out for printConstant'); + reject('Request timed out for callConstant'); }) .build(); }); diff --git a/src/constantsCall.ts b/src/constantsCall.ts new file mode 100644 index 0000000..f1177a3 --- /dev/null +++ b/src/constantsCall.ts @@ -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(async (resolve, reject) => { + await callConstant(client, (a: string) => { + resolve(a) + }); + }) +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 77bad11..7ee1cdc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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) +}) diff --git a/src/streamCall.ts b/src/streamCall.ts new file mode 100644 index 0000000..f2263a8 --- /dev/null +++ b/src/streamCall.ts @@ -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"]) +} \ No newline at end of file