Doc improvements, script generator (#979)

This commit is contained in:
Dima 2020-11-19 15:32:49 +03:00 committed by GitHub
parent 95be4bbd6d
commit f7e99247d1
4 changed files with 135 additions and 3 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "fluence", "name": "fluence",
"version": "0.7.68", "version": "0.7.77",
"description": "the browser js-libp2p client for the Fluence network", "description": "the browser js-libp2p client for the Fluence network",
"main": "./dist/fluence.js", "main": "./dist/fluence.js",
"typings": "./dist/fluence.d.ts", "typings": "./dist/fluence.d.ts",

123
src/aqua/scripts.ts Normal file
View File

@ -0,0 +1,123 @@
/*
* Copyright 2020 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Experimental attempts to generate aqua code through typescript functions.
*/
export interface Value<T> {
name: string,
value: T
}
export function value<T>(name: string, v: T): Value<T> {
return { name, value: v }
}
function updateData(value: Value<any>, data: Map<string, any>): void {
if (!data.get(value.name)) {
data.set(value.name, value.value)
}
}
function isValue<T>(value: string | Value<T>): value is Value<T> {
return (value as Value<T>).name !== undefined;
}
/**
* Generate a script with a call. Data is modified.
* @param target
* @param service
* @param functionV
* @param args
* @param returnName
* @param data
*/
export function call(target: string | Value<string>, service: string | Value<string>,
functionV: string | Value<string>, args: (string | Value<any>)[],
returnName: string | undefined, data: Map<string, any>): string {
let targetName = target;
if (isValue(target)) {
updateData(target, data)
targetName = target.name
}
let serviceName = service;
if (isValue(service)) {
updateData(service, data)
serviceName = service.name;
}
let functionName = functionV;
if (isValue(functionV)) {
updateData(functionV, data)
functionName = functionV.name;
}
let argsStr: string[] = []
args.forEach((v) => {
if (isValue(v)) {
updateData(v, data)
argsStr.push(v.name)
} else {
argsStr.push(v)
}
})
if (!returnName) {
returnName = ""
}
return `(call ${targetName} ("${serviceName}" "${functionName}") [${argsStr.join(" ")}] ${returnName})`
}
function wrap(command: string, scripts: string[]): string {
if (scripts.length === 2) {
return `(${command}
${scripts[0]}
${scripts[1]}
)`
} else {
let first = scripts.shift()
return `(${command}
${first}
${wrap(command, scripts)}
)`
}
}
/**
* Wrap an array of scripts with multiple 'seq' commands
* @param scripts
*/
export function seq(scripts: string[]): string {
if (scripts.length < 2) {
throw new Error("For 'seq' there must be at least 2 scripts")
}
return wrap("seq", scripts)
}
/**
* Wrap a script with 'par' command
* @param script
*/
export function par(script: string): string {
return `par(
${script}
)
`
}

View File

@ -41,7 +41,12 @@ export default class Fluence {
* @param multiaddr must contain host peer id * @param multiaddr must contain host peer id
* @param peerId your peer id. Should be with a private key. Could be generated by `generatePeerId()` function * @param peerId your peer id. Should be with a private key. Could be generated by `generatePeerId()` function
*/ */
static async connect(multiaddr: string | Multiaddr, peerId: PeerId): Promise<FluenceClient> { static async connect(multiaddr: string | Multiaddr, peerId?: PeerId): Promise<FluenceClient> {
if (!peerId) {
peerId = await Fluence.generatePeerId()
}
let client = new FluenceClient(peerId); let client = new FluenceClient(peerId);
await client.connect(multiaddr); await client.connect(multiaddr);

View File

@ -23,7 +23,7 @@ import pipe from "it-pipe";
import Multiaddr from "multiaddr"; import Multiaddr from "multiaddr";
import PeerId from "peer-id"; import PeerId from "peer-id";
import * as log from 'loglevel'; import * as log from 'loglevel';
import {parseParticle, Particle, stringifyParticle} from "./particle"; import {build, parseParticle, Particle, stringifyParticle} from "./particle";
export const PROTOCOL_NAME = '/fluence/faas/1.0.0'; export const PROTOCOL_NAME = '/fluence/faas/1.0.0';
@ -119,6 +119,10 @@ export class FluenceConnection {
this.status = Status.Disconnected; this.status = Status.Disconnected;
} }
async buildParticle(script: string, data: Map<string, any>, ttl?: number): Promise<Particle> {
return build(this.selfPeerId, script, data, ttl)
}
async sendParticle(particle: Particle): Promise<void> { async sendParticle(particle: Particle): Promise<void> {
this.checkConnectedOrThrow(); this.checkConnectedOrThrow();