mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2024-12-04 18:00:18 +00:00
feat(js-client): Add fire-and-forget flag [DXJ-562] (#400)
* Add fire-and-forget flag * Fix test * Store particle behavior in queue item * Fix tests
This commit is contained in:
parent
9b629eef2e
commit
86a73027e5
@ -80,6 +80,7 @@ describe("User API methods", () => {
|
||||
args: {},
|
||||
peer,
|
||||
script,
|
||||
fireAndForget: false,
|
||||
});
|
||||
|
||||
expect(res).toBe(7);
|
||||
|
@ -134,6 +134,7 @@ export const v5_callFunction = async (
|
||||
peer: peerOrArg,
|
||||
args: callArgs,
|
||||
config,
|
||||
fireAndForget: returnTypeVoid,
|
||||
});
|
||||
|
||||
if (returnTypeVoid) {
|
||||
|
@ -55,7 +55,7 @@ describe("FluenceClient usage test suite", () => {
|
||||
},
|
||||
});
|
||||
|
||||
peer.internals.initiateParticle(particle, resolve, reject);
|
||||
peer.internals.initiateParticle(particle, resolve, reject, false);
|
||||
});
|
||||
|
||||
await expect(promise).rejects.toThrow(ExpirationError);
|
||||
|
@ -49,6 +49,7 @@ export type CallAquaFunctionArgs = {
|
||||
config?: CallAquaFunctionConfig | undefined;
|
||||
peer: FluencePeer;
|
||||
args: { [key: string]: JSONValue | ArgCallbackFunction };
|
||||
fireAndForget: boolean;
|
||||
};
|
||||
|
||||
export type CallAquaFunctionConfig = {
|
||||
@ -60,6 +61,7 @@ export const callAquaFunction = async ({
|
||||
config = {},
|
||||
peer,
|
||||
args,
|
||||
fireAndForget,
|
||||
}: CallAquaFunctionArgs) => {
|
||||
log.trace("calling aqua function %j", { script, config, args });
|
||||
|
||||
@ -85,6 +87,6 @@ export const callAquaFunction = async ({
|
||||
|
||||
registerParticleScopeService(peer, particle, errorHandlingService(reject));
|
||||
|
||||
peer.internals.initiateParticle(particle, resolve, reject);
|
||||
peer.internals.initiateParticle(particle, resolve, reject, fireAndForget);
|
||||
});
|
||||
};
|
||||
|
@ -255,11 +255,13 @@ export abstract class FluencePeer {
|
||||
* @param particle - particle to start execution of
|
||||
* @param onSuccess - callback which is called when particle execution succeed
|
||||
* @param onError - callback which is called when particle execution fails
|
||||
* @param fireAndForget - determines whether particle has fire-and-forget behavior
|
||||
*/
|
||||
initiateParticle: (
|
||||
particle: IParticle,
|
||||
onSuccess: (result: JSONValue) => void,
|
||||
onError: (error: Error) => void,
|
||||
fireAndForget: boolean = true,
|
||||
): void => {
|
||||
if (!this.isInitialized) {
|
||||
throw new Error(
|
||||
@ -278,6 +280,7 @@ export abstract class FluencePeer {
|
||||
callResults: [],
|
||||
onSuccess,
|
||||
onError,
|
||||
fireAndForget,
|
||||
});
|
||||
},
|
||||
|
||||
@ -607,6 +610,9 @@ export abstract class FluencePeer {
|
||||
if (item.result.callRequests.length > 0) {
|
||||
// TS doesn't allow to pass just 'item'
|
||||
void this.execCallRequests({ ...item, result: item.result });
|
||||
} else if (item.fireAndForget === true) {
|
||||
// Local work done.
|
||||
item.onSuccess(null);
|
||||
}
|
||||
|
||||
return connectionPromise;
|
||||
|
@ -187,6 +187,7 @@ export interface ParticleQueueItem {
|
||||
callResults: CallResultsArray;
|
||||
onSuccess: (result: JSONValue) => void;
|
||||
onError: (error: Error) => void;
|
||||
fireAndForget?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -103,11 +103,18 @@ export const compileAqua = async (aquaFile: string): Promise<CompiledFile> => {
|
||||
const functions = Object.entries(compilationResult.functions)
|
||||
.map(([name, fnInfo]: [string, FunctionInfo]) => {
|
||||
const callFn = (peer: FluencePeer, args: PassedArgs) => {
|
||||
const def = fnInfo.funcDef;
|
||||
|
||||
const isReturnTypeVoid =
|
||||
def.arrow.codomain.tag === "nil" ||
|
||||
def.arrow.codomain.items.length === 0;
|
||||
|
||||
return callAquaFunction({
|
||||
script: fnInfo.script,
|
||||
config: {},
|
||||
peer: peer,
|
||||
args,
|
||||
fireAndForget: isReturnTypeVoid,
|
||||
});
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user