smartipc/ts/index.ts

105 lines
2.5 KiB
TypeScript
Raw Normal View History

2019-04-05 18:30:43 +00:00
import * as plugins from './smartipc.plugins';
2019-04-09 10:30:12 +00:00
import { EventEmitter } from 'events';
2019-04-05 18:30:43 +00:00
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;
2019-04-09 10:30:12 +00:00
handlerFunc: (dataArg: string) => void;
2019-04-08 17:42:23 +00:00
}
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() {
2019-04-09 10:30:12 +00:00
const done = plugins.smartpromise.defer();
let ipcEventEmitter;
2019-04-08 17:42:23 +00:00
switch (this.options.type) {
case 'server':
2019-04-08 17:56:21 +00:00
this.ipc.config.id = this.options.ipcSpace;
this.ipc.serve(() => {
2019-04-09 10:30:12 +00:00
ipcEventEmitter = this.ipc.server;
2019-04-08 17:42:23 +00:00
done.resolve();
});
2019-04-09 10:30:12 +00:00
this.ipc.server.start();
await plugins.smartdelay.delayFor(1000);
2019-04-08 17:42:23 +00:00
await done.promise;
break;
case 'client':
2019-04-09 10:30:12 +00:00
this.ipc.connectTo(this.options.ipcSpace, () => {
ipcEventEmitter = this.ipc.of[this.options.ipcSpace];
done.resolve();
});
await done.promise;
break;
2019-04-08 17:42:23 +00:00
default:
throw new Error('type of ipc is not valid. Must be "server" or "client"');
}
2019-04-09 10:30:12 +00:00
for (const handler of this.handlers) {
2019-04-09 10:34:15 +00:00
ipcEventEmitter.on(handler.keyword, dataArg => {
2019-04-09 10:30:12 +00:00
handler.handlerFunc(dataArg);
});
}
2019-04-08 17:42:23 +00:00
}
/**
* should stop the server
*/
public async stop() {
2019-04-09 10:30:12 +00:00
switch (this.options.type) {
case 'server':
2019-04-09 10:34:15 +00:00
this.ipc.server.stop();
break;
2019-04-09 10:30:12 +00:00
case 'client':
2019-04-09 10:34:15 +00:00
break;
2019-04-09 10:30:12 +00:00
}
plugins.smartdelay.delayFor(2000).then(() => {
process.exit(0);
});
2019-04-08 17:42:23 +00:00
}
/**
* regsiters a handler
*/
2019-04-09 10:30:12 +00:00
public registerHandler(handlerPackage: ISmartIpcHandlerPackage) {
2019-04-08 17:42:23 +00:00
this.handlers.push(handlerPackage);
}
2019-04-08 17:56:21 +00:00
2019-04-09 10:30:12 +00:00
/**
* sends a message
* @param payloadArg
*/
public sendMessage(messageIdentifierArg: string, payloadArg: string | any) {
let payload: string = null;
if (typeof payloadArg === 'string') {
payload = payloadArg;
} else {
payload = JSON.stringify(payloadArg);
}
2019-04-08 17:56:21 +00:00
switch (this.options.type) {
2019-04-09 10:30:12 +00:00
case 'server':
this.ipc.server.emit(messageIdentifierArg, payload);
break;
case 'client':
this.ipc.of[this.options.ipcSpace].emit(messageIdentifierArg, payload);
2019-04-08 17:56:21 +00:00
}
}
2019-04-08 17:42:23 +00:00
}