fix(core): update
This commit is contained in:
54
ts/index.ts
54
ts/index.ts
@ -1,4 +1,5 @@
|
||||
import * as plugins from './smartipc.plugins';
|
||||
import { EventEmitter } from 'events';
|
||||
|
||||
export interface ISmartIpcConstructorOptions {
|
||||
type: 'server' | 'client';
|
||||
@ -11,7 +12,7 @@ export interface ISmartIpcConstructorOptions {
|
||||
|
||||
export interface ISmartIpcHandlerPackage {
|
||||
keyword: string;
|
||||
handlerFunc: () => void;
|
||||
handlerFunc: (dataArg: string) => void;
|
||||
}
|
||||
|
||||
export class SmartIpc {
|
||||
@ -27,38 +28,77 @@ export class SmartIpc {
|
||||
* connect to the channel
|
||||
*/
|
||||
public async start() {
|
||||
const done = plugins.smartpromise.defer();
|
||||
let ipcEventEmitter;
|
||||
switch (this.options.type) {
|
||||
case 'server':
|
||||
this.ipc.config.id = this.options.ipcSpace;
|
||||
const done = plugins.smartpromise.defer();
|
||||
this.ipc.serve(() => {
|
||||
ipcEventEmitter = this.ipc.server;
|
||||
done.resolve();
|
||||
});
|
||||
this.ipc.server.start();
|
||||
await plugins.smartdelay.delayFor(1000);
|
||||
await done.promise;
|
||||
break;
|
||||
case 'client':
|
||||
this.ipc.connectTo(this.options.ipcSpace, () => {
|
||||
ipcEventEmitter = this.ipc.of[this.options.ipcSpace];
|
||||
done.resolve();
|
||||
});
|
||||
await done.promise;
|
||||
break;
|
||||
case 'client':
|
||||
this.ipc.connectTo(this.options.ipcSpace);
|
||||
default:
|
||||
throw new Error('type of ipc is not valid. Must be "server" or "client"');
|
||||
}
|
||||
|
||||
for (const handler of this.handlers) {
|
||||
ipcEventEmitter.on(handler.keyword, (dataArg) => {
|
||||
handler.handlerFunc(dataArg);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* should stop the server
|
||||
*/
|
||||
public async stop() {
|
||||
plugins.nodeIpc.server.stop();
|
||||
switch (this.options.type) {
|
||||
case 'server':
|
||||
this.ipc.server.stop();
|
||||
break;
|
||||
case 'client':
|
||||
break;
|
||||
}
|
||||
plugins.smartdelay.delayFor(2000).then(() => {
|
||||
process.exit(0);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* regsiters a handler
|
||||
*/
|
||||
registerHandler(handlerPackage: ISmartIpcHandlerPackage) {
|
||||
public registerHandler(handlerPackage: ISmartIpcHandlerPackage) {
|
||||
this.handlers.push(handlerPackage);
|
||||
}
|
||||
|
||||
sendMessage() {
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
switch (this.options.type) {
|
||||
case 'server':
|
||||
this.ipc.server.emit(messageIdentifierArg, payload);
|
||||
break;
|
||||
case 'client':
|
||||
this.ipc.of[this.options.ipcSpace].emit(messageIdentifierArg, payload);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,9 @@
|
||||
// pushrocks scope
|
||||
import * as smartdelay from '@pushrocks/smartdelay';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as smartrx from '@pushrocks/smartrx';
|
||||
|
||||
export { smartpromise, smartrx };
|
||||
export { smartdelay, smartpromise, smartrx };
|
||||
|
||||
// third party scope
|
||||
import * as nodeIpc from 'node-ipc';
|
||||
|
Reference in New Issue
Block a user