feat: Cleaning up technical debts (#295)

This commit is contained in:
Pavel
2023-04-03 21:52:40 +04:00
committed by GitHub
parent 00b62f1459
commit 0b2f12d8ac
94 changed files with 3459 additions and 2943 deletions

View File

@ -29,13 +29,12 @@ import { getFluenceInterface } from '../util.js';
* @param def - function definition generated by the Aqua compiler
* @param script - air script with function execution logic generated by the Aqua compiler
*/
export const callFunction = async (rawFnArgs: Array<any>, def: FunctionCallDef, script: string): Promise<unknown> => {
export const v5_callFunction = async (
rawFnArgs: Array<any>,
def: FunctionCallDef,
script: string,
): Promise<unknown> => {
const { args, client: peer, config } = await extractFunctionArgs(rawFnArgs, def);
if (peer.internals.getConnectionState() !== 'connected') {
throw new Error(
'Could not call the Aqua function because client is disconnected. Did you forget to call Fluence.connect()?',
);
}
const fluence = await getFluenceInterface();
return fluence.callAquaFunction({
@ -53,17 +52,9 @@ export const callFunction = async (rawFnArgs: Array<any>, def: FunctionCallDef,
* @param args - raw arguments passed by user to the generated function
* @param def - service definition generated by the Aqua compiler
*/
export const registerService = async (args: any[], def: ServiceDef): Promise<unknown> => {
export const v5_registerService = async (args: any[], def: ServiceDef): Promise<unknown> => {
const { peer, service, serviceId } = await extractServiceArgs(args, def.defaultServiceId);
// TODO: TBH service registration is just putting some stuff into a hashmap
// there should not be such a check at all
if (peer.internals.getConnectionState() !== 'connected') {
throw new Error(
'Could not register Aqua service because the client is disconnected. Did you forget to call Fluence.connect()?',
);
}
const fluence = await getFluenceInterface();
return fluence.registerService({
def,
@ -104,6 +95,11 @@ const extractFunctionArgs = async (
config = args[numberOfExpectedArgs + 1];
} else {
const fluence = await getFluenceInterface();
if (!fluence.defaultClient) {
throw new Error(
'Could not register Aqua service because the client is not initialized. Did you forget to call Fluence.connect()?',
);
}
peer = fluence.defaultClient;
structuredArgs = args.slice(0, numberOfExpectedArgs);
config = args[numberOfExpectedArgs];
@ -145,6 +141,11 @@ const extractServiceArgs = async (
peer = args[0];
} else {
const fluence = await getFluenceInterface();
if (!fluence.defaultClient) {
throw new Error(
'Could not register Aqua service because the client is not initialized. Did you forget to call Fluence.connect()?',
);
}
peer = fluence.defaultClient;
}

View File

@ -1,12 +1,28 @@
/*
* Copyright 2023 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { getFluenceInterface, getFluenceInterfaceFromGlobalThis } from './util.js';
import {
IFluenceClient,
ClientOptions,
ClientConfig,
RelayOptions,
ConnectionState,
ConnectionStates,
CallAquaFunctionType,
RegisterServiceType,
} from '@fluencelabs/interfaces';
export type { IFluenceClient, ClientOptions, CallParams } from '@fluencelabs/interfaces';
export type { IFluenceClient, ClientConfig, CallParams } from '@fluencelabs/interfaces';
export {
ArrayType,
@ -14,7 +30,6 @@ export {
ArrowWithCallbacks,
ArrowWithoutCallbacks,
BottomType,
FnConfig,
FunctionCallConstants,
FunctionCallDef,
LabeledProductType,
@ -28,12 +43,15 @@ export {
StructType,
TopType,
UnlabeledProductType,
CallAquaFunctionType,
CallAquaFunctionArgs,
PassedArgs,
FnConfig,
RegisterServiceType,
RegisterServiceArgs,
} from '@fluencelabs/interfaces';
export {
callFunction as v5_callFunction,
registerService as v5_registerService,
} from './compilerSupport/implementation.js';
export { v5_callFunction, v5_registerService } from './compilerSupport/implementation.js';
/**
* Public interface to Fluence Network
@ -42,11 +60,12 @@ export const Fluence = {
/**
* Connect to the Fluence network
* @param relay - relay node to connect to
* @param options - client options
* @param config - client configuration
*/
connect: async (relay: RelayOptions, options?: ClientOptions): Promise<void> => {
connect: async (relay: RelayOptions, config?: ClientConfig): Promise<void> => {
const fluence = await getFluenceInterface();
return fluence.defaultClient.connect(relay, options);
const client = await fluence.clientFactory(relay, config);
fluence.defaultClient = client;
},
/**
@ -54,7 +73,8 @@ export const Fluence = {
*/
disconnect: async (): Promise<void> => {
const fluence = await getFluenceInterface();
return fluence.defaultClient.disconnect();
await fluence.defaultClient?.disconnect();
fluence.defaultClient = undefined;
},
/**
@ -62,11 +82,13 @@ export const Fluence = {
*/
onConnectionStateChange(handler: (state: ConnectionState) => void): ConnectionState {
const optimisticResult = getFluenceInterfaceFromGlobalThis();
if (optimisticResult) {
if (optimisticResult && optimisticResult.defaultClient) {
return optimisticResult.defaultClient.onConnectionStateChange(handler);
}
getFluenceInterface().then((fluence) => fluence.defaultClient.onConnectionStateChange(handler));
getFluenceInterface().then((fluence) => {
fluence.defaultClient?.onConnectionStateChange(handler);
});
return 'disconnected';
},
@ -77,6 +99,9 @@ export const Fluence = {
*/
getClient: async (): Promise<IFluenceClient> => {
const fluence = await getFluenceInterface();
if (!fluence.defaultClient) {
throw new Error('Fluence client is not initialized. Call Fluence.connect() first');
}
return fluence.defaultClient;
},
};
@ -85,7 +110,23 @@ export const Fluence = {
* Low level API. Generally you need Fluence.connect() instead.
* @returns IFluenceClient instance
*/
export const createClient = async (): Promise<IFluenceClient> => {
export const createClient = async (relay: RelayOptions, config?: ClientConfig): Promise<IFluenceClient> => {
const fluence = await getFluenceInterface();
return fluence.clientFactory();
return await fluence.clientFactory(relay, config);
};
/**
* Low level API. Generally you should use code generated by the Aqua compiler.
*/
export const callAquaFunction: CallAquaFunctionType = async (args) => {
const fluence = await getFluenceInterface();
return await fluence.callAquaFunction(args);
};
/**
* Low level API. Generally you should use code generated by the Aqua compiler.
*/
export const registerService: RegisterServiceType = async (args) => {
const fluence = await getFluenceInterface();
return await fluence.registerService(args);
};

View File

@ -1,10 +1,31 @@
import type { CallAquaFunction, IFluenceClient, RegisterService } from '@fluencelabs/interfaces';
/*
* Copyright 2023 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type {
CallAquaFunctionType,
ClientConfig,
IFluenceClient,
RegisterServiceType,
RelayOptions,
} from '@fluencelabs/interfaces';
type PublicFluenceInterface = {
clientFactory: () => IFluenceClient;
defaultClient: IFluenceClient;
callAquaFunction: CallAquaFunction;
registerService: RegisterService;
defaultClient: IFluenceClient | undefined;
clientFactory: (relay: RelayOptions, config?: ClientConfig) => Promise<IFluenceClient>;
callAquaFunction: CallAquaFunctionType;
registerService: RegisterServiceType;
};
export const getFluenceInterfaceFromGlobalThis = (): PublicFluenceInterface | undefined => {