@microsoft/windows-admin-center-sdk
Version:
Microsoft - Windows Admin Center Shell
71 lines (69 loc) • 2.41 kB
JavaScript
import { from, Subject } from 'rxjs';
import { filter, map, mergeMap, take } from 'rxjs/operators';
import { LogLevel } from '../diagnostics/log-level';
import { Logging } from '../diagnostics/logging';
import { RpcElectronResponseKey } from '../rpc/electron/rpc-electron-model';
import { RpcElectronRequestClient } from '../rpc/electron/rpc-electron-request-client';
/**
* Electron Module Side Service.
*/
export class Electron {
rpc;
/**
* Subject for tracking RPC responses from the electron host
*/
watcher;
/**
* Initializes a new instance of the Electron Manager class
* @param rpc The rpc to forward auth requests to a parent window
*/
constructor(rpc) {
this.rpc = rpc;
this.watcher = new Subject();
}
/**
* Send Electron Host Request
* @param eventName The electron host event
* @param payload The the event payload
* @returns An observable for te response message from the electron host
*/
request(eventName, payload) {
const request = {
eventName: eventName,
requestId: MsftSme.newGuid(),
payload: payload
};
Logging.log({
level: LogLevel.Debug,
message: 'Sending request to Electron Shell Service. Request:{0}',
source: 'Electron',
params: request
});
return from(RpcElectronRequestClient.electronRequest(this.rpc, request))
.pipe(mergeMap(() => this.watcher), filter((result) => result.requestId === request.requestId), take(1), map((result) => {
if (result.response) {
return result.response;
}
else if (result.error) {
throw result.error;
}
throw new Error('Unexpected Response from Electron RPC Request');
}));
}
/**
* Initializes the electron rpc response listener
* This is a module side only class. It should not be called from the shell
*/
initialize() {
this.rpc.register(RpcElectronResponseKey.command, this.onRpcResponse.bind(this));
}
/**
* Handles rpc response messages from the electron host.
* @param data the result of the electron host request
*/
onRpcResponse(data) {
this.watcher.next(data);
return Promise.resolve();
}
}
//# sourceMappingURL=electron.js.map