Skip to content
Permalink
Browse files

[Mediator] Added a channel argument to the mediator hooks

  • Loading branch information
v-- committed May 22, 2016
1 parent b885976 commit 4c93a4f2e99ba6ef3ec4d93e611ab2ecd969288e
Showing with 10 additions and 10 deletions.
  1. +2 −2 readme.md
  2. +6 −6 source/subscribed/mediator.d
  3. +2 −2 source/subscribed/package.d
@@ -63,8 +63,8 @@ mediator.on!"increment"((int amount) {
// Make sure nothing happens while the machine is not running.
// The listeners are only ran if the beforeEach hooks all return true.
mediator.beforeEach ~= () {
return machine.state == SimpleMachine.State.running;
mediator.beforeEach ~= (string channel) {
return channel == "reset" || machine.state == SimpleMachine.State.running;
};
// Bind some events to the machine state changes.
@@ -71,10 +71,10 @@ struct Mediator(Channel[] channels)
import std.array : array, join;

/// The hook to be executed before any transition. If false is returned, the no transition occurs.
Event!(bool delegate()) beforeEach;
Event!(bool delegate(string)) beforeEach;

/// The hook to be executed after a successful transition.
Event!(void delegate()) afterEach;
Event!(void delegate(string)) afterEach;

version (D_Ddoc)
{
@@ -155,7 +155,7 @@ struct Mediator(Channel[] channels)

%2$sEventType.ReturnType emit(string channel: "%2$s")(%2$sEventType.ParamTypes params)
{
foreach (condition; beforeEach()) {
foreach (condition; beforeEach(channel)) {
if (!condition) {
static if (is(%2$sEventType.ReturnType == void))
return;
@@ -167,12 +167,12 @@ struct Mediator(Channel[] channels)
static if (is(%2$sEventType.ReturnType == void))
{
_%2$s.call(params);
afterEach();
afterEach(channel);
}
else
{
auto result = _%2$s.call(params);
afterEach();
afterEach(channel);
return result;
}
}
@@ -209,7 +209,7 @@ unittest
mediator.emit!"dec";
assert(counter == 0, "The mediator does not call one of it's functions.");

mediator.beforeEach ~= () => false;
mediator.beforeEach ~= string => false;

assert(counter == 0, "The beforeEach hook does not work.");
mediator.emit!"inc";
@@ -31,8 +31,8 @@ unittest

// Make sure nothing happens while the machine is not running.
// The listeners are only ran if the beforeEach hooks all return true.
mediator.beforeEach ~= () {
return machine.state == SimpleMachine.State.running;
mediator.beforeEach ~= (string channel) {
return channel == "reset" || machine.state == SimpleMachine.State.running;
};

// Bind some events to the machine state changes.

0 comments on commit 4c93a4f

Please sign in to comment.
You can’t perform that action at this time.