This package is an event and message passing system developed for Fluid.
import event_pipe;A Subscriber receives and processes data received from another class. It is more-or-less
equivalent to a delegate.
interface Subscriber(Ts...) {
void opCall(Ts args);
}A Publisher emits data through Subscriber objects.
interface Publisher(Outputs...) {
void subscribe(Subscriber!Output subscriber);
}Additionally, Publisher offers a shorthand then function that accepts a delegate:
publisher
.then((int input) => input + 1);Under the hood, then converts the delegate into a Subscriber object (this object is always
a Pipe).
A Pipe is a basic implementation of both Subscriber and Publisher. It runs a function
on the input and "pipes" the result to a publisher.
/// Pipe as a Subscriber:
unittest {
auto sub = pipe((string input) {
return "Hello, " ~ input ~ "!";
});
sub.then((string input) {
writeln(input);
});
sub("World"); // Outputs "Hello, World!"
}Any instance of then implicitly creates a Pipe, allowing for chaining. If a delegate passed
into then returns a Publisher
unittest {
auto start = pipe(() => "Hello, ");
auto setName = pipe((string input) => input);
start
// Take `prefix` and wait for setName
.then((string prefix) => setName
.then((string name) => prefix ~ name))
// Having concatenated the two, write them out
.then((string result) => writeln(result));
start();
setName("Night");
}To distribute events to a set of subscribers, one can use the Event struct.
unittest {
Event!string event;
event ~= pipe((string input) => writeln("Hello, ", input));
event ~= pipe((string input) => writeln("Goodnight, ", input));
event("World");
}For more information see documentation inside the code.