diff --git a/packages/core/js-client/src/api.spec.ts b/packages/core/js-client/src/api.spec.ts index a53dba27..d2a19e1d 100644 --- a/packages/core/js-client/src/api.spec.ts +++ b/packages/core/js-client/src/api.spec.ts @@ -80,6 +80,7 @@ describe("User API methods", () => { args: {}, peer, script, + fireAndForget: false, }); expect(res).toBe(7); diff --git a/packages/core/js-client/src/api.ts b/packages/core/js-client/src/api.ts index 309ecaed..e61ce1fa 100644 --- a/packages/core/js-client/src/api.ts +++ b/packages/core/js-client/src/api.ts @@ -134,6 +134,7 @@ export const v5_callFunction = async ( peer: peerOrArg, args: callArgs, config, + fireAndForget: returnTypeVoid, }); if (returnTypeVoid) { diff --git a/packages/core/js-client/src/clientPeer/__test__/client.spec.ts b/packages/core/js-client/src/clientPeer/__test__/client.spec.ts index 4da9f978..98af037f 100644 --- a/packages/core/js-client/src/clientPeer/__test__/client.spec.ts +++ b/packages/core/js-client/src/clientPeer/__test__/client.spec.ts @@ -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); diff --git a/packages/core/js-client/src/compilerSupport/callFunction.ts b/packages/core/js-client/src/compilerSupport/callFunction.ts index f07cc107..1e2971e9 100644 --- a/packages/core/js-client/src/compilerSupport/callFunction.ts +++ b/packages/core/js-client/src/compilerSupport/callFunction.ts @@ -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); }); }; diff --git a/packages/core/js-client/src/jsPeer/FluencePeer.ts b/packages/core/js-client/src/jsPeer/FluencePeer.ts index 5f2e736c..926788b4 100644 --- a/packages/core/js-client/src/jsPeer/FluencePeer.ts +++ b/packages/core/js-client/src/jsPeer/FluencePeer.ts @@ -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; diff --git a/packages/core/js-client/src/particle/Particle.ts b/packages/core/js-client/src/particle/Particle.ts index e9c41a9f..cf3db44a 100644 --- a/packages/core/js-client/src/particle/Particle.ts +++ b/packages/core/js-client/src/particle/Particle.ts @@ -187,6 +187,7 @@ export interface ParticleQueueItem { callResults: CallResultsArray; onSuccess: (result: JSONValue) => void; onError: (error: Error) => void; + fireAndForget?: boolean; } /** diff --git a/packages/core/js-client/src/util/testUtils.ts b/packages/core/js-client/src/util/testUtils.ts index 6e502abb..8ba6a3de 100644 --- a/packages/core/js-client/src/util/testUtils.ts +++ b/packages/core/js-client/src/util/testUtils.ts @@ -103,11 +103,18 @@ export const compileAqua = async (aquaFile: string): Promise => { 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, }); };