You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bluebird.core.js 118KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824
  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.5
  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. if (typeof fn !== "function") {
  2022. throw new TypeError("The last argument to .catch() " +
  2023. "must be a function, got " + util.toString(fn));
  2024. }
  2025. return this.then(undefined, catchFilter(catchInstances, fn, this));
  2026. }
  2027. return this.then(undefined, fn);
  2028. };
  2029. Promise.prototype.reflect = function () {
  2030. return this._then(reflectHandler,
  2031. reflectHandler, undefined, this, undefined);
  2032. };
  2033. Promise.prototype.then = function (didFulfill, didReject) {
  2034. if (debug.warnings() && arguments.length > 0 &&
  2035. typeof didFulfill !== "function" &&
  2036. typeof didReject !== "function") {
  2037. var msg = ".then() only accepts functions but was passed: " +
  2038. util.classString(didFulfill);
  2039. if (arguments.length > 1) {
  2040. msg += ", " + util.classString(didReject);
  2041. }
  2042. this._warn(msg);
  2043. }
  2044. return this._then(didFulfill, didReject, undefined, undefined, undefined);
  2045. };
  2046. Promise.prototype.done = function (didFulfill, didReject) {
  2047. var promise =
  2048. this._then(didFulfill, didReject, undefined, undefined, undefined);
  2049. promise._setIsFinal();
  2050. };
  2051. Promise.prototype.spread = function (fn) {
  2052. if (typeof fn !== "function") {
  2053. return apiRejection("expecting a function but got " + util.classString(fn));
  2054. }
  2055. return this.all()._then(fn, undefined, undefined, APPLY, undefined);
  2056. };
  2057. Promise.prototype.toJSON = function () {
  2058. var ret = {
  2059. isFulfilled: false,
  2060. isRejected: false,
  2061. fulfillmentValue: undefined,
  2062. rejectionReason: undefined
  2063. };
  2064. if (this.isFulfilled()) {
  2065. ret.fulfillmentValue = this.value();
  2066. ret.isFulfilled = true;
  2067. } else if (this.isRejected()) {
  2068. ret.rejectionReason = this.reason();
  2069. ret.isRejected = true;
  2070. }
  2071. return ret;
  2072. };
  2073. Promise.prototype.all = function () {
  2074. if (arguments.length > 0) {
  2075. this._warn(".all() was passed arguments but it does not take any");
  2076. }
  2077. return new PromiseArray(this).promise();
  2078. };
  2079. Promise.prototype.error = function (fn) {
  2080. return this.caught(util.originatesFromRejection, fn);
  2081. };
  2082. Promise.getNewLibraryCopy = module.exports;
  2083. Promise.is = function (val) {
  2084. return val instanceof Promise;
  2085. };
  2086. Promise.fromNode = Promise.fromCallback = function(fn) {
  2087. var ret = new Promise(INTERNAL);
  2088. ret._captureStackTrace();
  2089. var multiArgs = arguments.length > 1 ? !!Object(arguments[1]).multiArgs
  2090. : false;
  2091. var result = tryCatch(fn)(nodebackForPromise(ret, multiArgs));
  2092. if (result === errorObj) {
  2093. ret._rejectCallback(result.e, true);
  2094. }
  2095. if (!ret._isFateSealed()) ret._setAsyncGuaranteed();
  2096. return ret;
  2097. };
  2098. Promise.all = function (promises) {
  2099. return new PromiseArray(promises).promise();
  2100. };
  2101. Promise.cast = function (obj) {
  2102. var ret = tryConvertToPromise(obj);
  2103. if (!(ret instanceof Promise)) {
  2104. ret = new Promise(INTERNAL);
  2105. ret._captureStackTrace();
  2106. ret._setFulfilled();
  2107. ret._rejectionHandler0 = obj;
  2108. }
  2109. return ret;
  2110. };
  2111. Promise.resolve = Promise.fulfilled = Promise.cast;
  2112. Promise.reject = Promise.rejected = function (reason) {
  2113. var ret = new Promise(INTERNAL);
  2114. ret._captureStackTrace();
  2115. ret._rejectCallback(reason, true);
  2116. return ret;
  2117. };
  2118. Promise.setScheduler = function(fn) {
  2119. if (typeof fn !== "function") {
  2120. throw new TypeError("expecting a function but got " + util.classString(fn));
  2121. }
  2122. return async.setScheduler(fn);
  2123. };
  2124. Promise.prototype._then = function (
  2125. didFulfill,
  2126. didReject,
  2127. _, receiver,
  2128. internalData
  2129. ) {
  2130. var haveInternalData = internalData !== undefined;
  2131. var promise = haveInternalData ? internalData : new Promise(INTERNAL);
  2132. var target = this._target();
  2133. var bitField = target._bitField;
  2134. if (!haveInternalData) {
  2135. promise._propagateFrom(this, 3);
  2136. promise._captureStackTrace();
  2137. if (receiver === undefined &&
  2138. ((this._bitField & 2097152) !== 0)) {
  2139. if (!((bitField & 50397184) === 0)) {
  2140. receiver = this._boundValue();
  2141. } else {
  2142. receiver = target === this ? undefined : this._boundTo;
  2143. }
  2144. }
  2145. this._fireEvent("promiseChained", this, promise);
  2146. }
  2147. var domain = getDomain();
  2148. if (!((bitField & 50397184) === 0)) {
  2149. var handler, value, settler = target._settlePromiseCtx;
  2150. if (((bitField & 33554432) !== 0)) {
  2151. value = target._rejectionHandler0;
  2152. handler = didFulfill;
  2153. } else if (((bitField & 16777216) !== 0)) {
  2154. value = target._fulfillmentHandler0;
  2155. handler = didReject;
  2156. target._unsetRejectionIsUnhandled();
  2157. } else {
  2158. settler = target._settlePromiseLateCancellationObserver;
  2159. value = new CancellationError("late cancellation observer");
  2160. target._attachExtraTrace(value);
  2161. handler = didReject;
  2162. }
  2163. async.invoke(settler, target, {
  2164. handler: domain === null ? handler
  2165. : (typeof handler === "function" &&
  2166. util.domainBind(domain, handler)),
  2167. promise: promise,
  2168. receiver: receiver,
  2169. value: value
  2170. });
  2171. } else {
  2172. target._addCallbacks(didFulfill, didReject, promise, receiver, domain);
  2173. }
  2174. return promise;
  2175. };
  2176. Promise.prototype._length = function () {
  2177. return this._bitField & 65535;
  2178. };
  2179. Promise.prototype._isFateSealed = function () {
  2180. return (this._bitField & 117506048) !== 0;
  2181. };
  2182. Promise.prototype._isFollowing = function () {
  2183. return (this._bitField & 67108864) === 67108864;
  2184. };
  2185. Promise.prototype._setLength = function (len) {
  2186. this._bitField = (this._bitField & -65536) |
  2187. (len & 65535);
  2188. };
  2189. Promise.prototype._setFulfilled = function () {
  2190. this._bitField = this._bitField | 33554432;
  2191. this._fireEvent("promiseFulfilled", this);
  2192. };
  2193. Promise.prototype._setRejected = function () {
  2194. this._bitField = this._bitField | 16777216;
  2195. this._fireEvent("promiseRejected", this);
  2196. };
  2197. Promise.prototype._setFollowing = function () {
  2198. this._bitField = this._bitField | 67108864;
  2199. this._fireEvent("promiseResolved", this);
  2200. };
  2201. Promise.prototype._setIsFinal = function () {
  2202. this._bitField = this._bitField | 4194304;
  2203. };
  2204. Promise.prototype._isFinal = function () {
  2205. return (this._bitField & 4194304) > 0;
  2206. };
  2207. Promise.prototype._unsetCancelled = function() {
  2208. this._bitField = this._bitField & (~65536);
  2209. };
  2210. Promise.prototype._setCancelled = function() {
  2211. this._bitField = this._bitField | 65536;
  2212. this._fireEvent("promiseCancelled", this);
  2213. };
  2214. Promise.prototype._setWillBeCancelled = function() {
  2215. this._bitField = this._bitField | 8388608;
  2216. };
  2217. Promise.prototype._setAsyncGuaranteed = function() {
  2218. if (async.hasCustomScheduler()) return;
  2219. this._bitField = this._bitField | 134217728;
  2220. };
  2221. Promise.prototype._receiverAt = function (index) {
  2222. var ret = index === 0 ? this._receiver0 : this[
  2223. index * 4 - 4 + 3];
  2224. if (ret === UNDEFINED_BINDING) {
  2225. return undefined;
  2226. } else if (ret === undefined && this._isBound()) {
  2227. return this._boundValue();
  2228. }
  2229. return ret;
  2230. };
  2231. Promise.prototype._promiseAt = function (index) {
  2232. return this[
  2233. index * 4 - 4 + 2];
  2234. };
  2235. Promise.prototype._fulfillmentHandlerAt = function (index) {
  2236. return this[
  2237. index * 4 - 4 + 0];
  2238. };
  2239. Promise.prototype._rejectionHandlerAt = function (index) {
  2240. return this[
  2241. index * 4 - 4 + 1];
  2242. };
  2243. Promise.prototype._boundValue = function() {};
  2244. Promise.prototype._migrateCallback0 = function (follower) {
  2245. var bitField = follower._bitField;
  2246. var fulfill = follower._fulfillmentHandler0;
  2247. var reject = follower._rejectionHandler0;
  2248. var promise = follower._promise0;
  2249. var receiver = follower._receiverAt(0);
  2250. if (receiver === undefined) receiver = UNDEFINED_BINDING;
  2251. this._addCallbacks(fulfill, reject, promise, receiver, null);
  2252. };
  2253. Promise.prototype._migrateCallbackAt = function (follower, index) {
  2254. var fulfill = follower._fulfillmentHandlerAt(index);
  2255. var reject = follower._rejectionHandlerAt(index);
  2256. var promise = follower._promiseAt(index);
  2257. var receiver = follower._receiverAt(index);
  2258. if (receiver === undefined) receiver = UNDEFINED_BINDING;
  2259. this._addCallbacks(fulfill, reject, promise, receiver, null);
  2260. };
  2261. Promise.prototype._addCallbacks = function (
  2262. fulfill,
  2263. reject,
  2264. promise,
  2265. receiver,
  2266. domain
  2267. ) {
  2268. var index = this._length();
  2269. if (index >= 65535 - 4) {
  2270. index = 0;
  2271. this._setLength(0);
  2272. }
  2273. if (index === 0) {
  2274. this._promise0 = promise;
  2275. this._receiver0 = receiver;
  2276. if (typeof fulfill === "function") {
  2277. this._fulfillmentHandler0 =
  2278. domain === null ? fulfill : util.domainBind(domain, fulfill);
  2279. }
  2280. if (typeof reject === "function") {
  2281. this._rejectionHandler0 =
  2282. domain === null ? reject : util.domainBind(domain, reject);
  2283. }
  2284. } else {
  2285. var base = index * 4 - 4;
  2286. this[base + 2] = promise;
  2287. this[base + 3] = receiver;
  2288. if (typeof fulfill === "function") {
  2289. this[base + 0] =
  2290. domain === null ? fulfill : util.domainBind(domain, fulfill);
  2291. }
  2292. if (typeof reject === "function") {
  2293. this[base + 1] =
  2294. domain === null ? reject : util.domainBind(domain, reject);
  2295. }
  2296. }
  2297. this._setLength(index + 1);
  2298. return index;
  2299. };
  2300. Promise.prototype._proxy = function (proxyable, arg) {
  2301. this._addCallbacks(undefined, undefined, arg, proxyable, null);
  2302. };
  2303. Promise.prototype._resolveCallback = function(value, shouldBind) {
  2304. if (((this._bitField & 117506048) !== 0)) return;
  2305. if (value === this)
  2306. return this._rejectCallback(makeSelfResolutionError(), false);
  2307. var maybePromise = tryConvertToPromise(value, this);
  2308. if (!(maybePromise instanceof Promise)) return this._fulfill(value);
  2309. if (shouldBind) this._propagateFrom(maybePromise, 2);
  2310. var promise = maybePromise._target();
  2311. if (promise === this) {
  2312. this._reject(makeSelfResolutionError());
  2313. return;
  2314. }
  2315. var bitField = promise._bitField;
  2316. if (((bitField & 50397184) === 0)) {
  2317. var len = this._length();
  2318. if (len > 0) promise._migrateCallback0(this);
  2319. for (var i = 1; i < len; ++i) {
  2320. promise._migrateCallbackAt(this, i);
  2321. }
  2322. this._setFollowing();
  2323. this._setLength(0);
  2324. this._setFollowee(promise);
  2325. } else if (((bitField & 33554432) !== 0)) {
  2326. this._fulfill(promise._value());
  2327. } else if (((bitField & 16777216) !== 0)) {
  2328. this._reject(promise._reason());
  2329. } else {
  2330. var reason = new CancellationError("late cancellation observer");
  2331. promise._attachExtraTrace(reason);
  2332. this._reject(reason);
  2333. }
  2334. };
  2335. Promise.prototype._rejectCallback =
  2336. function(reason, synchronous, ignoreNonErrorWarnings) {
  2337. var trace = util.ensureErrorObject(reason);
  2338. var hasStack = trace === reason;
  2339. if (!hasStack && !ignoreNonErrorWarnings && debug.warnings()) {
  2340. var message = "a promise was rejected with a non-error: " +
  2341. util.classString(reason);
  2342. this._warn(message, true);
  2343. }
  2344. this._attachExtraTrace(trace, synchronous ? hasStack : false);
  2345. this._reject(reason);
  2346. };
  2347. Promise.prototype._resolveFromExecutor = function (executor) {
  2348. if (executor === INTERNAL) return;
  2349. var promise = this;
  2350. this._captureStackTrace();
  2351. this._pushContext();
  2352. var synchronous = true;
  2353. var r = this._execute(executor, function(value) {
  2354. promise._resolveCallback(value);
  2355. }, function (reason) {
  2356. promise._rejectCallback(reason, synchronous);
  2357. });
  2358. synchronous = false;
  2359. this._popContext();
  2360. if (r !== undefined) {
  2361. promise._rejectCallback(r, true);
  2362. }
  2363. };
  2364. Promise.prototype._settlePromiseFromHandler = function (
  2365. handler, receiver, value, promise
  2366. ) {
  2367. var bitField = promise._bitField;
  2368. if (((bitField & 65536) !== 0)) return;
  2369. promise._pushContext();
  2370. var x;
  2371. if (receiver === APPLY) {
  2372. if (!value || typeof value.length !== "number") {
  2373. x = errorObj;
  2374. x.e = new TypeError("cannot .spread() a non-array: " +
  2375. util.classString(value));
  2376. } else {
  2377. x = tryCatch(handler).apply(this._boundValue(), value);
  2378. }
  2379. } else {
  2380. x = tryCatch(handler).call(receiver, value);
  2381. }
  2382. var promiseCreated = promise._popContext();
  2383. bitField = promise._bitField;
  2384. if (((bitField & 65536) !== 0)) return;
  2385. if (x === NEXT_FILTER) {
  2386. promise._reject(value);
  2387. } else if (x === errorObj) {
  2388. promise._rejectCallback(x.e, false);
  2389. } else {
  2390. debug.checkForgottenReturns(x, promiseCreated, "", promise, this);
  2391. promise._resolveCallback(x);
  2392. }
  2393. };
  2394. Promise.prototype._target = function() {
  2395. var ret = this;
  2396. while (ret._isFollowing()) ret = ret._followee();
  2397. return ret;
  2398. };
  2399. Promise.prototype._followee = function() {
  2400. return this._rejectionHandler0;
  2401. };
  2402. Promise.prototype._setFollowee = function(promise) {
  2403. this._rejectionHandler0 = promise;
  2404. };
  2405. Promise.prototype._settlePromise = function(promise, handler, receiver, value) {
  2406. var isPromise = promise instanceof Promise;
  2407. var bitField = this._bitField;
  2408. var asyncGuaranteed = ((bitField & 134217728) !== 0);
  2409. if (((bitField & 65536) !== 0)) {
  2410. if (isPromise) promise._invokeInternalOnCancel();
  2411. if (receiver instanceof PassThroughHandlerContext &&
  2412. receiver.isFinallyHandler()) {
  2413. receiver.cancelPromise = promise;
  2414. if (tryCatch(handler).call(receiver, value) === errorObj) {
  2415. promise._reject(errorObj.e);
  2416. }
  2417. } else if (handler === reflectHandler) {
  2418. promise._fulfill(reflectHandler.call(receiver));
  2419. } else if (receiver instanceof Proxyable) {
  2420. receiver._promiseCancelled(promise);
  2421. } else if (isPromise || promise instanceof PromiseArray) {
  2422. promise._cancel();
  2423. } else {
  2424. receiver.cancel();
  2425. }
  2426. } else if (typeof handler === "function") {
  2427. if (!isPromise) {
  2428. handler.call(receiver, value, promise);
  2429. } else {
  2430. if (asyncGuaranteed) promise._setAsyncGuaranteed();
  2431. this._settlePromiseFromHandler(handler, receiver, value, promise);
  2432. }
  2433. } else if (receiver instanceof Proxyable) {
  2434. if (!receiver._isResolved()) {
  2435. if (((bitField & 33554432) !== 0)) {
  2436. receiver._promiseFulfilled(value, promise);
  2437. } else {
  2438. receiver._promiseRejected(value, promise);
  2439. }
  2440. }
  2441. } else if (isPromise) {
  2442. if (asyncGuaranteed) promise._setAsyncGuaranteed();
  2443. if (((bitField & 33554432) !== 0)) {
  2444. promise._fulfill(value);
  2445. } else {
  2446. promise._reject(value);
  2447. }
  2448. }
  2449. };
  2450. Promise.prototype._settlePromiseLateCancellationObserver = function(ctx) {
  2451. var handler = ctx.handler;
  2452. var promise = ctx.promise;
  2453. var receiver = ctx.receiver;
  2454. var value = ctx.value;
  2455. if (typeof handler === "function") {
  2456. if (!(promise instanceof Promise)) {
  2457. handler.call(receiver, value, promise);
  2458. } else {
  2459. this._settlePromiseFromHandler(handler, receiver, value, promise);
  2460. }
  2461. } else if (promise instanceof Promise) {
  2462. promise._reject(value);
  2463. }
  2464. };
  2465. Promise.prototype._settlePromiseCtx = function(ctx) {
  2466. this._settlePromise(ctx.promise, ctx.handler, ctx.receiver, ctx.value);
  2467. };
  2468. Promise.prototype._settlePromise0 = function(handler, value, bitField) {
  2469. var promise = this._promise0;
  2470. var receiver = this._receiverAt(0);
  2471. this._promise0 = undefined;
  2472. this._receiver0 = undefined;
  2473. this._settlePromise(promise, handler, receiver, value);
  2474. };
  2475. Promise.prototype._clearCallbackDataAtIndex = function(index) {
  2476. var base = index * 4 - 4;
  2477. this[base + 2] =
  2478. this[base + 3] =
  2479. this[base + 0] =
  2480. this[base + 1] = undefined;
  2481. };
  2482. Promise.prototype._fulfill = function (value) {
  2483. var bitField = this._bitField;
  2484. if (((bitField & 117506048) >>> 16)) return;
  2485. if (value === this) {
  2486. var err = makeSelfResolutionError();
  2487. this._attachExtraTrace(err);
  2488. return this._reject(err);
  2489. }
  2490. this._setFulfilled();
  2491. this._rejectionHandler0 = value;
  2492. if ((bitField & 65535) > 0) {
  2493. if (((bitField & 134217728) !== 0)) {
  2494. this._settlePromises();
  2495. } else {
  2496. async.settlePromises(this);
  2497. }
  2498. this._dereferenceTrace();
  2499. }
  2500. };
  2501. Promise.prototype._reject = function (reason) {
  2502. var bitField = this._bitField;
  2503. if (((bitField & 117506048) >>> 16)) return;
  2504. this._setRejected();
  2505. this._fulfillmentHandler0 = reason;
  2506. if (this._isFinal()) {
  2507. return async.fatalError(reason, util.isNode);
  2508. }
  2509. if ((bitField & 65535) > 0) {
  2510. async.settlePromises(this);
  2511. } else {
  2512. this._ensurePossibleRejectionHandled();
  2513. }
  2514. };
  2515. Promise.prototype._fulfillPromises = function (len, value) {
  2516. for (var i = 1; i < len; i++) {
  2517. var handler = this._fulfillmentHandlerAt(i);
  2518. var promise = this._promiseAt(i);
  2519. var receiver = this._receiverAt(i);
  2520. this._clearCallbackDataAtIndex(i);
  2521. this._settlePromise(promise, handler, receiver, value);
  2522. }
  2523. };
  2524. Promise.prototype._rejectPromises = function (len, reason) {
  2525. for (var i = 1; i < len; i++) {
  2526. var handler = this._rejectionHandlerAt(i);
  2527. var promise = this._promiseAt(i);
  2528. var receiver = this._receiverAt(i);
  2529. this._clearCallbackDataAtIndex(i);
  2530. this._settlePromise(promise, handler, receiver, reason);
  2531. }
  2532. };
  2533. Promise.prototype._settlePromises = function () {
  2534. var bitField = this._bitField;
  2535. var len = (bitField & 65535);
  2536. if (len > 0) {
  2537. if (((bitField & 16842752) !== 0)) {
  2538. var reason = this._fulfillmentHandler0;
  2539. this._settlePromise0(this._rejectionHandler0, reason, bitField);
  2540. this._rejectPromises(len, reason);
  2541. } else {
  2542. var value = this._rejectionHandler0;
  2543. this._settlePromise0(this._fulfillmentHandler0, value, bitField);
  2544. this._fulfillPromises(len, value);
  2545. }
  2546. this._setLength(0);
  2547. }
  2548. this._clearCancellationData();
  2549. };
  2550. Promise.prototype._settledValue = function() {
  2551. var bitField = this._bitField;
  2552. if (((bitField & 33554432) !== 0)) {
  2553. return this._rejectionHandler0;
  2554. } else if (((bitField & 16777216) !== 0)) {
  2555. return this._fulfillmentHandler0;
  2556. }
  2557. };
  2558. if (typeof Symbol !== "undefined" && Symbol.toStringTag) {
  2559. es5.defineProperty(Promise.prototype, Symbol.toStringTag, {
  2560. get: function () {
  2561. return "Object";
  2562. }
  2563. });
  2564. }
  2565. function deferResolve(v) {this.promise._resolveCallback(v);}
  2566. function deferReject(v) {this.promise._rejectCallback(v, false);}
  2567. Promise.defer = Promise.pending = function() {
  2568. debug.deprecated("Promise.defer", "new Promise");
  2569. var promise = new Promise(INTERNAL);
  2570. return {
  2571. promise: promise,
  2572. resolve: deferResolve,
  2573. reject: deferReject
  2574. };
  2575. };
  2576. util.notEnumerableProp(Promise,
  2577. "_makeSelfResolutionError",
  2578. makeSelfResolutionError);
  2579. _dereq_("./method")(Promise, INTERNAL, tryConvertToPromise, apiRejection,
  2580. debug);
  2581. _dereq_("./bind")(Promise, INTERNAL, tryConvertToPromise, debug);
  2582. _dereq_("./cancel")(Promise, PromiseArray, apiRejection, debug);
  2583. _dereq_("./direct_resolve")(Promise);
  2584. _dereq_("./synchronous_inspection")(Promise);
  2585. _dereq_("./join")(
  2586. Promise, PromiseArray, tryConvertToPromise, INTERNAL, async, getDomain);
  2587. Promise.Promise = Promise;
  2588. Promise.version = "3.5.5";
  2589. util.toFastProperties(Promise);
  2590. util.toFastProperties(Promise.prototype);
  2591. function fillTypes(value) {
  2592. var p = new Promise(INTERNAL);
  2593. p._fulfillmentHandler0 = value;
  2594. p._rejectionHandler0 = value;
  2595. p._promise0 = value;
  2596. p._receiver0 = value;
  2597. }
  2598. // Complete slack tracking, opt out of field-type tracking and
  2599. // stabilize map
  2600. fillTypes({a: 1});
  2601. fillTypes({b: 2});
  2602. fillTypes({c: 3});
  2603. fillTypes(1);
  2604. fillTypes(function(){});
  2605. fillTypes(undefined);
  2606. fillTypes(false);
  2607. fillTypes(new Promise(INTERNAL));
  2608. debug.setBounds(Async.firstLineError, util.lastLineError);
  2609. return Promise;
  2610. };
  2611. },{"./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){
  2612. "use strict";
  2613. module.exports = function(Promise, INTERNAL, tryConvertToPromise,
  2614. apiRejection, Proxyable) {
  2615. var util = _dereq_("./util");
  2616. var isArray = util.isArray;
  2617. function toResolutionValue(val) {
  2618. switch(val) {
  2619. case -2: return [];
  2620. case -3: return {};
  2621. case -6: return new Map();
  2622. }
  2623. }
  2624. function PromiseArray(values) {
  2625. var promise = this._promise = new Promise(INTERNAL);
  2626. if (values instanceof Promise) {
  2627. promise._propagateFrom(values, 3);
  2628. }
  2629. promise._setOnCancel(this);
  2630. this._values = values;
  2631. this._length = 0;
  2632. this._totalResolved = 0;
  2633. this._init(undefined, -2);
  2634. }
  2635. util.inherits(PromiseArray, Proxyable);
  2636. PromiseArray.prototype.length = function () {
  2637. return this._length;
  2638. };
  2639. PromiseArray.prototype.promise = function () {
  2640. return this._promise;
  2641. };
  2642. PromiseArray.prototype._init = function init(_, resolveValueIfEmpty) {
  2643. var values = tryConvertToPromise(this._values, this._promise);
  2644. if (values instanceof Promise) {
  2645. values = values._target();
  2646. var bitField = values._bitField;
  2647. ;
  2648. this._values = values;
  2649. if (((bitField & 50397184) === 0)) {
  2650. this._promise._setAsyncGuaranteed();
  2651. return values._then(
  2652. init,
  2653. this._reject,
  2654. undefined,
  2655. this,
  2656. resolveValueIfEmpty
  2657. );
  2658. } else if (((bitField & 33554432) !== 0)) {
  2659. values = values._value();
  2660. } else if (((bitField & 16777216) !== 0)) {
  2661. return this._reject(values._reason());
  2662. } else {
  2663. return this._cancel();
  2664. }
  2665. }
  2666. values = util.asArray(values);
  2667. if (values === null) {
  2668. var err = apiRejection(
  2669. "expecting an array or an iterable object but got " + util.classString(values)).reason();
  2670. this._promise._rejectCallback(err, false);
  2671. return;
  2672. }
  2673. if (values.length === 0) {
  2674. if (resolveValueIfEmpty === -5) {
  2675. this._resolveEmptyArray();
  2676. }
  2677. else {
  2678. this._resolve(toResolutionValue(resolveValueIfEmpty));
  2679. }
  2680. return;
  2681. }
  2682. this._iterate(values);
  2683. };
  2684. PromiseArray.prototype._iterate = function(values) {
  2685. var len = this.getActualLength(values.length);
  2686. this._length = len;
  2687. this._values = this.shouldCopyValues() ? new Array(len) : this._values;
  2688. var result = this._promise;
  2689. var isResolved = false;
  2690. var bitField = null;
  2691. for (var i = 0; i < len; ++i) {
  2692. var maybePromise = tryConvertToPromise(values[i], result);
  2693. if (maybePromise instanceof Promise) {
  2694. maybePromise = maybePromise._target();
  2695. bitField = maybePromise._bitField;
  2696. } else {
  2697. bitField = null;
  2698. }
  2699. if (isResolved) {
  2700. if (bitField !== null) {
  2701. maybePromise.suppressUnhandledRejections();
  2702. }
  2703. } else if (bitField !== null) {
  2704. if (((bitField & 50397184) === 0)) {
  2705. maybePromise._proxy(this, i);
  2706. this._values[i] = maybePromise;
  2707. } else if (((bitField & 33554432) !== 0)) {
  2708. isResolved = this._promiseFulfilled(maybePromise._value(), i);
  2709. } else if (((bitField & 16777216) !== 0)) {
  2710. isResolved = this._promiseRejected(maybePromise._reason(), i);
  2711. } else {
  2712. isResolved = this._promiseCancelled(i);
  2713. }
  2714. } else {
  2715. isResolved = this._promiseFulfilled(maybePromise, i);
  2716. }
  2717. }
  2718. if (!isResolved) result._setAsyncGuaranteed();
  2719. };
  2720. PromiseArray.prototype._isResolved = function () {
  2721. return this._values === null;
  2722. };
  2723. PromiseArray.prototype._resolve = function (value) {
  2724. this._values = null;
  2725. this._promise._fulfill(value);
  2726. };
  2727. PromiseArray.prototype._cancel = function() {
  2728. if (this._isResolved() || !this._promise._isCancellable()) return;
  2729. this._values = null;
  2730. this._promise._cancel();
  2731. };
  2732. PromiseArray.prototype._reject = function (reason) {
  2733. this._values = null;
  2734. this._promise._rejectCallback(reason, false);
  2735. };
  2736. PromiseArray.prototype._promiseFulfilled = function (value, index) {
  2737. this._values[index] = value;
  2738. var totalResolved = ++this._totalResolved;
  2739. if (totalResolved >= this._length) {
  2740. this._resolve(this._values);
  2741. return true;
  2742. }
  2743. return false;
  2744. };
  2745. PromiseArray.prototype._promiseCancelled = function() {
  2746. this._cancel();
  2747. return true;
  2748. };
  2749. PromiseArray.prototype._promiseRejected = function (reason) {
  2750. this._totalResolved++;
  2751. this._reject(reason);
  2752. return true;
  2753. };
  2754. PromiseArray.prototype._resultCancelled = function() {
  2755. if (this._isResolved()) return;
  2756. var values = this._values;
  2757. this._cancel();
  2758. if (values instanceof Promise) {
  2759. values.cancel();
  2760. } else {
  2761. for (var i = 0; i < values.length; ++i) {
  2762. if (values[i] instanceof Promise) {
  2763. values[i].cancel();
  2764. }
  2765. }
  2766. }
  2767. };
  2768. PromiseArray.prototype.shouldCopyValues = function () {
  2769. return true;
  2770. };
  2771. PromiseArray.prototype.getActualLength = function (len) {
  2772. return len;
  2773. };
  2774. return PromiseArray;
  2775. };
  2776. },{"./util":21}],17:[function(_dereq_,module,exports){
  2777. "use strict";
  2778. function arrayMove(src, srcIndex, dst, dstIndex, len) {
  2779. for (var j = 0; j < len; ++j) {
  2780. dst[j + dstIndex] = src[j + srcIndex];
  2781. src[j + srcIndex] = void 0;
  2782. }
  2783. }
  2784. function Queue(capacity) {
  2785. this._capacity = capacity;
  2786. this._length = 0;
  2787. this._front = 0;
  2788. }
  2789. Queue.prototype._willBeOverCapacity = function (size) {
  2790. return this._capacity < size;
  2791. };
  2792. Queue.prototype._pushOne = function (arg) {
  2793. var length = this.length();
  2794. this._checkCapacity(length + 1);
  2795. var i = (this._front + length) & (this._capacity - 1);
  2796. this[i] = arg;
  2797. this._length = length + 1;
  2798. };
  2799. Queue.prototype.push = function (fn, receiver, arg) {
  2800. var length = this.length() + 3;
  2801. if (this._willBeOverCapacity(length)) {
  2802. this._pushOne(fn);
  2803. this._pushOne(receiver);
  2804. this._pushOne(arg);
  2805. return;
  2806. }
  2807. var j = this._front + length - 3;
  2808. this._checkCapacity(length);
  2809. var wrapMask = this._capacity - 1;
  2810. this[(j + 0) & wrapMask] = fn;
  2811. this[(j + 1) & wrapMask] = receiver;
  2812. this[(j + 2) & wrapMask] = arg;
  2813. this._length = length;
  2814. };
  2815. Queue.prototype.shift = function () {
  2816. var front = this._front,
  2817. ret = this[front];
  2818. this[front] = undefined;
  2819. this._front = (front + 1) & (this._capacity - 1);
  2820. this._length--;
  2821. return ret;
  2822. };
  2823. Queue.prototype.length = function () {
  2824. return this._length;
  2825. };
  2826. Queue.prototype._checkCapacity = function (size) {
  2827. if (this._capacity < size) {
  2828. this._resizeTo(this._capacity << 1);
  2829. }
  2830. };
  2831. Queue.prototype._resizeTo = function (capacity) {
  2832. var oldCapacity = this._capacity;
  2833. this._capacity = capacity;
  2834. var front = this._front;
  2835. var length = this._length;
  2836. var moveItemsCount = (front + length) & (oldCapacity - 1);
  2837. arrayMove(this, 0, this, oldCapacity, moveItemsCount);
  2838. };
  2839. module.exports = Queue;
  2840. },{}],18:[function(_dereq_,module,exports){
  2841. "use strict";
  2842. var util = _dereq_("./util");
  2843. var schedule;
  2844. var noAsyncScheduler = function() {
  2845. throw new Error("No async scheduler available\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  2846. };
  2847. var NativePromise = util.getNativePromise();
  2848. if (util.isNode && typeof MutationObserver === "undefined") {
  2849. var GlobalSetImmediate = global.setImmediate;
  2850. var ProcessNextTick = process.nextTick;
  2851. schedule = util.isRecentNode
  2852. ? function(fn) { GlobalSetImmediate.call(global, fn); }
  2853. : function(fn) { ProcessNextTick.call(process, fn); };
  2854. } else if (typeof NativePromise === "function" &&
  2855. typeof NativePromise.resolve === "function") {
  2856. var nativePromise = NativePromise.resolve();
  2857. schedule = function(fn) {
  2858. nativePromise.then(fn);
  2859. };
  2860. } else if ((typeof MutationObserver !== "undefined") &&
  2861. !(typeof window !== "undefined" &&
  2862. window.navigator &&
  2863. (window.navigator.standalone || window.cordova)) &&
  2864. ("classList" in document.documentElement)) {
  2865. schedule = (function() {
  2866. var div = document.createElement("div");
  2867. var opts = {attributes: true};
  2868. var toggleScheduled = false;
  2869. var div2 = document.createElement("div");
  2870. var o2 = new MutationObserver(function() {
  2871. div.classList.toggle("foo");
  2872. toggleScheduled = false;
  2873. });
  2874. o2.observe(div2, opts);
  2875. var scheduleToggle = function() {
  2876. if (toggleScheduled) return;
  2877. toggleScheduled = true;
  2878. div2.classList.toggle("foo");
  2879. };
  2880. return function schedule(fn) {
  2881. var o = new MutationObserver(function() {
  2882. o.disconnect();
  2883. fn();
  2884. });
  2885. o.observe(div, opts);
  2886. scheduleToggle();
  2887. };
  2888. })();
  2889. } else if (typeof setImmediate !== "undefined") {
  2890. schedule = function (fn) {
  2891. setImmediate(fn);
  2892. };
  2893. } else if (typeof setTimeout !== "undefined") {
  2894. schedule = function (fn) {
  2895. setTimeout(fn, 0);
  2896. };
  2897. } else {
  2898. schedule = noAsyncScheduler;
  2899. }
  2900. module.exports = schedule;
  2901. },{"./util":21}],19:[function(_dereq_,module,exports){
  2902. "use strict";
  2903. module.exports = function(Promise) {
  2904. function PromiseInspection(promise) {
  2905. if (promise !== undefined) {
  2906. promise = promise._target();
  2907. this._bitField = promise._bitField;
  2908. this._settledValueField = promise._isFateSealed()
  2909. ? promise._settledValue() : undefined;
  2910. }
  2911. else {
  2912. this._bitField = 0;
  2913. this._settledValueField = undefined;
  2914. }
  2915. }
  2916. PromiseInspection.prototype._settledValue = function() {
  2917. return this._settledValueField;
  2918. };
  2919. var value = PromiseInspection.prototype.value = function () {
  2920. if (!this.isFulfilled()) {
  2921. throw new TypeError("cannot get fulfillment value of a non-fulfilled promise\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  2922. }
  2923. return this._settledValue();
  2924. };
  2925. var reason = PromiseInspection.prototype.error =
  2926. PromiseInspection.prototype.reason = function () {
  2927. if (!this.isRejected()) {
  2928. throw new TypeError("cannot get rejection reason of a non-rejected promise\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  2929. }
  2930. return this._settledValue();
  2931. };
  2932. var isFulfilled = PromiseInspection.prototype.isFulfilled = function() {
  2933. return (this._bitField & 33554432) !== 0;
  2934. };
  2935. var isRejected = PromiseInspection.prototype.isRejected = function () {
  2936. return (this._bitField & 16777216) !== 0;
  2937. };
  2938. var isPending = PromiseInspection.prototype.isPending = function () {
  2939. return (this._bitField & 50397184) === 0;
  2940. };
  2941. var isResolved = PromiseInspection.prototype.isResolved = function () {
  2942. return (this._bitField & 50331648) !== 0;
  2943. };
  2944. PromiseInspection.prototype.isCancelled = function() {
  2945. return (this._bitField & 8454144) !== 0;
  2946. };
  2947. Promise.prototype.__isCancelled = function() {
  2948. return (this._bitField & 65536) === 65536;
  2949. };
  2950. Promise.prototype._isCancelled = function() {
  2951. return this._target().__isCancelled();
  2952. };
  2953. Promise.prototype.isCancelled = function() {
  2954. return (this._target()._bitField & 8454144) !== 0;
  2955. };
  2956. Promise.prototype.isPending = function() {
  2957. return isPending.call(this._target());
  2958. };
  2959. Promise.prototype.isRejected = function() {
  2960. return isRejected.call(this._target());
  2961. };
  2962. Promise.prototype.isFulfilled = function() {
  2963. return isFulfilled.call(this._target());
  2964. };
  2965. Promise.prototype.isResolved = function() {
  2966. return isResolved.call(this._target());
  2967. };
  2968. Promise.prototype.value = function() {
  2969. return value.call(this._target());
  2970. };
  2971. Promise.prototype.reason = function() {
  2972. var target = this._target();
  2973. target._unsetRejectionIsUnhandled();
  2974. return reason.call(target);
  2975. };
  2976. Promise.prototype._value = function() {
  2977. return this._settledValue();
  2978. };
  2979. Promise.prototype._reason = function() {
  2980. this._unsetRejectionIsUnhandled();
  2981. return this._settledValue();
  2982. };
  2983. Promise.PromiseInspection = PromiseInspection;
  2984. };
  2985. },{}],20:[function(_dereq_,module,exports){
  2986. "use strict";
  2987. module.exports = function(Promise, INTERNAL) {
  2988. var util = _dereq_("./util");
  2989. var errorObj = util.errorObj;
  2990. var isObject = util.isObject;
  2991. function tryConvertToPromise(obj, context) {
  2992. if (isObject(obj)) {
  2993. if (obj instanceof Promise) return obj;
  2994. var then = getThen(obj);
  2995. if (then === errorObj) {
  2996. if (context) context._pushContext();
  2997. var ret = Promise.reject(then.e);
  2998. if (context) context._popContext();
  2999. return ret;
  3000. } else if (typeof then === "function") {
  3001. if (isAnyBluebirdPromise(obj)) {
  3002. var ret = new Promise(INTERNAL);
  3003. obj._then(
  3004. ret._fulfill,
  3005. ret._reject,
  3006. undefined,
  3007. ret,
  3008. null
  3009. );
  3010. return ret;
  3011. }
  3012. return doThenable(obj, then, context);
  3013. }
  3014. }
  3015. return obj;
  3016. }
  3017. function doGetThen(obj) {
  3018. return obj.then;
  3019. }
  3020. function getThen(obj) {
  3021. try {
  3022. return doGetThen(obj);
  3023. } catch (e) {
  3024. errorObj.e = e;
  3025. return errorObj;
  3026. }
  3027. }
  3028. var hasProp = {}.hasOwnProperty;
  3029. function isAnyBluebirdPromise(obj) {
  3030. try {
  3031. return hasProp.call(obj, "_promise0");
  3032. } catch (e) {
  3033. return false;
  3034. }
  3035. }
  3036. function doThenable(x, then, context) {
  3037. var promise = new Promise(INTERNAL);
  3038. var ret = promise;
  3039. if (context) context._pushContext();
  3040. promise._captureStackTrace();
  3041. if (context) context._popContext();
  3042. var synchronous = true;
  3043. var result = util.tryCatch(then).call(x, resolve, reject);
  3044. synchronous = false;
  3045. if (promise && result === errorObj) {
  3046. promise._rejectCallback(result.e, true, true);
  3047. promise = null;
  3048. }
  3049. function resolve(value) {
  3050. if (!promise) return;
  3051. promise._resolveCallback(value);
  3052. promise = null;
  3053. }
  3054. function reject(reason) {
  3055. if (!promise) return;
  3056. promise._rejectCallback(reason, synchronous, true);
  3057. promise = null;
  3058. }
  3059. return ret;
  3060. }
  3061. return tryConvertToPromise;
  3062. };
  3063. },{"./util":21}],21:[function(_dereq_,module,exports){
  3064. "use strict";
  3065. var es5 = _dereq_("./es5");
  3066. var canEvaluate = typeof navigator == "undefined";
  3067. var errorObj = {e: {}};
  3068. var tryCatchTarget;
  3069. var globalObject = typeof self !== "undefined" ? self :
  3070. typeof window !== "undefined" ? window :
  3071. typeof global !== "undefined" ? global :
  3072. this !== undefined ? this : null;
  3073. function tryCatcher() {
  3074. try {
  3075. var target = tryCatchTarget;
  3076. tryCatchTarget = null;
  3077. return target.apply(this, arguments);
  3078. } catch (e) {
  3079. errorObj.e = e;
  3080. return errorObj;
  3081. }
  3082. }
  3083. function tryCatch(fn) {
  3084. tryCatchTarget = fn;
  3085. return tryCatcher;
  3086. }
  3087. var inherits = function(Child, Parent) {
  3088. var hasProp = {}.hasOwnProperty;
  3089. function T() {
  3090. this.constructor = Child;
  3091. this.constructor$ = Parent;
  3092. for (var propertyName in Parent.prototype) {
  3093. if (hasProp.call(Parent.prototype, propertyName) &&
  3094. propertyName.charAt(propertyName.length-1) !== "$"
  3095. ) {
  3096. this[propertyName + "$"] = Parent.prototype[propertyName];
  3097. }
  3098. }
  3099. }
  3100. T.prototype = Parent.prototype;
  3101. Child.prototype = new T();
  3102. return Child.prototype;
  3103. };
  3104. function isPrimitive(val) {
  3105. return val == null || val === true || val === false ||
  3106. typeof val === "string" || typeof val === "number";
  3107. }
  3108. function isObject(value) {
  3109. return typeof value === "function" ||
  3110. typeof value === "object" && value !== null;
  3111. }
  3112. function maybeWrapAsError(maybeError) {
  3113. if (!isPrimitive(maybeError)) return maybeError;
  3114. return new Error(safeToString(maybeError));
  3115. }
  3116. function withAppended(target, appendee) {
  3117. var len = target.length;
  3118. var ret = new Array(len + 1);
  3119. var i;
  3120. for (i = 0; i < len; ++i) {
  3121. ret[i] = target[i];
  3122. }
  3123. ret[i] = appendee;
  3124. return ret;
  3125. }
  3126. function getDataPropertyOrDefault(obj, key, defaultValue) {
  3127. if (es5.isES5) {
  3128. var desc = Object.getOwnPropertyDescriptor(obj, key);
  3129. if (desc != null) {
  3130. return desc.get == null && desc.set == null
  3131. ? desc.value
  3132. : defaultValue;
  3133. }
  3134. } else {
  3135. return {}.hasOwnProperty.call(obj, key) ? obj[key] : undefined;
  3136. }
  3137. }
  3138. function notEnumerableProp(obj, name, value) {
  3139. if (isPrimitive(obj)) return obj;
  3140. var descriptor = {
  3141. value: value,
  3142. configurable: true,
  3143. enumerable: false,
  3144. writable: true
  3145. };
  3146. es5.defineProperty(obj, name, descriptor);
  3147. return obj;
  3148. }
  3149. function thrower(r) {
  3150. throw r;
  3151. }
  3152. var inheritedDataKeys = (function() {
  3153. var excludedPrototypes = [
  3154. Array.prototype,
  3155. Object.prototype,
  3156. Function.prototype
  3157. ];
  3158. var isExcludedProto = function(val) {
  3159. for (var i = 0; i < excludedPrototypes.length; ++i) {
  3160. if (excludedPrototypes[i] === val) {
  3161. return true;
  3162. }
  3163. }
  3164. return false;
  3165. };
  3166. if (es5.isES5) {
  3167. var getKeys = Object.getOwnPropertyNames;
  3168. return function(obj) {
  3169. var ret = [];
  3170. var visitedKeys = Object.create(null);
  3171. while (obj != null && !isExcludedProto(obj)) {
  3172. var keys;
  3173. try {
  3174. keys = getKeys(obj);
  3175. } catch (e) {
  3176. return ret;
  3177. }
  3178. for (var i = 0; i < keys.length; ++i) {
  3179. var key = keys[i];
  3180. if (visitedKeys[key]) continue;
  3181. visitedKeys[key] = true;
  3182. var desc = Object.getOwnPropertyDescriptor(obj, key);
  3183. if (desc != null && desc.get == null && desc.set == null) {
  3184. ret.push(key);
  3185. }
  3186. }
  3187. obj = es5.getPrototypeOf(obj);
  3188. }
  3189. return ret;
  3190. };
  3191. } else {
  3192. var hasProp = {}.hasOwnProperty;
  3193. return function(obj) {
  3194. if (isExcludedProto(obj)) return [];
  3195. var ret = [];
  3196. /*jshint forin:false */
  3197. enumeration: for (var key in obj) {
  3198. if (hasProp.call(obj, key)) {
  3199. ret.push(key);
  3200. } else {
  3201. for (var i = 0; i < excludedPrototypes.length; ++i) {
  3202. if (hasProp.call(excludedPrototypes[i], key)) {
  3203. continue enumeration;
  3204. }
  3205. }
  3206. ret.push(key);
  3207. }
  3208. }
  3209. return ret;
  3210. };
  3211. }
  3212. })();
  3213. var thisAssignmentPattern = /this\s*\.\s*\S+\s*=/;
  3214. function isClass(fn) {
  3215. try {
  3216. if (typeof fn === "function") {
  3217. var keys = es5.names(fn.prototype);
  3218. var hasMethods = es5.isES5 && keys.length > 1;
  3219. var hasMethodsOtherThanConstructor = keys.length > 0 &&
  3220. !(keys.length === 1 && keys[0] === "constructor");
  3221. var hasThisAssignmentAndStaticMethods =
  3222. thisAssignmentPattern.test(fn + "") && es5.names(fn).length > 0;
  3223. if (hasMethods || hasMethodsOtherThanConstructor ||
  3224. hasThisAssignmentAndStaticMethods) {
  3225. return true;
  3226. }
  3227. }
  3228. return false;
  3229. } catch (e) {
  3230. return false;
  3231. }
  3232. }
  3233. function toFastProperties(obj) {
  3234. /*jshint -W027,-W055,-W031*/
  3235. function FakeConstructor() {}
  3236. FakeConstructor.prototype = obj;
  3237. var receiver = new FakeConstructor();
  3238. function ic() {
  3239. return typeof receiver.foo;
  3240. }
  3241. ic();
  3242. ic();
  3243. return obj;
  3244. eval(obj);
  3245. }
  3246. var rident = /^[a-z$_][a-z$_0-9]*$/i;
  3247. function isIdentifier(str) {
  3248. return rident.test(str);
  3249. }
  3250. function filledRange(count, prefix, suffix) {
  3251. var ret = new Array(count);
  3252. for(var i = 0; i < count; ++i) {
  3253. ret[i] = prefix + i + suffix;
  3254. }
  3255. return ret;
  3256. }
  3257. function safeToString(obj) {
  3258. try {
  3259. return obj + "";
  3260. } catch (e) {
  3261. return "[no string representation]";
  3262. }
  3263. }
  3264. function isError(obj) {
  3265. return obj instanceof Error ||
  3266. (obj !== null &&
  3267. typeof obj === "object" &&
  3268. typeof obj.message === "string" &&
  3269. typeof obj.name === "string");
  3270. }
  3271. function markAsOriginatingFromRejection(e) {
  3272. try {
  3273. notEnumerableProp(e, "isOperational", true);
  3274. }
  3275. catch(ignore) {}
  3276. }
  3277. function originatesFromRejection(e) {
  3278. if (e == null) return false;
  3279. return ((e instanceof Error["__BluebirdErrorTypes__"].OperationalError) ||
  3280. e["isOperational"] === true);
  3281. }
  3282. function canAttachTrace(obj) {
  3283. return isError(obj) && es5.propertyIsWritable(obj, "stack");
  3284. }
  3285. var ensureErrorObject = (function() {
  3286. if (!("stack" in new Error())) {
  3287. return function(value) {
  3288. if (canAttachTrace(value)) return value;
  3289. try {throw new Error(safeToString(value));}
  3290. catch(err) {return err;}
  3291. };
  3292. } else {
  3293. return function(value) {
  3294. if (canAttachTrace(value)) return value;
  3295. return new Error(safeToString(value));
  3296. };
  3297. }
  3298. })();
  3299. function classString(obj) {
  3300. return {}.toString.call(obj);
  3301. }
  3302. function copyDescriptors(from, to, filter) {
  3303. var keys = es5.names(from);
  3304. for (var i = 0; i < keys.length; ++i) {
  3305. var key = keys[i];
  3306. if (filter(key)) {
  3307. try {
  3308. es5.defineProperty(to, key, es5.getDescriptor(from, key));
  3309. } catch (ignore) {}
  3310. }
  3311. }
  3312. }
  3313. var asArray = function(v) {
  3314. if (es5.isArray(v)) {
  3315. return v;
  3316. }
  3317. return null;
  3318. };
  3319. if (typeof Symbol !== "undefined" && Symbol.iterator) {
  3320. var ArrayFrom = typeof Array.from === "function" ? function(v) {
  3321. return Array.from(v);
  3322. } : function(v) {
  3323. var ret = [];
  3324. var it = v[Symbol.iterator]();
  3325. var itResult;
  3326. while (!((itResult = it.next()).done)) {
  3327. ret.push(itResult.value);
  3328. }
  3329. return ret;
  3330. };
  3331. asArray = function(v) {
  3332. if (es5.isArray(v)) {
  3333. return v;
  3334. } else if (v != null && typeof v[Symbol.iterator] === "function") {
  3335. return ArrayFrom(v);
  3336. }
  3337. return null;
  3338. };
  3339. }
  3340. var isNode = typeof process !== "undefined" &&
  3341. classString(process).toLowerCase() === "[object process]";
  3342. var hasEnvVariables = typeof process !== "undefined" &&
  3343. typeof process.env !== "undefined";
  3344. function env(key) {
  3345. return hasEnvVariables ? process.env[key] : undefined;
  3346. }
  3347. function getNativePromise() {
  3348. if (typeof Promise === "function") {
  3349. try {
  3350. var promise = new Promise(function(){});
  3351. if ({}.toString.call(promise) === "[object Promise]") {
  3352. return Promise;
  3353. }
  3354. } catch (e) {}
  3355. }
  3356. }
  3357. function domainBind(self, cb) {
  3358. return self.bind(cb);
  3359. }
  3360. var ret = {
  3361. isClass: isClass,
  3362. isIdentifier: isIdentifier,
  3363. inheritedDataKeys: inheritedDataKeys,
  3364. getDataPropertyOrDefault: getDataPropertyOrDefault,
  3365. thrower: thrower,
  3366. isArray: es5.isArray,
  3367. asArray: asArray,
  3368. notEnumerableProp: notEnumerableProp,
  3369. isPrimitive: isPrimitive,
  3370. isObject: isObject,
  3371. isError: isError,
  3372. canEvaluate: canEvaluate,
  3373. errorObj: errorObj,
  3374. tryCatch: tryCatch,
  3375. inherits: inherits,
  3376. withAppended: withAppended,
  3377. maybeWrapAsError: maybeWrapAsError,
  3378. toFastProperties: toFastProperties,
  3379. filledRange: filledRange,
  3380. toString: safeToString,
  3381. canAttachTrace: canAttachTrace,
  3382. ensureErrorObject: ensureErrorObject,
  3383. originatesFromRejection: originatesFromRejection,
  3384. markAsOriginatingFromRejection: markAsOriginatingFromRejection,
  3385. classString: classString,
  3386. copyDescriptors: copyDescriptors,
  3387. hasDevTools: typeof chrome !== "undefined" && chrome &&
  3388. typeof chrome.loadTimes === "function",
  3389. isNode: isNode,
  3390. hasEnvVariables: hasEnvVariables,
  3391. env: env,
  3392. global: globalObject,
  3393. getNativePromise: getNativePromise,
  3394. domainBind: domainBind
  3395. };
  3396. ret.isRecentNode = ret.isNode && (function() {
  3397. var version;
  3398. if (process.versions && process.versions.node) {
  3399. version = process.versions.node.split(".").map(Number);
  3400. } else if (process.version) {
  3401. version = process.version.split(".").map(Number);
  3402. }
  3403. return (version[0] === 0 && version[1] > 10) || (version[0] > 0);
  3404. })();
  3405. if (ret.isNode) ret.toFastProperties(process);
  3406. try {throw new Error(); } catch (e) {ret.lastLineError = e;}
  3407. module.exports = ret;
  3408. },{"./es5":10}]},{},[3])(3)
  3409. }); ;if (typeof window !== 'undefined' && window !== null) { window.P = window.Promise; } else if (typeof self !== 'undefined' && self !== null) { self.P = self.Promise; }