UNPKG

@microsoft/windows-admin-center-sdk

Version:

Microsoft - Windows Admin Center Shell

229 lines (228 loc) 7.9 kB
import { Observable } from 'rxjs'; import { NativeDeferred } from '../data/native-q'; import { EnvironmentModuleEntryPoint } from '../manifest/environment-modules'; import { CommandCallBackType, RpcDeactivateResult, RpcInboundCommands, RpcInitResult, RpcOpenResult, RpcOutboundCommands, RpcShutdownResult } from './rpc-base'; import { RpcManager } from './rpc-manager'; import { RpcSeekMode, RpcSeekResult } from './seek/rpc-seek-model'; /** * Deferred data object. */ export interface DeferredData<TData, TResult> { deferred: NativeDeferred<TResult>; data: TData; } /** * The Rpc class. */ export declare class Rpc { private static rpcTimeout; /** * This subject is updated whenever there's new reported data */ private rpcSubjects; /** * Deferred response collection. */ private deferredCollection; /** * Active status of rpc by Observable. */ private stateChangedInternal; /** * Active status of rpc. */ private stateActiveInternal; /** * Inbound module handlers to process when rpc is called. * - called from Module to Shell. */ private rpcInboundHandlers; /** * Outbound shell handlers to process when rpc is called. * - called from Shell to Module. * - if code reached a handler, module is not ready yet. * set timeout for RPC call. */ private rpcOutboundHandlers; /** * Rpc manager object. */ rpcManager: RpcManager; /** * Initializes a new instance of the Rpc class. * * @param http the Http class instance injected. */ constructor(); /** * Gets observable to watch the state change. */ get stateChanged(): Observable<boolean>; /** * Gets the state of rpc. */ get stateActive(): boolean; /** * Gets whether rpc is running on the shell. */ get isShell(): boolean; /** * Register inbound command handler. * * @param command The command name. * @param handler The command handler. */ registerInboundHandler(command: string, handler: CommandCallBackType): void; /** * Register outbound command handler. * * @param command The command name. * @param handler The command handler. */ registerOutboundHandler(command: string, handler: CommandCallBackType): void; /** * Initializes Rpc configuration */ init(): void; /*************************************************************** * Section for Shell usage. ***************************************************************/ /** * This updates its value every time there's a reported data from the rpc channel */ moduleSubjects<T>(commandType: RpcInboundCommands): Observable<DeferredData<T, any>>; /** * Connect a module with name and iframe. * - start pinging to iframe to wait for response. * * @param name the name of the module. * @param path the path to open the module the module name. * @param iframe the iframe window object. * @param primary the primary window to affect router url. * @return Promise<string> the promise with subName created for the window. */ moduleConnect(name: string, path: string, iframe: Window, primary: boolean): Promise<string>; /** * Init the module. * * @param name the name of module. * @param subName the sub name of rpc channel. * @param entryPointType the entry point type. * @return Promise<void> the promise object of init result. */ moduleInit(name: string, subName: string, entryPoint: EnvironmentModuleEntryPoint): Promise<RpcInitResult>; /** * Open the module by specifying the path and parameters. * * @param name the name of module. * @param subName the sub name of rpc channel. * @param path the open path. * @return Promise<RpcOpenResult> the promise object of RpcOpenResult. */ moduleOpen(name: string, subName: string, path: string): Promise<RpcOpenResult>; /** * Activate the module to start receiving data. * * @param name the module name. * @param subName the sub name of rpc channel. * @param primary the primary window to affect router url. * @param url the inner url to open. * @return Promise<void> the promise of activation result. */ moduleActivate(name: string, subName: string, primary: boolean, url: string): Promise<void>; /** * Deactivate 2 the module to stop receiving data. * * @param name the module name. * @param subName the sub name of rpc channel. * @return Promise<void> the promise of deactivation result. */ moduleDeactivate2(name: string, subName: string): Promise<RpcDeactivateResult>; /** * Request to shutdown the module. * * @param name the module name. * @param subName the sub name of rpc channel. * @param primary the primary window to affect router url. * @param force the forcible state. * @return Promise<RpcShutdownResult> the promise object of result. */ moduleShutdown(name: string, subName: string, primary: boolean, force: boolean): Promise<RpcShutdownResult>; /** * Remove the module from the rpc channel. * * @param name the module name. * @param subName the sub name of rpc channel. */ moduleRemove(name: string, subName: string): void; /** * Get module version string. * * @param name the name of module. * @param subName the sub name of rpc channel. * @return string the RPC version of module. */ moduleVersion(name: string, subName: string): string; /*************************************************************** * Section for Module usage. ***************************************************************/ /** * Register outbound handler. It accepts delay register in case of loading/initialization took a time for module. * * @param command the name of RPC Shell command. * @param handler the handler to handle Shell request. */ register(command: string, handler: (data: any) => Promise<any>): void; /** * Register outbound handler. It accepts delay register in case of loading/initialization took a time for module. * * @param command the enum ID of RPC Shell command. * @param handler the handler to handle Shell request. */ register(command: RpcOutboundCommands, handler: (data: any) => Promise<any>): void; /** * Module report a failure. */ failed(data: any): Promise<void>; /** * Seek shell frame. * * @param Promise<any> the promise object. */ seekShell(mode: RpcSeekMode): Promise<RpcSeekResult>; /** * Validate existing outbound connection and remove if it doesn't live anymore. * * @return number the count of removed outbound. */ validate(): number; /** * Change the active status of rpc. */ changeActiveState(state: boolean): void; /** * Create auto-failed timer promise. * * @param command the outbound command type. * @param timeoutMs the timeout milliseconds. * @param data the data context. * @return Promise<any> the promise. */ private createTimerPromise; /** * Create promise that does not timeout. * * @param command the outbound type. * @param data the data context. * @return Promise<any> the promise. */ private createPromise; /** * Process data pushing into next call of subject with deferred data type. * * @param command the inbound command type. * @param data the rpc data came from a module/iframe. * @return Promise the promise which receiver must settle within fixed waiting time (10 seconds) */ private processNextForSubject; }