promise-queue-plus.es6.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. /*!
  2. * promise-queue-plus v1.1.0
  3. * Homepage https://github.com/cnwhy/promise-queue-plus
  4. * License BSD-2-Clause
  5. */
  6. (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  7. (function (name, factory) {
  8. if (typeof define === 'function' && (define.amd || define.cmd)) {
  9. define([], factory);
  10. } else if (typeof window !== "undefined" || typeof self !== "undefined") {
  11. var global = typeof window !== "undefined" ? window : self;
  12. global[name] = factory();
  13. } else {
  14. throw new Error('Loading the "' + name + '" module failed!');
  15. }
  16. }('PromiseQueuePlus', function () {
  17. return require('../src/queue')(Promise);
  18. }));
  19. },{"../src/queue":4}],2:[function(require,module,exports){
  20. 'use strict';
  21. var utils = require('./utils')
  22. var isArray = utils.isArray
  23. ,isEmpty = utils.isEmpty
  24. ,isFunction = utils.isFunction
  25. ,isPlainObject = utils.isPlainObject
  26. ,arg2arr = utils.arg2arr
  27. function extendClass(Promise,obj,funnames){
  28. var QClass,source;
  29. if(obj){
  30. source = true
  31. QClass = obj;
  32. }else{
  33. QClass = Promise;
  34. }
  35. function asbind(name){
  36. if(isArray(funnames)){
  37. var nomark = false;
  38. for(var i = 0; i<funnames.length; i++){
  39. if(funnames[i] == name){
  40. nomark = true;
  41. break;
  42. }
  43. }
  44. if(!nomark) return false;
  45. }
  46. if(source){
  47. return !isFunction(QClass[name]);
  48. }
  49. return true;
  50. }
  51. if(!QClass.Promise && Promise != obj) QClass.Promise = Promise;
  52. //defer defer为最基础的实现
  53. if(isFunction(Promise) && isFunction(Promise.prototype.then)){
  54. QClass.defer = function() {
  55. var resolve, reject;
  56. var promise = new Promise(function(_resolve, _reject) {
  57. resolve = _resolve;
  58. reject = _reject;
  59. });
  60. return {
  61. promise: promise,
  62. resolve: resolve,
  63. reject: reject
  64. };
  65. }
  66. }else if(isFunction(Promise.defer)){
  67. QClass.defer = function(){return Promise.defer();}
  68. }else if(isFunction(Promise.deferred)){
  69. QClass.defer = function(){return Promise.deferred();}
  70. }else{
  71. throw new TypeError("此类不支持扩展!")
  72. }
  73. //delay
  74. if(asbind("delay")){
  75. QClass.delay = function(ms,value){
  76. var defer = QClass.defer();
  77. setTimeout(function(){
  78. //console.log('==========')
  79. defer.resolve(value);
  80. },ms)
  81. return defer.promise;
  82. }
  83. }
  84. //resolve
  85. if(asbind("resolve")){
  86. QClass.resolve = function(obj){
  87. var defer = QClass.defer();
  88. defer.resolve(obj);
  89. return defer.promise;
  90. }
  91. }
  92. //reject
  93. if(asbind("reject")){
  94. QClass.reject = function(obj){
  95. var defer = QClass.defer();
  96. defer.reject(obj);
  97. return defer.promise;
  98. }
  99. }
  100. function getall(map,count){
  101. if(!isEmpty(count)){
  102. count = +count > 0 ? +count : 0;
  103. }
  104. return function(promises) {
  105. var defer = QClass.defer();
  106. var data,_tempI = 0;
  107. var fillData = function(i){
  108. var _p = promises[i];
  109. QClass.resolve(_p).then(function(d) {
  110. if(typeof count != 'undefined'){
  111. data.push(d);
  112. }else{
  113. data[i] = d;
  114. }
  115. if (--_tempI == 0 || (!map && count && data.length>=count)) {
  116. defer.resolve(data);
  117. }
  118. }, function(err) {
  119. if (isEmpty(count)) {
  120. defer.reject(err);
  121. }else if(--_tempI == 0){
  122. defer.resolve(data);
  123. }
  124. })
  125. _tempI++;
  126. }
  127. if(isArray(promises)){
  128. data = [];
  129. if(promises.length == 0){defer.resolve(data)};
  130. for(var i = 0; i<promises.length; i++){
  131. fillData(i);
  132. }
  133. }else if(map && isPlainObject(promises)){
  134. var _mark = 0;
  135. data = {}
  136. for(var i in promises){
  137. fillData(i);
  138. _mark++;
  139. }
  140. if(_mark == 0) defer.resolve(data)
  141. }else{
  142. defer.reject(new TypeError("参数错误"));
  143. }
  144. return defer.promise;
  145. }
  146. }
  147. //all
  148. if(asbind("all")){
  149. QClass.all = getall()
  150. }
  151. if(asbind("allMap")){
  152. QClass.allMap = getall(true);
  153. }
  154. if(asbind("some")){
  155. QClass.some = function(proArr,count){
  156. count = +count >= 0 ? +count : 0;
  157. return getall(false,count)(proArr)
  158. }
  159. }
  160. //map
  161. if(asbind("map")){
  162. QClass.map = function(data,mapfun,options){
  163. var defer = QClass.defer();
  164. var promiseArr = [];
  165. var concurrency = options ? +options.concurrency : 0
  166. //无并发控制
  167. if(concurrency == 0 || concurrency != concurrency){
  168. for(var i in data){
  169. promiseArr.push(mapfun(data[i],i,data));
  170. }
  171. QClass.all(promiseArr).then(defer.resolve,defer.reject)
  172. return defer.promise;
  173. }
  174. var k = 0;
  175. var keys = (function(){
  176. var ks = [];
  177. for(var k in data){
  178. ks.push(k);
  179. }
  180. return ks;
  181. })();
  182. function next(){
  183. if(k<keys.length){
  184. var key = keys[k];
  185. var promise = QClass.resolve(mapfun(data[key],key,data)).then(function(v){
  186. next();
  187. return v;
  188. },defer.reject);
  189. promiseArr.push(promise);
  190. concurrency--;
  191. k++;
  192. }else{
  193. QClass.all(promiseArr).then(defer.resolve,defer.reject);
  194. }
  195. }
  196. do{
  197. next()
  198. }while(concurrency>0 && k<keys.length)
  199. return defer.promise
  200. }
  201. }
  202. function race(proArr) {
  203. var defer = QClass.defer();
  204. for (var i = 0; i < proArr.length; i++) {
  205. (function() {
  206. var _i = i;
  207. var _p = proArr[_i];
  208. QClass.resolve(_p).then(function(data) {
  209. defer.resolve(data);
  210. }, function(err) {
  211. defer.reject(err);
  212. })
  213. })()
  214. }
  215. return defer.promise;
  216. }
  217. //any | race
  218. if(asbind("race")){
  219. QClass.race = race;
  220. }
  221. if(asbind("any")){
  222. QClass.any = race;
  223. }
  224. /*封装CPS*/
  225. //callback Adapter
  226. function cbAdapter(defer){
  227. return function(err,data){
  228. if(err) return defer.reject(err);
  229. defer.resolve(data)
  230. }
  231. }
  232. function nfcall(f){
  233. var _this = this === QClass ? null : this;
  234. var defer = QClass.defer();
  235. var argsArray = arg2arr(arguments,1)
  236. argsArray.push(cbAdapter(defer))
  237. f.apply(_this,argsArray)
  238. return defer.promise;
  239. }
  240. if(asbind("nfcall")){
  241. QClass.nfcall = nfcall;
  242. }
  243. if(asbind("nfapply")){
  244. QClass.nfapply = function(f,args){
  245. var _this = this === QClass ? null : this;
  246. var defer = QClass.defer();
  247. if(isArray(args)){
  248. args.push(cbAdapter(defer));
  249. f.apply(_this,args)
  250. }else{
  251. throw TypeError('"args" is not Array')
  252. }
  253. return defer.promise;
  254. }
  255. }
  256. QClass.denodeify = function(f){
  257. var _this = this === QClass ? null : this;
  258. return function(){
  259. return nfcall.apply(_this,[].concat([f],arg2arr(arguments)))
  260. }
  261. }
  262. return QClass;
  263. }
  264. module.exports = extendClass;
  265. },{"./utils":3}],3:[function(require,module,exports){
  266. 'use strict';
  267. exports.isPlainObject = function(obj) {
  268. if (obj === null || typeof(obj) !== "object" || obj.nodeType || (obj === obj.window)) {
  269. return false;
  270. }
  271. if (obj.constructor && !Object.prototype.hasOwnProperty.call(obj.constructor.prototype, "isPrototypeOf")) {
  272. return false;
  273. }
  274. return true;
  275. }
  276. exports.isArray = function(obj){
  277. return Object.prototype.toString.call(obj) == "[object Array]"
  278. }
  279. exports.isFunction = function(obj){
  280. return typeof obj == "function"
  281. }
  282. exports.isEmpty = function(obj){
  283. return typeof obj == 'undefined' || obj === null;
  284. }
  285. exports.arg2arr = function(arg,b,s){
  286. return Array.prototype.slice.call(arg,b,s);
  287. }
  288. },{}],4:[function(require,module,exports){
  289. "use strict";
  290. var epc = require("extend-promise/src/extendClass");
  291. var utils = require("./utils");
  292. function use(Promise){
  293. var _Promise;
  294. setPromise(Promise);
  295. var ONERROR = function(err){
  296. try{
  297. console.error(err);
  298. }catch(e){}
  299. };
  300. function setPromise(Promise){
  301. _Promise = Queue.Q = Queue.Promise = epc(Promise,{});
  302. };
  303. function maxFormat(max){
  304. var _max = (+max)>>0;
  305. if(_max > 0){
  306. return _max;
  307. }else{
  308. throw new Error('The "max" value is invalid')
  309. }
  310. }
  311. function toPromise(fn){
  312. try{
  313. return _Promise.resolve(fn());
  314. }catch(e){
  315. return _Promise.reject(e);
  316. }
  317. }
  318. /**
  319. * 队列类
  320. * @param {Number} max 队列最大并行数
  321. * @param {Number} options 队列其他配置
  322. */
  323. function Queue(max,options) {
  324. var self = this;
  325. // var def = {
  326. // "event_queue_begin":null //队列开始
  327. // ,"event_queue_end":null //队列完成
  328. // ,"event_queue_add":null //有执行项添加进执行单元后执行
  329. // ,"event_item_resolve":null //成功
  330. // ,"event_item_reject":null //失败
  331. // ,"event_item_finally":null //一个执行单元结束后
  332. // ,"retry":0 //执行单元出错重试次数
  333. // ,"retry_type":0 //重试模式 0/false:搁置执行(插入队列尾部重试),1/true:优先执行 (插入队列头部重试)
  334. // ,"timeout":0 //执行单元超时时间(毫秒)
  335. // }
  336. var def = {
  337. "queueStart" : null //队列开始
  338. ,"queueEnd" : null //队列完成
  339. ,"workAdd" : null //有执行项添加进执行单元后执行
  340. ,"workResolve": null //成功
  341. ,"workReject" : null //失败
  342. ,"workFinally": null //一个执行单元结束后
  343. ,"retry" : 0 //执行单元出错重试次数
  344. ,"retryIsJump": false //重试模式 false:搁置执行(插入队列尾部重试),true:优先执行 (插入队列头部重试)
  345. ,"timeout" : 0 //执行单元超时时间(毫秒)
  346. }
  347. var _queue = [];
  348. var _max = maxFormat(max);
  349. var _runCount = 0;
  350. var _isStart = false;
  351. var _isStop = 0;
  352. this._options = def
  353. this.onError = ONERROR;
  354. if(utils.isObject(options)){
  355. for(var i in options){
  356. if(def.hasOwnProperty(i)) def[i] = options[i]
  357. }
  358. }
  359. //最大并行数
  360. this.getMax = function(){
  361. return _max;
  362. }
  363. this.setMax = function(max){
  364. try{
  365. _max = maxFormat(max);
  366. if(!_isStop && _runCount) self.start();
  367. }catch(e){
  368. onError.call(self,e)
  369. }
  370. }
  371. //正在排队的项数
  372. this.getLength = function(){
  373. return _queue.length;
  374. }
  375. //正在运行的项数
  376. this.getRunCount = function(){
  377. return _runCount;
  378. }
  379. //队列是否已开始运行
  380. this.isStart = function(){
  381. return !!_isStart;
  382. }
  383. /**
  384. * 向队列插入执行单元
  385. * @param {queueUnit} unit 执行单元对像
  386. * @param {bool} stack 是否以栈模式(后进先出)插入
  387. * @param {bool} start 是否启动队列
  388. * @param {bool} noAdd 是否调用队列workAdd方法 (重试模式不调用需要)
  389. */
  390. this._addItem = function(unit,stack,start,noAdd){
  391. if(!(unit instanceof QueueUnit)) throw new TypeError('"unit" is not QueueUnit')
  392. if(stack){
  393. _queue.unshift(unit);
  394. }else{
  395. _queue.push(unit);
  396. }
  397. noAdd || runAddEvent.call(self,unit);
  398. if(start){
  399. self.start();
  400. }else{
  401. _isStart && queueRun();
  402. }
  403. }
  404. //执行下一项
  405. function next(){
  406. if(_runCount < _max && !_isStop && _queue.length > 0){
  407. var unit = _queue.shift()
  408. //if(unit){
  409. var xc_timeout
  410. ,_mark=0
  411. var timeout = +getOption('timeout',unit,self)
  412. ,retryNo = getOption('retry',unit,self)
  413. ,retryType = getOption('retryIsJump',unit,self)
  414. ,_self = unit._options.self
  415. var fix = function(){
  416. if(xc_timeout) clearTimeout(xc_timeout)
  417. xc_timeout = 0;
  418. if(_mark++) return true;
  419. _runCount--;
  420. }
  421. var afinally = function(){
  422. autoRun(unit,self,'workFinally',self,self,unit)
  423. // if(runEvent.call(unit,'workFinally',self,self,unit) !== false){
  424. // onoff && runEvent.call(self,'workFinally',self,self,unit);
  425. // }
  426. }
  427. var issucc = function(data){
  428. if(fix()) return;
  429. unit.defer.resolve(data); //通知执行单元,成功
  430. autoRun(unit,self,'workResolve',self,data,self,unit)
  431. // if(runEvent.call(unit,'workResolve',self,data,self,unit) !== false){
  432. // onoff && runEvent.call(self,'workResolve',self,data,self,unit);
  433. // }
  434. afinally();
  435. }
  436. var iserr = function(err){
  437. if(fix()) return;
  438. if(retryNo > unit._errNo++){
  439. self._addItem(unit,retryType,true,false)
  440. }else{
  441. unit.defer.reject(err); //通知执行单元,失败
  442. autoRun(unit,self,'workReject',self,err,self,unit)
  443. // if(runEvent.call(unit,'workReject',self,err,self,unit) !== false){
  444. // onoff && runEvent.call(self,'workReject',self,err,self,unit);
  445. // }
  446. }
  447. afinally();
  448. };
  449. //队列开始执行事件
  450. if(_runCount == 0 && !_isStart){
  451. _isStart = true;
  452. runEvent.call(self,'queueStart',self,self);
  453. }
  454. var nextp = toPromise(function(){
  455. return unit.fn.apply((_self || null),unit.regs)
  456. }).then(issucc,iserr).then(function(){
  457. if(_queue.length>0){
  458. queueRun();
  459. }else if(_runCount == 0 && _isStart){//队列结束执行事件
  460. _isStart = false;
  461. runEvent.call(self,'queueEnd',self,self);
  462. }
  463. });
  464. _runCount += 1;
  465. //nextp.then(defer.resolve,defer.reject)
  466. if(timeout > 0){
  467. xc_timeout = setTimeout(function(){
  468. iserr("timeout")
  469. },timeout)
  470. }
  471. //return;
  472. //}
  473. return;
  474. }
  475. return true;
  476. }
  477. function queueRun(){
  478. while(!next()){}
  479. // if(_isStop) return;
  480. // do{
  481. // next();
  482. // }while(_queue.length && _runCount < _max)
  483. }
  484. /**队列控制**/
  485. //开始执行队列
  486. this.start = function(){
  487. _isStop = 0;
  488. queueRun();
  489. }
  490. this.stop = function(){
  491. //console.log('on stop')
  492. _isStop = 1;
  493. }
  494. //清空执行队列
  495. this.clear = function(err){
  496. while(_queue.length){
  497. var unit = _queue.shift();
  498. unit.defer.reject(err);
  499. }
  500. }
  501. }
  502. /**
  503. * 队列执行单类
  504. * @param {Function} fn 运行函数
  505. * @param {Array} args 元行函数的参数,可省略
  506. * @param {Object} options 其他配置
  507. */
  508. function QueueUnit(fn, args, options){
  509. var def = {
  510. 'workResolve' : true
  511. ,'workReject' : true
  512. ,'workFinally' : true
  513. ,'queueEventTrigger' : true
  514. ,'regs':[]
  515. ,'self':null
  516. }
  517. var oNames = [
  518. 'workResolve' //是否执行队列workResolve事件
  519. ,'workReject' //是否执行队列workReject事件
  520. ,'workFinally' //是否执行队列workFinally事件
  521. ,'queueEventTrigger' //队列事件开关
  522. ,'retry' //重试次数
  523. ,'retryIsJump' //重试模式
  524. ,'timeout' //超时
  525. ,'self' //运行函数self
  526. ];
  527. var oi = 1;
  528. if(!utils.isFunction(fn)){
  529. throw new TypeError("Queues only support function, '" + fn + "' is not function")
  530. }
  531. this.fn = fn;
  532. this._errNo = 0;
  533. this.defer = _Promise.defer();
  534. if(utils.isArray(args)){
  535. this.regs = args;
  536. oi++;
  537. }
  538. function inOptions(name){
  539. for(var i = 0; i<oNames.length; i++){
  540. if(name === oNames[i]) return true;
  541. }
  542. return false;
  543. }
  544. this._options = def;
  545. var configObj = arguments[oi];
  546. //console.log(configObj);
  547. if(utils.isObject(configObj)){
  548. for(var i in configObj){
  549. if(inOptions(i)){
  550. def[i] = configObj[i];
  551. }
  552. }
  553. }
  554. }
  555. function getOption(name,qobj,queue){
  556. if(name in qobj._options){
  557. return qobj._options[name];
  558. }else{
  559. return queue._options[name];
  560. }
  561. }
  562. function runEvent(eventName,self){
  563. var event = this._options[eventName]
  564. ,arg = utils.arg2arr(arguments,2);
  565. if(utils.isFunction(event)){
  566. try{
  567. return event.apply(self,arg)
  568. }catch(e){
  569. onError.call(self,e);
  570. }
  571. }else{
  572. return !!event;
  573. }
  574. }
  575. function autoRun(unit,queue){
  576. var onoff = unit._options.queueEventTrigger;
  577. var args = utils.arg2arr(arguments,2);
  578. if(runEvent.apply(unit,args) !== false){
  579. onoff && runEvent.apply(queue,args);
  580. }
  581. }
  582. function runAddEvent(unit){
  583. runEvent.call(this,'workAdd',this,unit,this);
  584. }
  585. //构建执行单元对象
  586. function getQueueUnit(fn,args,options){
  587. // try{
  588. return new QueueUnit(fn,args,options);
  589. // }catch(e){
  590. // if(utils.isFunction(this.onError)){
  591. // this.onError(e)
  592. // }
  593. // }
  594. }
  595. function onError(err){
  596. if(utils.isFunction(this.onError)){
  597. this.onError.call(this,err)
  598. }
  599. }
  600. function getAddArgs(data,fn,con,each){
  601. var isArray = utils.isArray(data);
  602. var rdata = isArray ? [] : {};
  603. function fill(k){
  604. var args = each ? [].concat([data[k]],[k],[data]) : [].concat(data[k]);
  605. rdata[k] = [fn,args,con];
  606. }
  607. if(isArray){
  608. for(var i=0; i<data.length; i++){
  609. fill(i);
  610. }
  611. }else{
  612. for(var k in data){
  613. fill(k);
  614. }
  615. }
  616. return rdata;
  617. }
  618. function getBatchArgs(array,fn,con){
  619. var baseN = 2,_con,start,jump;
  620. if(utils.isObject(con)){
  621. _con = con;
  622. baseN++;
  623. }
  624. return {
  625. con : _con,
  626. start : arguments[baseN],
  627. jump : arguments[++baseN]
  628. }
  629. }
  630. function AddBatch(data,fn){
  631. var queue = this.queue
  632. ,map = this.map
  633. ,each = this.each
  634. var addArgs;
  635. var args = getBatchArgs.apply(null,arguments)
  636. addArgs = getAddArgs(data,fn,args.con,each)
  637. if(map){
  638. return queue.addProps(addArgs,args.start,args.jump);
  639. }else{
  640. return queue.addArray(addArgs,args.start,args.jump);
  641. }
  642. }
  643. Queue.prototype = {
  644. //获取/设置配置
  645. option: function(name){
  646. if(arguments.length == 1){
  647. return this._options[name];
  648. }else if(arguments.length > 1){
  649. this._options[name] = arguments[1]
  650. }
  651. }
  652. //向队列尾部增加执行项,若队列未启动,暂时不会被执行
  653. ,'push' : function(){
  654. var o = this , unit = getQueueUnit.apply(o,arguments);
  655. o._addItem(unit,false);
  656. return unit.defer.promise;
  657. }
  658. //向队列头部增加执行项,若队列未启动,暂时不会被执行
  659. ,'unshift': function(){
  660. var o = this , unit = getQueueUnit.apply(o,arguments);
  661. o._addItem(unit,true);
  662. return unit.defer.promise;
  663. }
  664. //添加执行项,并会启动队列
  665. ,go: function(){
  666. var o = this , unit = getQueueUnit.apply(o,arguments);
  667. o._addItem(unit,false,true);
  668. return unit.defer.promise;
  669. }
  670. //在队列头部插入并执行项
  671. ,jump: function(){
  672. var o = this , unit = getQueueUnit.apply(o,arguments);
  673. o._addItem(unit,true,true);
  674. return unit.defer.promise;
  675. }
  676. ,add: function(fn,options){//fn,*options*,*start*,*jump*
  677. var o = this, _fun, _i = 1, unitArgs, start, jump, promise;
  678. if(!utils.isFunction(fn)) throw new TypeError("Queues only support function, '" + fn + "' is not function")
  679. _fun = function(){
  680. var defer = _Promise.defer();
  681. fn(defer.resolve,defer.reject);
  682. return defer.promise
  683. }
  684. unitArgs = [_fun]
  685. if(utils.isObject(options)){
  686. unitArgs.push(options);
  687. _i++;
  688. }
  689. start = !!arguments[_i]
  690. jump = !!arguments[_i+1];
  691. promise = jump ? o.unshift.apply(o,unitArgs) : o.push.apply(o,unitArgs);
  692. if(start) o.start();
  693. return promise;
  694. }
  695. ,addArray: function(array,start,jump){
  696. var parrs = [];
  697. var o = this;
  698. for(var i = 0;i<array.length;i++){
  699. +function(){
  700. var _i = i;
  701. var unitArgs = array[_i];
  702. var _p = jump ? o.unshift.apply(o,unitArgs) : o.push.apply(o,unitArgs);
  703. parrs.push(_p);
  704. }()
  705. }
  706. var nextP = _Promise.defer();
  707. _Promise.all(parrs).then(function(data){nextP.resolve(data)},function(err){nextP.reject(err)})
  708. if(start) o.start();
  709. return nextP.promise;
  710. }
  711. ,addProps: function(props,start,jump){
  712. var parrs = {};
  713. var o = this;
  714. for(var k in props){
  715. +function(){
  716. var _k = k;
  717. var unitArgs = props[_k];
  718. var _p = jump ? o.unshift.apply(o,unitArgs) : o.push.apply(o,unitArgs);
  719. parrs[_k] = _p;
  720. }()
  721. }
  722. var nextP = _Promise.defer();
  723. _Promise.allMap(parrs).then(function(data){nextP.resolve(data)},function(err){nextP.reject(err)})
  724. if(start) o.start();
  725. return nextP.promise;
  726. }
  727. ,addLikeArray: function(array,fn,con){
  728. return AddBatch.apply({queue:this},arguments);
  729. }
  730. ,addLikeProps: function(props,fn,con){
  731. return AddBatch.apply({queue:this,map:true},arguments);
  732. }
  733. ,addLikeArrayEach: function(array,fn,con){
  734. return AddBatch.apply({queue:this,each:true},arguments);
  735. }
  736. ,addLikePropsEach: function(array,fn,con){
  737. return AddBatch.apply({queue:this,each:true,map:true},arguments);
  738. }
  739. };
  740. Queue.use = setPromise;
  741. Queue.createUse = use;
  742. return Queue;
  743. };
  744. module.exports = use;
  745. },{"./utils":5,"extend-promise/src/extendClass":2}],5:[function(require,module,exports){
  746. 'use strict';
  747. exports.isArray = function(obj){
  748. return Object.prototype.toString.call(obj) == "[object Array]"
  749. }
  750. exports.isFunction = function(obj){
  751. return typeof obj === "function"
  752. }
  753. exports.isObject = function(obj){
  754. return typeof obj === "object" && obj !== null
  755. }
  756. exports.arg2arr = function(arg,b,s){
  757. return Array.prototype.slice.call(arg,b,s);
  758. }
  759. },{}]},{},[1])