Skip to content

Commit

Permalink
fix: app.messenger.broadcast send to self
Browse files Browse the repository at this point in the history
  • Loading branch information
ImHype authored and dead-horse committed Aug 28, 2018
1 parent 609f14a commit cc6b976
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
22 changes: 14 additions & 8 deletions lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,25 +236,31 @@ function getProperty(target, prop) {

function bindMessenger(Application, agent) {
const agentMessenger = agent.messenger;
return class MeesengerApplication extends Application {
return class MessengerApplication extends Application {
constructor(options) {
super(options);

const appMessenger = this.messenger;
agentMessenger.send = new Proxy(agentMessenger.send, {
apply(target, thisArg, [ action, data ]) {
setImmediate(() => appMessenger._onMessage({ action, data }));
},
apply: this._sendMessage.bind(this),
});
}
_sendMessage(target, thisArg, [ action, data, to ]) {
const appMessenger = this.messenger;
setImmediate(() => {

if (to === 'app') {
appMessenger._onMessage({ action, data });
} else {
agentMessenger._onMessage({ action, data });
}
});
}
get messenger() {
return this[MESSENGER];
}
set messenger(m) {
m.send = new Proxy(m.send, {
apply(target, thisArg, [ action, data ]) {
setImmediate(() => agentMessenger._onMessage({ action, data }));
},
apply: this._sendMessage.bind(this),
});
this[MESSENGER] = m;
}
Expand Down
11 changes: 11 additions & 0 deletions test/app_proxy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,5 +282,16 @@ describe('test/app_proxy.test.js', () => {
assert(app._agent.eggReadyData.baseDir === baseDir);
assert(app._app.eggReadyData.baseDir === baseDir);
});

it('should broadcast message successfully', () => {
assert(app._app.recievedBroadcastAction === true);
assert(app._agent.recievedBroadcastAction === true);
assert(app._app.recievedAgentRecievedAction === true);
});

it('should send message from app to app', () => {
assert(app._app.recievedAppAction === true);
});

});
});
5 changes: 5 additions & 0 deletions test/fixtures/messenger-binding/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ module.exports = agent => {
agent.received = [];
agent.messenger.on('action', data => agent.received.push(data));

agent.messenger.on('broadcast-action', () => {
agent.recievedBroadcastAction = true;
agent.messenger.sendToApp('agent-recieved-broadcast-action');
});

agent.messenger.sendToApp('action', 'send data when agent starting');
agent.ready(() => {
agent.messenger.sendToApp('action', 'send data when agent started');
Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/messenger-binding/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,24 @@ module.exports = app => {
app.received = [];
app.messenger.on('action', data => app.received.push(data));

app.messenger.on('app-action', () => {
app.recievedAppAction = true;
});

app.messenger.on('broadcast-action', () => {
app.recievedBroadcastAction = true;
});

app.messenger.on('agent-recieved-broadcast-action', () => {
app.recievedAgentRecievedAction = true;
});

app.messenger.sendToAgent('action', 'send data when app starting');

app.ready(() => {
app.messenger.sendToAgent('action', 'send data when app started');
app.messenger.broadcast('broadcast-action', 'broadcast action');
app.messenger.sendToApp('app-action', 'send action to app');
});
app.messenger.on('egg-ready', data => {
app.eggReady = true;
Expand Down

0 comments on commit cc6b976

Please sign in to comment.