bluebird.core.js 118 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810
  1. /* @preserve
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2013-2018 Petka Antonov
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. *
  24. */
  25. /**
  26. * bluebird build version 3.5.4
  27. * Features enabled: core
  28. * Features disabled: race, call_get, generators, map, nodeify, promisify, props, reduce, settle, some, using, timers, filter, any, each
  29. */
  30. !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.Promise=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof _dereq_=="function"&&_dereq_;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof _dereq_=="function"&&_dereq_;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
  31. "use strict";
  32. var firstLineError;
  33. try {throw new Error(); } catch (e) {firstLineError = e;}
  34. var schedule = _dereq_("./schedule");
  35. var Queue = _dereq_("./queue");
  36. var util = _dereq_("./util");
  37. function Async() {
  38. this._customScheduler = false;
  39. this._isTickUsed = false;
  40. this._lateQueue = new Queue(16);
  41. this._normalQueue = new Queue(16);
  42. this._haveDrainedQueues = false;
  43. this._trampolineEnabled = true;
  44. var self = this;
  45. this.drainQueues = function () {
  46. self._drainQueues();
  47. };
  48. this._schedule = schedule;
  49. }
  50. Async.prototype.setScheduler = function(fn) {
  51. var prev = this._schedule;
  52. this._schedule = fn;
  53. this._customScheduler = true;
  54. return prev;
  55. };
  56. Async.prototype.hasCustomScheduler = function() {
  57. return this._customScheduler;
  58. };
  59. Async.prototype.enableTrampoline = function() {
  60. this._trampolineEnabled = true;
  61. };
  62. Async.prototype.disableTrampolineIfNecessary = function() {
  63. if (util.hasDevTools) {
  64. this._trampolineEnabled = false;
  65. }
  66. };
  67. Async.prototype.haveItemsQueued = function () {
  68. return this._isTickUsed || this._haveDrainedQueues;
  69. };
  70. Async.prototype.fatalError = function(e, isNode) {
  71. if (isNode) {
  72. process.stderr.write("Fatal " + (e instanceof Error ? e.stack : e) +
  73. "\n");
  74. process.exit(2);
  75. } else {
  76. this.throwLater(e);
  77. }
  78. };
  79. Async.prototype.throwLater = function(fn, arg) {
  80. if (arguments.length === 1) {
  81. arg = fn;
  82. fn = function () { throw arg; };
  83. }
  84. if (typeof setTimeout !== "undefined") {
  85. setTimeout(function() {
  86. fn(arg);
  87. }, 0);
  88. } else try {
  89. this._schedule(function() {
  90. fn(arg);
  91. });
  92. } catch (e) {
  93. throw new Error("No async scheduler available\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  94. }
  95. };
  96. function AsyncInvokeLater(fn, receiver, arg) {
  97. this._lateQueue.push(fn, receiver, arg);
  98. this._queueTick();
  99. }
  100. function AsyncInvoke(fn, receiver, arg) {
  101. this._normalQueue.push(fn, receiver, arg);
  102. this._queueTick();
  103. }
  104. function AsyncSettlePromises(promise) {
  105. this._normalQueue._pushOne(promise);
  106. this._queueTick();
  107. }
  108. if (!util.hasDevTools) {
  109. Async.prototype.invokeLater = AsyncInvokeLater;
  110. Async.prototype.invoke = AsyncInvoke;
  111. Async.prototype.settlePromises = AsyncSettlePromises;
  112. } else {
  113. Async.prototype.invokeLater = function (fn, receiver, arg) {
  114. if (this._trampolineEnabled) {
  115. AsyncInvokeLater.call(this, fn, receiver, arg);
  116. } else {
  117. this._schedule(function() {
  118. setTimeout(function() {
  119. fn.call(receiver, arg);
  120. }, 100);
  121. });
  122. }
  123. };
  124. Async.prototype.invoke = function (fn, receiver, arg) {
  125. if (this._trampolineEnabled) {
  126. AsyncInvoke.call(this, fn, receiver, arg);
  127. } else {
  128. this._schedule(function() {
  129. fn.call(receiver, arg);
  130. });
  131. }
  132. };
  133. Async.prototype.settlePromises = function(promise) {
  134. if (this._trampolineEnabled) {
  135. AsyncSettlePromises.call(this, promise);
  136. } else {
  137. this._schedule(function() {
  138. promise._settlePromises();
  139. });
  140. }
  141. };
  142. }
  143. function _drainQueue(queue) {
  144. while (queue.length() > 0) {
  145. _drainQueueStep(queue);
  146. }
  147. }
  148. function _drainQueueStep(queue) {
  149. var fn = queue.shift();
  150. if (typeof fn !== "function") {
  151. fn._settlePromises();
  152. } else {
  153. var receiver = queue.shift();
  154. var arg = queue.shift();
  155. fn.call(receiver, arg);
  156. }
  157. }
  158. Async.prototype._drainQueues = function () {
  159. _drainQueue(this._normalQueue);
  160. this._reset();
  161. this._haveDrainedQueues = true;
  162. _drainQueue(this._lateQueue);
  163. };
  164. Async.prototype._queueTick = function () {
  165. if (!this._isTickUsed) {
  166. this._isTickUsed = true;
  167. this._schedule(this.drainQueues);
  168. }
  169. };
  170. Async.prototype._reset = function () {
  171. this._isTickUsed = false;
  172. };
  173. module.exports = Async;
  174. module.exports.firstLineError = firstLineError;
  175. },{"./queue":17,"./schedule":18,"./util":21}],2:[function(_dereq_,module,exports){
  176. "use strict";
  177. module.exports = function(Promise, INTERNAL, tryConvertToPromise, debug) {
  178. var calledBind = false;
  179. var rejectThis = function(_, e) {
  180. this._reject(e);
  181. };
  182. var targetRejected = function(e, context) {
  183. context.promiseRejectionQueued = true;
  184. context.bindingPromise._then(rejectThis, rejectThis, null, this, e);
  185. };
  186. var bindingResolved = function(thisArg, context) {
  187. if (((this._bitField & 50397184) === 0)) {
  188. this._resolveCallback(context.target);
  189. }
  190. };
  191. var bindingRejected = function(e, context) {
  192. if (!context.promiseRejectionQueued) this._reject(e);
  193. };
  194. Promise.prototype.bind = function (thisArg) {
  195. if (!calledBind) {
  196. calledBind = true;
  197. Promise.prototype._propagateFrom = debug.propagateFromFunction();
  198. Promise.prototype._boundValue = debug.boundValueFunction();
  199. }
  200. var maybePromise = tryConvertToPromise(thisArg);
  201. var ret = new Promise(INTERNAL);
  202. ret._propagateFrom(this, 1);
  203. var target = this._target();
  204. ret._setBoundTo(maybePromise);
  205. if (maybePromise instanceof Promise) {
  206. var context = {
  207. promiseRejectionQueued: false,
  208. promise: ret,
  209. target: target,
  210. bindingPromise: maybePromise
  211. };
  212. target._then(INTERNAL, targetRejected, undefined, ret, context);
  213. maybePromise._then(
  214. bindingResolved, bindingRejected, undefined, ret, context);
  215. ret._setOnCancel(maybePromise);
  216. } else {
  217. ret._resolveCallback(target);
  218. }
  219. return ret;
  220. };
  221. Promise.prototype._setBoundTo = function (obj) {
  222. if (obj !== undefined) {
  223. this._bitField = this._bitField | 2097152;
  224. this._boundTo = obj;
  225. } else {
  226. this._bitField = this._bitField & (~2097152);
  227. }
  228. };
  229. Promise.prototype._isBound = function () {
  230. return (this._bitField & 2097152) === 2097152;
  231. };
  232. Promise.bind = function (thisArg, value) {
  233. return Promise.resolve(value).bind(thisArg);
  234. };
  235. };
  236. },{}],3:[function(_dereq_,module,exports){
  237. "use strict";
  238. var old;
  239. if (typeof Promise !== "undefined") old = Promise;
  240. function noConflict() {
  241. try { if (Promise === bluebird) Promise = old; }
  242. catch (e) {}
  243. return bluebird;
  244. }
  245. var bluebird = _dereq_("./promise")();
  246. bluebird.noConflict = noConflict;
  247. module.exports = bluebird;
  248. },{"./promise":15}],4:[function(_dereq_,module,exports){
  249. "use strict";
  250. module.exports = function(Promise, PromiseArray, apiRejection, debug) {
  251. var util = _dereq_("./util");
  252. var tryCatch = util.tryCatch;
  253. var errorObj = util.errorObj;
  254. var async = Promise._async;
  255. Promise.prototype["break"] = Promise.prototype.cancel = function() {
  256. if (!debug.cancellation()) return this._warn("cancellation is disabled");
  257. var promise = this;
  258. var child = promise;
  259. while (promise._isCancellable()) {
  260. if (!promise._cancelBy(child)) {
  261. if (child._isFollowing()) {
  262. child._followee().cancel();
  263. } else {
  264. child._cancelBranched();
  265. }
  266. break;
  267. }
  268. var parent = promise._cancellationParent;
  269. if (parent == null || !parent._isCancellable()) {
  270. if (promise._isFollowing()) {
  271. promise._followee().cancel();
  272. } else {
  273. promise._cancelBranched();
  274. }
  275. break;
  276. } else {
  277. if (promise._isFollowing()) promise._followee().cancel();
  278. promise._setWillBeCancelled();
  279. child = promise;
  280. promise = parent;
  281. }
  282. }
  283. };
  284. Promise.prototype._branchHasCancelled = function() {
  285. this._branchesRemainingToCancel--;
  286. };
  287. Promise.prototype._enoughBranchesHaveCancelled = function() {
  288. return this._branchesRemainingToCancel === undefined ||
  289. this._branchesRemainingToCancel <= 0;
  290. };
  291. Promise.prototype._cancelBy = function(canceller) {
  292. if (canceller === this) {
  293. this._branchesRemainingToCancel = 0;
  294. this._invokeOnCancel();
  295. return true;
  296. } else {
  297. this._branchHasCancelled();
  298. if (this._enoughBranchesHaveCancelled()) {
  299. this._invokeOnCancel();
  300. return true;
  301. }
  302. }
  303. return false;
  304. };
  305. Promise.prototype._cancelBranched = function() {
  306. if (this._enoughBranchesHaveCancelled()) {
  307. this._cancel();
  308. }
  309. };
  310. Promise.prototype._cancel = function() {
  311. if (!this._isCancellable()) return;
  312. this._setCancelled();
  313. async.invoke(this._cancelPromises, this, undefined);
  314. };
  315. Promise.prototype._cancelPromises = function() {
  316. if (this._length() > 0) this._settlePromises();
  317. };
  318. Promise.prototype._unsetOnCancel = function() {
  319. this._onCancelField = undefined;
  320. };
  321. Promise.prototype._isCancellable = function() {
  322. return this.isPending() && !this._isCancelled();
  323. };
  324. Promise.prototype.isCancellable = function() {
  325. return this.isPending() && !this.isCancelled();
  326. };
  327. Promise.prototype._doInvokeOnCancel = function(onCancelCallback, internalOnly) {
  328. if (util.isArray(onCancelCallback)) {
  329. for (var i = 0; i < onCancelCallback.length; ++i) {
  330. this._doInvokeOnCancel(onCancelCallback[i], internalOnly);
  331. }
  332. } else if (onCancelCallback !== undefined) {
  333. if (typeof onCancelCallback === "function") {
  334. if (!internalOnly) {
  335. var e = tryCatch(onCancelCallback).call(this._boundValue());
  336. if (e === errorObj) {
  337. this._attachExtraTrace(e.e);
  338. async.throwLater(e.e);
  339. }
  340. }
  341. } else {
  342. onCancelCallback._resultCancelled(this);
  343. }
  344. }
  345. };
  346. Promise.prototype._invokeOnCancel = function() {
  347. var onCancelCallback = this._onCancel();
  348. this._unsetOnCancel();
  349. async.invoke(this._doInvokeOnCancel, this, onCancelCallback);
  350. };
  351. Promise.prototype._invokeInternalOnCancel = function() {
  352. if (this._isCancellable()) {
  353. this._doInvokeOnCancel(this._onCancel(), true);
  354. this._unsetOnCancel();
  355. }
  356. };
  357. Promise.prototype._resultCancelled = function() {
  358. this.cancel();
  359. };
  360. };
  361. },{"./util":21}],5:[function(_dereq_,module,exports){
  362. "use strict";
  363. module.exports = function(NEXT_FILTER) {
  364. var util = _dereq_("./util");
  365. var getKeys = _dereq_("./es5").keys;
  366. var tryCatch = util.tryCatch;
  367. var errorObj = util.errorObj;
  368. function catchFilter(instances, cb, promise) {
  369. return function(e) {
  370. var boundTo = promise._boundValue();
  371. predicateLoop: for (var i = 0; i < instances.length; ++i) {
  372. var item = instances[i];
  373. if (item === Error ||
  374. (item != null && item.prototype instanceof Error)) {
  375. if (e instanceof item) {
  376. return tryCatch(cb).call(boundTo, e);
  377. }
  378. } else if (typeof item === "function") {
  379. var matchesPredicate = tryCatch(item).call(boundTo, e);
  380. if (matchesPredicate === errorObj) {
  381. return matchesPredicate;
  382. } else if (matchesPredicate) {
  383. return tryCatch(cb).call(boundTo, e);
  384. }
  385. } else if (util.isObject(e)) {
  386. var keys = getKeys(item);
  387. for (var j = 0; j < keys.length; ++j) {
  388. var key = keys[j];
  389. if (item[key] != e[key]) {
  390. continue predicateLoop;
  391. }
  392. }
  393. return tryCatch(cb).call(boundTo, e);
  394. }
  395. }
  396. return NEXT_FILTER;
  397. };
  398. }
  399. return catchFilter;
  400. };
  401. },{"./es5":10,"./util":21}],6:[function(_dereq_,module,exports){
  402. "use strict";
  403. module.exports = function(Promise) {
  404. var longStackTraces = false;
  405. var contextStack = [];
  406. Promise.prototype._promiseCreated = function() {};
  407. Promise.prototype._pushContext = function() {};
  408. Promise.prototype._popContext = function() {return null;};
  409. Promise._peekContext = Promise.prototype._peekContext = function() {};
  410. function Context() {
  411. this._trace = new Context.CapturedTrace(peekContext());
  412. }
  413. Context.prototype._pushContext = function () {
  414. if (this._trace !== undefined) {
  415. this._trace._promiseCreated = null;
  416. contextStack.push(this._trace);
  417. }
  418. };
  419. Context.prototype._popContext = function () {
  420. if (this._trace !== undefined) {
  421. var trace = contextStack.pop();
  422. var ret = trace._promiseCreated;
  423. trace._promiseCreated = null;
  424. return ret;
  425. }
  426. return null;
  427. };
  428. function createContext() {
  429. if (longStackTraces) return new Context();
  430. }
  431. function peekContext() {
  432. var lastIndex = contextStack.length - 1;
  433. if (lastIndex >= 0) {
  434. return contextStack[lastIndex];
  435. }
  436. return undefined;
  437. }
  438. Context.CapturedTrace = null;
  439. Context.create = createContext;
  440. Context.deactivateLongStackTraces = function() {};
  441. Context.activateLongStackTraces = function() {
  442. var Promise_pushContext = Promise.prototype._pushContext;
  443. var Promise_popContext = Promise.prototype._popContext;
  444. var Promise_PeekContext = Promise._peekContext;
  445. var Promise_peekContext = Promise.prototype._peekContext;
  446. var Promise_promiseCreated = Promise.prototype._promiseCreated;
  447. Context.deactivateLongStackTraces = function() {
  448. Promise.prototype._pushContext = Promise_pushContext;
  449. Promise.prototype._popContext = Promise_popContext;
  450. Promise._peekContext = Promise_PeekContext;
  451. Promise.prototype._peekContext = Promise_peekContext;
  452. Promise.prototype._promiseCreated = Promise_promiseCreated;
  453. longStackTraces = false;
  454. };
  455. longStackTraces = true;
  456. Promise.prototype._pushContext = Context.prototype._pushContext;
  457. Promise.prototype._popContext = Context.prototype._popContext;
  458. Promise._peekContext = Promise.prototype._peekContext = peekContext;
  459. Promise.prototype._promiseCreated = function() {
  460. var ctx = this._peekContext();
  461. if (ctx && ctx._promiseCreated == null) ctx._promiseCreated = this;
  462. };
  463. };
  464. return Context;
  465. };
  466. },{}],7:[function(_dereq_,module,exports){
  467. "use strict";
  468. module.exports = function(Promise, Context) {
  469. var getDomain = Promise._getDomain;
  470. var async = Promise._async;
  471. var Warning = _dereq_("./errors").Warning;
  472. var util = _dereq_("./util");
  473. var es5 = _dereq_("./es5");
  474. var canAttachTrace = util.canAttachTrace;
  475. var unhandledRejectionHandled;
  476. var possiblyUnhandledRejection;
  477. var bluebirdFramePattern =
  478. /[\\\/]bluebird[\\\/]js[\\\/](release|debug|instrumented)/;
  479. var nodeFramePattern = /\((?:timers\.js):\d+:\d+\)/;
  480. var parseLinePattern = /[\/<\(](.+?):(\d+):(\d+)\)?\s*$/;
  481. var stackFramePattern = null;
  482. var formatStack = null;
  483. var indentStackFrames = false;
  484. var printWarning;
  485. var debugging = !!(util.env("BLUEBIRD_DEBUG") != 0 &&
  486. (true ||
  487. util.env("BLUEBIRD_DEBUG") ||
  488. util.env("NODE_ENV") === "development"));
  489. var warnings = !!(util.env("BLUEBIRD_WARNINGS") != 0 &&
  490. (debugging || util.env("BLUEBIRD_WARNINGS")));
  491. var longStackTraces = !!(util.env("BLUEBIRD_LONG_STACK_TRACES") != 0 &&
  492. (debugging || util.env("BLUEBIRD_LONG_STACK_TRACES")));
  493. var wForgottenReturn = util.env("BLUEBIRD_W_FORGOTTEN_RETURN") != 0 &&
  494. (warnings || !!util.env("BLUEBIRD_W_FORGOTTEN_RETURN"));
  495. Promise.prototype.suppressUnhandledRejections = function() {
  496. var target = this._target();
  497. target._bitField = ((target._bitField & (~1048576)) |
  498. 524288);
  499. };
  500. Promise.prototype._ensurePossibleRejectionHandled = function () {
  501. if ((this._bitField & 524288) !== 0) return;
  502. this._setRejectionIsUnhandled();
  503. var self = this;
  504. setTimeout(function() {
  505. self._notifyUnhandledRejection();
  506. }, 1);
  507. };
  508. Promise.prototype._notifyUnhandledRejectionIsHandled = function () {
  509. fireRejectionEvent("rejectionHandled",
  510. unhandledRejectionHandled, undefined, this);
  511. };
  512. Promise.prototype._setReturnedNonUndefined = function() {
  513. this._bitField = this._bitField | 268435456;
  514. };
  515. Promise.prototype._returnedNonUndefined = function() {
  516. return (this._bitField & 268435456) !== 0;
  517. };
  518. Promise.prototype._notifyUnhandledRejection = function () {
  519. if (this._isRejectionUnhandled()) {
  520. var reason = this._settledValue();
  521. this._setUnhandledRejectionIsNotified();
  522. fireRejectionEvent("unhandledRejection",
  523. possiblyUnhandledRejection, reason, this);
  524. }
  525. };
  526. Promise.prototype._setUnhandledRejectionIsNotified = function () {
  527. this._bitField = this._bitField | 262144;
  528. };
  529. Promise.prototype._unsetUnhandledRejectionIsNotified = function () {
  530. this._bitField = this._bitField & (~262144);
  531. };
  532. Promise.prototype._isUnhandledRejectionNotified = function () {
  533. return (this._bitField & 262144) > 0;
  534. };
  535. Promise.prototype._setRejectionIsUnhandled = function () {
  536. this._bitField = this._bitField | 1048576;
  537. };
  538. Promise.prototype._unsetRejectionIsUnhandled = function () {
  539. this._bitField = this._bitField & (~1048576);
  540. if (this._isUnhandledRejectionNotified()) {
  541. this._unsetUnhandledRejectionIsNotified();
  542. this._notifyUnhandledRejectionIsHandled();
  543. }
  544. };
  545. Promise.prototype._isRejectionUnhandled = function () {
  546. return (this._bitField & 1048576) > 0;
  547. };
  548. Promise.prototype._warn = function(message, shouldUseOwnTrace, promise) {
  549. return warn(message, shouldUseOwnTrace, promise || this);
  550. };
  551. Promise.onPossiblyUnhandledRejection = function (fn) {
  552. var domain = getDomain();
  553. possiblyUnhandledRejection =
  554. typeof fn === "function" ? (domain === null ?
  555. fn : util.domainBind(domain, fn))
  556. : undefined;
  557. };
  558. Promise.onUnhandledRejectionHandled = function (fn) {
  559. var domain = getDomain();
  560. unhandledRejectionHandled =
  561. typeof fn === "function" ? (domain === null ?
  562. fn : util.domainBind(domain, fn))
  563. : undefined;
  564. };
  565. var disableLongStackTraces = function() {};
  566. Promise.longStackTraces = function () {
  567. if (async.haveItemsQueued() && !config.longStackTraces) {
  568. throw new Error("cannot enable long stack traces after promises have been created\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  569. }
  570. if (!config.longStackTraces && longStackTracesIsSupported()) {
  571. var Promise_captureStackTrace = Promise.prototype._captureStackTrace;
  572. var Promise_attachExtraTrace = Promise.prototype._attachExtraTrace;
  573. var Promise_dereferenceTrace = Promise.prototype._dereferenceTrace;
  574. config.longStackTraces = true;
  575. disableLongStackTraces = function() {
  576. if (async.haveItemsQueued() && !config.longStackTraces) {
  577. throw new Error("cannot enable long stack traces after promises have been created\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  578. }
  579. Promise.prototype._captureStackTrace = Promise_captureStackTrace;
  580. Promise.prototype._attachExtraTrace = Promise_attachExtraTrace;
  581. Promise.prototype._dereferenceTrace = Promise_dereferenceTrace;
  582. Context.deactivateLongStackTraces();
  583. async.enableTrampoline();
  584. config.longStackTraces = false;
  585. };
  586. Promise.prototype._captureStackTrace = longStackTracesCaptureStackTrace;
  587. Promise.prototype._attachExtraTrace = longStackTracesAttachExtraTrace;
  588. Promise.prototype._dereferenceTrace = longStackTracesDereferenceTrace;
  589. Context.activateLongStackTraces();
  590. async.disableTrampolineIfNecessary();
  591. }
  592. };
  593. Promise.hasLongStackTraces = function () {
  594. return config.longStackTraces && longStackTracesIsSupported();
  595. };
  596. var fireDomEvent = (function() {
  597. try {
  598. if (typeof CustomEvent === "function") {
  599. var event = new CustomEvent("CustomEvent");
  600. util.global.dispatchEvent(event);
  601. return function(name, event) {
  602. var eventData = {
  603. detail: event,
  604. cancelable: true
  605. };
  606. es5.defineProperty(
  607. eventData, "promise", {value: event.promise});
  608. es5.defineProperty(eventData, "reason", {value: event.reason});
  609. var domEvent = new CustomEvent(name.toLowerCase(), eventData);
  610. return !util.global.dispatchEvent(domEvent);
  611. };
  612. } else if (typeof Event === "function") {
  613. var event = new Event("CustomEvent");
  614. util.global.dispatchEvent(event);
  615. return function(name, event) {
  616. var domEvent = new Event(name.toLowerCase(), {
  617. cancelable: true
  618. });
  619. domEvent.detail = event;
  620. es5.defineProperty(domEvent, "promise", {value: event.promise});
  621. es5.defineProperty(domEvent, "reason", {value: event.reason});
  622. return !util.global.dispatchEvent(domEvent);
  623. };
  624. } else {
  625. var event = document.createEvent("CustomEvent");
  626. event.initCustomEvent("testingtheevent", false, true, {});
  627. util.global.dispatchEvent(event);
  628. return function(name, event) {
  629. var domEvent = document.createEvent("CustomEvent");
  630. domEvent.initCustomEvent(name.toLowerCase(), false, true,
  631. event);
  632. return !util.global.dispatchEvent(domEvent);
  633. };
  634. }
  635. } catch (e) {}
  636. return function() {
  637. return false;
  638. };
  639. })();
  640. var fireGlobalEvent = (function() {
  641. if (util.isNode) {
  642. return function() {
  643. return process.emit.apply(process, arguments);
  644. };
  645. } else {
  646. if (!util.global) {
  647. return function() {
  648. return false;
  649. };
  650. }
  651. return function(name) {
  652. var methodName = "on" + name.toLowerCase();
  653. var method = util.global[methodName];
  654. if (!method) return false;
  655. method.apply(util.global, [].slice.call(arguments, 1));
  656. return true;
  657. };
  658. }
  659. })();
  660. function generatePromiseLifecycleEventObject(name, promise) {
  661. return {promise: promise};
  662. }
  663. var eventToObjectGenerator = {
  664. promiseCreated: generatePromiseLifecycleEventObject,
  665. promiseFulfilled: generatePromiseLifecycleEventObject,
  666. promiseRejected: generatePromiseLifecycleEventObject,
  667. promiseResolved: generatePromiseLifecycleEventObject,
  668. promiseCancelled: generatePromiseLifecycleEventObject,
  669. promiseChained: function(name, promise, child) {
  670. return {promise: promise, child: child};
  671. },
  672. warning: function(name, warning) {
  673. return {warning: warning};
  674. },
  675. unhandledRejection: function (name, reason, promise) {
  676. return {reason: reason, promise: promise};
  677. },
  678. rejectionHandled: generatePromiseLifecycleEventObject
  679. };
  680. var activeFireEvent = function (name) {
  681. var globalEventFired = false;
  682. try {
  683. globalEventFired = fireGlobalEvent.apply(null, arguments);
  684. } catch (e) {
  685. async.throwLater(e);
  686. globalEventFired = true;
  687. }
  688. var domEventFired = false;
  689. try {
  690. domEventFired = fireDomEvent(name,
  691. eventToObjectGenerator[name].apply(null, arguments));
  692. } catch (e) {
  693. async.throwLater(e);
  694. domEventFired = true;
  695. }
  696. return domEventFired || globalEventFired;
  697. };
  698. Promise.config = function(opts) {
  699. opts = Object(opts);
  700. if ("longStackTraces" in opts) {
  701. if (opts.longStackTraces) {
  702. Promise.longStackTraces();
  703. } else if (!opts.longStackTraces && Promise.hasLongStackTraces()) {
  704. disableLongStackTraces();
  705. }
  706. }
  707. if ("warnings" in opts) {
  708. var warningsOption = opts.warnings;
  709. config.warnings = !!warningsOption;
  710. wForgottenReturn = config.warnings;
  711. if (util.isObject(warningsOption)) {
  712. if ("wForgottenReturn" in warningsOption) {
  713. wForgottenReturn = !!warningsOption.wForgottenReturn;
  714. }
  715. }
  716. }
  717. if ("cancellation" in opts && opts.cancellation && !config.cancellation) {
  718. if (async.haveItemsQueued()) {
  719. throw new Error(
  720. "cannot enable cancellation after promises are in use");
  721. }
  722. Promise.prototype._clearCancellationData =
  723. cancellationClearCancellationData;
  724. Promise.prototype._propagateFrom = cancellationPropagateFrom;
  725. Promise.prototype._onCancel = cancellationOnCancel;
  726. Promise.prototype._setOnCancel = cancellationSetOnCancel;
  727. Promise.prototype._attachCancellationCallback =
  728. cancellationAttachCancellationCallback;
  729. Promise.prototype._execute = cancellationExecute;
  730. propagateFromFunction = cancellationPropagateFrom;
  731. config.cancellation = true;
  732. }
  733. if ("monitoring" in opts) {
  734. if (opts.monitoring && !config.monitoring) {
  735. config.monitoring = true;
  736. Promise.prototype._fireEvent = activeFireEvent;
  737. } else if (!opts.monitoring && config.monitoring) {
  738. config.monitoring = false;
  739. Promise.prototype._fireEvent = defaultFireEvent;
  740. }
  741. }
  742. return Promise;
  743. };
  744. function defaultFireEvent() { return false; }
  745. Promise.prototype._fireEvent = defaultFireEvent;
  746. Promise.prototype._execute = function(executor, resolve, reject) {
  747. try {
  748. executor(resolve, reject);
  749. } catch (e) {
  750. return e;
  751. }
  752. };
  753. Promise.prototype._onCancel = function () {};
  754. Promise.prototype._setOnCancel = function (handler) { ; };
  755. Promise.prototype._attachCancellationCallback = function(onCancel) {
  756. ;
  757. };
  758. Promise.prototype._captureStackTrace = function () {};
  759. Promise.prototype._attachExtraTrace = function () {};
  760. Promise.prototype._dereferenceTrace = function () {};
  761. Promise.prototype._clearCancellationData = function() {};
  762. Promise.prototype._propagateFrom = function (parent, flags) {
  763. ;
  764. ;
  765. };
  766. function cancellationExecute(executor, resolve, reject) {
  767. var promise = this;
  768. try {
  769. executor(resolve, reject, function(onCancel) {
  770. if (typeof onCancel !== "function") {
  771. throw new TypeError("onCancel must be a function, got: " +
  772. util.toString(onCancel));
  773. }
  774. promise._attachCancellationCallback(onCancel);
  775. });
  776. } catch (e) {
  777. return e;
  778. }
  779. }
  780. function cancellationAttachCancellationCallback(onCancel) {
  781. if (!this._isCancellable()) return this;
  782. var previousOnCancel = this._onCancel();
  783. if (previousOnCancel !== undefined) {
  784. if (util.isArray(previousOnCancel)) {
  785. previousOnCancel.push(onCancel);
  786. } else {
  787. this._setOnCancel([previousOnCancel, onCancel]);
  788. }
  789. } else {
  790. this._setOnCancel(onCancel);
  791. }
  792. }
  793. function cancellationOnCancel() {
  794. return this._onCancelField;
  795. }
  796. function cancellationSetOnCancel(onCancel) {
  797. this._onCancelField = onCancel;
  798. }
  799. function cancellationClearCancellationData() {
  800. this._cancellationParent = undefined;
  801. this._onCancelField = undefined;
  802. }
  803. function cancellationPropagateFrom(parent, flags) {
  804. if ((flags & 1) !== 0) {
  805. this._cancellationParent = parent;
  806. var branchesRemainingToCancel = parent._branchesRemainingToCancel;
  807. if (branchesRemainingToCancel === undefined) {
  808. branchesRemainingToCancel = 0;
  809. }
  810. parent._branchesRemainingToCancel = branchesRemainingToCancel + 1;
  811. }
  812. if ((flags & 2) !== 0 && parent._isBound()) {
  813. this._setBoundTo(parent._boundTo);
  814. }
  815. }
  816. function bindingPropagateFrom(parent, flags) {
  817. if ((flags & 2) !== 0 && parent._isBound()) {
  818. this._setBoundTo(parent._boundTo);
  819. }
  820. }
  821. var propagateFromFunction = bindingPropagateFrom;
  822. function boundValueFunction() {
  823. var ret = this._boundTo;
  824. if (ret !== undefined) {
  825. if (ret instanceof Promise) {
  826. if (ret.isFulfilled()) {
  827. return ret.value();
  828. } else {
  829. return undefined;
  830. }
  831. }
  832. }
  833. return ret;
  834. }
  835. function longStackTracesCaptureStackTrace() {
  836. this._trace = new CapturedTrace(this._peekContext());
  837. }
  838. function longStackTracesAttachExtraTrace(error, ignoreSelf) {
  839. if (canAttachTrace(error)) {
  840. var trace = this._trace;
  841. if (trace !== undefined) {
  842. if (ignoreSelf) trace = trace._parent;
  843. }
  844. if (trace !== undefined) {
  845. trace.attachExtraTrace(error);
  846. } else if (!error.__stackCleaned__) {
  847. var parsed = parseStackAndMessage(error);
  848. util.notEnumerableProp(error, "stack",
  849. parsed.message + "\n" + parsed.stack.join("\n"));
  850. util.notEnumerableProp(error, "__stackCleaned__", true);
  851. }
  852. }
  853. }
  854. function longStackTracesDereferenceTrace() {
  855. this._trace = undefined;
  856. }
  857. function checkForgottenReturns(returnValue, promiseCreated, name, promise,
  858. parent) {
  859. if (returnValue === undefined && promiseCreated !== null &&
  860. wForgottenReturn) {
  861. if (parent !== undefined && parent._returnedNonUndefined()) return;
  862. if ((promise._bitField & 65535) === 0) return;
  863. if (name) name = name + " ";
  864. var handlerLine = "";
  865. var creatorLine = "";
  866. if (promiseCreated._trace) {
  867. var traceLines = promiseCreated._trace.stack.split("\n");
  868. var stack = cleanStack(traceLines);
  869. for (var i = stack.length - 1; i >= 0; --i) {
  870. var line = stack[i];
  871. if (!nodeFramePattern.test(line)) {
  872. var lineMatches = line.match(parseLinePattern);
  873. if (lineMatches) {
  874. handlerLine = "at " + lineMatches[1] +
  875. ":" + lineMatches[2] + ":" + lineMatches[3] + " ";
  876. }
  877. break;
  878. }
  879. }
  880. if (stack.length > 0) {
  881. var firstUserLine = stack[0];
  882. for (var i = 0; i < traceLines.length; ++i) {
  883. if (traceLines[i] === firstUserLine) {
  884. if (i > 0) {
  885. creatorLine = "\n" + traceLines[i - 1];
  886. }
  887. break;
  888. }
  889. }
  890. }
  891. }
  892. var msg = "a promise was created in a " + name +
  893. "handler " + handlerLine + "but was not returned from it, " +
  894. "see http://goo.gl/rRqMUw" +
  895. creatorLine;
  896. promise._warn(msg, true, promiseCreated);
  897. }
  898. }
  899. function deprecated(name, replacement) {
  900. var message = name +
  901. " is deprecated and will be removed in a future version.";
  902. if (replacement) message += " Use " + replacement + " instead.";
  903. return warn(message);
  904. }
  905. function warn(message, shouldUseOwnTrace, promise) {
  906. if (!config.warnings) return;
  907. var warning = new Warning(message);
  908. var ctx;
  909. if (shouldUseOwnTrace) {
  910. promise._attachExtraTrace(warning);
  911. } else if (config.longStackTraces && (ctx = Promise._peekContext())) {
  912. ctx.attachExtraTrace(warning);
  913. } else {
  914. var parsed = parseStackAndMessage(warning);
  915. warning.stack = parsed.message + "\n" + parsed.stack.join("\n");
  916. }
  917. if (!activeFireEvent("warning", warning)) {
  918. formatAndLogError(warning, "", true);
  919. }
  920. }
  921. function reconstructStack(message, stacks) {
  922. for (var i = 0; i < stacks.length - 1; ++i) {
  923. stacks[i].push("From previous event:");
  924. stacks[i] = stacks[i].join("\n");
  925. }
  926. if (i < stacks.length) {
  927. stacks[i] = stacks[i].join("\n");
  928. }
  929. return message + "\n" + stacks.join("\n");
  930. }
  931. function removeDuplicateOrEmptyJumps(stacks) {
  932. for (var i = 0; i < stacks.length; ++i) {
  933. if (stacks[i].length === 0 ||
  934. ((i + 1 < stacks.length) && stacks[i][0] === stacks[i+1][0])) {
  935. stacks.splice(i, 1);
  936. i--;
  937. }
  938. }
  939. }
  940. function removeCommonRoots(stacks) {
  941. var current = stacks[0];
  942. for (var i = 1; i < stacks.length; ++i) {
  943. var prev = stacks[i];
  944. var currentLastIndex = current.length - 1;
  945. var currentLastLine = current[currentLastIndex];
  946. var commonRootMeetPoint = -1;
  947. for (var j = prev.length - 1; j >= 0; --j) {
  948. if (prev[j] === currentLastLine) {
  949. commonRootMeetPoint = j;
  950. break;
  951. }
  952. }
  953. for (var j = commonRootMeetPoint; j >= 0; --j) {
  954. var line = prev[j];
  955. if (current[currentLastIndex] === line) {
  956. current.pop();
  957. currentLastIndex--;
  958. } else {
  959. break;
  960. }
  961. }
  962. current = prev;
  963. }
  964. }
  965. function cleanStack(stack) {
  966. var ret = [];
  967. for (var i = 0; i < stack.length; ++i) {
  968. var line = stack[i];
  969. var isTraceLine = " (No stack trace)" === line ||
  970. stackFramePattern.test(line);
  971. var isInternalFrame = isTraceLine && shouldIgnore(line);
  972. if (isTraceLine && !isInternalFrame) {
  973. if (indentStackFrames && line.charAt(0) !== " ") {
  974. line = " " + line;
  975. }
  976. ret.push(line);
  977. }
  978. }
  979. return ret;
  980. }
  981. function stackFramesAsArray(error) {
  982. var stack = error.stack.replace(/\s+$/g, "").split("\n");
  983. for (var i = 0; i < stack.length; ++i) {
  984. var line = stack[i];
  985. if (" (No stack trace)" === line || stackFramePattern.test(line)) {
  986. break;
  987. }
  988. }
  989. if (i > 0 && error.name != "SyntaxError") {
  990. stack = stack.slice(i);
  991. }
  992. return stack;
  993. }
  994. function parseStackAndMessage(error) {
  995. var stack = error.stack;
  996. var message = error.toString();
  997. stack = typeof stack === "string" && stack.length > 0
  998. ? stackFramesAsArray(error) : [" (No stack trace)"];
  999. return {
  1000. message: message,
  1001. stack: error.name == "SyntaxError" ? stack : cleanStack(stack)
  1002. };
  1003. }
  1004. function formatAndLogError(error, title, isSoft) {
  1005. if (typeof console !== "undefined") {
  1006. var message;
  1007. if (util.isObject(error)) {
  1008. var stack = error.stack;
  1009. message = title + formatStack(stack, error);
  1010. } else {
  1011. message = title + String(error);
  1012. }
  1013. if (typeof printWarning === "function") {
  1014. printWarning(message, isSoft);
  1015. } else if (typeof console.log === "function" ||
  1016. typeof console.log === "object") {
  1017. console.log(message);
  1018. }
  1019. }
  1020. }
  1021. function fireRejectionEvent(name, localHandler, reason, promise) {
  1022. var localEventFired = false;
  1023. try {
  1024. if (typeof localHandler === "function") {
  1025. localEventFired = true;
  1026. if (name === "rejectionHandled") {
  1027. localHandler(promise);
  1028. } else {
  1029. localHandler(reason, promise);
  1030. }
  1031. }
  1032. } catch (e) {
  1033. async.throwLater(e);
  1034. }
  1035. if (name === "unhandledRejection") {
  1036. if (!activeFireEvent(name, reason, promise) && !localEventFired) {
  1037. formatAndLogError(reason, "Unhandled rejection ");
  1038. }
  1039. } else {
  1040. activeFireEvent(name, promise);
  1041. }
  1042. }
  1043. function formatNonError(obj) {
  1044. var str;
  1045. if (typeof obj === "function") {
  1046. str = "[function " +
  1047. (obj.name || "anonymous") +
  1048. "]";
  1049. } else {
  1050. str = obj && typeof obj.toString === "function"
  1051. ? obj.toString() : util.toString(obj);
  1052. var ruselessToString = /\[object [a-zA-Z0-9$_]+\]/;
  1053. if (ruselessToString.test(str)) {
  1054. try {
  1055. var newStr = JSON.stringify(obj);
  1056. str = newStr;
  1057. }
  1058. catch(e) {
  1059. }
  1060. }
  1061. if (str.length === 0) {
  1062. str = "(empty array)";
  1063. }
  1064. }
  1065. return ("(<" + snip(str) + ">, no stack trace)");
  1066. }
  1067. function snip(str) {
  1068. var maxChars = 41;
  1069. if (str.length < maxChars) {
  1070. return str;
  1071. }
  1072. return str.substr(0, maxChars - 3) + "...";
  1073. }
  1074. function longStackTracesIsSupported() {
  1075. return typeof captureStackTrace === "function";
  1076. }
  1077. var shouldIgnore = function() { return false; };
  1078. var parseLineInfoRegex = /[\/<\(]([^:\/]+):(\d+):(?:\d+)\)?\s*$/;
  1079. function parseLineInfo(line) {
  1080. var matches = line.match(parseLineInfoRegex);
  1081. if (matches) {
  1082. return {
  1083. fileName: matches[1],
  1084. line: parseInt(matches[2], 10)
  1085. };
  1086. }
  1087. }
  1088. function setBounds(firstLineError, lastLineError) {
  1089. if (!longStackTracesIsSupported()) return;
  1090. var firstStackLines = firstLineError.stack.split("\n");
  1091. var lastStackLines = lastLineError.stack.split("\n");
  1092. var firstIndex = -1;
  1093. var lastIndex = -1;
  1094. var firstFileName;
  1095. var lastFileName;
  1096. for (var i = 0; i < firstStackLines.length; ++i) {
  1097. var result = parseLineInfo(firstStackLines[i]);
  1098. if (result) {
  1099. firstFileName = result.fileName;
  1100. firstIndex = result.line;
  1101. break;
  1102. }
  1103. }
  1104. for (var i = 0; i < lastStackLines.length; ++i) {
  1105. var result = parseLineInfo(lastStackLines[i]);
  1106. if (result) {
  1107. lastFileName = result.fileName;
  1108. lastIndex = result.line;
  1109. break;
  1110. }
  1111. }
  1112. if (firstIndex < 0 || lastIndex < 0 || !firstFileName || !lastFileName ||
  1113. firstFileName !== lastFileName || firstIndex >= lastIndex) {
  1114. return;
  1115. }
  1116. shouldIgnore = function(line) {
  1117. if (bluebirdFramePattern.test(line)) return true;
  1118. var info = parseLineInfo(line);
  1119. if (info) {
  1120. if (info.fileName === firstFileName &&
  1121. (firstIndex <= info.line && info.line <= lastIndex)) {
  1122. return true;
  1123. }
  1124. }
  1125. return false;
  1126. };
  1127. }
  1128. function CapturedTrace(parent) {
  1129. this._parent = parent;
  1130. this._promisesCreated = 0;
  1131. var length = this._length = 1 + (parent === undefined ? 0 : parent._length);
  1132. captureStackTrace(this, CapturedTrace);
  1133. if (length > 32) this.uncycle();
  1134. }
  1135. util.inherits(CapturedTrace, Error);
  1136. Context.CapturedTrace = CapturedTrace;
  1137. CapturedTrace.prototype.uncycle = function() {
  1138. var length = this._length;
  1139. if (length < 2) return;
  1140. var nodes = [];
  1141. var stackToIndex = {};
  1142. for (var i = 0, node = this; node !== undefined; ++i) {
  1143. nodes.push(node);
  1144. node = node._parent;
  1145. }
  1146. length = this._length = i;
  1147. for (var i = length - 1; i >= 0; --i) {
  1148. var stack = nodes[i].stack;
  1149. if (stackToIndex[stack] === undefined) {
  1150. stackToIndex[stack] = i;
  1151. }
  1152. }
  1153. for (var i = 0; i < length; ++i) {
  1154. var currentStack = nodes[i].stack;
  1155. var index = stackToIndex[currentStack];
  1156. if (index !== undefined && index !== i) {
  1157. if (index > 0) {
  1158. nodes[index - 1]._parent = undefined;
  1159. nodes[index - 1]._length = 1;
  1160. }
  1161. nodes[i]._parent = undefined;
  1162. nodes[i]._length = 1;
  1163. var cycleEdgeNode = i > 0 ? nodes[i - 1] : this;
  1164. if (index < length - 1) {
  1165. cycleEdgeNode._parent = nodes[index + 1];
  1166. cycleEdgeNode._parent.uncycle();
  1167. cycleEdgeNode._length =
  1168. cycleEdgeNode._parent._length + 1;
  1169. } else {
  1170. cycleEdgeNode._parent = undefined;
  1171. cycleEdgeNode._length = 1;
  1172. }
  1173. var currentChildLength = cycleEdgeNode._length + 1;
  1174. for (var j = i - 2; j >= 0; --j) {
  1175. nodes[j]._length = currentChildLength;
  1176. currentChildLength++;
  1177. }
  1178. return;
  1179. }
  1180. }
  1181. };
  1182. CapturedTrace.prototype.attachExtraTrace = function(error) {
  1183. if (error.__stackCleaned__) return;
  1184. this.uncycle();
  1185. var parsed = parseStackAndMessage(error);
  1186. var message = parsed.message;
  1187. var stacks = [parsed.stack];
  1188. var trace = this;
  1189. while (trace !== undefined) {
  1190. stacks.push(cleanStack(trace.stack.split("\n")));
  1191. trace = trace._parent;
  1192. }
  1193. removeCommonRoots(stacks);
  1194. removeDuplicateOrEmptyJumps(stacks);
  1195. util.notEnumerableProp(error, "stack", reconstructStack(message, stacks));
  1196. util.notEnumerableProp(error, "__stackCleaned__", true);
  1197. };
  1198. var captureStackTrace = (function stackDetection() {
  1199. var v8stackFramePattern = /^\s*at\s*/;
  1200. var v8stackFormatter = function(stack, error) {
  1201. if (typeof stack === "string") return stack;
  1202. if (error.name !== undefined &&
  1203. error.message !== undefined) {
  1204. return error.toString();
  1205. }
  1206. return formatNonError(error);
  1207. };
  1208. if (typeof Error.stackTraceLimit === "number" &&
  1209. typeof Error.captureStackTrace === "function") {
  1210. Error.stackTraceLimit += 6;
  1211. stackFramePattern = v8stackFramePattern;
  1212. formatStack = v8stackFormatter;
  1213. var captureStackTrace = Error.captureStackTrace;
  1214. shouldIgnore = function(line) {
  1215. return bluebirdFramePattern.test(line);
  1216. };
  1217. return function(receiver, ignoreUntil) {
  1218. Error.stackTraceLimit += 6;
  1219. captureStackTrace(receiver, ignoreUntil);
  1220. Error.stackTraceLimit -= 6;
  1221. };
  1222. }
  1223. var err = new Error();
  1224. if (typeof err.stack === "string" &&
  1225. err.stack.split("\n")[0].indexOf("stackDetection@") >= 0) {
  1226. stackFramePattern = /@/;
  1227. formatStack = v8stackFormatter;
  1228. indentStackFrames = true;
  1229. return function captureStackTrace(o) {
  1230. o.stack = new Error().stack;
  1231. };
  1232. }
  1233. var hasStackAfterThrow;
  1234. try { throw new Error(); }
  1235. catch(e) {
  1236. hasStackAfterThrow = ("stack" in e);
  1237. }
  1238. if (!("stack" in err) && hasStackAfterThrow &&
  1239. typeof Error.stackTraceLimit === "number") {
  1240. stackFramePattern = v8stackFramePattern;
  1241. formatStack = v8stackFormatter;
  1242. return function captureStackTrace(o) {
  1243. Error.stackTraceLimit += 6;
  1244. try { throw new Error(); }
  1245. catch(e) { o.stack = e.stack; }
  1246. Error.stackTraceLimit -= 6;
  1247. };
  1248. }
  1249. formatStack = function(stack, error) {
  1250. if (typeof stack === "string") return stack;
  1251. if ((typeof error === "object" ||
  1252. typeof error === "function") &&
  1253. error.name !== undefined &&
  1254. error.message !== undefined) {
  1255. return error.toString();
  1256. }
  1257. return formatNonError(error);
  1258. };
  1259. return null;
  1260. })([]);
  1261. if (typeof console !== "undefined" && typeof console.warn !== "undefined") {
  1262. printWarning = function (message) {
  1263. console.warn(message);
  1264. };
  1265. if (util.isNode && process.stderr.isTTY) {
  1266. printWarning = function(message, isSoft) {
  1267. var color = isSoft ? "\u001b[33m" : "\u001b[31m";
  1268. console.warn(color + message + "\u001b[0m\n");
  1269. };
  1270. } else if (!util.isNode && typeof (new Error().stack) === "string") {
  1271. printWarning = function(message, isSoft) {
  1272. console.warn("%c" + message,
  1273. isSoft ? "color: darkorange" : "color: red");
  1274. };
  1275. }
  1276. }
  1277. var config = {
  1278. warnings: warnings,
  1279. longStackTraces: false,
  1280. cancellation: false,
  1281. monitoring: false
  1282. };
  1283. if (longStackTraces) Promise.longStackTraces();
  1284. return {
  1285. longStackTraces: function() {
  1286. return config.longStackTraces;
  1287. },
  1288. warnings: function() {
  1289. return config.warnings;
  1290. },
  1291. cancellation: function() {
  1292. return config.cancellation;
  1293. },
  1294. monitoring: function() {
  1295. return config.monitoring;
  1296. },
  1297. propagateFromFunction: function() {
  1298. return propagateFromFunction;
  1299. },
  1300. boundValueFunction: function() {
  1301. return boundValueFunction;
  1302. },
  1303. checkForgottenReturns: checkForgottenReturns,
  1304. setBounds: setBounds,
  1305. warn: warn,
  1306. deprecated: deprecated,
  1307. CapturedTrace: CapturedTrace,
  1308. fireDomEvent: fireDomEvent,
  1309. fireGlobalEvent: fireGlobalEvent
  1310. };
  1311. };
  1312. },{"./errors":9,"./es5":10,"./util":21}],8:[function(_dereq_,module,exports){
  1313. "use strict";
  1314. module.exports = function(Promise) {
  1315. function returner() {
  1316. return this.value;
  1317. }
  1318. function thrower() {
  1319. throw this.reason;
  1320. }
  1321. Promise.prototype["return"] =
  1322. Promise.prototype.thenReturn = function (value) {
  1323. if (value instanceof Promise) value.suppressUnhandledRejections();
  1324. return this._then(
  1325. returner, undefined, undefined, {value: value}, undefined);
  1326. };
  1327. Promise.prototype["throw"] =
  1328. Promise.prototype.thenThrow = function (reason) {
  1329. return this._then(
  1330. thrower, undefined, undefined, {reason: reason}, undefined);
  1331. };
  1332. Promise.prototype.catchThrow = function (reason) {
  1333. if (arguments.length <= 1) {
  1334. return this._then(
  1335. undefined, thrower, undefined, {reason: reason}, undefined);
  1336. } else {
  1337. var _reason = arguments[1];
  1338. var handler = function() {throw _reason;};
  1339. return this.caught(reason, handler);
  1340. }
  1341. };
  1342. Promise.prototype.catchReturn = function (value) {
  1343. if (arguments.length <= 1) {
  1344. if (value instanceof Promise) value.suppressUnhandledRejections();
  1345. return this._then(
  1346. undefined, returner, undefined, {value: value}, undefined);
  1347. } else {
  1348. var _value = arguments[1];
  1349. if (_value instanceof Promise) _value.suppressUnhandledRejections();
  1350. var handler = function() {return _value;};
  1351. return this.caught(value, handler);
  1352. }
  1353. };
  1354. };
  1355. },{}],9:[function(_dereq_,module,exports){
  1356. "use strict";
  1357. var es5 = _dereq_("./es5");
  1358. var Objectfreeze = es5.freeze;
  1359. var util = _dereq_("./util");
  1360. var inherits = util.inherits;
  1361. var notEnumerableProp = util.notEnumerableProp;
  1362. function subError(nameProperty, defaultMessage) {
  1363. function SubError(message) {
  1364. if (!(this instanceof SubError)) return new SubError(message);
  1365. notEnumerableProp(this, "message",
  1366. typeof message === "string" ? message : defaultMessage);
  1367. notEnumerableProp(this, "name", nameProperty);
  1368. if (Error.captureStackTrace) {
  1369. Error.captureStackTrace(this, this.constructor);
  1370. } else {
  1371. Error.call(this);
  1372. }
  1373. }
  1374. inherits(SubError, Error);
  1375. return SubError;
  1376. }
  1377. var _TypeError, _RangeError;
  1378. var Warning = subError("Warning", "warning");
  1379. var CancellationError = subError("CancellationError", "cancellation error");
  1380. var TimeoutError = subError("TimeoutError", "timeout error");
  1381. var AggregateError = subError("AggregateError", "aggregate error");
  1382. try {
  1383. _TypeError = TypeError;
  1384. _RangeError = RangeError;
  1385. } catch(e) {
  1386. _TypeError = subError("TypeError", "type error");
  1387. _RangeError = subError("RangeError", "range error");
  1388. }
  1389. var methods = ("join pop push shift unshift slice filter forEach some " +
  1390. "every map indexOf lastIndexOf reduce reduceRight sort reverse").split(" ");
  1391. for (var i = 0; i < methods.length; ++i) {
  1392. if (typeof Array.prototype[methods[i]] === "function") {
  1393. AggregateError.prototype[methods[i]] = Array.prototype[methods[i]];
  1394. }
  1395. }
  1396. es5.defineProperty(AggregateError.prototype, "length", {
  1397. value: 0,
  1398. configurable: false,
  1399. writable: true,
  1400. enumerable: true
  1401. });
  1402. AggregateError.prototype["isOperational"] = true;
  1403. var level = 0;
  1404. AggregateError.prototype.toString = function() {
  1405. var indent = Array(level * 4 + 1).join(" ");
  1406. var ret = "\n" + indent + "AggregateError of:" + "\n";
  1407. level++;
  1408. indent = Array(level * 4 + 1).join(" ");
  1409. for (var i = 0; i < this.length; ++i) {
  1410. var str = this[i] === this ? "[Circular AggregateError]" : this[i] + "";
  1411. var lines = str.split("\n");
  1412. for (var j = 0; j < lines.length; ++j) {
  1413. lines[j] = indent + lines[j];
  1414. }
  1415. str = lines.join("\n");
  1416. ret += str + "\n";
  1417. }
  1418. level--;
  1419. return ret;
  1420. };
  1421. function OperationalError(message) {
  1422. if (!(this instanceof OperationalError))
  1423. return new OperationalError(message);
  1424. notEnumerableProp(this, "name", "OperationalError");
  1425. notEnumerableProp(this, "message", message);
  1426. this.cause = message;
  1427. this["isOperational"] = true;
  1428. if (message instanceof Error) {
  1429. notEnumerableProp(this, "message", message.message);
  1430. notEnumerableProp(this, "stack", message.stack);
  1431. } else if (Error.captureStackTrace) {
  1432. Error.captureStackTrace(this, this.constructor);
  1433. }
  1434. }
  1435. inherits(OperationalError, Error);
  1436. var errorTypes = Error["__BluebirdErrorTypes__"];
  1437. if (!errorTypes) {
  1438. errorTypes = Objectfreeze({
  1439. CancellationError: CancellationError,
  1440. TimeoutError: TimeoutError,
  1441. OperationalError: OperationalError,
  1442. RejectionError: OperationalError,
  1443. AggregateError: AggregateError
  1444. });
  1445. es5.defineProperty(Error, "__BluebirdErrorTypes__", {
  1446. value: errorTypes,
  1447. writable: false,
  1448. enumerable: false,
  1449. configurable: false
  1450. });
  1451. }
  1452. module.exports = {
  1453. Error: Error,
  1454. TypeError: _TypeError,
  1455. RangeError: _RangeError,
  1456. CancellationError: errorTypes.CancellationError,
  1457. OperationalError: errorTypes.OperationalError,
  1458. TimeoutError: errorTypes.TimeoutError,
  1459. AggregateError: errorTypes.AggregateError,
  1460. Warning: Warning
  1461. };
  1462. },{"./es5":10,"./util":21}],10:[function(_dereq_,module,exports){
  1463. var isES5 = (function(){
  1464. "use strict";
  1465. return this === undefined;
  1466. })();
  1467. if (isES5) {
  1468. module.exports = {
  1469. freeze: Object.freeze,
  1470. defineProperty: Object.defineProperty,
  1471. getDescriptor: Object.getOwnPropertyDescriptor,
  1472. keys: Object.keys,
  1473. names: Object.getOwnPropertyNames,
  1474. getPrototypeOf: Object.getPrototypeOf,
  1475. isArray: Array.isArray,
  1476. isES5: isES5,
  1477. propertyIsWritable: function(obj, prop) {
  1478. var descriptor = Object.getOwnPropertyDescriptor(obj, prop);
  1479. return !!(!descriptor || descriptor.writable || descriptor.set);
  1480. }
  1481. };
  1482. } else {
  1483. var has = {}.hasOwnProperty;
  1484. var str = {}.toString;
  1485. var proto = {}.constructor.prototype;
  1486. var ObjectKeys = function (o) {
  1487. var ret = [];
  1488. for (var key in o) {
  1489. if (has.call(o, key)) {
  1490. ret.push(key);
  1491. }
  1492. }
  1493. return ret;
  1494. };
  1495. var ObjectGetDescriptor = function(o, key) {
  1496. return {value: o[key]};
  1497. };
  1498. var ObjectDefineProperty = function (o, key, desc) {
  1499. o[key] = desc.value;
  1500. return o;
  1501. };
  1502. var ObjectFreeze = function (obj) {
  1503. return obj;
  1504. };
  1505. var ObjectGetPrototypeOf = function (obj) {
  1506. try {
  1507. return Object(obj).constructor.prototype;
  1508. }
  1509. catch (e) {
  1510. return proto;
  1511. }
  1512. };
  1513. var ArrayIsArray = function (obj) {
  1514. try {
  1515. return str.call(obj) === "[object Array]";
  1516. }
  1517. catch(e) {
  1518. return false;
  1519. }
  1520. };
  1521. module.exports = {
  1522. isArray: ArrayIsArray,
  1523. keys: ObjectKeys,
  1524. names: ObjectKeys,
  1525. defineProperty: ObjectDefineProperty,
  1526. getDescriptor: ObjectGetDescriptor,
  1527. freeze: ObjectFreeze,
  1528. getPrototypeOf: ObjectGetPrototypeOf,
  1529. isES5: isES5,
  1530. propertyIsWritable: function() {
  1531. return true;
  1532. }
  1533. };
  1534. }
  1535. },{}],11:[function(_dereq_,module,exports){
  1536. "use strict";
  1537. module.exports = function(Promise, tryConvertToPromise, NEXT_FILTER) {
  1538. var util = _dereq_("./util");
  1539. var CancellationError = Promise.CancellationError;
  1540. var errorObj = util.errorObj;
  1541. var catchFilter = _dereq_("./catch_filter")(NEXT_FILTER);
  1542. function PassThroughHandlerContext(promise, type, handler) {
  1543. this.promise = promise;
  1544. this.type = type;
  1545. this.handler = handler;
  1546. this.called = false;
  1547. this.cancelPromise = null;
  1548. }
  1549. PassThroughHandlerContext.prototype.isFinallyHandler = function() {
  1550. return this.type === 0;
  1551. };
  1552. function FinallyHandlerCancelReaction(finallyHandler) {
  1553. this.finallyHandler = finallyHandler;
  1554. }
  1555. FinallyHandlerCancelReaction.prototype._resultCancelled = function() {
  1556. checkCancel(this.finallyHandler);
  1557. };
  1558. function checkCancel(ctx, reason) {
  1559. if (ctx.cancelPromise != null) {
  1560. if (arguments.length > 1) {
  1561. ctx.cancelPromise._reject(reason);
  1562. } else {
  1563. ctx.cancelPromise._cancel();
  1564. }
  1565. ctx.cancelPromise = null;
  1566. return true;
  1567. }
  1568. return false;
  1569. }
  1570. function succeed() {
  1571. return finallyHandler.call(this, this.promise._target()._settledValue());
  1572. }
  1573. function fail(reason) {
  1574. if (checkCancel(this, reason)) return;
  1575. errorObj.e = reason;
  1576. return errorObj;
  1577. }
  1578. function finallyHandler(reasonOrValue) {
  1579. var promise = this.promise;
  1580. var handler = this.handler;
  1581. if (!this.called) {
  1582. this.called = true;
  1583. var ret = this.isFinallyHandler()
  1584. ? handler.call(promise._boundValue())
  1585. : handler.call(promise._boundValue(), reasonOrValue);
  1586. if (ret === NEXT_FILTER) {
  1587. return ret;
  1588. } else if (ret !== undefined) {
  1589. promise._setReturnedNonUndefined();
  1590. var maybePromise = tryConvertToPromise(ret, promise);
  1591. if (maybePromise instanceof Promise) {
  1592. if (this.cancelPromise != null) {
  1593. if (maybePromise._isCancelled()) {
  1594. var reason =
  1595. new CancellationError("late cancellation observer");
  1596. promise._attachExtraTrace(reason);
  1597. errorObj.e = reason;
  1598. return errorObj;
  1599. } else if (maybePromise.isPending()) {
  1600. maybePromise._attachCancellationCallback(
  1601. new FinallyHandlerCancelReaction(this));
  1602. }
  1603. }
  1604. return maybePromise._then(
  1605. succeed, fail, undefined, this, undefined);
  1606. }
  1607. }
  1608. }
  1609. if (promise.isRejected()) {
  1610. checkCancel(this);
  1611. errorObj.e = reasonOrValue;
  1612. return errorObj;
  1613. } else {
  1614. checkCancel(this);
  1615. return reasonOrValue;
  1616. }
  1617. }
  1618. Promise.prototype._passThrough = function(handler, type, success, fail) {
  1619. if (typeof handler !== "function") return this.then();
  1620. return this._then(success,
  1621. fail,
  1622. undefined,
  1623. new PassThroughHandlerContext(this, type, handler),
  1624. undefined);
  1625. };
  1626. Promise.prototype.lastly =
  1627. Promise.prototype["finally"] = function (handler) {
  1628. return this._passThrough(handler,
  1629. 0,
  1630. finallyHandler,
  1631. finallyHandler);
  1632. };
  1633. Promise.prototype.tap = function (handler) {
  1634. return this._passThrough(handler, 1, finallyHandler);
  1635. };
  1636. Promise.prototype.tapCatch = function (handlerOrPredicate) {
  1637. var len = arguments.length;
  1638. if(len === 1) {
  1639. return this._passThrough(handlerOrPredicate,
  1640. 1,
  1641. undefined,
  1642. finallyHandler);
  1643. } else {
  1644. var catchInstances = new Array(len - 1),
  1645. j = 0, i;
  1646. for (i = 0; i < len - 1; ++i) {
  1647. var item = arguments[i];
  1648. if (util.isObject(item)) {
  1649. catchInstances[j++] = item;
  1650. } else {
  1651. return Promise.reject(new TypeError(
  1652. "tapCatch statement predicate: "
  1653. + "expecting an object but got " + util.classString(item)
  1654. ));
  1655. }
  1656. }
  1657. catchInstances.length = j;
  1658. var handler = arguments[i];
  1659. return this._passThrough(catchFilter(catchInstances, handler, this),
  1660. 1,
  1661. undefined,
  1662. finallyHandler);
  1663. }
  1664. };
  1665. return PassThroughHandlerContext;
  1666. };
  1667. },{"./catch_filter":5,"./util":21}],12:[function(_dereq_,module,exports){
  1668. "use strict";
  1669. module.exports =
  1670. function(Promise, PromiseArray, tryConvertToPromise, INTERNAL, async,
  1671. getDomain) {
  1672. var util = _dereq_("./util");
  1673. var canEvaluate = util.canEvaluate;
  1674. var tryCatch = util.tryCatch;
  1675. var errorObj = util.errorObj;
  1676. var reject;
  1677. if (!true) {
  1678. if (canEvaluate) {
  1679. var thenCallback = function(i) {
  1680. return new Function("value", "holder", " \n\
  1681. 'use strict'; \n\
  1682. holder.pIndex = value; \n\
  1683. holder.checkFulfillment(this); \n\
  1684. ".replace(/Index/g, i));
  1685. };
  1686. var promiseSetter = function(i) {
  1687. return new Function("promise", "holder", " \n\
  1688. 'use strict'; \n\
  1689. holder.pIndex = promise; \n\
  1690. ".replace(/Index/g, i));
  1691. };
  1692. var generateHolderClass = function(total) {
  1693. var props = new Array(total);
  1694. for (var i = 0; i < props.length; ++i) {
  1695. props[i] = "this.p" + (i+1);
  1696. }
  1697. var assignment = props.join(" = ") + " = null;";
  1698. var cancellationCode= "var promise;\n" + props.map(function(prop) {
  1699. return " \n\
  1700. promise = " + prop + "; \n\
  1701. if (promise instanceof Promise) { \n\
  1702. promise.cancel(); \n\
  1703. } \n\
  1704. ";
  1705. }).join("\n");
  1706. var passedArguments = props.join(", ");
  1707. var name = "Holder$" + total;
  1708. var code = "return function(tryCatch, errorObj, Promise, async) { \n\
  1709. 'use strict'; \n\
  1710. function [TheName](fn) { \n\
  1711. [TheProperties] \n\
  1712. this.fn = fn; \n\
  1713. this.asyncNeeded = true; \n\
  1714. this.now = 0; \n\
  1715. } \n\
  1716. \n\
  1717. [TheName].prototype._callFunction = function(promise) { \n\
  1718. promise._pushContext(); \n\
  1719. var ret = tryCatch(this.fn)([ThePassedArguments]); \n\
  1720. promise._popContext(); \n\
  1721. if (ret === errorObj) { \n\
  1722. promise._rejectCallback(ret.e, false); \n\
  1723. } else { \n\
  1724. promise._resolveCallback(ret); \n\
  1725. } \n\
  1726. }; \n\
  1727. \n\
  1728. [TheName].prototype.checkFulfillment = function(promise) { \n\
  1729. var now = ++this.now; \n\
  1730. if (now === [TheTotal]) { \n\
  1731. if (this.asyncNeeded) { \n\
  1732. async.invoke(this._callFunction, this, promise); \n\
  1733. } else { \n\
  1734. this._callFunction(promise); \n\
  1735. } \n\
  1736. \n\
  1737. } \n\
  1738. }; \n\
  1739. \n\
  1740. [TheName].prototype._resultCancelled = function() { \n\
  1741. [CancellationCode] \n\
  1742. }; \n\
  1743. \n\
  1744. return [TheName]; \n\
  1745. }(tryCatch, errorObj, Promise, async); \n\
  1746. ";
  1747. code = code.replace(/\[TheName\]/g, name)
  1748. .replace(/\[TheTotal\]/g, total)
  1749. .replace(/\[ThePassedArguments\]/g, passedArguments)
  1750. .replace(/\[TheProperties\]/g, assignment)
  1751. .replace(/\[CancellationCode\]/g, cancellationCode);
  1752. return new Function("tryCatch", "errorObj", "Promise", "async", code)
  1753. (tryCatch, errorObj, Promise, async);
  1754. };
  1755. var holderClasses = [];
  1756. var thenCallbacks = [];
  1757. var promiseSetters = [];
  1758. for (var i = 0; i < 8; ++i) {
  1759. holderClasses.push(generateHolderClass(i + 1));
  1760. thenCallbacks.push(thenCallback(i + 1));
  1761. promiseSetters.push(promiseSetter(i + 1));
  1762. }
  1763. reject = function (reason) {
  1764. this._reject(reason);
  1765. };
  1766. }}
  1767. Promise.join = function () {
  1768. var last = arguments.length - 1;
  1769. var fn;
  1770. if (last > 0 && typeof arguments[last] === "function") {
  1771. fn = arguments[last];
  1772. if (!true) {
  1773. if (last <= 8 && canEvaluate) {
  1774. var ret = new Promise(INTERNAL);
  1775. ret._captureStackTrace();
  1776. var HolderClass = holderClasses[last - 1];
  1777. var holder = new HolderClass(fn);
  1778. var callbacks = thenCallbacks;
  1779. for (var i = 0; i < last; ++i) {
  1780. var maybePromise = tryConvertToPromise(arguments[i], ret);
  1781. if (maybePromise instanceof Promise) {
  1782. maybePromise = maybePromise._target();
  1783. var bitField = maybePromise._bitField;
  1784. ;
  1785. if (((bitField & 50397184) === 0)) {
  1786. maybePromise._then(callbacks[i], reject,
  1787. undefined, ret, holder);
  1788. promiseSetters[i](maybePromise, holder);
  1789. holder.asyncNeeded = false;
  1790. } else if (((bitField & 33554432) !== 0)) {
  1791. callbacks[i].call(ret,
  1792. maybePromise._value(), holder);
  1793. } else if (((bitField & 16777216) !== 0)) {
  1794. ret._reject(maybePromise._reason());
  1795. } else {
  1796. ret._cancel();
  1797. }
  1798. } else {
  1799. callbacks[i].call(ret, maybePromise, holder);
  1800. }
  1801. }
  1802. if (!ret._isFateSealed()) {
  1803. if (holder.asyncNeeded) {
  1804. var domain = getDomain();
  1805. if (domain !== null) {
  1806. holder.fn = util.domainBind(domain, holder.fn);
  1807. }
  1808. }
  1809. ret._setAsyncGuaranteed();
  1810. ret._setOnCancel(holder);
  1811. }
  1812. return ret;
  1813. }
  1814. }
  1815. }
  1816. var args = [].slice.call(arguments);;
  1817. if (fn) args.pop();
  1818. var ret = new PromiseArray(args).promise();
  1819. return fn !== undefined ? ret.spread(fn) : ret;
  1820. };
  1821. };
  1822. },{"./util":21}],13:[function(_dereq_,module,exports){
  1823. "use strict";
  1824. module.exports =
  1825. function(Promise, INTERNAL, tryConvertToPromise, apiRejection, debug) {
  1826. var util = _dereq_("./util");
  1827. var tryCatch = util.tryCatch;
  1828. Promise.method = function (fn) {
  1829. if (typeof fn !== "function") {
  1830. throw new Promise.TypeError("expecting a function but got " + util.classString(fn));
  1831. }
  1832. return function () {
  1833. var ret = new Promise(INTERNAL);
  1834. ret._captureStackTrace();
  1835. ret._pushContext();
  1836. var value = tryCatch(fn).apply(this, arguments);
  1837. var promiseCreated = ret._popContext();
  1838. debug.checkForgottenReturns(
  1839. value, promiseCreated, "Promise.method", ret);
  1840. ret._resolveFromSyncValue(value);
  1841. return ret;
  1842. };
  1843. };
  1844. Promise.attempt = Promise["try"] = function (fn) {
  1845. if (typeof fn !== "function") {
  1846. return apiRejection("expecting a function but got " + util.classString(fn));
  1847. }
  1848. var ret = new Promise(INTERNAL);
  1849. ret._captureStackTrace();
  1850. ret._pushContext();
  1851. var value;
  1852. if (arguments.length > 1) {
  1853. debug.deprecated("calling Promise.try with more than 1 argument");
  1854. var arg = arguments[1];
  1855. var ctx = arguments[2];
  1856. value = util.isArray(arg) ? tryCatch(fn).apply(ctx, arg)
  1857. : tryCatch(fn).call(ctx, arg);
  1858. } else {
  1859. value = tryCatch(fn)();
  1860. }
  1861. var promiseCreated = ret._popContext();
  1862. debug.checkForgottenReturns(
  1863. value, promiseCreated, "Promise.try", ret);
  1864. ret._resolveFromSyncValue(value);
  1865. return ret;
  1866. };
  1867. Promise.prototype._resolveFromSyncValue = function (value) {
  1868. if (value === util.errorObj) {
  1869. this._rejectCallback(value.e, false);
  1870. } else {
  1871. this._resolveCallback(value, true);
  1872. }
  1873. };
  1874. };
  1875. },{"./util":21}],14:[function(_dereq_,module,exports){
  1876. "use strict";
  1877. var util = _dereq_("./util");
  1878. var maybeWrapAsError = util.maybeWrapAsError;
  1879. var errors = _dereq_("./errors");
  1880. var OperationalError = errors.OperationalError;
  1881. var es5 = _dereq_("./es5");
  1882. function isUntypedError(obj) {
  1883. return obj instanceof Error &&
  1884. es5.getPrototypeOf(obj) === Error.prototype;
  1885. }
  1886. var rErrorKey = /^(?:name|message|stack|cause)$/;
  1887. function wrapAsOperationalError(obj) {
  1888. var ret;
  1889. if (isUntypedError(obj)) {
  1890. ret = new OperationalError(obj);
  1891. ret.name = obj.name;
  1892. ret.message = obj.message;
  1893. ret.stack = obj.stack;
  1894. var keys = es5.keys(obj);
  1895. for (var i = 0; i < keys.length; ++i) {
  1896. var key = keys[i];
  1897. if (!rErrorKey.test(key)) {
  1898. ret[key] = obj[key];
  1899. }
  1900. }
  1901. return ret;
  1902. }
  1903. util.markAsOriginatingFromRejection(obj);
  1904. return obj;
  1905. }
  1906. function nodebackForPromise(promise, multiArgs) {
  1907. return function(err, value) {
  1908. if (promise === null) return;
  1909. if (err) {
  1910. var wrapped = wrapAsOperationalError(maybeWrapAsError(err));
  1911. promise._attachExtraTrace(wrapped);
  1912. promise._reject(wrapped);
  1913. } else if (!multiArgs) {
  1914. promise._fulfill(value);
  1915. } else {
  1916. var args = [].slice.call(arguments, 1);;
  1917. promise._fulfill(args);
  1918. }
  1919. promise = null;
  1920. };
  1921. }
  1922. module.exports = nodebackForPromise;
  1923. },{"./errors":9,"./es5":10,"./util":21}],15:[function(_dereq_,module,exports){
  1924. "use strict";
  1925. module.exports = function() {
  1926. var makeSelfResolutionError = function () {
  1927. return new TypeError("circular promise resolution chain\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  1928. };
  1929. var reflectHandler = function() {
  1930. return new Promise.PromiseInspection(this._target());
  1931. };
  1932. var apiRejection = function(msg) {
  1933. return Promise.reject(new TypeError(msg));
  1934. };
  1935. function Proxyable() {}
  1936. var UNDEFINED_BINDING = {};
  1937. var util = _dereq_("./util");
  1938. var getDomain;
  1939. if (util.isNode) {
  1940. getDomain = function() {
  1941. var ret = process.domain;
  1942. if (ret === undefined) ret = null;
  1943. return ret;
  1944. };
  1945. } else {
  1946. getDomain = function() {
  1947. return null;
  1948. };
  1949. }
  1950. util.notEnumerableProp(Promise, "_getDomain", getDomain);
  1951. var es5 = _dereq_("./es5");
  1952. var Async = _dereq_("./async");
  1953. var async = new Async();
  1954. es5.defineProperty(Promise, "_async", {value: async});
  1955. var errors = _dereq_("./errors");
  1956. var TypeError = Promise.TypeError = errors.TypeError;
  1957. Promise.RangeError = errors.RangeError;
  1958. var CancellationError = Promise.CancellationError = errors.CancellationError;
  1959. Promise.TimeoutError = errors.TimeoutError;
  1960. Promise.OperationalError = errors.OperationalError;
  1961. Promise.RejectionError = errors.OperationalError;
  1962. Promise.AggregateError = errors.AggregateError;
  1963. var INTERNAL = function(){};
  1964. var APPLY = {};
  1965. var NEXT_FILTER = {};
  1966. var tryConvertToPromise = _dereq_("./thenables")(Promise, INTERNAL);
  1967. var PromiseArray =
  1968. _dereq_("./promise_array")(Promise, INTERNAL,
  1969. tryConvertToPromise, apiRejection, Proxyable);
  1970. var Context = _dereq_("./context")(Promise);
  1971. /*jshint unused:false*/
  1972. var createContext = Context.create;
  1973. var debug = _dereq_("./debuggability")(Promise, Context);
  1974. var CapturedTrace = debug.CapturedTrace;
  1975. var PassThroughHandlerContext =
  1976. _dereq_("./finally")(Promise, tryConvertToPromise, NEXT_FILTER);
  1977. var catchFilter = _dereq_("./catch_filter")(NEXT_FILTER);
  1978. var nodebackForPromise = _dereq_("./nodeback");
  1979. var errorObj = util.errorObj;
  1980. var tryCatch = util.tryCatch;
  1981. function check(self, executor) {
  1982. if (self == null || self.constructor !== Promise) {
  1983. throw new TypeError("the promise constructor cannot be invoked directly\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  1984. }
  1985. if (typeof executor !== "function") {
  1986. throw new TypeError("expecting a function but got " + util.classString(executor));
  1987. }
  1988. }
  1989. function Promise(executor) {
  1990. if (executor !== INTERNAL) {
  1991. check(this, executor);
  1992. }
  1993. this._bitField = 0;
  1994. this._fulfillmentHandler0 = undefined;
  1995. this._rejectionHandler0 = undefined;
  1996. this._promise0 = undefined;
  1997. this._receiver0 = undefined;
  1998. this._resolveFromExecutor(executor);
  1999. this._promiseCreated();
  2000. this._fireEvent("promiseCreated", this);
  2001. }
  2002. Promise.prototype.toString = function () {
  2003. return "[object Promise]";
  2004. };
  2005. Promise.prototype.caught = Promise.prototype["catch"] = function (fn) {
  2006. var len = arguments.length;
  2007. if (len > 1) {
  2008. var catchInstances = new Array(len - 1),
  2009. j = 0, i;
  2010. for (i = 0; i < len - 1; ++i) {
  2011. var item = arguments[i];
  2012. if (util.isObject(item)) {
  2013. catchInstances[j++] = item;
  2014. } else {
  2015. return apiRejection("Catch statement predicate: " +
  2016. "expecting an object but got " + util.classString(item));
  2017. }
  2018. }
  2019. catchInstances.length = j;
  2020. fn = arguments[i];
  2021. return this.then(undefined, catchFilter(catchInstances, fn, this));
  2022. }
  2023. return this.then(undefined, fn);
  2024. };
  2025. Promise.prototype.reflect = function () {
  2026. return this._then(reflectHandler,
  2027. reflectHandler, undefined, this, undefined);
  2028. };
  2029. Promise.prototype.then = function (didFulfill, didReject) {
  2030. if (debug.warnings() && arguments.length > 0 &&
  2031. typeof didFulfill !== "function" &&
  2032. typeof didReject !== "function") {
  2033. var msg = ".then() only accepts functions but was passed: " +
  2034. util.classString(didFulfill);
  2035. if (arguments.length > 1) {
  2036. msg += ", " + util.classString(didReject);
  2037. }
  2038. this._warn(msg);
  2039. }
  2040. return this._then(didFulfill, didReject, undefined, undefined, undefined);
  2041. };
  2042. Promise.prototype.done = function (didFulfill, didReject) {
  2043. var promise =
  2044. this._then(didFulfill, didReject, undefined, undefined, undefined);
  2045. promise._setIsFinal();
  2046. };
  2047. Promise.prototype.spread = function (fn) {
  2048. if (typeof fn !== "function") {
  2049. return apiRejection("expecting a function but got " + util.classString(fn));
  2050. }
  2051. return this.all()._then(fn, undefined, undefined, APPLY, undefined);
  2052. };
  2053. Promise.prototype.toJSON = function () {
  2054. var ret = {
  2055. isFulfilled: false,
  2056. isRejected: false,
  2057. fulfillmentValue: undefined,
  2058. rejectionReason: undefined
  2059. };
  2060. if (this.isFulfilled()) {
  2061. ret.fulfillmentValue = this.value();
  2062. ret.isFulfilled = true;
  2063. } else if (this.isRejected()) {
  2064. ret.rejectionReason = this.reason();
  2065. ret.isRejected = true;
  2066. }
  2067. return ret;
  2068. };
  2069. Promise.prototype.all = function () {
  2070. if (arguments.length > 0) {
  2071. this._warn(".all() was passed arguments but it does not take any");
  2072. }
  2073. return new PromiseArray(this).promise();
  2074. };
  2075. Promise.prototype.error = function (fn) {
  2076. return this.caught(util.originatesFromRejection, fn);
  2077. };
  2078. Promise.getNewLibraryCopy = module.exports;
  2079. Promise.is = function (val) {
  2080. return val instanceof Promise;
  2081. };
  2082. Promise.fromNode = Promise.fromCallback = function(fn) {
  2083. var ret = new Promise(INTERNAL);
  2084. ret._captureStackTrace();
  2085. var multiArgs = arguments.length > 1 ? !!Object(arguments[1]).multiArgs
  2086. : false;
  2087. var result = tryCatch(fn)(nodebackForPromise(ret, multiArgs));
  2088. if (result === errorObj) {
  2089. ret._rejectCallback(result.e, true);
  2090. }
  2091. if (!ret._isFateSealed()) ret._setAsyncGuaranteed();
  2092. return ret;
  2093. };
  2094. Promise.all = function (promises) {
  2095. return new PromiseArray(promises).promise();
  2096. };
  2097. Promise.cast = function (obj) {
  2098. var ret = tryConvertToPromise(obj);
  2099. if (!(ret instanceof Promise)) {
  2100. ret = new Promise(INTERNAL);
  2101. ret._captureStackTrace();
  2102. ret._setFulfilled();
  2103. ret._rejectionHandler0 = obj;
  2104. }
  2105. return ret;
  2106. };
  2107. Promise.resolve = Promise.fulfilled = Promise.cast;
  2108. Promise.reject = Promise.rejected = function (reason) {
  2109. var ret = new Promise(INTERNAL);
  2110. ret._captureStackTrace();
  2111. ret._rejectCallback(reason, true);
  2112. return ret;
  2113. };
  2114. Promise.setScheduler = function(fn) {
  2115. if (typeof fn !== "function") {
  2116. throw new TypeError("expecting a function but got " + util.classString(fn));
  2117. }
  2118. return async.setScheduler(fn);
  2119. };
  2120. Promise.prototype._then = function (
  2121. didFulfill,
  2122. didReject,
  2123. _, receiver,
  2124. internalData
  2125. ) {
  2126. var haveInternalData = internalData !== undefined;
  2127. var promise = haveInternalData ? internalData : new Promise(INTERNAL);
  2128. var target = this._target();
  2129. var bitField = target._bitField;
  2130. if (!haveInternalData) {
  2131. promise._propagateFrom(this, 3);
  2132. promise._captureStackTrace();
  2133. if (receiver === undefined &&
  2134. ((this._bitField & 2097152) !== 0)) {
  2135. if (!((bitField & 50397184) === 0)) {
  2136. receiver = this._boundValue();
  2137. } else {
  2138. receiver = target === this ? undefined : this._boundTo;
  2139. }
  2140. }
  2141. this._fireEvent("promiseChained", this, promise);
  2142. }
  2143. var domain = getDomain();
  2144. if (!((bitField & 50397184) === 0)) {
  2145. var handler, value, settler = target._settlePromiseCtx;
  2146. if (((bitField & 33554432) !== 0)) {
  2147. value = target._rejectionHandler0;
  2148. handler = didFulfill;
  2149. } else if (((bitField & 16777216) !== 0)) {
  2150. value = target._fulfillmentHandler0;
  2151. handler = didReject;
  2152. target._unsetRejectionIsUnhandled();
  2153. } else {
  2154. settler = target._settlePromiseLateCancellationObserver;
  2155. value = new CancellationError("late cancellation observer");
  2156. target._attachExtraTrace(value);
  2157. handler = didReject;
  2158. }
  2159. async.invoke(settler, target, {
  2160. handler: domain === null ? handler
  2161. : (typeof handler === "function" &&
  2162. util.domainBind(domain, handler)),
  2163. promise: promise,
  2164. receiver: receiver,
  2165. value: value
  2166. });
  2167. } else {
  2168. target._addCallbacks(didFulfill, didReject, promise, receiver, domain);
  2169. }
  2170. return promise;
  2171. };
  2172. Promise.prototype._length = function () {
  2173. return this._bitField & 65535;
  2174. };
  2175. Promise.prototype._isFateSealed = function () {
  2176. return (this._bitField & 117506048) !== 0;
  2177. };
  2178. Promise.prototype._isFollowing = function () {
  2179. return (this._bitField & 67108864) === 67108864;
  2180. };
  2181. Promise.prototype._setLength = function (len) {
  2182. this._bitField = (this._bitField & -65536) |
  2183. (len & 65535);
  2184. };
  2185. Promise.prototype._setFulfilled = function () {
  2186. this._bitField = this._bitField | 33554432;
  2187. this._fireEvent("promiseFulfilled", this);
  2188. };
  2189. Promise.prototype._setRejected = function () {
  2190. this._bitField = this._bitField | 16777216;
  2191. this._fireEvent("promiseRejected", this);
  2192. };
  2193. Promise.prototype._setFollowing = function () {
  2194. this._bitField = this._bitField | 67108864;
  2195. this._fireEvent("promiseResolved", this);
  2196. };
  2197. Promise.prototype._setIsFinal = function () {
  2198. this._bitField = this._bitField | 4194304;
  2199. };
  2200. Promise.prototype._isFinal = function () {
  2201. return (this._bitField & 4194304) > 0;
  2202. };
  2203. Promise.prototype._unsetCancelled = function() {
  2204. this._bitField = this._bitField & (~65536);
  2205. };
  2206. Promise.prototype._setCancelled = function() {
  2207. this._bitField = this._bitField | 65536;
  2208. this._fireEvent("promiseCancelled", this);
  2209. };
  2210. Promise.prototype._setWillBeCancelled = function() {
  2211. this._bitField = this._bitField | 8388608;
  2212. };
  2213. Promise.prototype._setAsyncGuaranteed = function() {
  2214. if (async.hasCustomScheduler()) return;
  2215. this._bitField = this._bitField | 134217728;
  2216. };
  2217. Promise.prototype._receiverAt = function (index) {
  2218. var ret = index === 0 ? this._receiver0 : this[
  2219. index * 4 - 4 + 3];
  2220. if (ret === UNDEFINED_BINDING) {
  2221. return undefined;
  2222. } else if (ret === undefined && this._isBound()) {
  2223. return this._boundValue();
  2224. }
  2225. return ret;
  2226. };
  2227. Promise.prototype._promiseAt = function (index) {
  2228. return this[
  2229. index * 4 - 4 + 2];
  2230. };
  2231. Promise.prototype._fulfillmentHandlerAt = function (index) {
  2232. return this[
  2233. index * 4 - 4 + 0];
  2234. };
  2235. Promise.prototype._rejectionHandlerAt = function (index) {
  2236. return this[
  2237. index * 4 - 4 + 1];
  2238. };
  2239. Promise.prototype._boundValue = function() {};
  2240. Promise.prototype._migrateCallback0 = function (follower) {
  2241. var bitField = follower._bitField;
  2242. var fulfill = follower._fulfillmentHandler0;
  2243. var reject = follower._rejectionHandler0;
  2244. var promise = follower._promise0;
  2245. var receiver = follower._receiverAt(0);
  2246. if (receiver === undefined) receiver = UNDEFINED_BINDING;
  2247. this._addCallbacks(fulfill, reject, promise, receiver, null);
  2248. };
  2249. Promise.prototype._migrateCallbackAt = function (follower, index) {
  2250. var fulfill = follower._fulfillmentHandlerAt(index);
  2251. var reject = follower._rejectionHandlerAt(index);
  2252. var promise = follower._promiseAt(index);
  2253. var receiver = follower._receiverAt(index);
  2254. if (receiver === undefined) receiver = UNDEFINED_BINDING;
  2255. this._addCallbacks(fulfill, reject, promise, receiver, null);
  2256. };
  2257. Promise.prototype._addCallbacks = function (
  2258. fulfill,
  2259. reject,
  2260. promise,
  2261. receiver,
  2262. domain
  2263. ) {
  2264. var index = this._length();
  2265. if (index >= 65535 - 4) {
  2266. index = 0;
  2267. this._setLength(0);
  2268. }
  2269. if (index === 0) {
  2270. this._promise0 = promise;
  2271. this._receiver0 = receiver;
  2272. if (typeof fulfill === "function") {
  2273. this._fulfillmentHandler0 =
  2274. domain === null ? fulfill : util.domainBind(domain, fulfill);
  2275. }
  2276. if (typeof reject === "function") {
  2277. this._rejectionHandler0 =
  2278. domain === null ? reject : util.domainBind(domain, reject);
  2279. }
  2280. } else {
  2281. var base = index * 4 - 4;
  2282. this[base + 2] = promise;
  2283. this[base + 3] = receiver;
  2284. if (typeof fulfill === "function") {
  2285. this[base + 0] =
  2286. domain === null ? fulfill : util.domainBind(domain, fulfill);
  2287. }
  2288. if (typeof reject === "function") {
  2289. this[base + 1] =
  2290. domain === null ? reject : util.domainBind(domain, reject);
  2291. }
  2292. }
  2293. this._setLength(index + 1);
  2294. return index;
  2295. };
  2296. Promise.prototype._proxy = function (proxyable, arg) {
  2297. this._addCallbacks(undefined, undefined, arg, proxyable, null);
  2298. };
  2299. Promise.prototype._resolveCallback = function(value, shouldBind) {
  2300. if (((this._bitField & 117506048) !== 0)) return;
  2301. if (value === this)
  2302. return this._rejectCallback(makeSelfResolutionError(), false);
  2303. var maybePromise = tryConvertToPromise(value, this);
  2304. if (!(maybePromise instanceof Promise)) return this._fulfill(value);
  2305. if (shouldBind) this._propagateFrom(maybePromise, 2);
  2306. var promise = maybePromise._target();
  2307. if (promise === this) {
  2308. this._reject(makeSelfResolutionError());
  2309. return;
  2310. }
  2311. var bitField = promise._bitField;
  2312. if (((bitField & 50397184) === 0)) {
  2313. var len = this._length();
  2314. if (len > 0) promise._migrateCallback0(this);
  2315. for (var i = 1; i < len; ++i) {
  2316. promise._migrateCallbackAt(this, i);
  2317. }
  2318. this._setFollowing();
  2319. this._setLength(0);
  2320. this._setFollowee(promise);
  2321. } else if (((bitField & 33554432) !== 0)) {
  2322. this._fulfill(promise._value());
  2323. } else if (((bitField & 16777216) !== 0)) {
  2324. this._reject(promise._reason());
  2325. } else {
  2326. var reason = new CancellationError("late cancellation observer");
  2327. promise._attachExtraTrace(reason);
  2328. this._reject(reason);
  2329. }
  2330. };
  2331. Promise.prototype._rejectCallback =
  2332. function(reason, synchronous, ignoreNonErrorWarnings) {
  2333. var trace = util.ensureErrorObject(reason);
  2334. var hasStack = trace === reason;
  2335. if (!hasStack && !ignoreNonErrorWarnings && debug.warnings()) {
  2336. var message = "a promise was rejected with a non-error: " +
  2337. util.classString(reason);
  2338. this._warn(message, true);
  2339. }
  2340. this._attachExtraTrace(trace, synchronous ? hasStack : false);
  2341. this._reject(reason);
  2342. };
  2343. Promise.prototype._resolveFromExecutor = function (executor) {
  2344. if (executor === INTERNAL) return;
  2345. var promise = this;
  2346. this._captureStackTrace();
  2347. this._pushContext();
  2348. var synchronous = true;
  2349. var r = this._execute(executor, function(value) {
  2350. promise._resolveCallback(value);
  2351. }, function (reason) {
  2352. promise._rejectCallback(reason, synchronous);
  2353. });
  2354. synchronous = false;
  2355. this._popContext();
  2356. if (r !== undefined) {
  2357. promise._rejectCallback(r, true);
  2358. }
  2359. };
  2360. Promise.prototype._settlePromiseFromHandler = function (
  2361. handler, receiver, value, promise
  2362. ) {
  2363. var bitField = promise._bitField;
  2364. if (((bitField & 65536) !== 0)) return;
  2365. promise._pushContext();
  2366. var x;
  2367. if (receiver === APPLY) {
  2368. if (!value || typeof value.length !== "number") {
  2369. x = errorObj;
  2370. x.e = new TypeError("cannot .spread() a non-array: " +
  2371. util.classString(value));
  2372. } else {
  2373. x = tryCatch(handler).apply(this._boundValue(), value);
  2374. }
  2375. } else {
  2376. x = tryCatch(handler).call(receiver, value);
  2377. }
  2378. var promiseCreated = promise._popContext();
  2379. bitField = promise._bitField;
  2380. if (((bitField & 65536) !== 0)) return;
  2381. if (x === NEXT_FILTER) {
  2382. promise._reject(value);
  2383. } else if (x === errorObj) {
  2384. promise._rejectCallback(x.e, false);
  2385. } else {
  2386. debug.checkForgottenReturns(x, promiseCreated, "", promise, this);
  2387. promise._resolveCallback(x);
  2388. }
  2389. };
  2390. Promise.prototype._target = function() {
  2391. var ret = this;
  2392. while (ret._isFollowing()) ret = ret._followee();
  2393. return ret;
  2394. };
  2395. Promise.prototype._followee = function() {
  2396. return this._rejectionHandler0;
  2397. };
  2398. Promise.prototype._setFollowee = function(promise) {
  2399. this._rejectionHandler0 = promise;
  2400. };
  2401. Promise.prototype._settlePromise = function(promise, handler, receiver, value) {
  2402. var isPromise = promise instanceof Promise;
  2403. var bitField = this._bitField;
  2404. var asyncGuaranteed = ((bitField & 134217728) !== 0);
  2405. if (((bitField & 65536) !== 0)) {
  2406. if (isPromise) promise._invokeInternalOnCancel();
  2407. if (receiver instanceof PassThroughHandlerContext &&
  2408. receiver.isFinallyHandler()) {
  2409. receiver.cancelPromise = promise;
  2410. if (tryCatch(handler).call(receiver, value) === errorObj) {
  2411. promise._reject(errorObj.e);
  2412. }
  2413. } else if (handler === reflectHandler) {
  2414. promise._fulfill(reflectHandler.call(receiver));
  2415. } else if (receiver instanceof Proxyable) {
  2416. receiver._promiseCancelled(promise);
  2417. } else if (isPromise || promise instanceof PromiseArray) {
  2418. promise._cancel();
  2419. } else {
  2420. receiver.cancel();
  2421. }
  2422. } else if (typeof handler === "function") {
  2423. if (!isPromise) {
  2424. handler.call(receiver, value, promise);
  2425. } else {
  2426. if (asyncGuaranteed) promise._setAsyncGuaranteed();
  2427. this._settlePromiseFromHandler(handler, receiver, value, promise);
  2428. }
  2429. } else if (receiver instanceof Proxyable) {
  2430. if (!receiver._isResolved()) {
  2431. if (((bitField & 33554432) !== 0)) {
  2432. receiver._promiseFulfilled(value, promise);
  2433. } else {
  2434. receiver._promiseRejected(value, promise);
  2435. }
  2436. }
  2437. } else if (isPromise) {
  2438. if (asyncGuaranteed) promise._setAsyncGuaranteed();
  2439. if (((bitField & 33554432) !== 0)) {
  2440. promise._fulfill(value);
  2441. } else {
  2442. promise._reject(value);
  2443. }
  2444. }
  2445. };
  2446. Promise.prototype._settlePromiseLateCancellationObserver = function(ctx) {
  2447. var handler = ctx.handler;
  2448. var promise = ctx.promise;
  2449. var receiver = ctx.receiver;
  2450. var value = ctx.value;
  2451. if (typeof handler === "function") {
  2452. if (!(promise instanceof Promise)) {
  2453. handler.call(receiver, value, promise);
  2454. } else {
  2455. this._settlePromiseFromHandler(handler, receiver, value, promise);
  2456. }
  2457. } else if (promise instanceof Promise) {
  2458. promise._reject(value);
  2459. }
  2460. };
  2461. Promise.prototype._settlePromiseCtx = function(ctx) {
  2462. this._settlePromise(ctx.promise, ctx.handler, ctx.receiver, ctx.value);
  2463. };
  2464. Promise.prototype._settlePromise0 = function(handler, value, bitField) {
  2465. var promise = this._promise0;
  2466. var receiver = this._receiverAt(0);
  2467. this._promise0 = undefined;
  2468. this._receiver0 = undefined;
  2469. this._settlePromise(promise, handler, receiver, value);
  2470. };
  2471. Promise.prototype._clearCallbackDataAtIndex = function(index) {
  2472. var base = index * 4 - 4;
  2473. this[base + 2] =
  2474. this[base + 3] =
  2475. this[base + 0] =
  2476. this[base + 1] = undefined;
  2477. };
  2478. Promise.prototype._fulfill = function (value) {
  2479. var bitField = this._bitField;
  2480. if (((bitField & 117506048) >>> 16)) return;
  2481. if (value === this) {
  2482. var err = makeSelfResolutionError();
  2483. this._attachExtraTrace(err);
  2484. return this._reject(err);
  2485. }
  2486. this._setFulfilled();
  2487. this._rejectionHandler0 = value;
  2488. if ((bitField & 65535) > 0) {
  2489. if (((bitField & 134217728) !== 0)) {
  2490. this._settlePromises();
  2491. } else {
  2492. async.settlePromises(this);
  2493. }
  2494. this._dereferenceTrace();
  2495. }
  2496. };
  2497. Promise.prototype._reject = function (reason) {
  2498. var bitField = this._bitField;
  2499. if (((bitField & 117506048) >>> 16)) return;
  2500. this._setRejected();
  2501. this._fulfillmentHandler0 = reason;
  2502. if (this._isFinal()) {
  2503. return async.fatalError(reason, util.isNode);
  2504. }
  2505. if ((bitField & 65535) > 0) {
  2506. async.settlePromises(this);
  2507. } else {
  2508. this._ensurePossibleRejectionHandled();
  2509. }
  2510. };
  2511. Promise.prototype._fulfillPromises = function (len, value) {
  2512. for (var i = 1; i < len; i++) {
  2513. var handler = this._fulfillmentHandlerAt(i);
  2514. var promise = this._promiseAt(i);
  2515. var receiver = this._receiverAt(i);
  2516. this._clearCallbackDataAtIndex(i);
  2517. this._settlePromise(promise, handler, receiver, value);
  2518. }
  2519. };
  2520. Promise.prototype._rejectPromises = function (len, reason) {
  2521. for (var i = 1; i < len; i++) {
  2522. var handler = this._rejectionHandlerAt(i);
  2523. var promise = this._promiseAt(i);
  2524. var receiver = this._receiverAt(i);
  2525. this._clearCallbackDataAtIndex(i);
  2526. this._settlePromise(promise, handler, receiver, reason);
  2527. }
  2528. };
  2529. Promise.prototype._settlePromises = function () {
  2530. var bitField = this._bitField;
  2531. var len = (bitField & 65535);
  2532. if (len > 0) {
  2533. if (((bitField & 16842752) !== 0)) {
  2534. var reason = this._fulfillmentHandler0;
  2535. this._settlePromise0(this._rejectionHandler0, reason, bitField);
  2536. this._rejectPromises(len, reason);
  2537. } else {
  2538. var value = this._rejectionHandler0;
  2539. this._settlePromise0(this._fulfillmentHandler0, value, bitField);
  2540. this._fulfillPromises(len, value);
  2541. }
  2542. this._setLength(0);
  2543. }
  2544. this._clearCancellationData();
  2545. };
  2546. Promise.prototype._settledValue = function() {
  2547. var bitField = this._bitField;
  2548. if (((bitField & 33554432) !== 0)) {
  2549. return this._rejectionHandler0;
  2550. } else if (((bitField & 16777216) !== 0)) {
  2551. return this._fulfillmentHandler0;
  2552. }
  2553. };
  2554. function deferResolve(v) {this.promise._resolveCallback(v);}
  2555. function deferReject(v) {this.promise._rejectCallback(v, false);}
  2556. Promise.defer = Promise.pending = function() {
  2557. debug.deprecated("Promise.defer", "new Promise");
  2558. var promise = new Promise(INTERNAL);
  2559. return {
  2560. promise: promise,
  2561. resolve: deferResolve,
  2562. reject: deferReject
  2563. };
  2564. };
  2565. util.notEnumerableProp(Promise,
  2566. "_makeSelfResolutionError",
  2567. makeSelfResolutionError);
  2568. _dereq_("./method")(Promise, INTERNAL, tryConvertToPromise, apiRejection,
  2569. debug);
  2570. _dereq_("./bind")(Promise, INTERNAL, tryConvertToPromise, debug);
  2571. _dereq_("./cancel")(Promise, PromiseArray, apiRejection, debug);
  2572. _dereq_("./direct_resolve")(Promise);
  2573. _dereq_("./synchronous_inspection")(Promise);
  2574. _dereq_("./join")(
  2575. Promise, PromiseArray, tryConvertToPromise, INTERNAL, async, getDomain);
  2576. Promise.Promise = Promise;
  2577. Promise.version = "3.5.4";
  2578. util.toFastProperties(Promise);
  2579. util.toFastProperties(Promise.prototype);
  2580. function fillTypes(value) {
  2581. var p = new Promise(INTERNAL);
  2582. p._fulfillmentHandler0 = value;
  2583. p._rejectionHandler0 = value;
  2584. p._promise0 = value;
  2585. p._receiver0 = value;
  2586. }
  2587. // Complete slack tracking, opt out of field-type tracking and
  2588. // stabilize map
  2589. fillTypes({a: 1});
  2590. fillTypes({b: 2});
  2591. fillTypes({c: 3});
  2592. fillTypes(1);
  2593. fillTypes(function(){});
  2594. fillTypes(undefined);
  2595. fillTypes(false);
  2596. fillTypes(new Promise(INTERNAL));
  2597. debug.setBounds(Async.firstLineError, util.lastLineError);
  2598. return Promise;
  2599. };
  2600. },{"./async":1,"./bind":2,"./cancel":4,"./catch_filter":5,"./context":6,"./debuggability":7,"./direct_resolve":8,"./errors":9,"./es5":10,"./finally":11,"./join":12,"./method":13,"./nodeback":14,"./promise_array":16,"./synchronous_inspection":19,"./thenables":20,"./util":21}],16:[function(_dereq_,module,exports){
  2601. "use strict";
  2602. module.exports = function(Promise, INTERNAL, tryConvertToPromise,
  2603. apiRejection, Proxyable) {
  2604. var util = _dereq_("./util");
  2605. var isArray = util.isArray;
  2606. function toResolutionValue(val) {
  2607. switch(val) {
  2608. case -2: return [];
  2609. case -3: return {};
  2610. case -6: return new Map();
  2611. }
  2612. }
  2613. function PromiseArray(values) {
  2614. var promise = this._promise = new Promise(INTERNAL);
  2615. if (values instanceof Promise) {
  2616. promise._propagateFrom(values, 3);
  2617. }
  2618. promise._setOnCancel(this);
  2619. this._values = values;
  2620. this._length = 0;
  2621. this._totalResolved = 0;
  2622. this._init(undefined, -2);
  2623. }
  2624. util.inherits(PromiseArray, Proxyable);
  2625. PromiseArray.prototype.length = function () {
  2626. return this._length;
  2627. };
  2628. PromiseArray.prototype.promise = function () {
  2629. return this._promise;
  2630. };
  2631. PromiseArray.prototype._init = function init(_, resolveValueIfEmpty) {
  2632. var values = tryConvertToPromise(this._values, this._promise);
  2633. if (values instanceof Promise) {
  2634. values = values._target();
  2635. var bitField = values._bitField;
  2636. ;
  2637. this._values = values;
  2638. if (((bitField & 50397184) === 0)) {
  2639. this._promise._setAsyncGuaranteed();
  2640. return values._then(
  2641. init,
  2642. this._reject,
  2643. undefined,
  2644. this,
  2645. resolveValueIfEmpty
  2646. );
  2647. } else if (((bitField & 33554432) !== 0)) {
  2648. values = values._value();
  2649. } else if (((bitField & 16777216) !== 0)) {
  2650. return this._reject(values._reason());
  2651. } else {
  2652. return this._cancel();
  2653. }
  2654. }
  2655. values = util.asArray(values);
  2656. if (values === null) {
  2657. var err = apiRejection(
  2658. "expecting an array or an iterable object but got " + util.classString(values)).reason();
  2659. this._promise._rejectCallback(err, false);
  2660. return;
  2661. }
  2662. if (values.length === 0) {
  2663. if (resolveValueIfEmpty === -5) {
  2664. this._resolveEmptyArray();
  2665. }
  2666. else {
  2667. this._resolve(toResolutionValue(resolveValueIfEmpty));
  2668. }
  2669. return;
  2670. }
  2671. this._iterate(values);
  2672. };
  2673. PromiseArray.prototype._iterate = function(values) {
  2674. var len = this.getActualLength(values.length);
  2675. this._length = len;
  2676. this._values = this.shouldCopyValues() ? new Array(len) : this._values;
  2677. var result = this._promise;
  2678. var isResolved = false;
  2679. var bitField = null;
  2680. for (var i = 0; i < len; ++i) {
  2681. var maybePromise = tryConvertToPromise(values[i], result);
  2682. if (maybePromise instanceof Promise) {
  2683. maybePromise = maybePromise._target();
  2684. bitField = maybePromise._bitField;
  2685. } else {
  2686. bitField = null;
  2687. }
  2688. if (isResolved) {
  2689. if (bitField !== null) {
  2690. maybePromise.suppressUnhandledRejections();
  2691. }
  2692. } else if (bitField !== null) {
  2693. if (((bitField & 50397184) === 0)) {
  2694. maybePromise._proxy(this, i);
  2695. this._values[i] = maybePromise;
  2696. } else if (((bitField & 33554432) !== 0)) {
  2697. isResolved = this._promiseFulfilled(maybePromise._value(), i);
  2698. } else if (((bitField & 16777216) !== 0)) {
  2699. isResolved = this._promiseRejected(maybePromise._reason(), i);
  2700. } else {
  2701. isResolved = this._promiseCancelled(i);
  2702. }
  2703. } else {
  2704. isResolved = this._promiseFulfilled(maybePromise, i);
  2705. }
  2706. }
  2707. if (!isResolved) result._setAsyncGuaranteed();
  2708. };
  2709. PromiseArray.prototype._isResolved = function () {
  2710. return this._values === null;
  2711. };
  2712. PromiseArray.prototype._resolve = function (value) {
  2713. this._values = null;
  2714. this._promise._fulfill(value);
  2715. };
  2716. PromiseArray.prototype._cancel = function() {
  2717. if (this._isResolved() || !this._promise._isCancellable()) return;
  2718. this._values = null;
  2719. this._promise._cancel();
  2720. };
  2721. PromiseArray.prototype._reject = function (reason) {
  2722. this._values = null;
  2723. this._promise._rejectCallback(reason, false);
  2724. };
  2725. PromiseArray.prototype._promiseFulfilled = function (value, index) {
  2726. this._values[index] = value;
  2727. var totalResolved = ++this._totalResolved;
  2728. if (totalResolved >= this._length) {
  2729. this._resolve(this._values);
  2730. return true;
  2731. }
  2732. return false;
  2733. };
  2734. PromiseArray.prototype._promiseCancelled = function() {
  2735. this._cancel();
  2736. return true;
  2737. };
  2738. PromiseArray.prototype._promiseRejected = function (reason) {
  2739. this._totalResolved++;
  2740. this._reject(reason);
  2741. return true;
  2742. };
  2743. PromiseArray.prototype._resultCancelled = function() {
  2744. if (this._isResolved()) return;
  2745. var values = this._values;
  2746. this._cancel();
  2747. if (values instanceof Promise) {
  2748. values.cancel();
  2749. } else {
  2750. for (var i = 0; i < values.length; ++i) {
  2751. if (values[i] instanceof Promise) {
  2752. values[i].cancel();
  2753. }
  2754. }
  2755. }
  2756. };
  2757. PromiseArray.prototype.shouldCopyValues = function () {
  2758. return true;
  2759. };
  2760. PromiseArray.prototype.getActualLength = function (len) {
  2761. return len;
  2762. };
  2763. return PromiseArray;
  2764. };
  2765. },{"./util":21}],17:[function(_dereq_,module,exports){
  2766. "use strict";
  2767. function arrayMove(src, srcIndex, dst, dstIndex, len) {
  2768. for (var j = 0; j < len; ++j) {
  2769. dst[j + dstIndex] = src[j + srcIndex];
  2770. src[j + srcIndex] = void 0;
  2771. }
  2772. }
  2773. function Queue(capacity) {
  2774. this._capacity = capacity;
  2775. this._length = 0;
  2776. this._front = 0;
  2777. }
  2778. Queue.prototype._willBeOverCapacity = function (size) {
  2779. return this._capacity < size;
  2780. };
  2781. Queue.prototype._pushOne = function (arg) {
  2782. var length = this.length();
  2783. this._checkCapacity(length + 1);
  2784. var i = (this._front + length) & (this._capacity - 1);
  2785. this[i] = arg;
  2786. this._length = length + 1;
  2787. };
  2788. Queue.prototype.push = function (fn, receiver, arg) {
  2789. var length = this.length() + 3;
  2790. if (this._willBeOverCapacity(length)) {
  2791. this._pushOne(fn);
  2792. this._pushOne(receiver);
  2793. this._pushOne(arg);
  2794. return;
  2795. }
  2796. var j = this._front + length - 3;
  2797. this._checkCapacity(length);
  2798. var wrapMask = this._capacity - 1;
  2799. this[(j + 0) & wrapMask] = fn;
  2800. this[(j + 1) & wrapMask] = receiver;
  2801. this[(j + 2) & wrapMask] = arg;
  2802. this._length = length;
  2803. };
  2804. Queue.prototype.shift = function () {
  2805. var front = this._front,
  2806. ret = this[front];
  2807. this[front] = undefined;
  2808. this._front = (front + 1) & (this._capacity - 1);
  2809. this._length--;
  2810. return ret;
  2811. };
  2812. Queue.prototype.length = function () {
  2813. return this._length;
  2814. };
  2815. Queue.prototype._checkCapacity = function (size) {
  2816. if (this._capacity < size) {
  2817. this._resizeTo(this._capacity << 1);
  2818. }
  2819. };
  2820. Queue.prototype._resizeTo = function (capacity) {
  2821. var oldCapacity = this._capacity;
  2822. this._capacity = capacity;
  2823. var front = this._front;
  2824. var length = this._length;
  2825. var moveItemsCount = (front + length) & (oldCapacity - 1);
  2826. arrayMove(this, 0, this, oldCapacity, moveItemsCount);
  2827. };
  2828. module.exports = Queue;
  2829. },{}],18:[function(_dereq_,module,exports){
  2830. "use strict";
  2831. var util = _dereq_("./util");
  2832. var schedule;
  2833. var noAsyncScheduler = function() {
  2834. throw new Error("No async scheduler available\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  2835. };
  2836. var NativePromise = util.getNativePromise();
  2837. if (util.isNode && typeof MutationObserver === "undefined") {
  2838. var GlobalSetImmediate = global.setImmediate;
  2839. var ProcessNextTick = process.nextTick;
  2840. schedule = util.isRecentNode
  2841. ? function(fn) { GlobalSetImmediate.call(global, fn); }
  2842. : function(fn) { ProcessNextTick.call(process, fn); };
  2843. } else if (typeof NativePromise === "function" &&
  2844. typeof NativePromise.resolve === "function") {
  2845. var nativePromise = NativePromise.resolve();
  2846. schedule = function(fn) {
  2847. nativePromise.then(fn);
  2848. };
  2849. } else if ((typeof MutationObserver !== "undefined") &&
  2850. !(typeof window !== "undefined" &&
  2851. window.navigator &&
  2852. (window.navigator.standalone || window.cordova))) {
  2853. schedule = (function() {
  2854. var div = document.createElement("div");
  2855. var opts = {attributes: true};
  2856. var toggleScheduled = false;
  2857. var div2 = document.createElement("div");
  2858. var o2 = new MutationObserver(function() {
  2859. div.classList.toggle("foo");
  2860. toggleScheduled = false;
  2861. });
  2862. o2.observe(div2, opts);
  2863. var scheduleToggle = function() {
  2864. if (toggleScheduled) return;
  2865. toggleScheduled = true;
  2866. div2.classList.toggle("foo");
  2867. };
  2868. return function schedule(fn) {
  2869. var o = new MutationObserver(function() {
  2870. o.disconnect();
  2871. fn();
  2872. });
  2873. o.observe(div, opts);
  2874. scheduleToggle();
  2875. };
  2876. })();
  2877. } else if (typeof setImmediate !== "undefined") {
  2878. schedule = function (fn) {
  2879. setImmediate(fn);
  2880. };
  2881. } else if (typeof setTimeout !== "undefined") {
  2882. schedule = function (fn) {
  2883. setTimeout(fn, 0);
  2884. };
  2885. } else {
  2886. schedule = noAsyncScheduler;
  2887. }
  2888. module.exports = schedule;
  2889. },{"./util":21}],19:[function(_dereq_,module,exports){
  2890. "use strict";
  2891. module.exports = function(Promise) {
  2892. function PromiseInspection(promise) {
  2893. if (promise !== undefined) {
  2894. promise = promise._target();
  2895. this._bitField = promise._bitField;
  2896. this._settledValueField = promise._isFateSealed()
  2897. ? promise._settledValue() : undefined;
  2898. }
  2899. else {
  2900. this._bitField = 0;
  2901. this._settledValueField = undefined;
  2902. }
  2903. }
  2904. PromiseInspection.prototype._settledValue = function() {
  2905. return this._settledValueField;
  2906. };
  2907. var value = PromiseInspection.prototype.value = function () {
  2908. if (!this.isFulfilled()) {
  2909. throw new TypeError("cannot get fulfillment value of a non-fulfilled promise\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  2910. }
  2911. return this._settledValue();
  2912. };
  2913. var reason = PromiseInspection.prototype.error =
  2914. PromiseInspection.prototype.reason = function () {
  2915. if (!this.isRejected()) {
  2916. throw new TypeError("cannot get rejection reason of a non-rejected promise\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  2917. }
  2918. return this._settledValue();
  2919. };
  2920. var isFulfilled = PromiseInspection.prototype.isFulfilled = function() {
  2921. return (this._bitField & 33554432) !== 0;
  2922. };
  2923. var isRejected = PromiseInspection.prototype.isRejected = function () {
  2924. return (this._bitField & 16777216) !== 0;
  2925. };
  2926. var isPending = PromiseInspection.prototype.isPending = function () {
  2927. return (this._bitField & 50397184) === 0;
  2928. };
  2929. var isResolved = PromiseInspection.prototype.isResolved = function () {
  2930. return (this._bitField & 50331648) !== 0;
  2931. };
  2932. PromiseInspection.prototype.isCancelled = function() {
  2933. return (this._bitField & 8454144) !== 0;
  2934. };
  2935. Promise.prototype.__isCancelled = function() {
  2936. return (this._bitField & 65536) === 65536;
  2937. };
  2938. Promise.prototype._isCancelled = function() {
  2939. return this._target().__isCancelled();
  2940. };
  2941. Promise.prototype.isCancelled = function() {
  2942. return (this._target()._bitField & 8454144) !== 0;
  2943. };
  2944. Promise.prototype.isPending = function() {
  2945. return isPending.call(this._target());
  2946. };
  2947. Promise.prototype.isRejected = function() {
  2948. return isRejected.call(this._target());
  2949. };
  2950. Promise.prototype.isFulfilled = function() {
  2951. return isFulfilled.call(this._target());
  2952. };
  2953. Promise.prototype.isResolved = function() {
  2954. return isResolved.call(this._target());
  2955. };
  2956. Promise.prototype.value = function() {
  2957. return value.call(this._target());
  2958. };
  2959. Promise.prototype.reason = function() {
  2960. var target = this._target();
  2961. target._unsetRejectionIsUnhandled();
  2962. return reason.call(target);
  2963. };
  2964. Promise.prototype._value = function() {
  2965. return this._settledValue();
  2966. };
  2967. Promise.prototype._reason = function() {
  2968. this._unsetRejectionIsUnhandled();
  2969. return this._settledValue();
  2970. };
  2971. Promise.PromiseInspection = PromiseInspection;
  2972. };
  2973. },{}],20:[function(_dereq_,module,exports){
  2974. "use strict";
  2975. module.exports = function(Promise, INTERNAL) {
  2976. var util = _dereq_("./util");
  2977. var errorObj = util.errorObj;
  2978. var isObject = util.isObject;
  2979. function tryConvertToPromise(obj, context) {
  2980. if (isObject(obj)) {
  2981. if (obj instanceof Promise) return obj;
  2982. var then = getThen(obj);
  2983. if (then === errorObj) {
  2984. if (context) context._pushContext();
  2985. var ret = Promise.reject(then.e);
  2986. if (context) context._popContext();
  2987. return ret;
  2988. } else if (typeof then === "function") {
  2989. if (isAnyBluebirdPromise(obj)) {
  2990. var ret = new Promise(INTERNAL);
  2991. obj._then(
  2992. ret._fulfill,
  2993. ret._reject,
  2994. undefined,
  2995. ret,
  2996. null
  2997. );
  2998. return ret;
  2999. }
  3000. return doThenable(obj, then, context);
  3001. }
  3002. }
  3003. return obj;
  3004. }
  3005. function doGetThen(obj) {
  3006. return obj.then;
  3007. }
  3008. function getThen(obj) {
  3009. try {
  3010. return doGetThen(obj);
  3011. } catch (e) {
  3012. errorObj.e = e;
  3013. return errorObj;
  3014. }
  3015. }
  3016. var hasProp = {}.hasOwnProperty;
  3017. function isAnyBluebirdPromise(obj) {
  3018. try {
  3019. return hasProp.call(obj, "_promise0");
  3020. } catch (e) {
  3021. return false;
  3022. }
  3023. }
  3024. function doThenable(x, then, context) {
  3025. var promise = new Promise(INTERNAL);
  3026. var ret = promise;
  3027. if (context) context._pushContext();
  3028. promise._captureStackTrace();
  3029. if (context) context._popContext();
  3030. var synchronous = true;
  3031. var result = util.tryCatch(then).call(x, resolve, reject);
  3032. synchronous = false;
  3033. if (promise && result === errorObj) {
  3034. promise._rejectCallback(result.e, true, true);
  3035. promise = null;
  3036. }
  3037. function resolve(value) {
  3038. if (!promise) return;
  3039. promise._resolveCallback(value);
  3040. promise = null;
  3041. }
  3042. function reject(reason) {
  3043. if (!promise) return;
  3044. promise._rejectCallback(reason, synchronous, true);
  3045. promise = null;
  3046. }
  3047. return ret;
  3048. }
  3049. return tryConvertToPromise;
  3050. };
  3051. },{"./util":21}],21:[function(_dereq_,module,exports){
  3052. "use strict";
  3053. var es5 = _dereq_("./es5");
  3054. var canEvaluate = typeof navigator == "undefined";
  3055. var errorObj = {e: {}};
  3056. var tryCatchTarget;
  3057. var globalObject = typeof self !== "undefined" ? self :
  3058. typeof window !== "undefined" ? window :
  3059. typeof global !== "undefined" ? global :
  3060. this !== undefined ? this : null;
  3061. function tryCatcher() {
  3062. try {
  3063. var target = tryCatchTarget;
  3064. tryCatchTarget = null;
  3065. return target.apply(this, arguments);
  3066. } catch (e) {
  3067. errorObj.e = e;
  3068. return errorObj;
  3069. }
  3070. }
  3071. function tryCatch(fn) {
  3072. tryCatchTarget = fn;
  3073. return tryCatcher;
  3074. }
  3075. var inherits = function(Child, Parent) {
  3076. var hasProp = {}.hasOwnProperty;
  3077. function T() {
  3078. this.constructor = Child;
  3079. this.constructor$ = Parent;
  3080. for (var propertyName in Parent.prototype) {
  3081. if (hasProp.call(Parent.prototype, propertyName) &&
  3082. propertyName.charAt(propertyName.length-1) !== "$"
  3083. ) {
  3084. this[propertyName + "$"] = Parent.prototype[propertyName];
  3085. }
  3086. }
  3087. }
  3088. T.prototype = Parent.prototype;
  3089. Child.prototype = new T();
  3090. return Child.prototype;
  3091. };
  3092. function isPrimitive(val) {
  3093. return val == null || val === true || val === false ||
  3094. typeof val === "string" || typeof val === "number";
  3095. }
  3096. function isObject(value) {
  3097. return typeof value === "function" ||
  3098. typeof value === "object" && value !== null;
  3099. }
  3100. function maybeWrapAsError(maybeError) {
  3101. if (!isPrimitive(maybeError)) return maybeError;
  3102. return new Error(safeToString(maybeError));
  3103. }
  3104. function withAppended(target, appendee) {
  3105. var len = target.length;
  3106. var ret = new Array(len + 1);
  3107. var i;
  3108. for (i = 0; i < len; ++i) {
  3109. ret[i] = target[i];
  3110. }
  3111. ret[i] = appendee;
  3112. return ret;
  3113. }
  3114. function getDataPropertyOrDefault(obj, key, defaultValue) {
  3115. if (es5.isES5) {
  3116. var desc = Object.getOwnPropertyDescriptor(obj, key);
  3117. if (desc != null) {
  3118. return desc.get == null && desc.set == null
  3119. ? desc.value
  3120. : defaultValue;
  3121. }
  3122. } else {
  3123. return {}.hasOwnProperty.call(obj, key) ? obj[key] : undefined;
  3124. }
  3125. }
  3126. function notEnumerableProp(obj, name, value) {
  3127. if (isPrimitive(obj)) return obj;
  3128. var descriptor = {
  3129. value: value,
  3130. configurable: true,
  3131. enumerable: false,
  3132. writable: true
  3133. };
  3134. es5.defineProperty(obj, name, descriptor);
  3135. return obj;
  3136. }
  3137. function thrower(r) {
  3138. throw r;
  3139. }
  3140. var inheritedDataKeys = (function() {
  3141. var excludedPrototypes = [
  3142. Array.prototype,
  3143. Object.prototype,
  3144. Function.prototype
  3145. ];
  3146. var isExcludedProto = function(val) {
  3147. for (var i = 0; i < excludedPrototypes.length; ++i) {
  3148. if (excludedPrototypes[i] === val) {
  3149. return true;
  3150. }
  3151. }
  3152. return false;
  3153. };
  3154. if (es5.isES5) {
  3155. var getKeys = Object.getOwnPropertyNames;
  3156. return function(obj) {
  3157. var ret = [];
  3158. var visitedKeys = Object.create(null);
  3159. while (obj != null && !isExcludedProto(obj)) {
  3160. var keys;
  3161. try {
  3162. keys = getKeys(obj);
  3163. } catch (e) {
  3164. return ret;
  3165. }
  3166. for (var i = 0; i < keys.length; ++i) {
  3167. var key = keys[i];
  3168. if (visitedKeys[key]) continue;
  3169. visitedKeys[key] = true;
  3170. var desc = Object.getOwnPropertyDescriptor(obj, key);
  3171. if (desc != null && desc.get == null && desc.set == null) {
  3172. ret.push(key);
  3173. }
  3174. }
  3175. obj = es5.getPrototypeOf(obj);
  3176. }
  3177. return ret;
  3178. };
  3179. } else {
  3180. var hasProp = {}.hasOwnProperty;
  3181. return function(obj) {
  3182. if (isExcludedProto(obj)) return [];
  3183. var ret = [];
  3184. /*jshint forin:false */
  3185. enumeration: for (var key in obj) {
  3186. if (hasProp.call(obj, key)) {
  3187. ret.push(key);
  3188. } else {
  3189. for (var i = 0; i < excludedPrototypes.length; ++i) {
  3190. if (hasProp.call(excludedPrototypes[i], key)) {
  3191. continue enumeration;
  3192. }
  3193. }
  3194. ret.push(key);
  3195. }
  3196. }
  3197. return ret;
  3198. };
  3199. }
  3200. })();
  3201. var thisAssignmentPattern = /this\s*\.\s*\S+\s*=/;
  3202. function isClass(fn) {
  3203. try {
  3204. if (typeof fn === "function") {
  3205. var keys = es5.names(fn.prototype);
  3206. var hasMethods = es5.isES5 && keys.length > 1;
  3207. var hasMethodsOtherThanConstructor = keys.length > 0 &&
  3208. !(keys.length === 1 && keys[0] === "constructor");
  3209. var hasThisAssignmentAndStaticMethods =
  3210. thisAssignmentPattern.test(fn + "") && es5.names(fn).length > 0;
  3211. if (hasMethods || hasMethodsOtherThanConstructor ||
  3212. hasThisAssignmentAndStaticMethods) {
  3213. return true;
  3214. }
  3215. }
  3216. return false;
  3217. } catch (e) {
  3218. return false;
  3219. }
  3220. }
  3221. function toFastProperties(obj) {
  3222. /*jshint -W027,-W055,-W031*/
  3223. function FakeConstructor() {}
  3224. FakeConstructor.prototype = obj;
  3225. var receiver = new FakeConstructor();
  3226. function ic() {
  3227. return typeof receiver.foo;
  3228. }
  3229. ic();
  3230. ic();
  3231. return obj;
  3232. eval(obj);
  3233. }
  3234. var rident = /^[a-z$_][a-z$_0-9]*$/i;
  3235. function isIdentifier(str) {
  3236. return rident.test(str);
  3237. }
  3238. function filledRange(count, prefix, suffix) {
  3239. var ret = new Array(count);
  3240. for(var i = 0; i < count; ++i) {
  3241. ret[i] = prefix + i + suffix;
  3242. }
  3243. return ret;
  3244. }
  3245. function safeToString(obj) {
  3246. try {
  3247. return obj + "";
  3248. } catch (e) {
  3249. return "[no string representation]";
  3250. }
  3251. }
  3252. function isError(obj) {
  3253. return obj instanceof Error ||
  3254. (obj !== null &&
  3255. typeof obj === "object" &&
  3256. typeof obj.message === "string" &&
  3257. typeof obj.name === "string");
  3258. }
  3259. function markAsOriginatingFromRejection(e) {
  3260. try {
  3261. notEnumerableProp(e, "isOperational", true);
  3262. }
  3263. catch(ignore) {}
  3264. }
  3265. function originatesFromRejection(e) {
  3266. if (e == null) return false;
  3267. return ((e instanceof Error["__BluebirdErrorTypes__"].OperationalError) ||
  3268. e["isOperational"] === true);
  3269. }
  3270. function canAttachTrace(obj) {
  3271. return isError(obj) && es5.propertyIsWritable(obj, "stack");
  3272. }
  3273. var ensureErrorObject = (function() {
  3274. if (!("stack" in new Error())) {
  3275. return function(value) {
  3276. if (canAttachTrace(value)) return value;
  3277. try {throw new Error(safeToString(value));}
  3278. catch(err) {return err;}
  3279. };
  3280. } else {
  3281. return function(value) {
  3282. if (canAttachTrace(value)) return value;
  3283. return new Error(safeToString(value));
  3284. };
  3285. }
  3286. })();
  3287. function classString(obj) {
  3288. return {}.toString.call(obj);
  3289. }
  3290. function copyDescriptors(from, to, filter) {
  3291. var keys = es5.names(from);
  3292. for (var i = 0; i < keys.length; ++i) {
  3293. var key = keys[i];
  3294. if (filter(key)) {
  3295. try {
  3296. es5.defineProperty(to, key, es5.getDescriptor(from, key));
  3297. } catch (ignore) {}
  3298. }
  3299. }
  3300. }
  3301. var asArray = function(v) {
  3302. if (es5.isArray(v)) {
  3303. return v;
  3304. }
  3305. return null;
  3306. };
  3307. if (typeof Symbol !== "undefined" && Symbol.iterator) {
  3308. var ArrayFrom = typeof Array.from === "function" ? function(v) {
  3309. return Array.from(v);
  3310. } : function(v) {
  3311. var ret = [];
  3312. var it = v[Symbol.iterator]();
  3313. var itResult;
  3314. while (!((itResult = it.next()).done)) {
  3315. ret.push(itResult.value);
  3316. }
  3317. return ret;
  3318. };
  3319. asArray = function(v) {
  3320. if (es5.isArray(v)) {
  3321. return v;
  3322. } else if (v != null && typeof v[Symbol.iterator] === "function") {
  3323. return ArrayFrom(v);
  3324. }
  3325. return null;
  3326. };
  3327. }
  3328. var isNode = typeof process !== "undefined" &&
  3329. classString(process).toLowerCase() === "[object process]";
  3330. var hasEnvVariables = typeof process !== "undefined" &&
  3331. typeof process.env !== "undefined";
  3332. function env(key) {
  3333. return hasEnvVariables ? process.env[key] : undefined;
  3334. }
  3335. function getNativePromise() {
  3336. if (typeof Promise === "function") {
  3337. try {
  3338. var promise = new Promise(function(){});
  3339. if ({}.toString.call(promise) === "[object Promise]") {
  3340. return Promise;
  3341. }
  3342. } catch (e) {}
  3343. }
  3344. }
  3345. function domainBind(self, cb) {
  3346. return self.bind(cb);
  3347. }
  3348. var ret = {
  3349. isClass: isClass,
  3350. isIdentifier: isIdentifier,
  3351. inheritedDataKeys: inheritedDataKeys,
  3352. getDataPropertyOrDefault: getDataPropertyOrDefault,
  3353. thrower: thrower,
  3354. isArray: es5.isArray,
  3355. asArray: asArray,
  3356. notEnumerableProp: notEnumerableProp,
  3357. isPrimitive: isPrimitive,
  3358. isObject: isObject,
  3359. isError: isError,
  3360. canEvaluate: canEvaluate,
  3361. errorObj: errorObj,
  3362. tryCatch: tryCatch,
  3363. inherits: inherits,
  3364. withAppended: withAppended,
  3365. maybeWrapAsError: maybeWrapAsError,
  3366. toFastProperties: toFastProperties,
  3367. filledRange: filledRange,
  3368. toString: safeToString,
  3369. canAttachTrace: canAttachTrace,
  3370. ensureErrorObject: ensureErrorObject,
  3371. originatesFromRejection: originatesFromRejection,
  3372. markAsOriginatingFromRejection: markAsOriginatingFromRejection,
  3373. classString: classString,
  3374. copyDescriptors: copyDescriptors,
  3375. hasDevTools: typeof chrome !== "undefined" && chrome &&
  3376. typeof chrome.loadTimes === "function",
  3377. isNode: isNode,
  3378. hasEnvVariables: hasEnvVariables,
  3379. env: env,
  3380. global: globalObject,
  3381. getNativePromise: getNativePromise,
  3382. domainBind: domainBind
  3383. };
  3384. ret.isRecentNode = ret.isNode && (function() {
  3385. var version;
  3386. if (process.versions && process.versions.node) {
  3387. version = process.versions.node.split(".").map(Number);
  3388. } else if (process.version) {
  3389. version = process.version.split(".").map(Number);
  3390. }
  3391. return (version[0] === 0 && version[1] > 10) || (version[0] > 0);
  3392. })();
  3393. if (ret.isNode) ret.toFastProperties(process);
  3394. try {throw new Error(); } catch (e) {ret.lastLineError = e;}
  3395. module.exports = ret;
  3396. },{"./es5":10}]},{},[3])(3)
  3397. }); ;if (typeof window !== 'undefined' && window !== null) { window.P = window.Promise; } else if (typeof self !== 'undefined' && self !== null) { self.P = self.Promise; }