API
The Turms package is a single object that provides access to constructor functions for Message
, Subscriber
and Hub
objects.
Message
is a factory function that returns a Message
object. It takes an options
object as an argument.
Valid options
peroperties are type
, data
, delay
and recipient
. Their values will be used to populate the Message
object.
The Message
object is a container object with type
, data
, delay
and recipient
fields. The type
field is required, the others are optional.
-
type:
A string that defines the type of the message message. Used by the
Hub
when defining subscriptions or routing messages. -
data:
The content of the message that can be acted on by a
Subscriber
in itsreceiveMessage
method. Can be aString
,Object
,Number
, etc.. -
The delay, in ms, that should pass before this message is sent. If not passed the message will be sent immediately.
-
A specific object that should be the only recipient of the message being sent. The recipient object should have a
receiveMessage
method and be subscribed to theMessage.type
at the sameHub
theMessage
is sent through.
Subscriber
is a factory function that returns a Subscriber
object.
A Subscriber
has only one function, receiveMessage
, which will be called by a Hub
when a Message
has been sent that a registered Subscriber
listens to.
Since Subscriber
doesn't do anything besides receive messages, typical usages involves extending the Subscriber
object returned from the factory, or by adding receiveMessage
to obj
with Object.assign(obj, Turms.Subscriber())
.
-
receiveMessage(action, message)
receiveMessage
calls theaction
defined in aSubscription
with theSubscriber
set asthisArg
for the function stored inaction
.receiveMessage
could also be called directly on aSubscriber
.
Hub
is factory function that returns a Hub
object.
Most of Turms logic takes place inside a Hub
. A Subscriber
can subscribe with a Hub
via addSubscription
. A subscription
object will be returned by addSubscription
, and this can later be used to unsubscribe by passing it to removeSubscription
.
-
addSubscription(subscriber, messageType, action):
Takes a
Subscriber
object and registers it to listen for anyMessage
whosetype
field corresponds to themessageType
parameter.
The action
is a callback that will be called by the by Subscriber.receiveMessage
. When called, action
will see the Subscriber
as this
and takes the Message
being sent as its argument, making the Message
and Subscriber
accessible inside action
.
addSubscription
Returns the created Subscription
object, which can be used with removeSubscription
.
-
removeSubscription(subscription):
Removes a
Subscription
from theHub
.Returns true if a
Subscription
was successfully removed,false
if not. -
Sends a
Message
to allSubscriber
s listening tomessage.type
. If themessage
hasdelay
it will be queued. Pass the return value ofMessage(options)
or another object with the same properties.Returns a
Receipt
. Ifmessage
hasdelay
, theReceipt
will store an activeTimer
, otherwise it will have onlymessage
. -
Queues a
Message
to be passed tosendMessage
after a delay defined inmessage.delay
. Called internally bysendMessage
ifmessage
has adelay
. Could also be used externally.Returns a
Receipt
containingmessage
and an activeTimer
. TheReceipt
can be used to cancel a queuedMessage
. -
Cancels a queued
Message
stored in thereceipt
parameter'smessage
property. Returnstrue
if aMessage
was canceled,false
if not.