DeferredList.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojo.DeferredList"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojo.DeferredList"] = true;
  8. dojo.provide("dojo.DeferredList");
  9. dojo.DeferredList = function(/*Array*/ list, /*Boolean?*/ fireOnOneCallback, /*Boolean?*/ fireOnOneErrback, /*Boolean?*/ consumeErrors, /*Function?*/ canceller){
  10. // summary:
  11. // Provides event handling for a group of Deferred objects.
  12. // description:
  13. // DeferredList takes an array of existing deferreds and returns a new deferred of its own
  14. // this new deferred will typically have its callback fired when all of the deferreds in
  15. // the given list have fired their own deferreds. The parameters `fireOnOneCallback` and
  16. // fireOnOneErrback, will fire before all the deferreds as appropriate
  17. //
  18. // list:
  19. // The list of deferreds to be synchronizied with this DeferredList
  20. // fireOnOneCallback:
  21. // Will cause the DeferredLists callback to be fired as soon as any
  22. // of the deferreds in its list have been fired instead of waiting until
  23. // the entire list has finished
  24. // fireonOneErrback:
  25. // Will cause the errback to fire upon any of the deferreds errback
  26. // canceller:
  27. // A deferred canceller function, see dojo.Deferred
  28. var resultList = [];
  29. dojo.Deferred.call(this);
  30. var self = this;
  31. if(list.length === 0 && !fireOnOneCallback){
  32. this.resolve([0, []]);
  33. }
  34. var finished = 0;
  35. dojo.forEach(list, function(item, i){
  36. item.then(function(result){
  37. if(fireOnOneCallback){
  38. self.resolve([i, result]);
  39. }else{
  40. addResult(true, result);
  41. }
  42. },function(error){
  43. if(fireOnOneErrback){
  44. self.reject(error);
  45. }else{
  46. addResult(false, error);
  47. }
  48. if(consumeErrors){
  49. return null;
  50. }
  51. throw error;
  52. });
  53. function addResult(succeeded, result){
  54. resultList[i] = [succeeded, result];
  55. finished++;
  56. if(finished === list.length){
  57. self.resolve(resultList);
  58. }
  59. }
  60. });
  61. };
  62. dojo.DeferredList.prototype = new dojo.Deferred();
  63. dojo.DeferredList.prototype.gatherResults= function(deferredList){
  64. // summary:
  65. // Gathers the results of the deferreds for packaging
  66. // as the parameters to the Deferred Lists' callback
  67. var d = new dojo.DeferredList(deferredList, false, true, false);
  68. d.addCallback(function(results){
  69. var ret = [];
  70. dojo.forEach(results, function(result){
  71. ret.push(result[1]);
  72. });
  73. return ret;
  74. });
  75. return d;
  76. };
  77. }