reactant-share
Version:
A framework for building shared web applications with Reactant
94 lines • 3.58 kB
TypeScript
import { Service } from 'reactant';
import { LastAction } from 'reactant-last-action';
import { loadFullStateActionName } from '../constants';
import type { CallbackWithHook, ClientEvents, Port, PortApp, Transports, Transport, ISharedAppOptions, ProxyExecParams } from '../interfaces';
type OnClientDestroy = (clientId: string) => unknown;
/**
* Port Detector
*
* It provides port detection and client/server port switching functions.
*/
export declare class PortDetector {
sharedAppOptions: ISharedAppOptions;
lastAction: LastAction;
protected portApp?: PortApp;
protected lastHooks?: Set<ReturnType<CallbackWithHook>>;
protected serverCallbacks: Set<CallbackWithHook<any>>;
protected clientCallbacks: Set<CallbackWithHook<any>>;
protected clientDestroyCallbacks: Set<OnClientDestroy>;
syncFullStatePromise?: ReturnType<ClientEvents[typeof loadFullStateActionName]>;
/**
* previous port
*/
previousPort?: Port;
/**
* client id, it will be generated when the port is client, it is null in server port.
*/
clientId: string | null;
/**
* allow Disable Sync
*/
allowDisableSync: () => boolean;
/**
* client ids, it will collect all the client ids when the port is server, it is an empty array in client port.
*/
clientIds: string[];
/**
* server hooks for delegate(this, key, args, { _extra: { serverHook: '$hookName' } }) method
*/
serverHooks: Record<string, (options: ProxyExecParams) => any>;
constructor(sharedAppOptions: ISharedAppOptions, lastAction: LastAction);
isolatedModules: Service[];
/**
* all isolated instances state will not be sync to other clients or server.
*/
disableShare(instance: object): void;
protected lastIsolatedInstances?: Service[];
protected lastIsolatedInstanceKeys?: (string | undefined)[];
get isolatedInstanceKeys(): (string | undefined)[];
hasIsolatedState(key: string): boolean;
get id(): string;
get shared(): boolean;
get name(): string;
get disableSyncClient(): boolean;
protected detectPort(port: Port): any;
/**
* onServer
*
* When the port is server, this hook will execute.
* And allow to return a function that will be executed when the current port is switched to client.
*/
onServer: (callback: CallbackWithHook<Required<Transports>["server"]>) => () => void;
/**
* onClient
*
* When the port is client, this hook will execute.
* And allow to return a function that will be executed when the current port is switched to server.
*/
onClient: (callback: CallbackWithHook<Required<Transports>["client"]>) => () => void;
/**
* emit client destroy event with clientId
*/
onClientDestroy: (callback: OnClientDestroy) => () => void;
get isWorkerMode(): boolean;
get isServerWorker(): boolean;
get isServer(): boolean;
get isClient(): boolean;
get transports(): Transports;
transport?: Transport;
setPort(currentPortApp: PortApp, transport: Required<Transports>[keyof Transports]): void;
syncToClients(): void;
syncFullState({ forceSync }?: {
forceSync?: boolean | undefined;
}): Promise<void>;
/**
* ignore router state and isolated state sync for last action
*/
protected getNextState(fullState: Record<string, any>): Record<string, any>;
/**
* transform port with new transport
*/
transform(port: Port, transport?: Transport): void;
}
export {};
//# sourceMappingURL=portDetector.d.ts.map