subscribed 1.0.1

A minimalistic event and pub-sub library


To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:

subscribe.d

This is a simple library for minimalistic in-process pub-sub and event operations.

There are two modules in the package (both can be imported using import subscribed:

  • subscribed.event provides a class for creating events, which are basically collections of delegates ("subscribers") that have the same signature. Events are called like functions (opCall) and return arrays, corresponding to the return values of individual functions:
import subscribed.event;

int add(int a, int b)
{
    return a + b;
}

int multiply(int a, int b)
{
    return a * b;
}

void doNothing() {}

// The first argument of the constructor specifies the return type, the rest specify the types of arguments this event should expect from it's delegates

auto event = new Event!(int, int, int);
event ~= &add;
event ~= &multiply;
assert(event(5, 5) == [10, 25]);
event.destroy

// For convenience, VoidEvent aliases Event!void (events, accepting void delegates without parameters). Obviously, these events do not return.

event = new VoidEvent;
event ~= doNothing;
event ~= doNothing;
event();

// You can add the same delegate multiple times. When removing it however, all mathing delegates get removed.

event -= doNothing; // Returns true, meaning that there were >=1 removed delegates
event -= doNothing; // Returns false, meaning that there were no removed delegates
assert(event.subscribers.length == 0); // No subscribers left

event ~= doNothing;

// You can off course shift and pop delegates
event.shift; // Doesn't throw
assertThrown!AssertError(event.shift); // Throws and AssertError, because there is nothing to return
  • subscribed.pubsub is based on events and provides functions for sending and receiving any sort of data (generally the transferred data is called a message, although in this context it may be inappropriate to call it so). The module doesn't use any messaging protocol and relies solely on Events to transfer messages, making it fast and easy to use for in-program events. Publishing data does not return, thus making all return values void.
void f(int a) {}
void g(int a, int b) {}
void h(string a) {}

subscribe("test", &f); // From now on, all delegates, subscribing to the "test" channel should have the same type as f: void function(int)

publish("test", 1); // Transfers 1 to all subscribed delegates, currently only f. Publishing to an event without subscribers simply does nothing, regardless of the "message".

publish("test", 1, 2)); // Throws an ChannelTypeException, because the data transmitted does not match what the underlying event expects

unsubscribe("test", &f); // Returns true, meaning that there were >=1 removed delegates
unsubscribe("test", &f); // Returns false, meaning that there were no removed delegates

destroyChannel("test"); // Destroys the underlying event, making it possible to subscribe delegates with a different signature
Authors:
  • ianis
Dependencies:
none
Versions:
2.0.0 2019-Nov-03
1.6.1 2016-May-22
1.6.0 2016-May-22
1.5.0 2016-May-21
1.4.2 2016-May-19
Show all 17 versions
Download Stats:
  • 0 downloads today

  • 0 downloads this week

  • 0 downloads this month

  • 233 downloads total

Score:
0.8
Short URL:
subscribed.dub.pm