Source: mixins/bussable.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"use strict";
/**
 * @fileOverview Provides easy access to the system bus and provides some helper methods for doing so
 * @module mixins/bussable
 * @requires postal
 * @requires lodash
 * @requires base
 */
var bus = require( "postal" );
var Base = require( "../base" );
var sys = require( "lodash" );

/**
 *  @classDesc Provides easy access to the system bus and provides some helper methods for doing so
 *  @exports mixins/bussable
 *  @mixin
 */
var Bussable = Base.compose( [Base], /** @lends mixins/bussable# */{
    declaredClass : "mixins/Bussable",
    constructor   : function () {
        /**
         * The list of subscriptions maintained by the mixin
         * @type {Array}
         * @memberof mixins/bussable#
         * @name _subscriptions
         * @private
         */
        this._subscriptions = {};

        this.log.trace( "Bussable constructor" );
    },

    /**
     * Subscribe to an event
     * @param {string} channel The channel to subscribe to
     * @param {string} topic The topic to subscribe to
     * @param {callback} [callback] What to do when you get the event
     * @returns {object} The subscription definition
     */
    subscribe : function ( channel, topic, callback ) {
        this.log.trace( "Bussable subscribe" );
        var sub = bus.subscribe( {channel : channel, topic : topic, callback : callback} );
        this.subscriptions[channel + "." + topic] = sub;
        return sub;
    },

    /**
     * Subscribe to an event once
     * @param {string} channel The channel to subscribe to
     * @param {string} [topic] The topic to subscribe to
     * @param {callback} [callback] What to do when you get the event
     * @returns {object} The subscription definition
     */
    once : function ( channel, topic, callback ) {
        this.log.trace( "Bussable once" );
        var sub = this.subscribe( channel, topic, callback );
        this.subscriptions[channel + "." + topic] = sub;
        sub.disposeAfter( 1 );
        return sub;
    },

    /**
     * Publish an event on the system bus
     * @param {string} [channel='2'] The channel to publish to
     * @param {string} [topic=1] The topic to publish to
     * @param {object} [options={}] What to pass to the event
     * @param {boolean} [options.ha=2] Test sub prop
     */
    publish : function ( channel, topic, options ) {
        this.log.trace( "Bussable publish" );
        bus.publish( {channel : channel, topic : topic, data : options} );
    },

    /**
     * Get a subscription definition
     *
     * @param {string} channel
     * @param {string} topic
     * @returns {object=} The subscription definition
     */
    getSubscription : function ( channel, topic ) {
        this.log.trace( "Bussable getSubscription" );
        return this.subscriptions[channel + "." + topic];
    },

    /**
     * Gets rid of all subscriptions for this object.
     * @private
     */
    destroy : function () {
        this.log.trace( "Bussable destroy" );

        sys.each( this.subscriptions, function ( sub ) {
            sub.unsubscribe();
        } );
    }
} );

module.exports = Bussable;