fix(core): update

This commit is contained in:
Philipp Kunz 2019-04-09 12:30:12 +02:00
parent cea7ea469c
commit fecda5e668
5 changed files with 68 additions and 21 deletions

7
package-lock.json generated
View File

@ -185,13 +185,6 @@
"integrity": "sha512-TXKDDqsc7sBTLl+oiYNaF6IdNk1n70i8ur8QfwcUU6tegTnrEkvMWy9h5Zdty/fq1ioCNpKLvuXoA+fgYVwKGQ==", "integrity": "sha512-TXKDDqsc7sBTLl+oiYNaF6IdNk1n70i8ur8QfwcUU6tegTnrEkvMWy9h5Zdty/fq1ioCNpKLvuXoA+fgYVwKGQ==",
"requires": { "requires": {
"@pushrocks/smartpromise": "^3.0.2" "@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": { "@pushrocks/smartevent": {

View File

@ -22,6 +22,7 @@
"tslint-config-prettier": "^1.15.0" "tslint-config-prettier": "^1.15.0"
}, },
"dependencies": { "dependencies": {
"@pushrocks/smartdelay": "^2.0.3",
"@pushrocks/smartpromise": "^3.0.2", "@pushrocks/smartpromise": "^3.0.2",
"@pushrocks/smartrx": "^2.0.3", "@pushrocks/smartrx": "^2.0.3",
"@types/node-ipc": "^9.1.1", "@types/node-ipc": "^9.1.1",

View File

@ -4,24 +4,36 @@ import * as smartipc from '../ts/index';
import * as smartspawn from '@pushrocks/smartspawn'; import * as smartspawn from '@pushrocks/smartspawn';
import * as smartpromise from '@pushrocks/smartpromise'; 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 () => { tap.test('should instantiate a valid instance', async () => {
testIpc = new smartipc.SmartIpc({ serverIpc = new smartipc.SmartIpc({
ipcSpace: 'testSmartIpc', ipcSpace: 'testSmartIpc',
type: 'server' type: 'server'
}); });
testIpc.start(); serverIpc.registerHandler({
keyword: 'hi',
handlerFunc: data => {
console.log(data);
}
});
await serverIpc.start();
}); });
tap.test('should create a client', async tools => { tap.test('should create a client', async tools => {
const clientIpc = new smartipc.SmartIpc({ clientIpc = new smartipc.SmartIpc({
ipcSpace: 'testSmartIpc', ipcSpace: 'testSmartIpc',
type: 'client' 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(); tap.start();

View File

@ -1,4 +1,5 @@
import * as plugins from './smartipc.plugins'; import * as plugins from './smartipc.plugins';
import { EventEmitter } from 'events';
export interface ISmartIpcConstructorOptions { export interface ISmartIpcConstructorOptions {
type: 'server' | 'client'; type: 'server' | 'client';
@ -11,7 +12,7 @@ export interface ISmartIpcConstructorOptions {
export interface ISmartIpcHandlerPackage { export interface ISmartIpcHandlerPackage {
keyword: string; keyword: string;
handlerFunc: () => void; handlerFunc: (dataArg: string) => void;
} }
export class SmartIpc { export class SmartIpc {
@ -27,38 +28,77 @@ export class SmartIpc {
* connect to the channel * connect to the channel
*/ */
public async start() { public async start() {
const done = plugins.smartpromise.defer();
let ipcEventEmitter;
switch (this.options.type) { switch (this.options.type) {
case 'server': case 'server':
this.ipc.config.id = this.options.ipcSpace; this.ipc.config.id = this.options.ipcSpace;
const done = plugins.smartpromise.defer();
this.ipc.serve(() => { 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(); done.resolve();
}); });
await done.promise; await done.promise;
break; break;
case 'client':
this.ipc.connectTo(this.options.ipcSpace);
default: default:
throw new Error('type of ipc is not valid. Must be "server" or "client"'); 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 * should stop the server
*/ */
public async stop() { 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 * regsiters a handler
*/ */
registerHandler(handlerPackage: ISmartIpcHandlerPackage) { public registerHandler(handlerPackage: ISmartIpcHandlerPackage) {
this.handlers.push(handlerPackage); 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) { switch (this.options.type) {
case 'server':
this.ipc.server.emit(messageIdentifierArg, payload);
break;
case 'client':
this.ipc.of[this.options.ipcSpace].emit(messageIdentifierArg, payload);
} }
} }
} }

View File

@ -1,8 +1,9 @@
// pushrocks scope // pushrocks scope
import * as smartdelay from '@pushrocks/smartdelay';
import * as smartpromise from '@pushrocks/smartpromise'; import * as smartpromise from '@pushrocks/smartpromise';
import * as smartrx from '@pushrocks/smartrx'; import * as smartrx from '@pushrocks/smartrx';
export { smartpromise, smartrx }; export { smartdelay, smartpromise, smartrx };
// third party scope // third party scope
import * as nodeIpc from 'node-ipc'; import * as nodeIpc from 'node-ipc';