Skip to content

Commit

Permalink
refactor notice inbox-no-longer-empty to hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Akamaozu committed Dec 11, 2018
1 parent 742e35c commit 41cd2bc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 36 deletions.
Expand Up @@ -21,7 +21,7 @@ module.exports = function( supervisor, config ){

citizen.mail.inbox.push( envelope );

if( citizen.mail.inbox.length === 1 ) supervisor.noticeboard.notify( citizen.name + '-inbox-no-longer-empty' );
if( citizen.mail.inbox.length === 1 ) supervisor.hook.run( citizen.name + '-inbox-no-longer-empty' );
};
});
}
52 changes: 18 additions & 34 deletions lib/modules/supervisor/mail/handle-mail-related-signals.js
Expand Up @@ -21,23 +21,7 @@ module.exports = function( supervisor ){
citizen.ref.send( citizen.state.current_mail );
}

else {

if( citizen.state.waiting_for_mail ) return;

citizen.state.waiting_for_mail = true;

supervisor.noticeboard.once( envelope.from + '-inbox-no-longer-empty', 'send-mail-to-ready-citizen', function( msg ){

citizen.state.waiting_for_mail = false;

if( citizen.state.pause_mail ) return;

citizen.state.current_mail = citizen.mail.inbox.splice( 0, 1 )[0];

citizen.ref.send( citizen.state.current_mail );
});
}
else citizen_start_waiting_for_mail( citizen );

break;

Expand All @@ -58,23 +42,7 @@ module.exports = function( supervisor ){
citizen.ref.send( citizen.state.current_mail );
}

else{

if( citizen.state.waiting_for_mail ) return;

citizen.state.waiting_for_mail = true;

supervisor.noticeboard.once( envelope.from + '-inbox-no-longer-empty', 'send-mail-to-ready-citizen', function( msg ){

citizen.state.waiting_for_mail = false;

if( citizen.state.pause_mail ) return;

citizen.state.current_mail = citizen.mail.inbox.splice( 0, 1 )[0];

citizen.ref.send( citizen.state.current_mail );
});
}
else citizen_start_waiting_for_mail( citizen );

break;

Expand All @@ -89,4 +57,20 @@ module.exports = function( supervisor ){
break;
}
});

function citizen_start_waiting_for_mail( citizen ){
if( citizen.state.waiting_for_mail ) return;

citizen.state.waiting_for_mail = true;

supervisor.hook.add( citizen.name + '-inbox-no-longer-empty', 'send-mail-to-ready-citizen', function(){
supervisor.hook.del( citizen.name + '-inbox-no-longer-empty', 'send-mail-to-ready-citizen' );

citizen.state.waiting_for_mail = false;
if( citizen.state.pause_mail ) return;

citizen.state.current_mail = citizen.mail.inbox.splice( 0, 1 )[0];
citizen.ref.send( citizen.state.current_mail );
});
}
}
2 changes: 1 addition & 1 deletion lib/modules/supervisor/mail/route-received-mail.js
Expand Up @@ -25,7 +25,7 @@ module.exports = function( supervisor, config ){
recipient.mail.inbox.push( envelope );
success = true;

if( recipient.mail.inbox.length === 1 ) supervisor.noticeboard.notify( envelope.to + '-inbox-no-longer-empty' );
if( recipient.mail.inbox.length === 1 ) supervisor.hook.run( envelope.to + '-inbox-no-longer-empty' );
break;

default:
Expand Down

0 comments on commit 41cd2bc

Please sign in to comment.