Skip to content

Commit

Permalink
fix: support send without to argument (#3472)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored and fengmk2 committed Feb 18, 2019
1 parent cf6ad1d commit eac4941
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 17 deletions.
36 changes: 19 additions & 17 deletions lib/core/messenger/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,29 +88,31 @@ class Messenger extends EventEmitter {
* @return {Messenger} this
*/
send(action, data, to) {
const { egg } = this;
let application;
let agent;
let opposite;
if (egg.type === 'application') {
application = egg;
agent = egg.agent;
opposite = agent;
} else {
agent = egg;
application = egg.application;
opposite = application;
}

// use nextTick to keep it async as IPC messenger
process.nextTick(() => {
if (application.messenger && (to === 'application' || to === 'both')) {
const { egg } = this;
let application;
let agent;
let opposite;

if (egg.type === 'application') {
application = egg;
agent = egg.agent;
opposite = agent;
} else {
agent = egg;
application = egg.application;
opposite = application;
}
if (!to) to = egg.type === 'application' ? 'agent' : 'application';

if (application && application.messenger && (to === 'application' || to === 'both')) {
application.messenger._onMessage({ action, data });
}
if (agent.messenger && (to === 'agent' || to === 'both')) {
if (agent && agent.messenger && (to === 'agent' || to === 'both')) {
agent.messenger._onMessage({ action, data });
}
if (opposite.messenger && to === 'opposite') {
if (opposite && opposite.messenger && to === 'opposite') {
opposite.messenger._onMessage({ action, data });
}
});
Expand Down
35 changes: 35 additions & 0 deletions test/lib/core/messenger/local.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const utils = require('../../../utils');
const pedding = require('pedding');
const assert = require('assert');
const mm = require('egg-mock');

describe('test/lib/core/messenger/local.test.js', () => {
let app;
Expand All @@ -14,6 +15,7 @@ describe('test/lib/core/messenger/local.test.js', () => {
after(() => app.close());

afterEach(() => {
mm.restore();
app.messenger.close();
app.agent.messenger.close();
});
Expand Down Expand Up @@ -161,4 +163,37 @@ describe('test/lib/core/messenger/local.test.js', () => {
app.agent.messenger.sendTo(process.pid, 'sendTo-event', { foo: 'bar' });
});
});

describe('send()', () => {
it('app.messenger.send should not throw when app.agent not exist', () => {
mm(app, 'agent', undefined);
app.messenger.send('send-event', { foo: 'bar' });
});

it('app.messenger.send should work', done => {
app.agent.messenger.once('send-event', msg => {
assert.deepEqual(msg, { foo: 'bar' });
done();
});

app.messenger.once('send-event', () => {
throw new Error('should not emit on app');
});

app.messenger.send('send-event', { foo: 'bar' });
});

it('agent.messenger.send should work', done => {
app.agent.messenger.once('send-event', () => {
throw new Error('should not emit on agent');
});

app.messenger.once('send-event', msg => {
assert.deepEqual(msg, { foo: 'bar' });
done();
});

app.agent.messenger.send('send-event', { foo: 'bar' });
});
});
});

0 comments on commit eac4941

Please sign in to comment.