mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-05-19 20:41:20 +00:00
Fix condition
This commit is contained in:
parent
2a230524ca
commit
357726a959
@ -17,114 +17,6 @@ import {
|
|||||||
FluencePeer as FluencePeer$$
|
FluencePeer as FluencePeer$$
|
||||||
} from '@fluencelabs/js-client';
|
} from '@fluencelabs/js-client';
|
||||||
|
|
||||||
/**
|
|
||||||
* @typedef {import("@fluencelabs/js-client").NonArrowSimpleType} NonArrowSimpleType
|
|
||||||
* @typedef {import("@fluencelabs/js-client").JSONValue} JSONValue
|
|
||||||
*/
|
|
||||||
/**
|
|
||||||
* Convert value from its representation in aqua language to representation in typescript
|
|
||||||
* @param {JSONValue} value - value as represented in aqua
|
|
||||||
* @param {NonArrowSimpleType} schema - definition of the aqua schema
|
|
||||||
* @returns {JSONValue} value represented in typescript
|
|
||||||
*/
|
|
||||||
export function aqua2ts(value, schema) {
|
|
||||||
if (schema.tag === "nil") {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
else if (schema.tag === "option") {
|
|
||||||
if (!Array.isArray(value)) {
|
|
||||||
throw new Error("Bad schema");
|
|
||||||
}
|
|
||||||
if (value.length === 0) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return aqua2ts(value[0], schema.type);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (schema.tag === "scalar" ||
|
|
||||||
schema.tag === "bottomType" ||
|
|
||||||
schema.tag === "topType") {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
else if (schema.tag === "array") {
|
|
||||||
if (!Array.isArray(value)) {
|
|
||||||
throw new Error("Bad schema");
|
|
||||||
}
|
|
||||||
return value.map((y) => {
|
|
||||||
return aqua2ts(y, schema.type);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else if (schema.tag === "unlabeledProduct") {
|
|
||||||
if (!Array.isArray(value)) {
|
|
||||||
throw new Error("Bad schema");
|
|
||||||
}
|
|
||||||
return value.map((y, i) => {
|
|
||||||
return aqua2ts(y, schema.items[i]);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else if (schema.tag === "struct" || schema.tag === "labeledProduct") {
|
|
||||||
if (typeof value !== "object" || value == null || Array.isArray(value)) {
|
|
||||||
throw new Error("Bad schema");
|
|
||||||
}
|
|
||||||
return Object.entries(schema.fields).reduce((agg, [key, type]) => {
|
|
||||||
const val = aqua2ts(value[key], type);
|
|
||||||
return { ...agg, [key]: val };
|
|
||||||
}, {});
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
throw new Error("Unexpected tag: " + JSON.stringify(schema));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Convert value from its typescript representation to representation in aqua
|
|
||||||
* @param value {JSONValue} the value as represented in typescript
|
|
||||||
* @param schema {NonArrowSimpleType} - definition of the aqua type
|
|
||||||
* @returns {JSONValue} represented in aqua
|
|
||||||
*/
|
|
||||||
export function ts2aqua(value, schema) {
|
|
||||||
if (schema.tag === "nil") {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
else if (schema.tag === "option") {
|
|
||||||
return value == null ? [] : [ts2aqua(value, schema.type)];
|
|
||||||
}
|
|
||||||
else if (schema.tag === "scalar" ||
|
|
||||||
schema.tag === "bottomType" ||
|
|
||||||
schema.tag === "topType") {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
else if (schema.tag === "array") {
|
|
||||||
if (!Array.isArray(value)) {
|
|
||||||
throw new Error("Bad schema");
|
|
||||||
}
|
|
||||||
return value.map((y) => {
|
|
||||||
return ts2aqua(y, schema.type);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else if (schema.tag === "unlabeledProduct") {
|
|
||||||
if (!Array.isArray(value)) {
|
|
||||||
throw new Error("Bad schema");
|
|
||||||
}
|
|
||||||
return value.map((y, i) => {
|
|
||||||
return ts2aqua(y, schema.items[i]);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else if (schema.tag === "struct" || schema.tag === "labeledProduct") {
|
|
||||||
if (typeof value !== "object" || value == null || Array.isArray(value)) {
|
|
||||||
throw new Error("Bad schema");
|
|
||||||
}
|
|
||||||
return Object.entries(schema.fields).reduce((agg, [key, type]) => {
|
|
||||||
const val = ts2aqua(value[key], type);
|
|
||||||
return { ...agg, [key]: val };
|
|
||||||
}, {});
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
throw new Error("Unexpected tag: " + JSON.stringify(schema));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Functions
|
// Functions
|
||||||
export const test_script = `
|
export const test_script = `
|
||||||
|
@ -17,114 +17,6 @@ import {
|
|||||||
FluencePeer as FluencePeer$$
|
FluencePeer as FluencePeer$$
|
||||||
} from '@fluencelabs/js-client';
|
} from '@fluencelabs/js-client';
|
||||||
|
|
||||||
/**
|
|
||||||
* @typedef {import("@fluencelabs/js-client").NonArrowSimpleType} NonArrowSimpleType
|
|
||||||
* @typedef {import("@fluencelabs/js-client").JSONValue} JSONValue
|
|
||||||
*/
|
|
||||||
/**
|
|
||||||
* Convert value from its representation in aqua language to representation in typescript
|
|
||||||
* @param {JSONValue} value - value as represented in aqua
|
|
||||||
* @param {NonArrowSimpleType} schema - definition of the aqua schema
|
|
||||||
* @returns {JSONValue} value represented in typescript
|
|
||||||
*/
|
|
||||||
export function aqua2ts(value, schema) {
|
|
||||||
if (schema.tag === "nil") {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
else if (schema.tag === "option") {
|
|
||||||
if (!Array.isArray(value)) {
|
|
||||||
throw new Error("Bad schema");
|
|
||||||
}
|
|
||||||
if (value.length === 0) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return aqua2ts(value[0], schema.type);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (schema.tag === "scalar" ||
|
|
||||||
schema.tag === "bottomType" ||
|
|
||||||
schema.tag === "topType") {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
else if (schema.tag === "array") {
|
|
||||||
if (!Array.isArray(value)) {
|
|
||||||
throw new Error("Bad schema");
|
|
||||||
}
|
|
||||||
return value.map((y) => {
|
|
||||||
return aqua2ts(y, schema.type);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else if (schema.tag === "unlabeledProduct") {
|
|
||||||
if (!Array.isArray(value)) {
|
|
||||||
throw new Error("Bad schema");
|
|
||||||
}
|
|
||||||
return value.map((y, i) => {
|
|
||||||
return aqua2ts(y, schema.items[i]);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else if (schema.tag === "struct" || schema.tag === "labeledProduct") {
|
|
||||||
if (typeof value !== "object" || value == null || Array.isArray(value)) {
|
|
||||||
throw new Error("Bad schema");
|
|
||||||
}
|
|
||||||
return Object.entries(schema.fields).reduce((agg, [key, type]) => {
|
|
||||||
const val = aqua2ts(value[key], type);
|
|
||||||
return { ...agg, [key]: val };
|
|
||||||
}, {});
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
throw new Error("Unexpected tag: " + JSON.stringify(schema));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Convert value from its typescript representation to representation in aqua
|
|
||||||
* @param value {JSONValue} the value as represented in typescript
|
|
||||||
* @param schema {NonArrowSimpleType} - definition of the aqua type
|
|
||||||
* @returns {JSONValue} represented in aqua
|
|
||||||
*/
|
|
||||||
export function ts2aqua(value, schema) {
|
|
||||||
if (schema.tag === "nil") {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
else if (schema.tag === "option") {
|
|
||||||
return value == null ? [] : [ts2aqua(value, schema.type)];
|
|
||||||
}
|
|
||||||
else if (schema.tag === "scalar" ||
|
|
||||||
schema.tag === "bottomType" ||
|
|
||||||
schema.tag === "topType") {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
else if (schema.tag === "array") {
|
|
||||||
if (!Array.isArray(value)) {
|
|
||||||
throw new Error("Bad schema");
|
|
||||||
}
|
|
||||||
return value.map((y) => {
|
|
||||||
return ts2aqua(y, schema.type);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else if (schema.tag === "unlabeledProduct") {
|
|
||||||
if (!Array.isArray(value)) {
|
|
||||||
throw new Error("Bad schema");
|
|
||||||
}
|
|
||||||
return value.map((y, i) => {
|
|
||||||
return ts2aqua(y, schema.items[i]);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else if (schema.tag === "struct" || schema.tag === "labeledProduct") {
|
|
||||||
if (typeof value !== "object" || value == null || Array.isArray(value)) {
|
|
||||||
throw new Error("Bad schema");
|
|
||||||
}
|
|
||||||
return Object.entries(schema.fields).reduce((agg, [key, type]) => {
|
|
||||||
const val = ts2aqua(value[key], type);
|
|
||||||
return { ...agg, [key]: val };
|
|
||||||
}, {});
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
throw new Error("Unexpected tag: " + JSON.stringify(schema));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Services
|
// Services
|
||||||
export interface SrvDef {
|
export interface SrvDef {
|
||||||
create: (wasm_b64_content: string, callParams: ParticleContext$$) => { error: string | null; service_id: string | null; success: boolean; } | Promise<{ error: string | null; service_id: string | null; success: boolean; }>;
|
create: (wasm_b64_content: string, callParams: ParticleContext$$) => { error: string | null; service_id: string | null; success: boolean; } | Promise<{ error: string | null; service_id: string | null; success: boolean; }>;
|
||||||
|
@ -91,7 +91,7 @@ export const callAquaFunction = async ({
|
|||||||
registerParticleScopeService(peer, particle, service);
|
registerParticleScopeService(peer, particle, service);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fireAndForget) {
|
if (!fireAndForget) {
|
||||||
registerParticleScopeService(peer, particle, responseService(resolve));
|
registerParticleScopeService(peer, particle, responseService(resolve));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user