2021-03-03 22:01:05 +03:00
|
|
|
|
import { RequestFlowBuilder } from './internal/RequestFlowBuilder';
|
2021-03-05 17:50:52 +03:00
|
|
|
|
import { FluenceClient } from './FluenceClient';
|
2021-05-18 09:53:12 +03:00
|
|
|
|
import { CallServiceResultType } from './internal/CallServiceHandler';
|
|
|
|
|
import { SecurityTetraplet } from '@fluencelabs/avm';
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
2021-03-03 22:01:05 +03:00
|
|
|
|
/**
|
|
|
|
|
* The class representing Particle - a data structure used to perform operations on Fluence Network. It originates on some peer in the network, travels the network through a predefined path, triggering function execution along its way.
|
|
|
|
|
*/
|
|
|
|
|
export class Particle {
|
|
|
|
|
script: string;
|
|
|
|
|
data: Map<string, any>;
|
|
|
|
|
ttl: number;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a particle with specified parameters.
|
|
|
|
|
* @param { String }script - Air script which defines the execution of a particle – its path, functions it triggers on peers, and so on.
|
|
|
|
|
* @param { Map<string, any> | Record<string, any> } data - Variables passed to the particle in the form of either JS Map or JS object with keys representing variable names and values representing values correspondingly
|
2021-05-18 09:53:12 +03:00
|
|
|
|
* @param { [Number]=7000 } ttl - Time to live, a timout after which the particle execution is stopped by AVM.
|
2021-03-03 22:01:05 +03:00
|
|
|
|
*/
|
|
|
|
|
constructor(script: string, data?: Map<string, any> | Record<string, any>, ttl?: number) {
|
|
|
|
|
this.script = script;
|
|
|
|
|
if (data === undefined) {
|
|
|
|
|
this.data = new Map();
|
|
|
|
|
} else if (data instanceof Map) {
|
|
|
|
|
this.data = data;
|
2021-01-19 15:47:49 +03:00
|
|
|
|
} else {
|
2021-03-03 22:01:05 +03:00
|
|
|
|
this.data = new Map();
|
|
|
|
|
for (let k in data) {
|
|
|
|
|
this.data.set(k, data[k]);
|
|
|
|
|
}
|
2021-01-19 15:47:49 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-03 22:01:05 +03:00
|
|
|
|
this.ttl = ttl ?? 7000;
|
2021-01-19 15:47:49 +03:00
|
|
|
|
}
|
2021-03-03 22:01:05 +03:00
|
|
|
|
}
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
2021-01-29 16:48:27 +03:00
|
|
|
|
/**
|
|
|
|
|
* Send a particle to Fluence Network using the specified Fluence Client.
|
|
|
|
|
* @param { FluenceClient } client - The Fluence Client instance.
|
|
|
|
|
* @param { Particle } particle - The particle to send.
|
|
|
|
|
*/
|
2021-03-03 22:01:05 +03:00
|
|
|
|
export const sendParticle = async (
|
|
|
|
|
client: FluenceClient,
|
|
|
|
|
particle: Particle,
|
|
|
|
|
onError?: (err) => void,
|
|
|
|
|
): Promise<string> => {
|
|
|
|
|
const [req, errorPromise] = new RequestFlowBuilder()
|
|
|
|
|
.withRawScript(particle.script)
|
|
|
|
|
.withVariables(particle.data)
|
|
|
|
|
.withTTL(particle.ttl)
|
|
|
|
|
.buildWithErrorHandling();
|
|
|
|
|
|
|
|
|
|
errorPromise.catch(onError);
|
|
|
|
|
|
2021-03-05 17:50:52 +03:00
|
|
|
|
await client.initiateFlow(req);
|
2021-03-03 22:01:05 +03:00
|
|
|
|
return req.id;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
This map stores functions which unregister callbacks registered by registerServiceFunction
|
|
|
|
|
The key sould be created with makeKey. The value is the unresitration function
|
|
|
|
|
This is only needed to support legacy api
|
|
|
|
|
*/
|
|
|
|
|
const handlersUnregistratorsMap = new Map();
|
|
|
|
|
const makeKey = (client: FluenceClient, serviceId: string, fnName: string) => {
|
|
|
|
|
const pid = client.selfPeerId || '';
|
|
|
|
|
return `${pid}/${serviceId}/${fnName}`;
|
2021-01-19 15:47:49 +03:00
|
|
|
|
};
|
|
|
|
|
|
2021-01-29 16:48:27 +03:00
|
|
|
|
/**
|
2021-05-18 09:53:12 +03:00
|
|
|
|
* Registers a function which can be called on the client from AVM. The registration is per client basis.
|
2021-01-29 16:48:27 +03:00
|
|
|
|
* @param { FluenceClient } client - The Fluence Client instance.
|
2021-05-18 09:53:12 +03:00
|
|
|
|
* @param { string } serviceId - The identifier of service which would be used to make calls from AVM
|
|
|
|
|
* @param { string } fnName - The identifier of function which would be used to make calls from AVM
|
|
|
|
|
* @param { (args: any[], tetraplets: SecurityTetraplet[][]) => object | boolean | number | string } handler - The handler which would be called by AVM. The result is any object passed back to AVM
|
2021-01-29 16:48:27 +03:00
|
|
|
|
*/
|
2021-01-19 15:47:49 +03:00
|
|
|
|
export const registerServiceFunction = (
|
|
|
|
|
client: FluenceClient,
|
|
|
|
|
serviceId: string,
|
|
|
|
|
fnName: string,
|
2021-05-18 09:53:12 +03:00
|
|
|
|
handler: (args: any[], tetraplets: SecurityTetraplet[][]) => CallServiceResultType,
|
2021-01-19 15:47:49 +03:00
|
|
|
|
) => {
|
2021-05-18 09:53:12 +03:00
|
|
|
|
const unregister = client.callServiceHandler.on(serviceId, fnName, handler);
|
2021-03-03 22:01:05 +03:00
|
|
|
|
handlersUnregistratorsMap.set(makeKey(client, serviceId, fnName), unregister);
|
2021-01-19 15:47:49 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// prettier-ignore
|
2021-01-29 16:48:27 +03:00
|
|
|
|
/**
|
|
|
|
|
* Removes registers for the function previously registered with {@link registerServiceFunction}
|
|
|
|
|
* @param { FluenceClient } client - The Fluence Client instance.
|
|
|
|
|
* @param { string } serviceId - The identifier of service used in {@link registerServiceFunction} call
|
|
|
|
|
* @param { string } fnName - The identifier of function used in {@link registerServiceFunction} call
|
|
|
|
|
*/
|
2021-01-19 15:47:49 +03:00
|
|
|
|
export const unregisterServiceFunction = (
|
|
|
|
|
client: FluenceClient,
|
|
|
|
|
serviceId: string,
|
|
|
|
|
fnName: string
|
|
|
|
|
) => {
|
2021-03-03 22:01:05 +03:00
|
|
|
|
const key = makeKey(client, serviceId, fnName);
|
|
|
|
|
const unuse = handlersUnregistratorsMap.get(key);
|
|
|
|
|
if(unuse) {
|
|
|
|
|
unuse();
|
|
|
|
|
}
|
|
|
|
|
handlersUnregistratorsMap.delete(key);
|
2021-01-19 15:47:49 +03:00
|
|
|
|
};
|
|
|
|
|
|
2021-01-29 16:48:27 +03:00
|
|
|
|
/**
|
2021-05-18 09:53:12 +03:00
|
|
|
|
* Registers an event-like handler for all calls to the specific service\function pair from AVM. The registration is per client basis. Return a function which when called removes the subscription.
|
2021-01-29 16:48:27 +03:00
|
|
|
|
* Same as registerServiceFunction which immediately returns empty object.
|
|
|
|
|
* @param { FluenceClient } client - The Fluence Client instance.
|
2021-05-18 09:53:12 +03:00
|
|
|
|
* @param { string } serviceId - The identifier of service calls to which from AVM are transformed into events.
|
|
|
|
|
* @param { string } fnName - The identifier of function calls to which from AVM are transformed into events.
|
|
|
|
|
* @param { (args: any[], tetraplets: SecurityTetraplet[][]) => object } handler - The handler which would be called by AVM
|
2021-01-29 16:48:27 +03:00
|
|
|
|
* @returns { Function } - A function which when called removes the subscription.
|
|
|
|
|
*/
|
2021-01-19 15:47:49 +03:00
|
|
|
|
export const subscribeToEvent = (
|
|
|
|
|
client: FluenceClient,
|
|
|
|
|
serviceId: string,
|
|
|
|
|
fnName: string,
|
|
|
|
|
handler: (args: any[], tetraplets: SecurityTetraplet[][]) => void,
|
|
|
|
|
): Function => {
|
|
|
|
|
const realHandler = (args: any[], tetraplets: SecurityTetraplet[][]) => {
|
|
|
|
|
// dont' block
|
2021-02-02 14:13:52 +03:00
|
|
|
|
setTimeout(() => {
|
2021-01-19 15:47:49 +03:00
|
|
|
|
handler(args, tetraplets);
|
2021-02-02 14:13:52 +03:00
|
|
|
|
}, 0);
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
};
|
|
|
|
|
registerServiceFunction(client, serviceId, fnName, realHandler);
|
|
|
|
|
return () => {
|
|
|
|
|
unregisterServiceFunction(client, serviceId, fnName);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2021-01-29 16:48:27 +03:00
|
|
|
|
/**
|
|
|
|
|
* Send a particle with a fetch-like semantics. In order to for this to work you have to you have to make a call to the same callbackServiceId\callbackFnName pair from Air script as specified by the parameters. The arguments of the call are returned as the resolve value of promise
|
|
|
|
|
* @param { FluenceClient } client - The Fluence Client instance.
|
|
|
|
|
* @param { Particle } particle - The particle to send.
|
|
|
|
|
* @param { string } callbackFnName - The identifier of function which should be used in Air script to pass the data to fetch "promise"
|
|
|
|
|
* @param { [string]='_callback' } callbackServiceId - The service identifier which should be used in Air script to pass the data to fetch "promise"
|
2021-05-18 09:53:12 +03:00
|
|
|
|
* @returns { Promise<T> } - A promise which would be resolved with the data returned from AVM
|
2021-01-29 16:48:27 +03:00
|
|
|
|
*/
|
2021-01-19 15:47:49 +03:00
|
|
|
|
export const sendParticleAsFetch = async <T>(
|
|
|
|
|
client: FluenceClient,
|
|
|
|
|
particle: Particle,
|
|
|
|
|
callbackFnName: string,
|
|
|
|
|
callbackServiceId: string = '_callback',
|
|
|
|
|
): Promise<T> => {
|
2021-03-03 22:01:05 +03:00
|
|
|
|
const [request, promise] = new RequestFlowBuilder()
|
|
|
|
|
.withRawScript(particle.script)
|
|
|
|
|
.withVariables(particle.data)
|
|
|
|
|
.withTTL(particle.ttl)
|
|
|
|
|
.buildAsFetch<T>(callbackServiceId, callbackFnName);
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
2021-03-05 17:50:52 +03:00
|
|
|
|
await client.initiateFlow(request);
|
2021-01-19 15:47:49 +03:00
|
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
|
};
|