Node-Red configuration
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

callbacks.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. define( [
  2. "./core",
  3. "./core/toType",
  4. "./var/isFunction",
  5. "./var/rnothtmlwhite"
  6. ], function( jQuery, toType, isFunction, rnothtmlwhite ) {
  7. "use strict";
  8. // Convert String-formatted options into Object-formatted ones
  9. function createOptions( options ) {
  10. var object = {};
  11. jQuery.each( options.match( rnothtmlwhite ) || [], function( _, flag ) {
  12. object[ flag ] = true;
  13. } );
  14. return object;
  15. }
  16. /*
  17. * Create a callback list using the following parameters:
  18. *
  19. * options: an optional list of space-separated options that will change how
  20. * the callback list behaves or a more traditional option object
  21. *
  22. * By default a callback list will act like an event callback list and can be
  23. * "fired" multiple times.
  24. *
  25. * Possible options:
  26. *
  27. * once: will ensure the callback list can only be fired once (like a Deferred)
  28. *
  29. * memory: will keep track of previous values and will call any callback added
  30. * after the list has been fired right away with the latest "memorized"
  31. * values (like a Deferred)
  32. *
  33. * unique: will ensure a callback can only be added once (no duplicate in the list)
  34. *
  35. * stopOnFalse: interrupt callings when a callback returns false
  36. *
  37. */
  38. jQuery.Callbacks = function( options ) {
  39. // Convert options from String-formatted to Object-formatted if needed
  40. // (we check in cache first)
  41. options = typeof options === "string" ?
  42. createOptions( options ) :
  43. jQuery.extend( {}, options );
  44. var // Flag to know if list is currently firing
  45. firing,
  46. // Last fire value for non-forgettable lists
  47. memory,
  48. // Flag to know if list was already fired
  49. fired,
  50. // Flag to prevent firing
  51. locked,
  52. // Actual callback list
  53. list = [],
  54. // Queue of execution data for repeatable lists
  55. queue = [],
  56. // Index of currently firing callback (modified by add/remove as needed)
  57. firingIndex = -1,
  58. // Fire callbacks
  59. fire = function() {
  60. // Enforce single-firing
  61. locked = locked || options.once;
  62. // Execute callbacks for all pending executions,
  63. // respecting firingIndex overrides and runtime changes
  64. fired = firing = true;
  65. for ( ; queue.length; firingIndex = -1 ) {
  66. memory = queue.shift();
  67. while ( ++firingIndex < list.length ) {
  68. // Run callback and check for early termination
  69. if ( list[ firingIndex ].apply( memory[ 0 ], memory[ 1 ] ) === false &&
  70. options.stopOnFalse ) {
  71. // Jump to end and forget the data so .add doesn't re-fire
  72. firingIndex = list.length;
  73. memory = false;
  74. }
  75. }
  76. }
  77. // Forget the data if we're done with it
  78. if ( !options.memory ) {
  79. memory = false;
  80. }
  81. firing = false;
  82. // Clean up if we're done firing for good
  83. if ( locked ) {
  84. // Keep an empty list if we have data for future add calls
  85. if ( memory ) {
  86. list = [];
  87. // Otherwise, this object is spent
  88. } else {
  89. list = "";
  90. }
  91. }
  92. },
  93. // Actual Callbacks object
  94. self = {
  95. // Add a callback or a collection of callbacks to the list
  96. add: function() {
  97. if ( list ) {
  98. // If we have memory from a past run, we should fire after adding
  99. if ( memory && !firing ) {
  100. firingIndex = list.length - 1;
  101. queue.push( memory );
  102. }
  103. ( function add( args ) {
  104. jQuery.each( args, function( _, arg ) {
  105. if ( isFunction( arg ) ) {
  106. if ( !options.unique || !self.has( arg ) ) {
  107. list.push( arg );
  108. }
  109. } else if ( arg && arg.length && toType( arg ) !== "string" ) {
  110. // Inspect recursively
  111. add( arg );
  112. }
  113. } );
  114. } )( arguments );
  115. if ( memory && !firing ) {
  116. fire();
  117. }
  118. }
  119. return this;
  120. },
  121. // Remove a callback from the list
  122. remove: function() {
  123. jQuery.each( arguments, function( _, arg ) {
  124. var index;
  125. while ( ( index = jQuery.inArray( arg, list, index ) ) > -1 ) {
  126. list.splice( index, 1 );
  127. // Handle firing indexes
  128. if ( index <= firingIndex ) {
  129. firingIndex--;
  130. }
  131. }
  132. } );
  133. return this;
  134. },
  135. // Check if a given callback is in the list.
  136. // If no argument is given, return whether or not list has callbacks attached.
  137. has: function( fn ) {
  138. return fn ?
  139. jQuery.inArray( fn, list ) > -1 :
  140. list.length > 0;
  141. },
  142. // Remove all callbacks from the list
  143. empty: function() {
  144. if ( list ) {
  145. list = [];
  146. }
  147. return this;
  148. },
  149. // Disable .fire and .add
  150. // Abort any current/pending executions
  151. // Clear all callbacks and values
  152. disable: function() {
  153. locked = queue = [];
  154. list = memory = "";
  155. return this;
  156. },
  157. disabled: function() {
  158. return !list;
  159. },
  160. // Disable .fire
  161. // Also disable .add unless we have memory (since it would have no effect)
  162. // Abort any pending executions
  163. lock: function() {
  164. locked = queue = [];
  165. if ( !memory && !firing ) {
  166. list = memory = "";
  167. }
  168. return this;
  169. },
  170. locked: function() {
  171. return !!locked;
  172. },
  173. // Call all callbacks with the given context and arguments
  174. fireWith: function( context, args ) {
  175. if ( !locked ) {
  176. args = args || [];
  177. args = [ context, args.slice ? args.slice() : args ];
  178. queue.push( args );
  179. if ( !firing ) {
  180. fire();
  181. }
  182. }
  183. return this;
  184. },
  185. // Call all the callbacks with the given arguments
  186. fire: function() {
  187. self.fireWith( this, arguments );
  188. return this;
  189. },
  190. // To know if the callbacks have already been called at least once
  191. fired: function() {
  192. return !!fired;
  193. }
  194. };
  195. return self;
  196. };
  197. return jQuery;
  198. } );