Mediator

A simple mediator implementation. More precisely, an event collection with a unified interface and beforeEach/afterEach hooks.

Members

Aliases

EventType
alias EventType = Event!(void function())
Undocumented in source.
IType
alias IType = typeof(params[0])

The type of the (indexing) channel names.

Functions

emit
void emit(EventType.ParamTypes params)

Calls all the registered listeners for the channel in order.

off
void off(EventType.ListenerType[] listeners)

A function for removing listeners from the channel event.

on
void on(EventType.ListenerType[] listeners)

A function for appending listeners to the channel event.

Mixins

__anonymous
mixin bindChannel!channel
Undocumented in source.

Variables

afterEach
Event!(void delegate(IType)) afterEach;

The hook to be executed after a successful transition.

beforeEach
Event!(bool delegate(IType)) beforeEach;

The hook to be executed before any transition. If false is returned, the no transition occurs.

Mixed In Members

From mixin bindChannel!channel

on
void on(EventType.ListenerType[] listeners)
Undocumented in source. Be warned that the author may not have intended to support it.
off
void off(EventType.ListenerType[] listeners)
Undocumented in source. Be warned that the author may not have intended to support it.
emit
void emit(EventType.ParamTypes params)
Undocumented in source. Be warned that the author may not have intended to support it.

Examples

The mediator events can be strings.

Mediator!(
    "start", void delegate(),
    "stop", void delegate()
) mediator;

mediator.emit!"start"();

The mediator events can be enum members.

enum Event { start, stop }

Mediator!(
    Event.start, void delegate(),
    Event.stop, void delegate()
) mediator;

mediator.emit!(Event.start)();

The mediator events cannot be of mixed type.

immutable canCompile = __traits(compiles, Mediator!(
    "start", void delegate(),
    3, void delegate()
));

assert(!canCompile, "Can compile mediators with mixed index types");

The mediator can subscribe, unsubscribe and broadcast events.

1 Mediator!(
2     "inc", void delegate(),
3     "dec", void delegate(),
4     "reset counter", void delegate()
5 ) mediator;
6 
7 int counter;
8 
9 void increment()
10 {
11     counter++;
12 }
13 
14 void decrement()
15 {
16     counter--;
17 }
18 
19 void reset()
20 {
21     counter = 0;
22 }
23 
24 mediator.on!"inc"(&increment);
25 mediator.on!"dec"(&decrement);
26 mediator.on!"reset counter"(&reset);
27 
28 assert(counter == 0, "Mediator functions are called before any action is performed");
29 mediator.emit!"inc"();
30 assert(counter == 1, "The mediator does not call one of it's functions");
31 mediator.emit!"dec"();
32 assert(counter == 0, "The mediator does not call one of it's functions");
33 
34 assert(counter == 0, "Mediator functions are called before any action is performed");
35 mediator.emit!"inc"();
36 assert(counter == 1, "The mediator does not call one of it's functions");
37 mediator.emit!"reset counter"();
38 assert(counter == 0, "The mediator does not call one of it's functions");
39 
40 mediator.beforeEach ~= string => false;
41 
42 assert(counter == 0, "The beforeEach hook does not work");
43 mediator.emit!"inc"();
44 assert(counter == 0, "The beforeEach hook does not work");
45 mediator.emit!"dec"();
46 assert(counter == 0, "The beforeEach hook does not work");
47 
48 mediator.beforeEach.clear();
49 
50 mediator.off!"inc"(&increment);
51 mediator.off!"dec"(&decrement);
52 
53 assert(counter == 0, "The mediator called one of it's functions while unregistering them");
54 mediator.emit!"inc"();
55 assert(counter == 0, "The mediator did not remove a listener");
56 mediator.emit!"dec"();
57 assert(counter == 0, "The mediator did not remove a listener");

Meta