fix(core): update
This commit is contained in:
parent
cea7ea469c
commit
fecda5e668
7
package-lock.json
generated
7
package-lock.json
generated
@ -185,13 +185,6 @@
|
||||
"integrity": "sha512-TXKDDqsc7sBTLl+oiYNaF6IdNk1n70i8ur8QfwcUU6tegTnrEkvMWy9h5Zdty/fq1ioCNpKLvuXoA+fgYVwKGQ==",
|
||||
"requires": {
|
||||
"@pushrocks/smartpromise": "^3.0.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"@pushrocks/smartpromise": {
|
||||
"version": "3.0.2",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartpromise/-/smartpromise-3.0.2.tgz",
|
||||
"integrity": "sha512-jmrJMUEmBCWChWK8CIcx4Vw3wv/8OgVNmkaxJrbs+WMaoRUfJtpWWJfrAwwHWt9ZXJbarJ+CwfwfYiiZXymndQ=="
|
||||
}
|
||||
}
|
||||
},
|
||||
"@pushrocks/smartevent": {
|
||||
|
@ -22,6 +22,7 @@
|
||||
"tslint-config-prettier": "^1.15.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@pushrocks/smartdelay": "^2.0.3",
|
||||
"@pushrocks/smartpromise": "^3.0.2",
|
||||
"@pushrocks/smartrx": "^2.0.3",
|
||||
"@types/node-ipc": "^9.1.1",
|
||||
|
24
test/test.ts
24
test/test.ts
@ -4,24 +4,36 @@ import * as smartipc from '../ts/index';
|
||||
import * as smartspawn from '@pushrocks/smartspawn';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
|
||||
let testIpc: smartipc.SmartIpc;
|
||||
let serverIpc: smartipc.SmartIpc;
|
||||
let clientIpc: smartipc.SmartIpc;
|
||||
|
||||
tap.test('should instantiate a valid instance', async () => {
|
||||
testIpc = new smartipc.SmartIpc({
|
||||
serverIpc = new smartipc.SmartIpc({
|
||||
ipcSpace: 'testSmartIpc',
|
||||
type: 'server'
|
||||
});
|
||||
testIpc.start();
|
||||
serverIpc.registerHandler({
|
||||
keyword: 'hi',
|
||||
handlerFunc: data => {
|
||||
console.log(data);
|
||||
}
|
||||
});
|
||||
await serverIpc.start();
|
||||
});
|
||||
|
||||
tap.test('should create a client', async tools => {
|
||||
const clientIpc = new smartipc.SmartIpc({
|
||||
clientIpc = new smartipc.SmartIpc({
|
||||
ipcSpace: 'testSmartIpc',
|
||||
type: 'client'
|
||||
});
|
||||
clientIpc.sendMessage();
|
||||
await clientIpc.start();
|
||||
clientIpc.sendMessage('hi', { awesome: 'yes' });
|
||||
});
|
||||
|
||||
tap.test('should terminate the smartipc process', async () => {});
|
||||
tap.test('should terminate the smartipc process', async (tools) => {
|
||||
await tools.delayFor(1000);
|
||||
await clientIpc.stop();
|
||||
await serverIpc.stop();
|
||||
});
|
||||
|
||||
tap.start();
|
||||
|
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';
|
||||
|
Loading…
Reference in New Issue
Block a user