onfire.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. // console.log(args)
  70. setTimeout(function () {
  71. _fire_func(eventName, args);
  72. });
  73. }
  74. /**
  75. * onfire.fireSync( event[, data1 [,data2] ... ] )
  76. * - event (String): The event name to publish
  77. * - data...: The data to pass to subscribers / callbacks
  78. * Sync Publishes / fires the the event, passing the data to it's subscribers / callbacks
  79. **/
  80. function fireSync(eventName) {
  81. _fire_func(eventName, slice(arguments, 1));
  82. }
  83. /**
  84. * onfire.un( event ) -> Boolean
  85. * - event (String / Object): The message to publish
  86. * When passed a event Object, removes a specific subscription.
  87. * When passed event name String, removes all subscriptions for that event name(hierarchy)
  88. *
  89. * Unsubscribe / unbind an event or event object.
  90. *
  91. * Examples
  92. *
  93. * // Example 1 - unsubscribing with a event object
  94. * var event_object = onfire.on('my_event', myFunc);
  95. * onfire.un(event_object);
  96. *
  97. * // Example 2 - unsubscribing with a event name string
  98. * onfire.un('my_event');
  99. **/
  100. function un(event) {
  101. var eventName, key, r = false, type = typeof event;
  102. if (type === string_str) {
  103. // cancel the event name if exist
  104. if (hasOwnKey(__onfireEvents, event)) {
  105. delete __onfireEvents[event];
  106. return true;
  107. }
  108. return false;
  109. }
  110. else if (type === 'object') {
  111. eventName = event[0];
  112. key = event[1];
  113. if (hasOwnKey(__onfireEvents, eventName) && hasOwnKey(__onfireEvents[eventName], key)) {
  114. delete __onfireEvents[eventName][key];
  115. return true;
  116. }
  117. // can not find this event, return false
  118. return false;
  119. }
  120. else if (type === function_str) {
  121. _each(__onfireEvents, function(key_1, item_1) {
  122. _each(item_1, function(key_2, item_2) {
  123. if (item_2[0] === event) {
  124. delete __onfireEvents[key_1][key_2];
  125. r = true;
  126. }
  127. });
  128. });
  129. return r;
  130. }
  131. return true;
  132. }
  133. /**
  134. * onfire.clear()
  135. * Clears all subscriptions
  136. **/
  137. function clear() {
  138. __onfireEvents = {};
  139. }
  140. return {
  141. on: on,
  142. one: one,
  143. un: un,
  144. fire: fire,
  145. fireSync: fireSync,
  146. clear: clear
  147. };
  148. });