{"version":3,"sources":["../../../packages/core/rpc/rpc-observable-server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,UAAU,EAAc,MAAM,MAAM,CAAC;AAErD,OAAO,EAAE,GAAG,EAAE,MAAM,OAAO,CAAC;AAG5B,OAAO,EAAwB,2BAA2B,EAAE,MAAM,0BAA0B,CAAC;AAC7F,OAAO,EAAuB,0BAA0B,EAAE,MAAM,yBAAyB,CAAC;AAI1F;;;;;;;GAOG;AACH,qBAAa,mBAAmB,CAC5B,QAAQ,SAAS,0BAA0B,EAC3C,OAAO,SAAS,2BAA2B,EAC3C,MAAM,SAAS,2BAA2B;IAY9B,SAAS,CAAC,GAAG,EAAE,GAAG;IAAE,SAAS,CAAC,OAAO,EAAE,MAAM;IAAE,SAAS,CAAC,OAAO,EAAE,MAAM;IAVpF,OAAO,CAAC,cAAc,CAAS;IAC/B,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,QAAQ,CAAiG;IAEjH;;;;;OAKG;gBACmB,GAAG,EAAE,GAAG,EAAY,OAAO,EAAE,MAAM,EAAY,OAAO,EAAE,MAAM;IAQpF;;OAEG;IACI,QAAQ,CAAC,KAAK,EAAE,CAAC,OAAO,EAAE,QAAQ,KAAK,UAAU,CAAC,OAAO,CAAC;IAIjE;;;;;;OAMG;IACH,OAAO,CAAC,OAAO;IAaf;;;;OAIG;IACH,OAAO,CAAC,OAAO;CA8BlB","file":"rpc-observable-server.d.ts","sourcesContent":["import { EMPTY, Observable, throwError } from 'rxjs';\r\nimport { catchError, switchMap } from 'rxjs/operators';\r\nimport { Rpc } from './rpc';\r\nimport { RpcType } from './rpc-base';\r\nimport { RpcInboundClient } from './rpc-inbound-client';\r\nimport { RpcObservableRequest, RpcObservableRequestContext } from './rpc-observable-request';\r\nimport { RpcObservableResult, RpcObservableResultContext } from './rpc-observable-result';\r\nimport { RpcOutbound } from './rpc-outbound';\r\nimport { RpcOutboundClient } from './rpc-outbound-client';\r\n\r\n/**\r\n * RPC Observable Server class.\r\n * Extends this class and populate rpcCall and callback properties.\r\n *\r\n * @template TRequest the request data object.\r\n * @template TResult the result data object.\r\n * @template TError the error object which must be serializable.\r\n */\r\nexport class RpcObservableServer<\r\n    TRequest extends RpcObservableResultContext,\r\n    TResult extends RpcObservableRequestContext,\r\n    TError extends RpcObservableRequestContext> {\r\n\r\n    private requestCommand: string;\r\n    private resultCommand: string;\r\n    private callback: (request: TRequest) => Observable<TResult> = () => throwError(() => new Error('not defined.'));\r\n\r\n    /**\r\n     * Initializes a new instance of the RpcObservableServer class.\r\n     *\r\n     * @param rpc the rpc object.\r\n     * @param command the command string.\r\n     */\r\n    constructor(protected rpc: Rpc, protected command: string, protected version: string) {\r\n        this.requestCommand = `${command}-Request`;\r\n        this.resultCommand = `${command}-Result`;\r\n        this.rpc.register(this.requestCommand, this.handler.bind(this));\r\n        this.rpc.registerInboundHandler(this.requestCommand, this.handler.bind(this));\r\n        this.rpc.registerOutboundHandler(this.requestCommand, this.handler.bind(this));\r\n    }\r\n\r\n    /**\r\n     * Register the call back observable.\r\n     */\r\n    public register(value: (request: TRequest) => Observable<TResult>) {\r\n        this.callback = value;\r\n    }\r\n\r\n    /**\r\n     * Making rpc call to shell or module.\r\n     *\r\n     * @param result the result packet data.\r\n     * @param outbound the outbound object from Shell to module request.\r\n     * @return Promise<void> the promise object.\r\n     */\r\n    private rpcCall(result: RpcObservableResult<TResult, TError>, outbound?: RpcOutbound): Promise<void> {\r\n        if (outbound) {\r\n            return RpcOutboundClient.callOutbound(\r\n                this.rpc.rpcManager.rpcChannel,\r\n                outbound,\r\n                this.resultCommand,\r\n                this.version,\r\n                result);\r\n        }\r\n\r\n        return RpcInboundClient.call(this.rpc, this.resultCommand, this.version, result);\r\n    }\r\n\r\n    /**\r\n     * Handle the request from inbound iframe window.\r\n     *\r\n     * @param observableRequest the observable request data.\r\n     */\r\n    private handler(observableRequest: RpcObservableRequest<TRequest>): Promise<void> {\r\n        const rpcOutbound = this.rpc.rpcManager.rpcChannel.getRpc<RpcOutbound>(\r\n            observableRequest.sourceName,\r\n            observableRequest.sourceSubName,\r\n            RpcType.Outbound,\r\n            true);\r\n        if (!rpcOutbound) {\r\n            const rpcInbound = this.rpc.rpcManager.rpcInbound;\r\n            if (observableRequest.sourceName !== rpcInbound.name || observableRequest.sourceSubName !== rpcInbound.subName) {\r\n                return Promise.reject(`Couldn't find routing back inbound/outbound windows: \"${this.requestCommand}\".`);\r\n            }\r\n        }\r\n\r\n        observableRequest.request.sourceName = observableRequest.sourceName;\r\n        observableRequest.request.sourceSubName = observableRequest.sourceSubName;\r\n        observableRequest.request.sourceVersion = observableRequest.sourceVersion;\r\n        this.callback(observableRequest.request)\r\n            .pipe(\r\n                catchError((error: TError) => {\r\n                    const observableResult: RpcObservableResult<TResult, TError> = { id: observableRequest.id, error };\r\n                    return this.rpcCall(observableResult, rpcOutbound).then(() => EMPTY, () => EMPTY);\r\n                }),\r\n                switchMap((result: TResult) => {\r\n                    const observableResult: RpcObservableResult<TResult, TError> = { id: observableRequest.id, result };\r\n                    return this.rpcCall(observableResult, rpcOutbound);\r\n                })\r\n            )\r\n            .subscribe();\r\n        return Promise.resolve();\r\n    }\r\n}\r\n"]}