mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-06-12 23:51:21 +00:00
Fix comments
This commit is contained in:
@ -27,7 +27,6 @@ import {
|
||||
aqua2ts,
|
||||
ts2aqua,
|
||||
wrapFunction,
|
||||
wrapServiceFunction,
|
||||
} from "./compilerSupport/conversions.js";
|
||||
import { ServiceImpl } from "./compilerSupport/types.js";
|
||||
import { FluencePeer } from "./jsPeer/FluencePeer.js";
|
||||
@ -43,7 +42,7 @@ import { callAquaFunction, Fluence, registerService } from "./index.js";
|
||||
* @param script - air script with function execution logic generated by the Aqua compiler
|
||||
*/
|
||||
export const v5_callFunction = async (
|
||||
args: (JSONValue | ((...args: JSONValue[]) => JSONValue))[],
|
||||
args: (JSONValue | ServiceImpl[string])[],
|
||||
def: FunctionCallDef,
|
||||
script: string,
|
||||
): Promise<unknown> => {
|
||||
@ -75,9 +74,7 @@ export const v5_callFunction = async (
|
||||
);
|
||||
}
|
||||
|
||||
const callArgs = Object.fromEntries<
|
||||
JSONValue | ((...args: JSONValue[]) => JSONValue)
|
||||
>(
|
||||
const callArgs = Object.fromEntries<JSONValue | ServiceImpl[string]>(
|
||||
args.slice(0, argCount).map((arg, i) => {
|
||||
const argSchema = functionArgs[argNames[i]];
|
||||
|
||||
@ -160,7 +157,7 @@ export const v5_registerService = (args: unknown[], def: ServiceDef): void => {
|
||||
|
||||
const wrappedServiceImpl = Object.fromEntries(
|
||||
Object.entries(serviceImpl).map(([name, func]) => {
|
||||
return [name, wrapServiceFunction(func, serviceSchema[name])];
|
||||
return [name, wrapFunction(func, serviceSchema[name])];
|
||||
}),
|
||||
);
|
||||
|
||||
|
@ -17,7 +17,6 @@
|
||||
import { JSONValue } from "@fluencelabs/interfaces";
|
||||
|
||||
import { FluencePeer } from "../jsPeer/FluencePeer.js";
|
||||
import { ParticleContext } from "../jsServiceHost/interfaces.js";
|
||||
import { logger } from "../util/logger.js";
|
||||
import { ArgCallbackFunction } from "../util/testUtils.js";
|
||||
|
||||
@ -74,16 +73,7 @@ export const callAquaFunction = async ({
|
||||
let service: ServiceDescription;
|
||||
|
||||
if (typeof argVal === "function") {
|
||||
service = userHandlerService(
|
||||
"callbackSrv",
|
||||
name,
|
||||
(...args: [...JSONValue[], ParticleContext]) => {
|
||||
// Impossible to extract all element except the last one and coerce type
|
||||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
||||
const jsonArgs = args.slice(0, args.length - 1) as JSONValue[];
|
||||
return argVal(jsonArgs);
|
||||
},
|
||||
);
|
||||
service = userHandlerService("callbackSrv", name, argVal);
|
||||
} else {
|
||||
service = injectValueService("getDataSrv", name, argVal);
|
||||
}
|
||||
|
@ -15,15 +15,17 @@
|
||||
*/
|
||||
|
||||
import {
|
||||
ArrowWithCallbacks,
|
||||
ArrowType,
|
||||
ArrowWithoutCallbacks,
|
||||
JSONValue,
|
||||
LabeledProductType,
|
||||
NonArrowSimpleType,
|
||||
SimpleTypes,
|
||||
} from "@fluencelabs/interfaces";
|
||||
|
||||
import { ParticleContext } from "../jsServiceHost/interfaces.js";
|
||||
|
||||
import { MaybePromise } from "./types.js";
|
||||
import { ServiceImpl } from "./types.js";
|
||||
|
||||
export function aqua2ts(
|
||||
value: JSONValue,
|
||||
@ -123,30 +125,9 @@ export function ts2aqua(
|
||||
}
|
||||
|
||||
export const wrapFunction = (
|
||||
value: (...args: JSONValue[]) => JSONValue,
|
||||
schema: ArrowWithoutCallbacks,
|
||||
): ((...args: JSONValue[]) => JSONValue) => {
|
||||
return (...args) => {
|
||||
const schemaArgs =
|
||||
schema.codomain.tag === "nil" ? [] : schema.codomain.items;
|
||||
|
||||
const tsArgs = args.map((arg, i) => {
|
||||
return aqua2ts(arg, schemaArgs[i]);
|
||||
});
|
||||
|
||||
const result = value(...tsArgs);
|
||||
return ts2aqua(result, schema.codomain);
|
||||
};
|
||||
};
|
||||
|
||||
export const wrapServiceFunction = (
|
||||
value: (
|
||||
...args: [...JSONValue[], ParticleContext]
|
||||
) => MaybePromise<JSONValue>,
|
||||
schema: ArrowWithCallbacks,
|
||||
): ((
|
||||
...args: [...JSONValue[], ParticleContext]
|
||||
) => MaybePromise<JSONValue>) => {
|
||||
value: ServiceImpl[string],
|
||||
schema: ArrowWithoutCallbacks | ArrowType<LabeledProductType<SimpleTypes>>,
|
||||
): ServiceImpl[string] => {
|
||||
return async (...args) => {
|
||||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
||||
const jsonArgs = args.slice(0, args.length - 1) as JSONValue[];
|
||||
@ -154,13 +135,24 @@ export const wrapServiceFunction = (
|
||||
const context = args[args.length - 1] as ParticleContext;
|
||||
|
||||
const schemaArgs =
|
||||
schema.codomain.tag === "nil" ? [] : schema.codomain.items;
|
||||
schema.domain.tag === "nil"
|
||||
? []
|
||||
: schema.domain.tag === "unlabeledProduct"
|
||||
? schema.domain.items
|
||||
: Object.values(schema.domain.fields);
|
||||
|
||||
const tsArgs = jsonArgs.map((arg, i) => {
|
||||
return aqua2ts(arg, schemaArgs[i]);
|
||||
});
|
||||
|
||||
const result = await value(...tsArgs, context);
|
||||
return ts2aqua(result, schema.codomain);
|
||||
|
||||
const valueSchema =
|
||||
schema.codomain.tag === "unlabeledProduct" &&
|
||||
schema.codomain.items.length === 1
|
||||
? schema.codomain.items[0]
|
||||
: schema.codomain;
|
||||
|
||||
return ts2aqua(result, valueSchema);
|
||||
};
|
||||
};
|
||||
|
@ -28,6 +28,7 @@ import { Subject, Subscribable } from "rxjs";
|
||||
import { ClientPeer, makeClientPeerConfig } from "../clientPeer/ClientPeer.js";
|
||||
import { ClientConfig, RelayOptions } from "../clientPeer/types.js";
|
||||
import { callAquaFunction } from "../compilerSupport/callFunction.js";
|
||||
import { ServiceImpl } from "../compilerSupport/types.js";
|
||||
import { IConnection } from "../connection/interfaces.js";
|
||||
import { DEFAULT_CONFIG, FluencePeer } from "../jsPeer/FluencePeer.js";
|
||||
import { CallServiceResultType } from "../jsServiceHost/interfaces.js";
|
||||
@ -76,9 +77,7 @@ interface FunctionInfo {
|
||||
/**
|
||||
* Type for callback passed as aqua function argument
|
||||
*/
|
||||
export type ArgCallbackFunction = (
|
||||
...args: JSONValue[]
|
||||
) => JSONValue | Promise<JSONValue>;
|
||||
export type ArgCallbackFunction = ServiceImpl[string];
|
||||
|
||||
/**
|
||||
* Arguments passed to Aqua function
|
||||
|
Reference in New Issue
Block a user