deferred.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. define([
  2. "./core",
  3. "./var/slice",
  4. "./callbacks"
  5. ], function( jQuery, slice ) {
  6. jQuery.extend({
  7. Deferred: function( func ) {
  8. var tuples = [
  9. // action, add listener, listener list, final state
  10. [ "resolve", "done", jQuery.Callbacks("once memory"), "resolved" ],
  11. [ "reject", "fail", jQuery.Callbacks("once memory"), "rejected" ],
  12. [ "notify", "progress", jQuery.Callbacks("memory") ]
  13. ],
  14. state = "pending",
  15. promise = {
  16. state: function() {
  17. return state;
  18. },
  19. always: function() {
  20. deferred.done( arguments ).fail( arguments );
  21. return this;
  22. },
  23. then: function( /* fnDone, fnFail, fnProgress */ ) {
  24. var fns = arguments;
  25. return jQuery.Deferred(function( newDefer ) {
  26. jQuery.each( tuples, function( i, tuple ) {
  27. var fn = jQuery.isFunction( fns[ i ] ) && fns[ i ];
  28. // deferred[ done | fail | progress ] for forwarding actions to newDefer
  29. deferred[ tuple[1] ](function() {
  30. var returned = fn && fn.apply( this, arguments );
  31. if ( returned && jQuery.isFunction( returned.promise ) ) {
  32. returned.promise()
  33. .done( newDefer.resolve )
  34. .fail( newDefer.reject )
  35. .progress( newDefer.notify );
  36. } else {
  37. newDefer[ tuple[ 0 ] + "With" ]( this === promise ? newDefer.promise() : this, fn ? [ returned ] : arguments );
  38. }
  39. });
  40. });
  41. fns = null;
  42. }).promise();
  43. },
  44. // Get a promise for this deferred
  45. // If obj is provided, the promise aspect is added to the object
  46. promise: function( obj ) {
  47. return obj != null ? jQuery.extend( obj, promise ) : promise;
  48. }
  49. },
  50. deferred = {};
  51. // Keep pipe for back-compat
  52. promise.pipe = promise.then;
  53. // Add list-specific methods
  54. jQuery.each( tuples, function( i, tuple ) {
  55. var list = tuple[ 2 ],
  56. stateString = tuple[ 3 ];
  57. // promise[ done | fail | progress ] = list.add
  58. promise[ tuple[1] ] = list.add;
  59. // Handle state
  60. if ( stateString ) {
  61. list.add(function() {
  62. // state = [ resolved | rejected ]
  63. state = stateString;
  64. // [ reject_list | resolve_list ].disable; progress_list.lock
  65. }, tuples[ i ^ 1 ][ 2 ].disable, tuples[ 2 ][ 2 ].lock );
  66. }
  67. // deferred[ resolve | reject | notify ]
  68. deferred[ tuple[0] ] = function() {
  69. deferred[ tuple[0] + "With" ]( this === deferred ? promise : this, arguments );
  70. return this;
  71. };
  72. deferred[ tuple[0] + "With" ] = list.fireWith;
  73. });
  74. // Make the deferred a promise
  75. promise.promise( deferred );
  76. // Call given func if any
  77. if ( func ) {
  78. func.call( deferred, deferred );
  79. }
  80. // All done!
  81. return deferred;
  82. },
  83. // Deferred helper
  84. when: function( subordinate /* , ..., subordinateN */ ) {
  85. var i = 0,
  86. resolveValues = slice.call( arguments ),
  87. length = resolveValues.length,
  88. // the count of uncompleted subordinates
  89. remaining = length !== 1 || ( subordinate && jQuery.isFunction( subordinate.promise ) ) ? length : 0,
  90. // the master Deferred. If resolveValues consist of only a single Deferred, just use that.
  91. deferred = remaining === 1 ? subordinate : jQuery.Deferred(),
  92. // Update function for both resolve and progress values
  93. updateFunc = function( i, contexts, values ) {
  94. return function( value ) {
  95. contexts[ i ] = this;
  96. values[ i ] = arguments.length > 1 ? slice.call( arguments ) : value;
  97. if ( values === progressValues ) {
  98. deferred.notifyWith( contexts, values );
  99. } else if ( !( --remaining ) ) {
  100. deferred.resolveWith( contexts, values );
  101. }
  102. };
  103. },
  104. progressValues, progressContexts, resolveContexts;
  105. // Add listeners to Deferred subordinates; treat others as resolved
  106. if ( length > 1 ) {
  107. progressValues = new Array( length );
  108. progressContexts = new Array( length );
  109. resolveContexts = new Array( length );
  110. for ( ; i < length; i++ ) {
  111. if ( resolveValues[ i ] && jQuery.isFunction( resolveValues[ i ].promise ) ) {
  112. resolveValues[ i ].promise()
  113. .done( updateFunc( i, resolveContexts, resolveValues ) )
  114. .fail( deferred.reject )
  115. .progress( updateFunc( i, progressContexts, progressValues ) );
  116. } else {
  117. --remaining;
  118. }
  119. }
  120. }
  121. // If we're not waiting on anything, resolve the master
  122. if ( !remaining ) {
  123. deferred.resolveWith( resolveContexts, resolveValues );
  124. }
  125. return deferred.promise();
  126. }
  127. });
  128. return jQuery;
  129. });