mirror of
https://github.com/fluencelabs/gitbook-docs
synced 2025-04-25 07:52:14 +00:00
GitBook: [docs] 2 pages modified
This commit is contained in:
parent
4d6f33b6f3
commit
e824ad2e2f
@ -1,4 +1,4 @@
|
||||
# Building A Frontend with JS SDK
|
||||
# Building a Frontend with JS-SDK
|
||||
|
||||
Fluence provides means to connect to the network from a javascript environment. It is currently tested to work in nodejs and modern browsers.
|
||||
|
||||
@ -8,20 +8,20 @@ Even though all the logic could be programmed by hand with raw air it is strongl
|
||||
|
||||
## Basic usage
|
||||
|
||||
As previously said you can use fluence with any frontend or nodejs framework. JS SDK could be as any other npm library. For the purpose of the demo we will init a bare-bones nodejs package and show you the steps needed install JS SDK and aqua compiler. Feel free to use the tool most suitable for the framework used in application, the installation process should be roughly the same
|
||||
As previously said you can use fluence with any frontend or nodejs framework. JS SDK could be as any other npm library. For the purpose of the demo we will init a bare-bones nodejs package and show you the steps needed install JS SDK and aqua compiler. Feel free to use the tool most suitable for the framework used in application, the installation process should be roughly the same
|
||||
|
||||
### 1. Start with npm package
|
||||
|
||||
For the purpose of the demo we will use a very minimal npm package with typescript support:
|
||||
|
||||
```text
|
||||
```
|
||||
src
|
||||
┗ index.ts (1)
|
||||
package.json (2)
|
||||
tsconfig.json
|
||||
```
|
||||
|
||||
index.ts \(1\):
|
||||
index.ts (1):
|
||||
|
||||
```typescript
|
||||
async function main() {
|
||||
@ -31,9 +31,9 @@ async function main() {
|
||||
main();
|
||||
```
|
||||
|
||||
package.json \(2\):
|
||||
package.json (2):
|
||||
|
||||
```javascript
|
||||
```json
|
||||
{
|
||||
"name": "demo",
|
||||
"version": "1.0.0",
|
||||
@ -49,6 +49,7 @@ package.json \(2\):
|
||||
"typescript": "^4.2.4"
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Let's test if it works:
|
||||
@ -94,6 +95,7 @@ async function main() {
|
||||
}
|
||||
|
||||
main();
|
||||
|
||||
```
|
||||
|
||||
Let's try it out:
|
||||
@ -106,6 +108,7 @@ $ npm run exec
|
||||
|
||||
Is client connected: true
|
||||
$
|
||||
|
||||
```
|
||||
|
||||
**Note**: typically you should have a single instance`FluenceClient` per application since it represents it's identity in the network. You are free to store the instance anywhere you like.
|
||||
@ -114,7 +117,7 @@ $
|
||||
|
||||
Aqua is the proffered language for the Fluence network. It can be used with javascript-based environments via npm package.
|
||||
|
||||
**Warning: the package requires java to be installed \(it will call "java -jar ... "\)**
|
||||
**Warning: the package requires java to be installed (it will call "java -jar ... ") **
|
||||
|
||||
```bash
|
||||
npm install --save-dev @fluencelabs/aqua-cli
|
||||
@ -122,13 +125,13 @@ npm install --save-dev @fluencelabs/aqua-cli
|
||||
|
||||
We will also need the standard library for the language
|
||||
|
||||
```text
|
||||
```
|
||||
npm install --save-dev @fluencelabs/aqua-lib
|
||||
```
|
||||
|
||||
Let's add our first aqua file:
|
||||
|
||||
```text
|
||||
```
|
||||
aqua (1)
|
||||
┗ demo.aqua (2)
|
||||
node_modules
|
||||
@ -140,11 +143,11 @@ package.json
|
||||
tsconfig.json
|
||||
```
|
||||
|
||||
Add two directories, one for aqua sources \(1\) and another for the typescript output \(3\)
|
||||
Add two directories, one for aqua sources (1) and another for the typescript output (3)
|
||||
|
||||
Create a new text file called `demo.aqua` \(2\):
|
||||
Create a new text file called `demo.aqua` (2):
|
||||
|
||||
```text
|
||||
```
|
||||
import "@fluencelabs/aqua-lib/builtin.aqua"
|
||||
|
||||
func demo(nodePeerId: PeerId) -> []string:
|
||||
@ -157,7 +160,7 @@ This script will gather the list of external addresses from some node in the net
|
||||
|
||||
The aqua code can now be compiled by using the compiler CLI. We suggest adding a script to the package.json file like so:
|
||||
|
||||
```javascript
|
||||
```json
|
||||
...
|
||||
"scripts": {
|
||||
"exec": "node -r ts-node/register src/index.ts",
|
||||
@ -174,7 +177,7 @@ npm run compile-aqua
|
||||
|
||||
A typescript file should be generated like so:
|
||||
|
||||
```text
|
||||
```
|
||||
aqua
|
||||
┗ demo.aqua
|
||||
node_modules
|
||||
@ -211,11 +214,12 @@ async function main() {
|
||||
}
|
||||
|
||||
main();
|
||||
|
||||
```
|
||||
|
||||
if everything is fine you have similar result:
|
||||
|
||||
```text
|
||||
```
|
||||
$ npm run exec
|
||||
|
||||
> demo@1.0.0 exec C:\work\demo
|
||||
@ -227,14 +231,17 @@ Node 12D3KooWHk9BjDQBUqnavciRPhAYFvqKBe4ZiPPvde7vDaqgn5er is connected to: /ip4/
|
||||
$
|
||||
```
|
||||
|
||||
|
||||
|
||||
## Advanced usage
|
||||
|
||||
Fluence JS SDK gives options to register own handlers for aqua vm service calls
|
||||
|
||||
**TBD**
|
||||
|
||||
|
||||
|
||||
## References
|
||||
|
||||
* For the list of compiler options see: [https://github.com/fluencelabs/aqua](https://github.com/fluencelabs/aqua)
|
||||
* Repository with additional examples: [https://github.com/fluencelabs/aqua-playground](https://github.com/fluencelabs/aqua-playground)
|
||||
|
||||
- For the list of compiler options see: https://github.com/fluencelabs/aqua
|
||||
- Repository with additional examples: https://github.com/fluencelabs/aqua-playground
|
@ -23,7 +23,7 @@ AIR instructions are intended to launch the execution of a service method as fol
|
||||
4. The arguments specified by the argument list are passed to the method
|
||||
5. The result of the method returned under the name output name
|
||||
|
||||
**Figure 2: Sequential Instruction** 
|
||||
**Figure 2: Sequential Instruction** 
|
||||
|
||||
The _**seq**_ instruction takes two instructions at most as its arguments and executes them sequentially, one after the other.
|
||||
|
||||
@ -33,7 +33,7 @@ The _**par**_ instruction takes two instructions at most as its arguments and pa
|
||||
|
||||
TODO: add better graphic showing the disticntion of branching vs seq.
|
||||
|
||||
**Figure 4: Fold Instruction** 
|
||||
**Figure 4: Fold Instruction** 
|
||||
|
||||
The _**fold**_ instruction iterates over the elements of an array and workds as follows:
|
||||
|
||||
@ -49,7 +49,7 @@ This instruction is intended for organizing branches in the flow of execution as
|
||||
* The first instruction is executed and if the execution is successful, then the second instruction is ignored
|
||||
* If the first instruction fails, then the second one is executed.
|
||||
|
||||
**Figure 6: Null Instruction** 
|
||||
**Figure 6: Null Instruction** 
|
||||
|
||||
This is an empty instruction: it takes no arguments and does nothing. The _**null**_ instruction is useful for generating code.
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user