mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-06-25 22:01:32 +00:00
Particle lifecycle (#21)
Complete rethinking and refactoring of the codebase. The codebase basically consists these 5 moving parts now: 1. Fluence client (and the Particle processor which might be merged with the client) - This part is responsible for initiating Request flows, managing existing requests flows (it keeps the queue of received particles), pulling right strings on request flows to update their state etc 2. Fluence connection - This part is responsible for connecting to network, sending\receiving particles 3. RequestFlow - This is where the state of particle execution process is kept. It is basically a state storage with some control levers to update the state. Each request flow contains some particle lifecycle methods and the AquaCallHandler where all callback logic is kept 4. RequestFlowBuilder - This is where requests are prepared by the user (the developer of the client application) before they are ready to be sent into the network. 5. AquaCallHandler - This is how interpreter callbacks are handled. It is very similar to express.js app and is made of middlewares. Aqua handler is the unified api for both callbacks for our Request flows and non-ours (i.e services that are expected to be called be other peers). See `AquaHandler.ts` for details
This commit is contained in:
@ -14,11 +14,10 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import bs58 from 'bs58';
|
||||
import { sendParticleAsFetch } from '../api';
|
||||
import { Particle } from './particle';
|
||||
import { FluenceClient } from '../FluenceClient';
|
||||
import { RequestFlow } from './RequestFlow';
|
||||
import { ModuleConfig } from './moduleConfig';
|
||||
import { RequestFlowBuilder } from './RequestFlowBuilder';
|
||||
import { FluenceClient } from 'src/api.unstable';
|
||||
|
||||
const nodeIdentityCall = (client: FluenceClient): string => {
|
||||
return `(call "${client.relayPeerId}" ("op" "identity") [])`;
|
||||
@ -56,7 +55,13 @@ const requestResponse = async <T>(
|
||||
)
|
||||
`;
|
||||
|
||||
const res = await sendParticleAsFetch<any[]>(client, new Particle(script, data, ttl), name);
|
||||
const [request, promise] = new RequestFlowBuilder()
|
||||
.withRawScript(script)
|
||||
.withVariables(data)
|
||||
.withTTL(ttl)
|
||||
.buildAsFetch<any[]>('_callback', name);
|
||||
await client.initiateFlow(request);
|
||||
const res = await promise;
|
||||
return handleResponse(res);
|
||||
};
|
||||
|
||||
@ -67,21 +72,25 @@ const requestResponse = async <T>(
|
||||
*/
|
||||
export const getModules = async (client: FluenceClient, ttl?: number): Promise<string[]> => {
|
||||
let callbackFn = 'getModules';
|
||||
const particle = new Particle(
|
||||
`
|
||||
const [req, promise] = new RequestFlowBuilder()
|
||||
.withRawScript(
|
||||
`
|
||||
(seq
|
||||
(call __relay ("dist" "list_modules") [] result)
|
||||
(call myPeerId ("_callback" "${callbackFn}") [result])
|
||||
)
|
||||
`,
|
||||
{
|
||||
)
|
||||
.withVariables({
|
||||
__relay: client.relayPeerId,
|
||||
myPeerId: client.selfPeerId,
|
||||
},
|
||||
ttl,
|
||||
);
|
||||
})
|
||||
.withTTL(ttl)
|
||||
.buildAsFetch<[string[]]>('_callback', callbackFn);
|
||||
client.initiateFlow(req);
|
||||
|
||||
return sendParticleAsFetch(client, particle, callbackFn);
|
||||
const [res] = await promise;
|
||||
return res;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -91,8 +100,9 @@ export const getModules = async (client: FluenceClient, ttl?: number): Promise<s
|
||||
*/
|
||||
export const getInterfaces = async (client: FluenceClient, ttl?: number): Promise<string[]> => {
|
||||
let callbackFn = 'getInterfaces';
|
||||
const particle = new Particle(
|
||||
`
|
||||
const [req, promise] = new RequestFlowBuilder()
|
||||
.withRawScript(
|
||||
`
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
@ -109,14 +119,17 @@ export const getInterfaces = async (client: FluenceClient, ttl?: number): Promis
|
||||
(call myPeerId ("_callback" "${callbackFn}") [interfaces])
|
||||
)
|
||||
`,
|
||||
{
|
||||
)
|
||||
.withVariables({
|
||||
relay: client.relayPeerId,
|
||||
myPeerId: client.selfPeerId,
|
||||
},
|
||||
ttl,
|
||||
);
|
||||
})
|
||||
.withTTL(ttl)
|
||||
.buildAsFetch<[string[]]>('_callback', callbackFn);
|
||||
|
||||
const [res] = await sendParticleAsFetch<[string[]]>(client, particle, callbackFn);
|
||||
client.initiateFlow(req);
|
||||
|
||||
const [res] = await promise;
|
||||
return res;
|
||||
};
|
||||
|
||||
@ -153,15 +166,22 @@ export const uploadModule = async (
|
||||
data.set('__relay', client.relayPeerId);
|
||||
data.set('myPeerId', client.selfPeerId);
|
||||
|
||||
let script = `
|
||||
const [req, promise] = new RequestFlowBuilder()
|
||||
.withRawScript(
|
||||
`
|
||||
(seq
|
||||
(call __relay ("dist" "add_module") [module_bytes module_config] result)
|
||||
(call myPeerId ("_callback" "getModules") [result])
|
||||
|
||||
)
|
||||
`;
|
||||
`,
|
||||
)
|
||||
.withVariables(data)
|
||||
.withTTL(ttl)
|
||||
.buildAsFetch<[string[]]>('_callback', 'getModules');
|
||||
|
||||
return sendParticleAsFetch(client, new Particle(script, data, ttl), 'getModules', '_callback');
|
||||
await client.initiateFlow(req);
|
||||
await promise;
|
||||
};
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user