smartipc/ts/index.ts

65 lines
1.3 KiB
TypeScript
Raw Normal View History

2019-04-05 18:30:43 +00:00
import * as plugins from './smartipc.plugins';
2019-04-08 17:42:23 +00:00
export interface ISmartIpcConstructorOptions {
type: 'server' | 'client';
/**
* the name of the message string
*/
ipcSpace: string;
}
export interface ISmartIpcHandlerPackage {
keyword: string;
handlerFunc: () => void;
}
export class SmartIpc {
2019-04-08 17:56:21 +00:00
public ipc = new plugins.nodeIpc.IPC();
2019-04-08 17:42:23 +00:00
public handlers: ISmartIpcHandlerPackage[] = [];
public options: ISmartIpcConstructorOptions;
constructor(optionsArg: ISmartIpcConstructorOptions) {
this.options = optionsArg;
}
/**
* connect to the channel
*/
public async start() {
switch (this.options.type) {
case 'server':
2019-04-08 17:56:21 +00:00
this.ipc.config.id = this.options.ipcSpace;
2019-04-08 17:42:23 +00:00
const done = plugins.smartpromise.defer();
2019-04-08 17:56:21 +00:00
this.ipc.serve(() => {
2019-04-08 17:42:23 +00:00
done.resolve();
});
await done.promise;
break;
case 'client':
2019-04-08 17:56:21 +00:00
this.ipc.connectTo(this.options.ipcSpace);
2019-04-08 17:42:23 +00:00
default:
throw new Error('type of ipc is not valid. Must be "server" or "client"');
}
}
/**
* should stop the server
*/
public async stop() {
plugins.nodeIpc.server.stop();
}
/**
* regsiters a handler
*/
2019-04-08 17:56:21 +00:00
registerHandler(handlerPackage: ISmartIpcHandlerPackage) {
2019-04-08 17:42:23 +00:00
this.handlers.push(handlerPackage);
}
2019-04-08 17:56:21 +00:00
sendMessage() {
switch (this.options.type) {
}
}
2019-04-08 17:42:23 +00:00
}