2023-02-16 14:38:48 +03:00
|
|
|
import { getFluenceInterface, getFluenceInterfaceFromGlobalThis } from './util.js';
|
|
|
|
import {
|
|
|
|
IFluenceClient,
|
|
|
|
ClientOptions,
|
|
|
|
RelayOptions,
|
|
|
|
ConnectionState,
|
|
|
|
ConnectionStates,
|
|
|
|
} from '@fluencelabs/interfaces';
|
2023-02-15 03:00:42 +03:00
|
|
|
export type { IFluenceClient, ClientOptions, CallParams } from '@fluencelabs/interfaces';
|
|
|
|
|
|
|
|
export {
|
|
|
|
ArrayType,
|
|
|
|
ArrowType,
|
|
|
|
ArrowWithCallbacks,
|
|
|
|
ArrowWithoutCallbacks,
|
|
|
|
BottomType,
|
|
|
|
FnConfig,
|
|
|
|
FunctionCallConstants,
|
|
|
|
FunctionCallDef,
|
|
|
|
LabeledProductType,
|
|
|
|
NilType,
|
|
|
|
NonArrowType,
|
|
|
|
OptionType,
|
|
|
|
ProductType,
|
|
|
|
ScalarNames,
|
|
|
|
ScalarType,
|
|
|
|
ServiceDef,
|
|
|
|
StructType,
|
|
|
|
TopType,
|
|
|
|
UnlabeledProductType,
|
|
|
|
} from '@fluencelabs/interfaces';
|
|
|
|
|
|
|
|
export {
|
|
|
|
callFunction as v5_callFunction,
|
|
|
|
registerService as v5_registerService,
|
|
|
|
} from './compilerSupport/implementation.js';
|
2023-02-13 17:41:35 +03:00
|
|
|
|
|
|
|
/**
|
2023-02-16 14:38:48 +03:00
|
|
|
* Public interface to Fluence Network
|
2023-02-13 17:41:35 +03:00
|
|
|
*/
|
|
|
|
export const Fluence = {
|
|
|
|
/**
|
2023-02-16 14:38:48 +03:00
|
|
|
* Connect to the Fluence network
|
|
|
|
* @param relay - relay node to connect to
|
|
|
|
* @param options - client options
|
2023-02-13 17:41:35 +03:00
|
|
|
*/
|
2023-02-16 14:38:48 +03:00
|
|
|
connect: async (relay: RelayOptions, options?: ClientOptions): Promise<void> => {
|
|
|
|
const fluence = await getFluenceInterface();
|
|
|
|
return fluence.defaultClient.connect(relay, options);
|
2023-02-13 17:41:35 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2023-02-16 14:38:48 +03:00
|
|
|
* Disconnect from the Fluence network
|
2023-02-13 17:41:35 +03:00
|
|
|
*/
|
2023-02-16 14:38:48 +03:00
|
|
|
disconnect: async (): Promise<void> => {
|
|
|
|
const fluence = await getFluenceInterface();
|
|
|
|
return fluence.defaultClient.disconnect();
|
2023-02-13 17:41:35 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2023-02-16 14:38:48 +03:00
|
|
|
* Handle connection state changes. Immediately returns the current connection state
|
2023-02-13 17:41:35 +03:00
|
|
|
*/
|
2023-02-16 14:38:48 +03:00
|
|
|
onConnectionStateChange(handler: (state: ConnectionState) => void): ConnectionState {
|
|
|
|
const optimisticResult = getFluenceInterfaceFromGlobalThis();
|
|
|
|
if (optimisticResult) {
|
|
|
|
return optimisticResult.defaultClient.onConnectionStateChange(handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
getFluenceInterface().then((fluence) => fluence.defaultClient.onConnectionStateChange(handler));
|
|
|
|
|
|
|
|
return 'disconnected';
|
2023-02-13 17:41:35 +03:00
|
|
|
},
|
2023-02-16 14:38:48 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Low level API. Get the underlying client instance which holds the connection to the network
|
|
|
|
* @returns IFluenceClient instance
|
|
|
|
*/
|
|
|
|
getClient: async (): Promise<IFluenceClient> => {
|
|
|
|
const fluence = await getFluenceInterface();
|
|
|
|
return fluence.defaultClient;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Low level API. Generally you need Fluence.connect() instead.
|
|
|
|
* @returns IFluenceClient instance
|
|
|
|
*/
|
|
|
|
export const createClient = async (): Promise<IFluenceClient> => {
|
|
|
|
const fluence = await getFluenceInterface();
|
|
|
|
return fluence.clientFactory();
|
2023-02-13 17:41:35 +03:00
|
|
|
};
|