mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-06-26 06:11:33 +00:00
extracting, re-arranging making sure interface don't get in the way
This commit is contained in:
@ -23,11 +23,70 @@ export type PeerIdB58 = string;
|
||||
|
||||
export type ParticleHandler = (particle: string) => void;
|
||||
|
||||
/**
|
||||
* Information about Fluence Peer connection.
|
||||
* Represented as object with the following keys:
|
||||
* - `isInitialized`: Is the peer initialized or not.
|
||||
* - `peerId`: Peer Id of the peer. Null if the peer is not initialized
|
||||
* - `isConnected`: Is the peer connected to network or not
|
||||
* - `relayPeerId`: Peer Id of the relay the peer is connected to. If the connection is direct relayPeerId is null
|
||||
* - `isDirect`: True if the peer is connected to the network directly (not through relay)
|
||||
*/
|
||||
export type PeerStatus =
|
||||
| {
|
||||
isInitialized: false;
|
||||
peerId: null;
|
||||
isConnected: false;
|
||||
relayPeerId: null;
|
||||
}
|
||||
| {
|
||||
isInitialized: true;
|
||||
peerId: PeerIdB58;
|
||||
isConnected: false;
|
||||
relayPeerId: null;
|
||||
}
|
||||
| {
|
||||
isInitialized: true;
|
||||
peerId: PeerIdB58;
|
||||
isConnected: true;
|
||||
relayPeerId: PeerIdB58;
|
||||
}
|
||||
| {
|
||||
isInitialized: true;
|
||||
peerId: PeerIdB58;
|
||||
isConnected: true;
|
||||
isDirect: true;
|
||||
relayPeerId: null;
|
||||
};
|
||||
|
||||
export interface IFluencePeer {
|
||||
start(config?: PeerConfig): Promise<void>;
|
||||
stop(): Promise<void>;
|
||||
getStatus(): PeerStatus;
|
||||
|
||||
// TODO: come up with a working interface for
|
||||
// - particle creation
|
||||
// - particle initialization
|
||||
// - service registration
|
||||
internals: any;
|
||||
}
|
||||
|
||||
export const asFluencePeer = (fluencePeerCandidate: unknown): IFluencePeer => {
|
||||
if (isFluencePeer(fluencePeerCandidate)) {
|
||||
return fluencePeerCandidate;
|
||||
}
|
||||
|
||||
throw new Error('');
|
||||
};
|
||||
|
||||
export const isFluencePeer = (fluencePeerCandidate: unknown): fluencePeerCandidate is IFluencePeer => {
|
||||
if (fluencePeerCandidate && (fluencePeerCandidate as any).__isFluenceAwesome) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
* Base class for connectivity layer to Fluence Network
|
||||
*/
|
||||
|
@ -16,7 +16,7 @@
|
||||
import 'buffer';
|
||||
|
||||
import { RelayConnection } from '../connection/index.js';
|
||||
import { FluenceConnection, IAvmRunner, IFluencePeer, IMarine } from '../interfaces/index.js';
|
||||
import { FluenceConnection, IAvmRunner, IFluencePeer, IMarine, PeerStatus } from '../interfaces/index.js';
|
||||
import { KeyPair } from '../keypair/index.js';
|
||||
import {
|
||||
CallServiceData,
|
||||
@ -42,42 +42,6 @@ import { registerNodeUtils } from './_aqua/node-utils.js';
|
||||
import { ConnectionOption, PeerConfig } from '../interfaces/peerConfig.js';
|
||||
import type { MultiaddrInput } from '@multiformats/multiaddr';
|
||||
|
||||
/**
|
||||
* Information about Fluence Peer connection.
|
||||
* Represented as object with the following keys:
|
||||
* - `isInitialized`: Is the peer initialized or not.
|
||||
* - `peerId`: Peer Id of the peer. Null if the peer is not initialized
|
||||
* - `isConnected`: Is the peer connected to network or not
|
||||
* - `relayPeerId`: Peer Id of the relay the peer is connected to. If the connection is direct relayPeerId is null
|
||||
* - `isDirect`: True if the peer is connected to the network directly (not through relay)
|
||||
*/
|
||||
export type PeerStatus =
|
||||
| {
|
||||
isInitialized: false;
|
||||
peerId: null;
|
||||
isConnected: false;
|
||||
relayPeerId: null;
|
||||
}
|
||||
| {
|
||||
isInitialized: true;
|
||||
peerId: PeerIdB58;
|
||||
isConnected: false;
|
||||
relayPeerId: null;
|
||||
}
|
||||
| {
|
||||
isInitialized: true;
|
||||
peerId: PeerIdB58;
|
||||
isConnected: true;
|
||||
relayPeerId: PeerIdB58;
|
||||
}
|
||||
| {
|
||||
isInitialized: true;
|
||||
peerId: PeerIdB58;
|
||||
isConnected: true;
|
||||
isDirect: true;
|
||||
relayPeerId: null;
|
||||
};
|
||||
|
||||
const DEFAULT_TTL = 7000;
|
||||
|
||||
export interface PeerConfig2 extends PeerConfig {
|
||||
@ -135,13 +99,12 @@ export class FluencePeer implements IFluencePeer {
|
||||
constructor(private marine: IMarine, private avmRunner: IAvmRunner) {}
|
||||
|
||||
/**
|
||||
* Checks whether the object is instance of FluencePeer class
|
||||
* @param obj - object to check if it is FluencePeer
|
||||
* @returns true if the object is FluencePeer false otherwise
|
||||
* Internal contract to cast unknown objects to IFluencePeer.
|
||||
* If an unknown object has this property then we assume it is in fact a Peer and it implements IFluencePeer
|
||||
* Check against this variable MUST NOT be coupled with any `FluencePeer` because otherwise it might get bundled
|
||||
* brining a lot of unnecessary stuff alongside with it
|
||||
*/
|
||||
static isInstance(obj: unknown): obj is FluencePeer {
|
||||
return obj instanceof FluencePeer;
|
||||
}
|
||||
__isFluenceAwesome = true;
|
||||
|
||||
/**
|
||||
* Get the peer's status
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { ArrowWithoutCallbacks, FnConfig, FunctionCallDef, NonArrowType } from './interface.js';
|
||||
import { FluencePeer } from '../FluencePeer.js';
|
||||
import { IFluencePeer } from '../../interfaces/index';
|
||||
|
||||
import {
|
||||
injectRelayService,
|
||||
@ -26,7 +26,7 @@ export function callFunctionImpl(
|
||||
def: FunctionCallDef,
|
||||
script: string,
|
||||
config: FnConfig,
|
||||
peer: FluencePeer,
|
||||
peer: IFluencePeer,
|
||||
args: { [key: string]: any },
|
||||
): Promise<unknown> {
|
||||
const argumentTypes = getArgumentTypes(def);
|
||||
@ -55,7 +55,7 @@ export function callFunctionImpl(
|
||||
|
||||
registerParticleScopeService(peer, particle, errorHandlingService(def, reject));
|
||||
|
||||
peer.internals.initiateParticle(particle, (stage) => {
|
||||
peer.internals.initiateParticle(particle, (stage: any) => {
|
||||
// If function is void, then it's completed when one of the two conditions is met:
|
||||
// 1. The particle is sent to the network (state 'sent')
|
||||
// 2. All CallRequests are executed, e.g., all variable loading and local function calls are completed (state 'localWorkDone')
|
||||
|
@ -1,13 +1,15 @@
|
||||
import type { FluencePeer } from '../FluencePeer.js';
|
||||
import type { IFluencePeer } from '../../interfaces/index';
|
||||
import { ServiceDef } from './interface.js';
|
||||
import { registerGlobalService, userHandlerService } from './services.js';
|
||||
|
||||
export const registerServiceImpl = (
|
||||
peer: FluencePeer,
|
||||
peer: IFluencePeer,
|
||||
def: ServiceDef,
|
||||
serviceId: string | undefined,
|
||||
service: any,
|
||||
) => {
|
||||
// TODO: TBH service registration is just putting some stuff into a hashmap
|
||||
// there should not be such a check at all
|
||||
if (!peer.getStatus().isInitialized) {
|
||||
throw new Error(
|
||||
'Could not register the service because the peer is not initialized. Are you passing the wrong peer to the register function?',
|
||||
|
@ -3,7 +3,7 @@ import { match } from 'ts-pattern';
|
||||
|
||||
import { Particle } from '../Particle.js';
|
||||
import { CallParams, CallServiceData, GenericCallServiceHandler, ResultCodes } from '../../interfaces/commonTypes.js';
|
||||
import { FluencePeer } from '../FluencePeer.js';
|
||||
import { IFluencePeer } from '../../interfaces/index';
|
||||
|
||||
import { aquaArgs2Ts, responseServiceValue2ts, returnType2Aqua, ts2aqua } from './conversions.js';
|
||||
import { ArrowWithoutCallbacks, FunctionCallConstants, FunctionCallDef, NonArrowType } from './interface.js';
|
||||
@ -17,7 +17,7 @@ export interface ServiceDescription {
|
||||
/**
|
||||
* Creates a service which injects relay's peer id into aqua space
|
||||
*/
|
||||
export const injectRelayService = (def: FunctionCallDef, peer: FluencePeer) => {
|
||||
export const injectRelayService = (def: FunctionCallDef, peer: IFluencePeer) => {
|
||||
return {
|
||||
serviceId: def.names.getDataSrv,
|
||||
fnName: def.names.relay,
|
||||
@ -162,10 +162,10 @@ const extractCallParams = (req: CallServiceData, arrow: ArrowWithoutCallbacks):
|
||||
return callParams;
|
||||
};
|
||||
|
||||
export const registerParticleScopeService = (peer: FluencePeer, particle: Particle, service: ServiceDescription) => {
|
||||
export const registerParticleScopeService = (peer: IFluencePeer, particle: Particle, service: ServiceDescription) => {
|
||||
peer.internals.regHandler.forParticle(particle.id, service.serviceId, service.fnName, service.handler);
|
||||
};
|
||||
|
||||
export const registerGlobalService = (peer: FluencePeer, service: ServiceDescription) => {
|
||||
export const registerGlobalService = (peer: IFluencePeer, service: ServiceDescription) => {
|
||||
peer.internals.regHandler.common(service.serviceId, service.fnName, service.handler);
|
||||
};
|
||||
|
Reference in New Issue
Block a user