onfire.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. Copyright (c) 2016 hustcc http://www.atool.org/
  3. License: MIT
  4. https://github.com/hustcc/onfire.js
  5. **/
  6. /* jshint expr: true */
  7. !function (root, factory) {
  8. if (typeof module === 'object' && module.exports)
  9. module.exports = factory();
  10. else
  11. root.onfire = factory();
  12. }(typeof window !== 'undefined' ? window : this, function () {
  13. var __onfireEvents = {},
  14. __cnt = 0, // evnet counter
  15. string_str = 'string',
  16. function_str = 'function',
  17. hasOwnKey = Function.call.bind(Object.hasOwnProperty), // bind es5
  18. slice = Function.call.bind(Array.prototype.slice), // bind es5
  19. _guid = 0;
  20. function _bind(eventName, callback, is_one, context) {
  21. if (typeof eventName !== string_str || typeof callback !== function_str) {
  22. throw new Error('args: '+string_str+', '+function_str+'');
  23. }
  24. if (! hasOwnKey(__onfireEvents, eventName)) {
  25. __onfireEvents[eventName] = {};
  26. }
  27. __onfireEvents[eventName][++__cnt] = [callback, is_one, context];
  28. return [eventName, __cnt];
  29. }
  30. function _each(obj, callback) {
  31. for (var key in obj) {
  32. if (hasOwnKey(obj, key)) callback(key, obj[key]);
  33. }
  34. }
  35. function getGuid()
  36. {
  37. return _guid++;
  38. }
  39. /**
  40. * onfire.on( event, func, context ) -> Object
  41. * - event (String): The event name to subscribe / bind to
  42. * - func (Function): The function to call when a new event is published / triggered
  43. * Bind / subscribe the event name, and the callback function when event is triggered, will return an event Object
  44. **/
  45. function on(eventName, callback, context) {
  46. return _bind(eventName, callback, 0, context);
  47. }
  48. /**
  49. * onfire.one( event, func, context ) -> Object
  50. * - event (String): The event name to subscribe / bind to
  51. * - func (Function): The function to call when a new event is published / triggered
  52. * Bind / subscribe the event name, and the callback function when event is triggered only once(can be triggered for one time), will return an event Object
  53. **/
  54. function one(eventName, callback, context) {
  55. return _bind(eventName, callback, 1, context);
  56. }
  57. function _fire_func(eventName, args) {
  58. if (hasOwnKey(__onfireEvents, eventName)) {
  59. _each(__onfireEvents[eventName], function(key, item) {
  60. item[0].apply(item[2], args); // do the function
  61. if (item[1]) delete __onfireEvents[eventName][key]; // when is one, delete it after triggle
  62. });
  63. }
  64. }
  65. /**
  66. * onfire.fire( event[, data1 [,data2] ... ] )
  67. * - event (String): The event name to publish
  68. * - data...: The data to pass to subscribers / callbacks
  69. * Async Publishes / fires the the event, passing the data to it's subscribers / callbacks
  70. **/
  71. function fire(eventName) {
  72. // fire events
  73. var args = slice(arguments, 1);
  74. setTimeout(function () {
  75. _fire_func(eventName, args);
  76. });
  77. }
  78. /**
  79. * onfire.fireSync( event[, data1 [,data2] ... ] )
  80. * - event (String): The event name to publish
  81. * - data...: The data to pass to subscribers / callbacks
  82. * Sync Publishes / fires the the event, passing the data to it's subscribers / callbacks
  83. **/
  84. function fireSync(eventName) {
  85. _fire_func(eventName, slice(arguments, 1));
  86. }
  87. /**
  88. * onfire.un( event ) -> Boolean
  89. * - event (String / Object): The message to publish
  90. * When passed a event Object, removes a specific subscription.
  91. * When passed event name String, removes all subscriptions for that event name(hierarchy)
  92. *
  93. * Unsubscribe / unbind an event or event object.
  94. *
  95. * Examples
  96. *
  97. * // Example 1 - unsubscribing with a event object
  98. * var event_object = onfire.on('my_event', myFunc);
  99. * onfire.un(event_object);
  100. *
  101. * // Example 2 - unsubscribing with a event name string
  102. * onfire.un('my_event');
  103. **/
  104. function un(event) {
  105. var eventName, key, r = false, type = typeof event;
  106. if (type === string_str) {
  107. // cancel the event name if exist
  108. if (hasOwnKey(__onfireEvents, event)) {
  109. delete __onfireEvents[event];
  110. return true;
  111. }
  112. return false;
  113. }
  114. else if (type === 'object') {
  115. eventName = event[0];
  116. key = event[1];
  117. if (hasOwnKey(__onfireEvents, eventName) && hasOwnKey(__onfireEvents[eventName], key)) {
  118. delete __onfireEvents[eventName][key];
  119. return true;
  120. }
  121. // can not find this event, return false
  122. return false;
  123. }
  124. else if (type === function_str) {
  125. _each(__onfireEvents, function(key_1, item_1) {
  126. _each(item_1, function(key_2, item_2) {
  127. if (item_2[0] === event) {
  128. delete __onfireEvents[key_1][key_2];
  129. r = true;
  130. }
  131. });
  132. });
  133. return r;
  134. }
  135. return true;
  136. }
  137. /**
  138. * onfire.clear()
  139. * Clears all subscriptions
  140. **/
  141. function clear() {
  142. __onfireEvents = {};
  143. }
  144. return {
  145. on: on,
  146. one: one,
  147. un: un,
  148. fire: fire,
  149. fireSync: fireSync,
  150. clear: clear,
  151. getGuid: getGuid
  152. };
  153. });