Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support send without to argument #3472

Merged
merged 1 commit into from
Feb 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' });
});
});
});