onfire.js 4.9 KB

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