fix(ipc): sends requests and events only when connected

Fixes #1063
This commit is contained in:
Jonathan Rainville 2018-11-16 15:18:16 -05:00 committed by Iuri Matias
parent 89e3eb6156
commit cabfa939e7
1 changed files with 12 additions and 2 deletions

View File

@ -65,7 +65,10 @@ class IPC {
ipc.server.emit(client, 'message', message); ipc.server.emit(client, 'message', message);
} }
listenTo(action, callback) { listenTo(action, callback = () => {}) {
if (!this.connected) {
return callback();
}
ipc.of['embark'].on(action, (messageString) => { ipc.of['embark'].on(action, (messageString) => {
callback(parse(messageString)); callback(parse(messageString));
}); });
@ -75,7 +78,10 @@ class IPC {
ipc.server.broadcast(action, stringify(data)); ipc.server.broadcast(action, stringify(data));
} }
once(action, cb) { once(action, cb = () => {}) {
if (!this.connected) {
return cb();
}
ipc.of['embark'].once('message', function(messageString) { ipc.of['embark'].once('message', function(messageString) {
const message = parse(messageString); const message = parse(messageString);
if (message.action !== action) { if (message.action !== action) {
@ -86,6 +92,10 @@ class IPC {
} }
request(action, data, cb) { request(action, data, cb) {
if (!this.connected) {
cb = cb || (() => {});
return cb();
}
if (cb) { if (cb) {
this.once(action, cb); this.once(action, cb);
} }