@microsoft/windows-admin-center-sdk
Version:
Microsoft - Windows Admin Center Shell
86 lines (84 loc) • 3.49 kB
JavaScript
import { EMPTY, throwError } from 'rxjs';
import { catchError, switchMap } from 'rxjs/operators';
import { RpcType } from './rpc-base';
import { RpcInboundClient } from './rpc-inbound-client';
import { RpcOutboundClient } from './rpc-outbound-client';
/**
* RPC Observable Server class.
* Extends this class and populate rpcCall and callback properties.
*
* @template TRequest the request data object.
* @template TResult the result data object.
* @template TError the error object which must be serializable.
*/
export class RpcObservableServer {
rpc;
command;
version;
requestCommand;
resultCommand;
callback = () => throwError(() => new Error('not defined.'));
/**
* Initializes a new instance of the RpcObservableServer class.
*
* @param rpc the rpc object.
* @param command the command string.
*/
constructor(rpc, command, version) {
this.rpc = rpc;
this.command = command;
this.version = version;
this.requestCommand = `${command}-Request`;
this.resultCommand = `${command}-Result`;
this.rpc.register(this.requestCommand, this.handler.bind(this));
this.rpc.registerInboundHandler(this.requestCommand, this.handler.bind(this));
this.rpc.registerOutboundHandler(this.requestCommand, this.handler.bind(this));
}
/**
* Register the call back observable.
*/
register(value) {
this.callback = value;
}
/**
* Making rpc call to shell or module.
*
* @param result the result packet data.
* @param outbound the outbound object from Shell to module request.
* @return Promise<void> the promise object.
*/
rpcCall(result, outbound) {
if (outbound) {
return RpcOutboundClient.callOutbound(this.rpc.rpcManager.rpcChannel, outbound, this.resultCommand, this.version, result);
}
return RpcInboundClient.call(this.rpc, this.resultCommand, this.version, result);
}
/**
* Handle the request from inbound iframe window.
*
* @param observableRequest the observable request data.
*/
handler(observableRequest) {
const rpcOutbound = this.rpc.rpcManager.rpcChannel.getRpc(observableRequest.sourceName, observableRequest.sourceSubName, RpcType.Outbound, true);
if (!rpcOutbound) {
const rpcInbound = this.rpc.rpcManager.rpcInbound;
if (observableRequest.sourceName !== rpcInbound.name || observableRequest.sourceSubName !== rpcInbound.subName) {
return Promise.reject(`Couldn't find routing back inbound/outbound windows: "${this.requestCommand}".`);
}
}
observableRequest.request.sourceName = observableRequest.sourceName;
observableRequest.request.sourceSubName = observableRequest.sourceSubName;
observableRequest.request.sourceVersion = observableRequest.sourceVersion;
this.callback(observableRequest.request)
.pipe(catchError((error) => {
const observableResult = { id: observableRequest.id, error };
return this.rpcCall(observableResult, rpcOutbound).then(() => EMPTY, () => EMPTY);
}), switchMap((result) => {
const observableResult = { id: observableRequest.id, result };
return this.rpcCall(observableResult, rpcOutbound);
}))
.subscribe();
return Promise.resolve();
}
}
//# sourceMappingURL=rpc-observable-server.js.map