bussable.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. "use strict";
  2. /**
  3. * @fileOverview Provides easy access to the system bus and provides some helper methods for doing so
  4. * @module mixins/bussable
  5. * @requires postal
  6. * @requires lodash
  7. * @requires base
  8. */
  9. var bus = require( "postal" );
  10. var Base = require( "../base" );
  11. var sys = require( "lodash" );
  12. /**
  13. * @classDesc Provides easy access to the system bus and provides some helper methods for doing so
  14. * @exports mixins/bussable
  15. * @mixin
  16. */
  17. var Bussable = Base.compose( [Base], /** @lends mixins/bussable# */{
  18. declaredClass : "mixins/Bussable",
  19. constructor : function () {
  20. /**
  21. * The list of subscriptions maintained by the mixin
  22. * @type {Array}
  23. * @memberof mixins/bussable#
  24. * @name _subscriptions
  25. * @private
  26. */
  27. this._subscriptions = {};
  28. this.log.trace( "Bussable constructor" );
  29. },
  30. /**
  31. * Subscribe to an event
  32. * @param {string} channel The channel to subscribe to
  33. * @param {string} topic The topic to subscribe to
  34. * @param {callback} [callback] What to do when you get the event
  35. * @returns {object} The subscription definition
  36. */
  37. subscribe : function ( channel, topic, callback ) {
  38. this.log.trace( "Bussable subscribe" );
  39. var sub = bus.subscribe( {channel : channel, topic : topic, callback : callback} );
  40. this.subscriptions[channel + "." + topic] = sub;
  41. return sub;
  42. },
  43. /**
  44. * Subscribe to an event once
  45. * @param {string} channel The channel to subscribe to
  46. * @param {string} [topic] The topic to subscribe to
  47. * @param {callback} [callback] What to do when you get the event
  48. * @returns {object} The subscription definition
  49. */
  50. once : function ( channel, topic, callback ) {
  51. this.log.trace( "Bussable once" );
  52. var sub = this.subscribe( channel, topic, callback );
  53. this.subscriptions[channel + "." + topic] = sub;
  54. sub.disposeAfter( 1 );
  55. return sub;
  56. },
  57. /**
  58. * Publish an event on the system bus
  59. * @param {string} [channel='2'] The channel to publish to
  60. * @param {string} [topic=1] The topic to publish to
  61. * @param {object} [options={}] What to pass to the event
  62. * @param {boolean} [options.ha=2] Test sub prop
  63. */
  64. publish : function ( channel, topic, options ) {
  65. this.log.trace( "Bussable publish" );
  66. bus.publish( {channel : channel, topic : topic, data : options} );
  67. },
  68. /**
  69. * Get a subscription definition
  70. *
  71. * @param {string} channel
  72. * @param {string} topic
  73. * @returns {object=} The subscription definition
  74. */
  75. getSubscription : function ( channel, topic ) {
  76. this.log.trace( "Bussable getSubscription" );
  77. return this.subscriptions[channel + "." + topic];
  78. },
  79. /**
  80. * Gets rid of all subscriptions for this object.
  81. * @private
  82. */
  83. destroy : function () {
  84. this.log.trace( "Bussable destroy" );
  85. sys.each( this.subscriptions, function ( sub ) {
  86. sub.unsubscribe();
  87. } );
  88. }
  89. } );
  90. module.exports = Bussable;