PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/MS Silk/Scripts/Debug/mstats.pubsub.js

#
JavaScript | 97 lines | 38 code | 6 blank | 53 comment | 7 complexity | 63374126fb2886e61c11eedf838c9667 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. //===================================================================================
  2. // Microsoft patterns & practices
  3. // Silk : Web Client Guidance
  4. //===================================================================================
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7. // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8. // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. // FITNESS FOR A PARTICULAR PURPOSE.
  10. //===================================================================================
  11. // The example companies, organizations, products, domain names,
  12. // e-mail addresses, logos, people, places, and events depicted
  13. // herein are fictitious. No association with any real company,
  14. // organization, product, domain name, email address, logo, person,
  15. // places, or events is intended or should be inferred.
  16. //===================================================================================
  17. /*jslint onevar: true, undef: true, newcap: true, regexp: true, plusplus: true, bitwise: true, devel: true, maxerr: 50 */
  18. /*global jQuery, setInterval, clearInterval */
  19. (function (mstats, $) {
  20. /**
  21. * PubSub is the messaging system used by
  22. * the widgets to listen for and fire events
  23. */
  24. mstats.pubsub = (function () {
  25. var queue = [],
  26. that = {};
  27. /**
  28. * Executes all callbacks associated with eventName using the
  29. * context provided in the subscription.
  30. *
  31. * @param {string} eventName The name of the event to publish
  32. * Uses a dot notation for consistency
  33. * @param {object} data Any data to be passed to the event
  34. * handler. Custom to the event.
  35. */
  36. that.publish = function (eventName, data) {
  37. var context, intervalId, idx = 0;
  38. if (queue[eventName]) {
  39. intervalId = setInterval(function () {
  40. if (queue[eventName][idx]) {
  41. context = queue[eventName][idx].context || this;
  42. queue[eventName][idx].callback.call(context, data);
  43. idx += 1;
  44. } else {
  45. clearInterval(intervalId);
  46. }
  47. }, 0);
  48. }
  49. };
  50. /**
  51. * Stores an event subscription. Subsequent event subscriptions
  52. * are always added (not overwritten). Use unsubscribe to remove
  53. * event subscriptions.
  54. *
  55. * @param {string} eventName The name of the event to publish
  56. * Uses a dot notation for consistency
  57. * @param {function} callback The function to be called when the
  58. * event is published.
  59. * @param {object} context The context to execute the callback
  60. */
  61. that.subscribe = function (eventName, callback, context) {
  62. if (!queue[eventName]) {
  63. queue[eventName] = [];
  64. }
  65. queue[eventName].push({
  66. callback: callback,
  67. context: context
  68. });
  69. };
  70. /**
  71. * Removes an event subscription.
  72. *
  73. * @param {string} eventName The name of the event to remove
  74. * @param {function} callback The function associated witht the
  75. * event. Used to ensure the correct
  76. * event is being removed.
  77. * @param {object} context The context associated with the
  78. * callback. Used to ensure the correct
  79. * event is being removed.
  80. */
  81. that.unsubscribe = function (eventName, callback, context) {
  82. if (queue[eventName]) {
  83. queue[eventName].pop({
  84. callback: callback,
  85. context: context
  86. });
  87. }
  88. };
  89. return that;
  90. } ());
  91. } (this.mstats = this.mstats || {}, jQuery));