src/promissory-arbiter.js
function subscribe (state, topic, subscription, options, context) {
    assert(typeof topic, 'string', 'Arbiter.subscribe', 'strings', 'topics');
    options = merge(state.options, options);

    var
      ancestor = addTopicLine(
        topic, ancestorTopicSearch(topic, state._topics)
      ),
      node = insert(
        getPriority,
        createSubscription(state, subscription, options, context),
        ancestor.subscriptions
      ),
      subscriptionToken = {
        topic: topic,
        id: node.id,
        priority: node.priority
      };

    // Notify late subscribers of persisted messages
    if (!options.ignorePersisted) {
      var
        persistedDescendents = map(getPersisted, descendents(ancestor)),
        persistedMessages = mergeBy(getFingerArrayOrder, persistedDescendents),

        persisted, i, n;

      for (i = 0, n = persistedMessages.length; i < n; i++) {
        persisted = persistedMessages[i];
        !subscription.suspended // eslint-disable-line no-unused-expressions
          && subscription.call(
            subscription.context, persisted.data, persisted.topic
          );
      }
    }

    return subscriptionToken;
  }
function subscribe (state, topic, subscription, options, context) {
    assert(typeof topic, 'string', 'Arbiter.subscribe', 'strings', 'topics');
    options = merge(state.options, options);

    var
      ancestor = addTopicLine(
        topic, ancestorTopicSearch(topic, state._topics)
      ),
      node = insert(
        getPriority,
        createSubscription(state, subscription, options, context),
        ancestor.subscriptions
      ),
      subscriptionToken = {
        topic: topic,
        id: node.id,
        priority: node.priority
      };

    // Notify late subscribers of persisted messages
    if (!options.ignorePersisted) {
      var
        persistedDescendents = map(getPersisted, descendents(ancestor)),
        persistedMessages = mergeBy(getFingerArrayOrder, persistedDescendents),

        persisted, i, n;

      for (i = 0, n = persistedMessages.length; i < n; i++) {
        persisted = persistedMessages[i];
        !subscription.suspended // eslint-disable-line no-unused-expressions
          && subscription.call(
            subscription.context, persisted.data, persisted.topic
          );
      }
    }

    return subscriptionToken;
  }
function subscribe ( state , topic , subscription , options , context ) { assert ( typeof topic , 'string' , 'Arbiter.subscribe' , 'strings' , 'topics' ) ; options = merge ( state . options , options ) ; var ancestor = addTopicLine ( topic , ancestorTopicSearch ( topic , state . _topics ) ) , node = insert ( getPriority , createSubscription ( state , subscription , options , context ) , ancestor . subscriptions ) , subscriptionToken = { topic : topic , id : node . id , priority : node . priority } ; // Notify late subscribers of persisted messages if ( ! options . ignorePersisted ) { var persistedDescendents = map ( getPersisted , descendents ( ancestor ) ) , persistedMessages = mergeBy ( getFingerArrayOrder , persistedDescendents ) , persisted , i , n ; for ( i = 0 , n = persistedMessages . length ; i < n ; i ++ ) { persisted = persistedMessages [ i ] ; ! subscription . suspended // eslint-disable-line no-unused-expressions && subscription . call ( subscription . context , persisted . data , persisted . topic ) ; } } return subscriptionToken ; }
`Arbiter.subscribe` registers a subscription to a topic and its
descendants. When a publication occurs it will be notified. The behavior
can be modified by using the options parameter. `options.priority`
establishes the order to notify subscribers when multiple subscribers
exist. The other option is `ignorePersisted`. This allows a subscriber
to skip being notified of saved messages.

@function subscribe
@memberof Arbiter

@param {Topic|Topic[]} topic The title of the topic to listen for
publications. Topics are hierarchical can be separated by `","`.
@param {Subscription} subscription The function to invoke every
time a publication occurs. If `subscription` is not a function, a
no-operation is put in its place.
@param {Object} [options]. An object that can have two properties.
`ignorePersited` and `priority`.
@param {Object} [context=null] The value of `this` for the subscription.
@return {Token} A unique token to remove this subscription from
the distribution list.

@example
Arbiter.publish('my.topic', null, {persist: true});
Arbiter.subscribe('my.topic', log, {ignorePersisted: true}); // => Nothing
 
A Subscription is a function provided to the `subscribe` method. It is used
as a callback when a publication occurs. The subscriber is considered
"done working" if it returns a value (even `undefined`). If an error is
thrown, then it is assumed that the subscriber failed. If it returns a
`Promise`, then it is "done" when the `Promise` is fulfilled. If rejected,
it is assumed to fail. If the subscriber function has a length of 3
or more, then it is provided with callback function as the third argument
to be treated as a node-style callback. The first argument to the callback
is the error and the second is the "return value".

@callback Subscription
@memberof Arbiter
@param {Object} data The data associated with the publication.
@param {Topic} topic The topic to which the publication belongs.
@param {Function} [callback] A node style callback.
@return {Object} This is either a `Promise` or a value used to
communicate when the subscriber is done.

@example

// All of the following look the same from a publishers perspective.
Arbiter.subscribe('my.topic', function() {
return new Promise(function(fulfill, reject) {
Math.random() > 0.5 ? fulfill('hi') : reject('bye');
});
});

Arbiter.subscribe('my.topic', function(data, topic, done) {
Math.random() > 0.5 ? done(null, 'hi') : done('bye');
});

Arbiter.subscribe('my.topic', function() {
if (Math.random() > 0.5) {
return 'hi';
} else {
throw 'bye';
}
});
Arbiter . subscribe registers a subscription to a topic and its descendants . When a publication occurs it will be notified . The behavior can be modified by using the options parameter . options . priority establishes the order to notify subscribers when multiple subscribers exist . The other option is ignorePersisted . This allows a subscriber to skip being notified of saved messages .