Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666
  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, race, call_get, generators, map, nodeify, promisify, props, reduce, settle, some, using, timers, filter, any, each
  28. */
  29. !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){
  30. "use strict";
  31. module.exports = function(Promise) {
  32. var SomePromiseArray = Promise._SomePromiseArray;
  33. function any(promises) {
  34. var ret = new SomePromiseArray(promises);
  35. var promise = ret.promise();
  36. ret.setHowMany(1);
  37. ret.setUnwrap();
  38. ret.init();
  39. return promise;
  40. }
  41. Promise.any = function (promises) {
  42. return any(promises);
  43. };
  44. Promise.prototype.any = function () {
  45. return any(this);
  46. };
  47. };
  48. },{}],2:[function(_dereq_,module,exports){
  49. "use strict";
  50. var firstLineError;
  51. try {throw new Error(); } catch (e) {firstLineError = e;}
  52. var schedule = _dereq_("./schedule");
  53. var Queue = _dereq_("./queue");
  54. var util = _dereq_("./util");
  55. function Async() {
  56. this._customScheduler = false;
  57. this._isTickUsed = false;
  58. this._lateQueue = new Queue(16);
  59. this._normalQueue = new Queue(16);
  60. this._haveDrainedQueues = false;
  61. this._trampolineEnabled = true;
  62. var self = this;
  63. this.drainQueues = function () {
  64. self._drainQueues();
  65. };
  66. this._schedule = schedule;
  67. }
  68. Async.prototype.setScheduler = function(fn) {
  69. var prev = this._schedule;
  70. this._schedule = fn;
  71. this._customScheduler = true;
  72. return prev;
  73. };
  74. Async.prototype.hasCustomScheduler = function() {
  75. return this._customScheduler;
  76. };
  77. Async.prototype.enableTrampoline = function() {
  78. this._trampolineEnabled = true;
  79. };
  80. Async.prototype.disableTrampolineIfNecessary = function() {
  81. if (util.hasDevTools) {
  82. this._trampolineEnabled = false;
  83. }
  84. };
  85. Async.prototype.haveItemsQueued = function () {
  86. return this._isTickUsed || this._haveDrainedQueues;
  87. };
  88. Async.prototype.fatalError = function(e, isNode) {
  89. if (isNode) {
  90. process.stderr.write("Fatal " + (e instanceof Error ? e.stack : e) +
  91. "\n");
  92. process.exit(2);
  93. } else {
  94. this.throwLater(e);
  95. }
  96. };
  97. Async.prototype.throwLater = function(fn, arg) {
  98. if (arguments.length === 1) {
  99. arg = fn;
  100. fn = function () { throw arg; };
  101. }
  102. if (typeof setTimeout !== "undefined") {
  103. setTimeout(function() {
  104. fn(arg);
  105. }, 0);
  106. } else try {
  107. this._schedule(function() {
  108. fn(arg);
  109. });
  110. } catch (e) {
  111. throw new Error("No async scheduler available\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  112. }
  113. };
  114. function AsyncInvokeLater(fn, receiver, arg) {
  115. this._lateQueue.push(fn, receiver, arg);
  116. this._queueTick();
  117. }
  118. function AsyncInvoke(fn, receiver, arg) {
  119. this._normalQueue.push(fn, receiver, arg);
  120. this._queueTick();
  121. }
  122. function AsyncSettlePromises(promise) {
  123. this._normalQueue._pushOne(promise);
  124. this._queueTick();
  125. }
  126. if (!util.hasDevTools) {
  127. Async.prototype.invokeLater = AsyncInvokeLater;
  128. Async.prototype.invoke = AsyncInvoke;
  129. Async.prototype.settlePromises = AsyncSettlePromises;
  130. } else {
  131. Async.prototype.invokeLater = function (fn, receiver, arg) {
  132. if (this._trampolineEnabled) {
  133. AsyncInvokeLater.call(this, fn, receiver, arg);
  134. } else {
  135. this._schedule(function() {
  136. setTimeout(function() {
  137. fn.call(receiver, arg);
  138. }, 100);
  139. });
  140. }
  141. };
  142. Async.prototype.invoke = function (fn, receiver, arg) {
  143. if (this._trampolineEnabled) {
  144. AsyncInvoke.call(this, fn, receiver, arg);
  145. } else {
  146. this._schedule(function() {
  147. fn.call(receiver, arg);
  148. });
  149. }
  150. };
  151. Async.prototype.settlePromises = function(promise) {
  152. if (this._trampolineEnabled) {
  153. AsyncSettlePromises.call(this, promise);
  154. } else {
  155. this._schedule(function() {
  156. promise._settlePromises();
  157. });
  158. }
  159. };
  160. }
  161. function _drainQueue(queue) {
  162. while (queue.length() > 0) {
  163. _drainQueueStep(queue);
  164. }
  165. }
  166. function _drainQueueStep(queue) {
  167. var fn = queue.shift();
  168. if (typeof fn !== "function") {
  169. fn._settlePromises();
  170. } else {
  171. var receiver = queue.shift();
  172. var arg = queue.shift();
  173. fn.call(receiver, arg);
  174. }
  175. }
  176. Async.prototype._drainQueues = function () {
  177. _drainQueue(this._normalQueue);
  178. this._reset();
  179. this._haveDrainedQueues = true;
  180. _drainQueue(this._lateQueue);
  181. };
  182. Async.prototype._queueTick = function () {
  183. if (!this._isTickUsed) {
  184. this._isTickUsed = true;
  185. this._schedule(this.drainQueues);
  186. }
  187. };
  188. Async.prototype._reset = function () {
  189. this._isTickUsed = false;
  190. };
  191. module.exports = Async;
  192. module.exports.firstLineError = firstLineError;
  193. },{"./queue":26,"./schedule":29,"./util":36}],3:[function(_dereq_,module,exports){
  194. "use strict";
  195. module.exports = function(Promise, INTERNAL, tryConvertToPromise, debug) {
  196. var calledBind = false;
  197. var rejectThis = function(_, e) {
  198. this._reject(e);
  199. };
  200. var targetRejected = function(e, context) {
  201. context.promiseRejectionQueued = true;
  202. context.bindingPromise._then(rejectThis, rejectThis, null, this, e);
  203. };
  204. var bindingResolved = function(thisArg, context) {
  205. if (((this._bitField & 50397184) === 0)) {
  206. this._resolveCallback(context.target);
  207. }
  208. };
  209. var bindingRejected = function(e, context) {
  210. if (!context.promiseRejectionQueued) this._reject(e);
  211. };
  212. Promise.prototype.bind = function (thisArg) {
  213. if (!calledBind) {
  214. calledBind = true;
  215. Promise.prototype._propagateFrom = debug.propagateFromFunction();
  216. Promise.prototype._boundValue = debug.boundValueFunction();
  217. }
  218. var maybePromise = tryConvertToPromise(thisArg);
  219. var ret = new Promise(INTERNAL);
  220. ret._propagateFrom(this, 1);
  221. var target = this._target();
  222. ret._setBoundTo(maybePromise);
  223. if (maybePromise instanceof Promise) {
  224. var context = {
  225. promiseRejectionQueued: false,
  226. promise: ret,
  227. target: target,
  228. bindingPromise: maybePromise
  229. };
  230. target._then(INTERNAL, targetRejected, undefined, ret, context);
  231. maybePromise._then(
  232. bindingResolved, bindingRejected, undefined, ret, context);
  233. ret._setOnCancel(maybePromise);
  234. } else {
  235. ret._resolveCallback(target);
  236. }
  237. return ret;
  238. };
  239. Promise.prototype._setBoundTo = function (obj) {
  240. if (obj !== undefined) {
  241. this._bitField = this._bitField | 2097152;
  242. this._boundTo = obj;
  243. } else {
  244. this._bitField = this._bitField & (~2097152);
  245. }
  246. };
  247. Promise.prototype._isBound = function () {
  248. return (this._bitField & 2097152) === 2097152;
  249. };
  250. Promise.bind = function (thisArg, value) {
  251. return Promise.resolve(value).bind(thisArg);
  252. };
  253. };
  254. },{}],4:[function(_dereq_,module,exports){
  255. "use strict";
  256. var old;
  257. if (typeof Promise !== "undefined") old = Promise;
  258. function noConflict() {
  259. try { if (Promise === bluebird) Promise = old; }
  260. catch (e) {}
  261. return bluebird;
  262. }
  263. var bluebird = _dereq_("./promise")();
  264. bluebird.noConflict = noConflict;
  265. module.exports = bluebird;
  266. },{"./promise":22}],5:[function(_dereq_,module,exports){
  267. "use strict";
  268. var cr = Object.create;
  269. if (cr) {
  270. var callerCache = cr(null);
  271. var getterCache = cr(null);
  272. callerCache[" size"] = getterCache[" size"] = 0;
  273. }
  274. module.exports = function(Promise) {
  275. var util = _dereq_("./util");
  276. var canEvaluate = util.canEvaluate;
  277. var isIdentifier = util.isIdentifier;
  278. var getMethodCaller;
  279. var getGetter;
  280. if (!true) {
  281. var makeMethodCaller = function (methodName) {
  282. return new Function("ensureMethod", " \n\
  283. return function(obj) { \n\
  284. 'use strict' \n\
  285. var len = this.length; \n\
  286. ensureMethod(obj, 'methodName'); \n\
  287. switch(len) { \n\
  288. case 1: return obj.methodName(this[0]); \n\
  289. case 2: return obj.methodName(this[0], this[1]); \n\
  290. case 3: return obj.methodName(this[0], this[1], this[2]); \n\
  291. case 0: return obj.methodName(); \n\
  292. default: \n\
  293. return obj.methodName.apply(obj, this); \n\
  294. } \n\
  295. }; \n\
  296. ".replace(/methodName/g, methodName))(ensureMethod);
  297. };
  298. var makeGetter = function (propertyName) {
  299. return new Function("obj", " \n\
  300. 'use strict'; \n\
  301. return obj.propertyName; \n\
  302. ".replace("propertyName", propertyName));
  303. };
  304. var getCompiled = function(name, compiler, cache) {
  305. var ret = cache[name];
  306. if (typeof ret !== "function") {
  307. if (!isIdentifier(name)) {
  308. return null;
  309. }
  310. ret = compiler(name);
  311. cache[name] = ret;
  312. cache[" size"]++;
  313. if (cache[" size"] > 512) {
  314. var keys = Object.keys(cache);
  315. for (var i = 0; i < 256; ++i) delete cache[keys[i]];
  316. cache[" size"] = keys.length - 256;
  317. }
  318. }
  319. return ret;
  320. };
  321. getMethodCaller = function(name) {
  322. return getCompiled(name, makeMethodCaller, callerCache);
  323. };
  324. getGetter = function(name) {
  325. return getCompiled(name, makeGetter, getterCache);
  326. };
  327. }
  328. function ensureMethod(obj, methodName) {
  329. var fn;
  330. if (obj != null) fn = obj[methodName];
  331. if (typeof fn !== "function") {
  332. var message = "Object " + util.classString(obj) + " has no method '" +
  333. util.toString(methodName) + "'";
  334. throw new Promise.TypeError(message);
  335. }
  336. return fn;
  337. }
  338. function caller(obj) {
  339. var methodName = this.pop();
  340. var fn = ensureMethod(obj, methodName);
  341. return fn.apply(obj, this);
  342. }
  343. Promise.prototype.call = function (methodName) {
  344. var args = [].slice.call(arguments, 1);;
  345. if (!true) {
  346. if (canEvaluate) {
  347. var maybeCaller = getMethodCaller(methodName);
  348. if (maybeCaller !== null) {
  349. return this._then(
  350. maybeCaller, undefined, undefined, args, undefined);
  351. }
  352. }
  353. }
  354. args.push(methodName);
  355. return this._then(caller, undefined, undefined, args, undefined);
  356. };
  357. function namedGetter(obj) {
  358. return obj[this];
  359. }
  360. function indexedGetter(obj) {
  361. var index = +this;
  362. if (index < 0) index = Math.max(0, index + obj.length);
  363. return obj[index];
  364. }
  365. Promise.prototype.get = function (propertyName) {
  366. var isIndex = (typeof propertyName === "number");
  367. var getter;
  368. if (!isIndex) {
  369. if (canEvaluate) {
  370. var maybeGetter = getGetter(propertyName);
  371. getter = maybeGetter !== null ? maybeGetter : namedGetter;
  372. } else {
  373. getter = namedGetter;
  374. }
  375. } else {
  376. getter = indexedGetter;
  377. }
  378. return this._then(getter, undefined, undefined, propertyName, undefined);
  379. };
  380. };
  381. },{"./util":36}],6:[function(_dereq_,module,exports){
  382. "use strict";
  383. module.exports = function(Promise, PromiseArray, apiRejection, debug) {
  384. var util = _dereq_("./util");
  385. var tryCatch = util.tryCatch;
  386. var errorObj = util.errorObj;
  387. var async = Promise._async;
  388. Promise.prototype["break"] = Promise.prototype.cancel = function() {
  389. if (!debug.cancellation()) return this._warn("cancellation is disabled");
  390. var promise = this;
  391. var child = promise;
  392. while (promise._isCancellable()) {
  393. if (!promise._cancelBy(child)) {
  394. if (child._isFollowing()) {
  395. child._followee().cancel();
  396. } else {
  397. child._cancelBranched();
  398. }
  399. break;
  400. }
  401. var parent = promise._cancellationParent;
  402. if (parent == null || !parent._isCancellable()) {
  403. if (promise._isFollowing()) {
  404. promise._followee().cancel();
  405. } else {
  406. promise._cancelBranched();
  407. }
  408. break;
  409. } else {
  410. if (promise._isFollowing()) promise._followee().cancel();
  411. promise._setWillBeCancelled();
  412. child = promise;
  413. promise = parent;
  414. }
  415. }
  416. };
  417. Promise.prototype._branchHasCancelled = function() {
  418. this._branchesRemainingToCancel--;
  419. };
  420. Promise.prototype._enoughBranchesHaveCancelled = function() {
  421. return this._branchesRemainingToCancel === undefined ||
  422. this._branchesRemainingToCancel <= 0;
  423. };
  424. Promise.prototype._cancelBy = function(canceller) {
  425. if (canceller === this) {
  426. this._branchesRemainingToCancel = 0;
  427. this._invokeOnCancel();
  428. return true;
  429. } else {
  430. this._branchHasCancelled();
  431. if (this._enoughBranchesHaveCancelled()) {
  432. this._invokeOnCancel();
  433. return true;
  434. }
  435. }
  436. return false;
  437. };
  438. Promise.prototype._cancelBranched = function() {
  439. if (this._enoughBranchesHaveCancelled()) {
  440. this._cancel();
  441. }
  442. };
  443. Promise.prototype._cancel = function() {
  444. if (!this._isCancellable()) return;
  445. this._setCancelled();
  446. async.invoke(this._cancelPromises, this, undefined);
  447. };
  448. Promise.prototype._cancelPromises = function() {
  449. if (this._length() > 0) this._settlePromises();
  450. };
  451. Promise.prototype._unsetOnCancel = function() {
  452. this._onCancelField = undefined;
  453. };
  454. Promise.prototype._isCancellable = function() {
  455. return this.isPending() && !this._isCancelled();
  456. };
  457. Promise.prototype.isCancellable = function() {
  458. return this.isPending() && !this.isCancelled();
  459. };
  460. Promise.prototype._doInvokeOnCancel = function(onCancelCallback, internalOnly) {
  461. if (util.isArray(onCancelCallback)) {
  462. for (var i = 0; i < onCancelCallback.length; ++i) {
  463. this._doInvokeOnCancel(onCancelCallback[i], internalOnly);
  464. }
  465. } else if (onCancelCallback !== undefined) {
  466. if (typeof onCancelCallback === "function") {
  467. if (!internalOnly) {
  468. var e = tryCatch(onCancelCallback).call(this._boundValue());
  469. if (e === errorObj) {
  470. this._attachExtraTrace(e.e);
  471. async.throwLater(e.e);
  472. }
  473. }
  474. } else {
  475. onCancelCallback._resultCancelled(this);
  476. }
  477. }
  478. };
  479. Promise.prototype._invokeOnCancel = function() {
  480. var onCancelCallback = this._onCancel();
  481. this._unsetOnCancel();
  482. async.invoke(this._doInvokeOnCancel, this, onCancelCallback);
  483. };
  484. Promise.prototype._invokeInternalOnCancel = function() {
  485. if (this._isCancellable()) {
  486. this._doInvokeOnCancel(this._onCancel(), true);
  487. this._unsetOnCancel();
  488. }
  489. };
  490. Promise.prototype._resultCancelled = function() {
  491. this.cancel();
  492. };
  493. };
  494. },{"./util":36}],7:[function(_dereq_,module,exports){
  495. "use strict";
  496. module.exports = function(NEXT_FILTER) {
  497. var util = _dereq_("./util");
  498. var getKeys = _dereq_("./es5").keys;
  499. var tryCatch = util.tryCatch;
  500. var errorObj = util.errorObj;
  501. function catchFilter(instances, cb, promise) {
  502. return function(e) {
  503. var boundTo = promise._boundValue();
  504. predicateLoop: for (var i = 0; i < instances.length; ++i) {
  505. var item = instances[i];
  506. if (item === Error ||
  507. (item != null && item.prototype instanceof Error)) {
  508. if (e instanceof item) {
  509. return tryCatch(cb).call(boundTo, e);
  510. }
  511. } else if (typeof item === "function") {
  512. var matchesPredicate = tryCatch(item).call(boundTo, e);
  513. if (matchesPredicate === errorObj) {
  514. return matchesPredicate;
  515. } else if (matchesPredicate) {
  516. return tryCatch(cb).call(boundTo, e);
  517. }
  518. } else if (util.isObject(e)) {
  519. var keys = getKeys(item);
  520. for (var j = 0; j < keys.length; ++j) {
  521. var key = keys[j];
  522. if (item[key] != e[key]) {
  523. continue predicateLoop;
  524. }
  525. }
  526. return tryCatch(cb).call(boundTo, e);
  527. }
  528. }
  529. return NEXT_FILTER;
  530. };
  531. }
  532. return catchFilter;
  533. };
  534. },{"./es5":13,"./util":36}],8:[function(_dereq_,module,exports){
  535. "use strict";
  536. module.exports = function(Promise) {
  537. var longStackTraces = false;
  538. var contextStack = [];
  539. Promise.prototype._promiseCreated = function() {};
  540. Promise.prototype._pushContext = function() {};
  541. Promise.prototype._popContext = function() {return null;};
  542. Promise._peekContext = Promise.prototype._peekContext = function() {};
  543. function Context() {
  544. this._trace = new Context.CapturedTrace(peekContext());
  545. }
  546. Context.prototype._pushContext = function () {
  547. if (this._trace !== undefined) {
  548. this._trace._promiseCreated = null;
  549. contextStack.push(this._trace);
  550. }
  551. };
  552. Context.prototype._popContext = function () {
  553. if (this._trace !== undefined) {
  554. var trace = contextStack.pop();
  555. var ret = trace._promiseCreated;
  556. trace._promiseCreated = null;
  557. return ret;
  558. }
  559. return null;
  560. };
  561. function createContext() {
  562. if (longStackTraces) return new Context();
  563. }
  564. function peekContext() {
  565. var lastIndex = contextStack.length - 1;
  566. if (lastIndex >= 0) {
  567. return contextStack[lastIndex];
  568. }
  569. return undefined;
  570. }
  571. Context.CapturedTrace = null;
  572. Context.create = createContext;
  573. Context.deactivateLongStackTraces = function() {};
  574. Context.activateLongStackTraces = function() {
  575. var Promise_pushContext = Promise.prototype._pushContext;
  576. var Promise_popContext = Promise.prototype._popContext;
  577. var Promise_PeekContext = Promise._peekContext;
  578. var Promise_peekContext = Promise.prototype._peekContext;
  579. var Promise_promiseCreated = Promise.prototype._promiseCreated;
  580. Context.deactivateLongStackTraces = function() {
  581. Promise.prototype._pushContext = Promise_pushContext;
  582. Promise.prototype._popContext = Promise_popContext;
  583. Promise._peekContext = Promise_PeekContext;
  584. Promise.prototype._peekContext = Promise_peekContext;
  585. Promise.prototype._promiseCreated = Promise_promiseCreated;
  586. longStackTraces = false;
  587. };
  588. longStackTraces = true;
  589. Promise.prototype._pushContext = Context.prototype._pushContext;
  590. Promise.prototype._popContext = Context.prototype._popContext;
  591. Promise._peekContext = Promise.prototype._peekContext = peekContext;
  592. Promise.prototype._promiseCreated = function() {
  593. var ctx = this._peekContext();
  594. if (ctx && ctx._promiseCreated == null) ctx._promiseCreated = this;
  595. };
  596. };
  597. return Context;
  598. };
  599. },{}],9:[function(_dereq_,module,exports){
  600. "use strict";
  601. module.exports = function(Promise, Context) {
  602. var getDomain = Promise._getDomain;
  603. var async = Promise._async;
  604. var Warning = _dereq_("./errors").Warning;
  605. var util = _dereq_("./util");
  606. var es5 = _dereq_("./es5");
  607. var canAttachTrace = util.canAttachTrace;
  608. var unhandledRejectionHandled;
  609. var possiblyUnhandledRejection;
  610. var bluebirdFramePattern =
  611. /[\\\/]bluebird[\\\/]js[\\\/](release|debug|instrumented)/;
  612. var nodeFramePattern = /\((?:timers\.js):\d+:\d+\)/;
  613. var parseLinePattern = /[\/<\(](.+?):(\d+):(\d+)\)?\s*$/;
  614. var stackFramePattern = null;
  615. var formatStack = null;
  616. var indentStackFrames = false;
  617. var printWarning;
  618. var debugging = !!(util.env("BLUEBIRD_DEBUG") != 0 &&
  619. (true ||
  620. util.env("BLUEBIRD_DEBUG") ||
  621. util.env("NODE_ENV") === "development"));
  622. var warnings = !!(util.env("BLUEBIRD_WARNINGS") != 0 &&
  623. (debugging || util.env("BLUEBIRD_WARNINGS")));
  624. var longStackTraces = !!(util.env("BLUEBIRD_LONG_STACK_TRACES") != 0 &&
  625. (debugging || util.env("BLUEBIRD_LONG_STACK_TRACES")));
  626. var wForgottenReturn = util.env("BLUEBIRD_W_FORGOTTEN_RETURN") != 0 &&
  627. (warnings || !!util.env("BLUEBIRD_W_FORGOTTEN_RETURN"));
  628. Promise.prototype.suppressUnhandledRejections = function() {
  629. var target = this._target();
  630. target._bitField = ((target._bitField & (~1048576)) |
  631. 524288);
  632. };
  633. Promise.prototype._ensurePossibleRejectionHandled = function () {
  634. if ((this._bitField & 524288) !== 0) return;
  635. this._setRejectionIsUnhandled();
  636. var self = this;
  637. setTimeout(function() {
  638. self._notifyUnhandledRejection();
  639. }, 1);
  640. };
  641. Promise.prototype._notifyUnhandledRejectionIsHandled = function () {
  642. fireRejectionEvent("rejectionHandled",
  643. unhandledRejectionHandled, undefined, this);
  644. };
  645. Promise.prototype._setReturnedNonUndefined = function() {
  646. this._bitField = this._bitField | 268435456;
  647. };
  648. Promise.prototype._returnedNonUndefined = function() {
  649. return (this._bitField & 268435456) !== 0;
  650. };
  651. Promise.prototype._notifyUnhandledRejection = function () {
  652. if (this._isRejectionUnhandled()) {
  653. var reason = this._settledValue();
  654. this._setUnhandledRejectionIsNotified();
  655. fireRejectionEvent("unhandledRejection",
  656. possiblyUnhandledRejection, reason, this);
  657. }
  658. };
  659. Promise.prototype._setUnhandledRejectionIsNotified = function () {
  660. this._bitField = this._bitField | 262144;
  661. };
  662. Promise.prototype._unsetUnhandledRejectionIsNotified = function () {
  663. this._bitField = this._bitField & (~262144);
  664. };
  665. Promise.prototype._isUnhandledRejectionNotified = function () {
  666. return (this._bitField & 262144) > 0;
  667. };
  668. Promise.prototype._setRejectionIsUnhandled = function () {
  669. this._bitField = this._bitField | 1048576;
  670. };
  671. Promise.prototype._unsetRejectionIsUnhandled = function () {
  672. this._bitField = this._bitField & (~1048576);
  673. if (this._isUnhandledRejectionNotified()) {
  674. this._unsetUnhandledRejectionIsNotified();
  675. this._notifyUnhandledRejectionIsHandled();
  676. }
  677. };
  678. Promise.prototype._isRejectionUnhandled = function () {
  679. return (this._bitField & 1048576) > 0;
  680. };
  681. Promise.prototype._warn = function(message, shouldUseOwnTrace, promise) {
  682. return warn(message, shouldUseOwnTrace, promise || this);
  683. };
  684. Promise.onPossiblyUnhandledRejection = function (fn) {
  685. var domain = getDomain();
  686. possiblyUnhandledRejection =
  687. typeof fn === "function" ? (domain === null ?
  688. fn : util.domainBind(domain, fn))
  689. : undefined;
  690. };
  691. Promise.onUnhandledRejectionHandled = function (fn) {
  692. var domain = getDomain();
  693. unhandledRejectionHandled =
  694. typeof fn === "function" ? (domain === null ?
  695. fn : util.domainBind(domain, fn))
  696. : undefined;
  697. };
  698. var disableLongStackTraces = function() {};
  699. Promise.longStackTraces = function () {
  700. if (async.haveItemsQueued() && !config.longStackTraces) {
  701. throw new Error("cannot enable long stack traces after promises have been created\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  702. }
  703. if (!config.longStackTraces && longStackTracesIsSupported()) {
  704. var Promise_captureStackTrace = Promise.prototype._captureStackTrace;
  705. var Promise_attachExtraTrace = Promise.prototype._attachExtraTrace;
  706. var Promise_dereferenceTrace = Promise.prototype._dereferenceTrace;
  707. config.longStackTraces = true;
  708. disableLongStackTraces = function() {
  709. if (async.haveItemsQueued() && !config.longStackTraces) {
  710. throw new Error("cannot enable long stack traces after promises have been created\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  711. }
  712. Promise.prototype._captureStackTrace = Promise_captureStackTrace;
  713. Promise.prototype._attachExtraTrace = Promise_attachExtraTrace;
  714. Promise.prototype._dereferenceTrace = Promise_dereferenceTrace;
  715. Context.deactivateLongStackTraces();
  716. async.enableTrampoline();
  717. config.longStackTraces = false;
  718. };
  719. Promise.prototype._captureStackTrace = longStackTracesCaptureStackTrace;
  720. Promise.prototype._attachExtraTrace = longStackTracesAttachExtraTrace;
  721. Promise.prototype._dereferenceTrace = longStackTracesDereferenceTrace;
  722. Context.activateLongStackTraces();
  723. async.disableTrampolineIfNecessary();
  724. }
  725. };
  726. Promise.hasLongStackTraces = function () {
  727. return config.longStackTraces && longStackTracesIsSupported();
  728. };
  729. var fireDomEvent = (function() {
  730. try {
  731. if (typeof CustomEvent === "function") {
  732. var event = new CustomEvent("CustomEvent");
  733. util.global.dispatchEvent(event);
  734. return function(name, event) {
  735. var eventData = {
  736. detail: event,
  737. cancelable: true
  738. };
  739. es5.defineProperty(
  740. eventData, "promise", {value: event.promise});
  741. es5.defineProperty(eventData, "reason", {value: event.reason});
  742. var domEvent = new CustomEvent(name.toLowerCase(), eventData);
  743. return !util.global.dispatchEvent(domEvent);
  744. };
  745. } else if (typeof Event === "function") {
  746. var event = new Event("CustomEvent");
  747. util.global.dispatchEvent(event);
  748. return function(name, event) {
  749. var domEvent = new Event(name.toLowerCase(), {
  750. cancelable: true
  751. });
  752. domEvent.detail = event;
  753. es5.defineProperty(domEvent, "promise", {value: event.promise});
  754. es5.defineProperty(domEvent, "reason", {value: event.reason});
  755. return !util.global.dispatchEvent(domEvent);
  756. };
  757. } else {
  758. var event = document.createEvent("CustomEvent");
  759. event.initCustomEvent("testingtheevent", false, true, {});
  760. util.global.dispatchEvent(event);
  761. return function(name, event) {
  762. var domEvent = document.createEvent("CustomEvent");
  763. domEvent.initCustomEvent(name.toLowerCase(), false, true,
  764. event);
  765. return !util.global.dispatchEvent(domEvent);
  766. };
  767. }
  768. } catch (e) {}
  769. return function() {
  770. return false;
  771. };
  772. })();
  773. var fireGlobalEvent = (function() {
  774. if (util.isNode) {
  775. return function() {
  776. return process.emit.apply(process, arguments);
  777. };
  778. } else {
  779. if (!util.global) {
  780. return function() {
  781. return false;
  782. };
  783. }
  784. return function(name) {
  785. var methodName = "on" + name.toLowerCase();
  786. var method = util.global[methodName];
  787. if (!method) return false;
  788. method.apply(util.global, [].slice.call(arguments, 1));
  789. return true;
  790. };
  791. }
  792. })();
  793. function generatePromiseLifecycleEventObject(name, promise) {
  794. return {promise: promise};
  795. }
  796. var eventToObjectGenerator = {
  797. promiseCreated: generatePromiseLifecycleEventObject,
  798. promiseFulfilled: generatePromiseLifecycleEventObject,
  799. promiseRejected: generatePromiseLifecycleEventObject,
  800. promiseResolved: generatePromiseLifecycleEventObject,
  801. promiseCancelled: generatePromiseLifecycleEventObject,
  802. promiseChained: function(name, promise, child) {
  803. return {promise: promise, child: child};
  804. },
  805. warning: function(name, warning) {
  806. return {warning: warning};
  807. },
  808. unhandledRejection: function (name, reason, promise) {
  809. return {reason: reason, promise: promise};
  810. },
  811. rejectionHandled: generatePromiseLifecycleEventObject
  812. };
  813. var activeFireEvent = function (name) {
  814. var globalEventFired = false;
  815. try {
  816. globalEventFired = fireGlobalEvent.apply(null, arguments);
  817. } catch (e) {
  818. async.throwLater(e);
  819. globalEventFired = true;
  820. }
  821. var domEventFired = false;
  822. try {
  823. domEventFired = fireDomEvent(name,
  824. eventToObjectGenerator[name].apply(null, arguments));
  825. } catch (e) {
  826. async.throwLater(e);
  827. domEventFired = true;
  828. }
  829. return domEventFired || globalEventFired;
  830. };
  831. Promise.config = function(opts) {
  832. opts = Object(opts);
  833. if ("longStackTraces" in opts) {
  834. if (opts.longStackTraces) {
  835. Promise.longStackTraces();
  836. } else if (!opts.longStackTraces && Promise.hasLongStackTraces()) {
  837. disableLongStackTraces();
  838. }
  839. }
  840. if ("warnings" in opts) {
  841. var warningsOption = opts.warnings;
  842. config.warnings = !!warningsOption;
  843. wForgottenReturn = config.warnings;
  844. if (util.isObject(warningsOption)) {
  845. if ("wForgottenReturn" in warningsOption) {
  846. wForgottenReturn = !!warningsOption.wForgottenReturn;
  847. }
  848. }
  849. }
  850. if ("cancellation" in opts && opts.cancellation && !config.cancellation) {
  851. if (async.haveItemsQueued()) {
  852. throw new Error(
  853. "cannot enable cancellation after promises are in use");
  854. }
  855. Promise.prototype._clearCancellationData =
  856. cancellationClearCancellationData;
  857. Promise.prototype._propagateFrom = cancellationPropagateFrom;
  858. Promise.prototype._onCancel = cancellationOnCancel;
  859. Promise.prototype._setOnCancel = cancellationSetOnCancel;
  860. Promise.prototype._attachCancellationCallback =
  861. cancellationAttachCancellationCallback;
  862. Promise.prototype._execute = cancellationExecute;
  863. propagateFromFunction = cancellationPropagateFrom;
  864. config.cancellation = true;
  865. }
  866. if ("monitoring" in opts) {
  867. if (opts.monitoring && !config.monitoring) {
  868. config.monitoring = true;
  869. Promise.prototype._fireEvent = activeFireEvent;
  870. } else if (!opts.monitoring && config.monitoring) {
  871. config.monitoring = false;
  872. Promise.prototype._fireEvent = defaultFireEvent;
  873. }
  874. }
  875. return Promise;
  876. };
  877. function defaultFireEvent() { return false; }
  878. Promise.prototype._fireEvent = defaultFireEvent;
  879. Promise.prototype._execute = function(executor, resolve, reject) {
  880. try {
  881. executor(resolve, reject);
  882. } catch (e) {
  883. return e;
  884. }
  885. };
  886. Promise.prototype._onCancel = function () {};
  887. Promise.prototype._setOnCancel = function (handler) { ; };
  888. Promise.prototype._attachCancellationCallback = function(onCancel) {
  889. ;
  890. };
  891. Promise.prototype._captureStackTrace = function () {};
  892. Promise.prototype._attachExtraTrace = function () {};
  893. Promise.prototype._dereferenceTrace = function () {};
  894. Promise.prototype._clearCancellationData = function() {};
  895. Promise.prototype._propagateFrom = function (parent, flags) {
  896. ;
  897. ;
  898. };
  899. function cancellationExecute(executor, resolve, reject) {
  900. var promise = this;
  901. try {
  902. executor(resolve, reject, function(onCancel) {
  903. if (typeof onCancel !== "function") {
  904. throw new TypeError("onCancel must be a function, got: " +
  905. util.toString(onCancel));
  906. }
  907. promise._attachCancellationCallback(onCancel);
  908. });
  909. } catch (e) {
  910. return e;
  911. }
  912. }
  913. function cancellationAttachCancellationCallback(onCancel) {
  914. if (!this._isCancellable()) return this;
  915. var previousOnCancel = this._onCancel();
  916. if (previousOnCancel !== undefined) {
  917. if (util.isArray(previousOnCancel)) {
  918. previousOnCancel.push(onCancel);
  919. } else {
  920. this._setOnCancel([previousOnCancel, onCancel]);
  921. }
  922. } else {
  923. this._setOnCancel(onCancel);
  924. }
  925. }
  926. function cancellationOnCancel() {
  927. return this._onCancelField;
  928. }
  929. function cancellationSetOnCancel(onCancel) {
  930. this._onCancelField = onCancel;
  931. }
  932. function cancellationClearCancellationData() {
  933. this._cancellationParent = undefined;
  934. this._onCancelField = undefined;
  935. }
  936. function cancellationPropagateFrom(parent, flags) {
  937. if ((flags & 1) !== 0) {
  938. this._cancellationParent = parent;
  939. var branchesRemainingToCancel = parent._branchesRemainingToCancel;
  940. if (branchesRemainingToCancel === undefined) {
  941. branchesRemainingToCancel = 0;
  942. }
  943. parent._branchesRemainingToCancel = branchesRemainingToCancel + 1;
  944. }
  945. if ((flags & 2) !== 0 && parent._isBound()) {
  946. this._setBoundTo(parent._boundTo);
  947. }
  948. }
  949. function bindingPropagateFrom(parent, flags) {
  950. if ((flags & 2) !== 0 && parent._isBound()) {
  951. this._setBoundTo(parent._boundTo);
  952. }
  953. }
  954. var propagateFromFunction = bindingPropagateFrom;
  955. function boundValueFunction() {
  956. var ret = this._boundTo;
  957. if (ret !== undefined) {
  958. if (ret instanceof Promise) {
  959. if (ret.isFulfilled()) {
  960. return ret.value();
  961. } else {
  962. return undefined;
  963. }
  964. }
  965. }
  966. return ret;
  967. }
  968. function longStackTracesCaptureStackTrace() {
  969. this._trace = new CapturedTrace(this._peekContext());
  970. }
  971. function longStackTracesAttachExtraTrace(error, ignoreSelf) {
  972. if (canAttachTrace(error)) {
  973. var trace = this._trace;
  974. if (trace !== undefined) {
  975. if (ignoreSelf) trace = trace._parent;
  976. }
  977. if (trace !== undefined) {
  978. trace.attachExtraTrace(error);
  979. } else if (!error.__stackCleaned__) {
  980. var parsed = parseStackAndMessage(error);
  981. util.notEnumerableProp(error, "stack",
  982. parsed.message + "\n" + parsed.stack.join("\n"));
  983. util.notEnumerableProp(error, "__stackCleaned__", true);
  984. }
  985. }
  986. }
  987. function longStackTracesDereferenceTrace() {
  988. this._trace = undefined;
  989. }
  990. function checkForgottenReturns(returnValue, promiseCreated, name, promise,
  991. parent) {
  992. if (returnValue === undefined && promiseCreated !== null &&
  993. wForgottenReturn) {
  994. if (parent !== undefined && parent._returnedNonUndefined()) return;
  995. if ((promise._bitField & 65535) === 0) return;
  996. if (name) name = name + " ";
  997. var handlerLine = "";
  998. var creatorLine = "";
  999. if (promiseCreated._trace) {
  1000. var traceLines = promiseCreated._trace.stack.split("\n");
  1001. var stack = cleanStack(traceLines);
  1002. for (var i = stack.length - 1; i >= 0; --i) {
  1003. var line = stack[i];
  1004. if (!nodeFramePattern.test(line)) {
  1005. var lineMatches = line.match(parseLinePattern);
  1006. if (lineMatches) {
  1007. handlerLine = "at " + lineMatches[1] +
  1008. ":" + lineMatches[2] + ":" + lineMatches[3] + " ";
  1009. }
  1010. break;
  1011. }
  1012. }
  1013. if (stack.length > 0) {
  1014. var firstUserLine = stack[0];
  1015. for (var i = 0; i < traceLines.length; ++i) {
  1016. if (traceLines[i] === firstUserLine) {
  1017. if (i > 0) {
  1018. creatorLine = "\n" + traceLines[i - 1];
  1019. }
  1020. break;
  1021. }
  1022. }
  1023. }
  1024. }
  1025. var msg = "a promise was created in a " + name +
  1026. "handler " + handlerLine + "but was not returned from it, " +
  1027. "see http://goo.gl/rRqMUw" +
  1028. creatorLine;
  1029. promise._warn(msg, true, promiseCreated);
  1030. }
  1031. }
  1032. function deprecated(name, replacement) {
  1033. var message = name +
  1034. " is deprecated and will be removed in a future version.";
  1035. if (replacement) message += " Use " + replacement + " instead.";
  1036. return warn(message);
  1037. }
  1038. function warn(message, shouldUseOwnTrace, promise) {
  1039. if (!config.warnings) return;
  1040. var warning = new Warning(message);
  1041. var ctx;
  1042. if (shouldUseOwnTrace) {
  1043. promise._attachExtraTrace(warning);
  1044. } else if (config.longStackTraces && (ctx = Promise._peekContext())) {
  1045. ctx.attachExtraTrace(warning);
  1046. } else {
  1047. var parsed = parseStackAndMessage(warning);
  1048. warning.stack = parsed.message + "\n" + parsed.stack.join("\n");
  1049. }
  1050. if (!activeFireEvent("warning", warning)) {
  1051. formatAndLogError(warning, "", true);
  1052. }
  1053. }
  1054. function reconstructStack(message, stacks) {
  1055. for (var i = 0; i < stacks.length - 1; ++i) {
  1056. stacks[i].push("From previous event:");
  1057. stacks[i] = stacks[i].join("\n");
  1058. }
  1059. if (i < stacks.length) {
  1060. stacks[i] = stacks[i].join("\n");
  1061. }
  1062. return message + "\n" + stacks.join("\n");
  1063. }
  1064. function removeDuplicateOrEmptyJumps(stacks) {
  1065. for (var i = 0; i < stacks.length; ++i) {
  1066. if (stacks[i].length === 0 ||
  1067. ((i + 1 < stacks.length) && stacks[i][0] === stacks[i+1][0])) {
  1068. stacks.splice(i, 1);
  1069. i--;
  1070. }
  1071. }
  1072. }
  1073. function removeCommonRoots(stacks) {
  1074. var current = stacks[0];
  1075. for (var i = 1; i < stacks.length; ++i) {
  1076. var prev = stacks[i];
  1077. var currentLastIndex = current.length - 1;
  1078. var currentLastLine = current[currentLastIndex];
  1079. var commonRootMeetPoint = -1;
  1080. for (var j = prev.length - 1; j >= 0; --j) {
  1081. if (prev[j] === currentLastLine) {
  1082. commonRootMeetPoint = j;
  1083. break;
  1084. }
  1085. }
  1086. for (var j = commonRootMeetPoint; j >= 0; --j) {
  1087. var line = prev[j];
  1088. if (current[currentLastIndex] === line) {
  1089. current.pop();
  1090. currentLastIndex--;
  1091. } else {
  1092. break;
  1093. }
  1094. }
  1095. current = prev;
  1096. }
  1097. }
  1098. function cleanStack(stack) {
  1099. var ret = [];
  1100. for (var i = 0; i < stack.length; ++i) {
  1101. var line = stack[i];
  1102. var isTraceLine = " (No stack trace)" === line ||
  1103. stackFramePattern.test(line);
  1104. var isInternalFrame = isTraceLine && shouldIgnore(line);
  1105. if (isTraceLine && !isInternalFrame) {
  1106. if (indentStackFrames && line.charAt(0) !== " ") {
  1107. line = " " + line;
  1108. }
  1109. ret.push(line);
  1110. }
  1111. }
  1112. return ret;
  1113. }
  1114. function stackFramesAsArray(error) {
  1115. var stack = error.stack.replace(/\s+$/g, "").split("\n");
  1116. for (var i = 0; i < stack.length; ++i) {
  1117. var line = stack[i];
  1118. if (" (No stack trace)" === line || stackFramePattern.test(line)) {
  1119. break;
  1120. }
  1121. }
  1122. if (i > 0 && error.name != "SyntaxError") {
  1123. stack = stack.slice(i);
  1124. }
  1125. return stack;
  1126. }
  1127. function parseStackAndMessage(error) {
  1128. var stack = error.stack;
  1129. var message = error.toString();
  1130. stack = typeof stack === "string" && stack.length > 0
  1131. ? stackFramesAsArray(error) : [" (No stack trace)"];
  1132. return {
  1133. message: message,
  1134. stack: error.name == "SyntaxError" ? stack : cleanStack(stack)
  1135. };
  1136. }
  1137. function formatAndLogError(error, title, isSoft) {
  1138. if (typeof console !== "undefined") {
  1139. var message;
  1140. if (util.isObject(error)) {
  1141. var stack = error.stack;
  1142. message = title + formatStack(stack, error);
  1143. } else {
  1144. message = title + String(error);
  1145. }
  1146. if (typeof printWarning === "function") {
  1147. printWarning(message, isSoft);
  1148. } else if (typeof console.log === "function" ||
  1149. typeof console.log === "object") {
  1150. console.log(message);
  1151. }
  1152. }
  1153. }
  1154. function fireRejectionEvent(name, localHandler, reason, promise) {
  1155. var localEventFired = false;
  1156. try {
  1157. if (typeof localHandler === "function") {
  1158. localEventFired = true;
  1159. if (name === "rejectionHandled") {
  1160. localHandler(promise);
  1161. } else {
  1162. localHandler(reason, promise);
  1163. }
  1164. }
  1165. } catch (e) {
  1166. async.throwLater(e);
  1167. }
  1168. if (name === "unhandledRejection") {
  1169. if (!activeFireEvent(name, reason, promise) && !localEventFired) {
  1170. formatAndLogError(reason, "Unhandled rejection ");
  1171. }
  1172. } else {
  1173. activeFireEvent(name, promise);
  1174. }
  1175. }
  1176. function formatNonError(obj) {
  1177. var str;
  1178. if (typeof obj === "function") {
  1179. str = "[function " +
  1180. (obj.name || "anonymous") +
  1181. "]";
  1182. } else {
  1183. str = obj && typeof obj.toString === "function"
  1184. ? obj.toString() : util.toString(obj);
  1185. var ruselessToString = /\[object [a-zA-Z0-9$_]+\]/;
  1186. if (ruselessToString.test(str)) {
  1187. try {
  1188. var newStr = JSON.stringify(obj);
  1189. str = newStr;
  1190. }
  1191. catch(e) {
  1192. }
  1193. }
  1194. if (str.length === 0) {
  1195. str = "(empty array)";
  1196. }
  1197. }
  1198. return ("(<" + snip(str) + ">, no stack trace)");
  1199. }
  1200. function snip(str) {
  1201. var maxChars = 41;
  1202. if (str.length < maxChars) {
  1203. return str;
  1204. }
  1205. return str.substr(0, maxChars - 3) + "...";
  1206. }
  1207. function longStackTracesIsSupported() {
  1208. return typeof captureStackTrace === "function";
  1209. }
  1210. var shouldIgnore = function() { return false; };
  1211. var parseLineInfoRegex = /[\/<\(]([^:\/]+):(\d+):(?:\d+)\)?\s*$/;
  1212. function parseLineInfo(line) {
  1213. var matches = line.match(parseLineInfoRegex);
  1214. if (matches) {
  1215. return {
  1216. fileName: matches[1],
  1217. line: parseInt(matches[2], 10)
  1218. };
  1219. }
  1220. }
  1221. function setBounds(firstLineError, lastLineError) {
  1222. if (!longStackTracesIsSupported()) return;
  1223. var firstStackLines = (firstLineError.stack || "").split("\n");
  1224. var lastStackLines = (lastLineError.stack || "").split("\n");
  1225. var firstIndex = -1;
  1226. var lastIndex = -1;
  1227. var firstFileName;
  1228. var lastFileName;
  1229. for (var i = 0; i < firstStackLines.length; ++i) {
  1230. var result = parseLineInfo(firstStackLines[i]);
  1231. if (result) {
  1232. firstFileName = result.fileName;
  1233. firstIndex = result.line;
  1234. break;
  1235. }
  1236. }
  1237. for (var i = 0; i < lastStackLines.length; ++i) {
  1238. var result = parseLineInfo(lastStackLines[i]);
  1239. if (result) {
  1240. lastFileName = result.fileName;
  1241. lastIndex = result.line;
  1242. break;
  1243. }
  1244. }
  1245. if (firstIndex < 0 || lastIndex < 0 || !firstFileName || !lastFileName ||
  1246. firstFileName !== lastFileName || firstIndex >= lastIndex) {
  1247. return;
  1248. }
  1249. shouldIgnore = function(line) {
  1250. if (bluebirdFramePattern.test(line)) return true;
  1251. var info = parseLineInfo(line);
  1252. if (info) {
  1253. if (info.fileName === firstFileName &&
  1254. (firstIndex <= info.line && info.line <= lastIndex)) {
  1255. return true;
  1256. }
  1257. }
  1258. return false;
  1259. };
  1260. }
  1261. function CapturedTrace(parent) {
  1262. this._parent = parent;
  1263. this._promisesCreated = 0;
  1264. var length = this._length = 1 + (parent === undefined ? 0 : parent._length);
  1265. captureStackTrace(this, CapturedTrace);
  1266. if (length > 32) this.uncycle();
  1267. }
  1268. util.inherits(CapturedTrace, Error);
  1269. Context.CapturedTrace = CapturedTrace;
  1270. CapturedTrace.prototype.uncycle = function() {
  1271. var length = this._length;
  1272. if (length < 2) return;
  1273. var nodes = [];
  1274. var stackToIndex = {};
  1275. for (var i = 0, node = this; node !== undefined; ++i) {
  1276. nodes.push(node);
  1277. node = node._parent;
  1278. }
  1279. length = this._length = i;
  1280. for (var i = length - 1; i >= 0; --i) {
  1281. var stack = nodes[i].stack;
  1282. if (stackToIndex[stack] === undefined) {
  1283. stackToIndex[stack] = i;
  1284. }
  1285. }
  1286. for (var i = 0; i < length; ++i) {
  1287. var currentStack = nodes[i].stack;
  1288. var index = stackToIndex[currentStack];
  1289. if (index !== undefined && index !== i) {
  1290. if (index > 0) {
  1291. nodes[index - 1]._parent = undefined;
  1292. nodes[index - 1]._length = 1;
  1293. }
  1294. nodes[i]._parent = undefined;
  1295. nodes[i]._length = 1;
  1296. var cycleEdgeNode = i > 0 ? nodes[i - 1] : this;
  1297. if (index < length - 1) {
  1298. cycleEdgeNode._parent = nodes[index + 1];
  1299. cycleEdgeNode._parent.uncycle();
  1300. cycleEdgeNode._length =
  1301. cycleEdgeNode._parent._length + 1;
  1302. } else {
  1303. cycleEdgeNode._parent = undefined;
  1304. cycleEdgeNode._length = 1;
  1305. }
  1306. var currentChildLength = cycleEdgeNode._length + 1;
  1307. for (var j = i - 2; j >= 0; --j) {
  1308. nodes[j]._length = currentChildLength;
  1309. currentChildLength++;
  1310. }
  1311. return;
  1312. }
  1313. }
  1314. };
  1315. CapturedTrace.prototype.attachExtraTrace = function(error) {
  1316. if (error.__stackCleaned__) return;
  1317. this.uncycle();
  1318. var parsed = parseStackAndMessage(error);
  1319. var message = parsed.message;
  1320. var stacks = [parsed.stack];
  1321. var trace = this;
  1322. while (trace !== undefined) {
  1323. stacks.push(cleanStack(trace.stack.split("\n")));
  1324. trace = trace._parent;
  1325. }
  1326. removeCommonRoots(stacks);
  1327. removeDuplicateOrEmptyJumps(stacks);
  1328. util.notEnumerableProp(error, "stack", reconstructStack(message, stacks));
  1329. util.notEnumerableProp(error, "__stackCleaned__", true);
  1330. };
  1331. var captureStackTrace = (function stackDetection() {
  1332. var v8stackFramePattern = /^\s*at\s*/;
  1333. var v8stackFormatter = function(stack, error) {
  1334. if (typeof stack === "string") return stack;
  1335. if (error.name !== undefined &&
  1336. error.message !== undefined) {
  1337. return error.toString();
  1338. }
  1339. return formatNonError(error);
  1340. };
  1341. if (typeof Error.stackTraceLimit === "number" &&
  1342. typeof Error.captureStackTrace === "function") {
  1343. Error.stackTraceLimit += 6;
  1344. stackFramePattern = v8stackFramePattern;
  1345. formatStack = v8stackFormatter;
  1346. var captureStackTrace = Error.captureStackTrace;
  1347. shouldIgnore = function(line) {
  1348. return bluebirdFramePattern.test(line);
  1349. };
  1350. return function(receiver, ignoreUntil) {
  1351. Error.stackTraceLimit += 6;
  1352. captureStackTrace(receiver, ignoreUntil);
  1353. Error.stackTraceLimit -= 6;
  1354. };
  1355. }
  1356. var err = new Error();
  1357. if (typeof err.stack === "string" &&
  1358. err.stack.split("\n")[0].indexOf("stackDetection@") >= 0) {
  1359. stackFramePattern = /@/;
  1360. formatStack = v8stackFormatter;
  1361. indentStackFrames = true;
  1362. return function captureStackTrace(o) {
  1363. o.stack = new Error().stack;
  1364. };
  1365. }
  1366. var hasStackAfterThrow;
  1367. try { throw new Error(); }
  1368. catch(e) {
  1369. hasStackAfterThrow = ("stack" in e);
  1370. }
  1371. if (!("stack" in err) && hasStackAfterThrow &&
  1372. typeof Error.stackTraceLimit === "number") {
  1373. stackFramePattern = v8stackFramePattern;
  1374. formatStack = v8stackFormatter;
  1375. return function captureStackTrace(o) {
  1376. Error.stackTraceLimit += 6;
  1377. try { throw new Error(); }
  1378. catch(e) { o.stack = e.stack; }
  1379. Error.stackTraceLimit -= 6;
  1380. };
  1381. }
  1382. formatStack = function(stack, error) {
  1383. if (typeof stack === "string") return stack;
  1384. if ((typeof error === "object" ||
  1385. typeof error === "function") &&
  1386. error.name !== undefined &&
  1387. error.message !== undefined) {
  1388. return error.toString();
  1389. }
  1390. return formatNonError(error);
  1391. };
  1392. return null;
  1393. })([]);
  1394. if (typeof console !== "undefined" && typeof console.warn !== "undefined") {
  1395. printWarning = function (message) {
  1396. console.warn(message);
  1397. };
  1398. if (util.isNode && process.stderr.isTTY) {
  1399. printWarning = function(message, isSoft) {
  1400. var color = isSoft ? "\u001b[33m" : "\u001b[31m";
  1401. console.warn(color + message + "\u001b[0m\n");
  1402. };
  1403. } else if (!util.isNode && typeof (new Error().stack) === "string") {
  1404. printWarning = function(message, isSoft) {
  1405. console.warn("%c" + message,
  1406. isSoft ? "color: darkorange" : "color: red");
  1407. };
  1408. }
  1409. }
  1410. var config = {
  1411. warnings: warnings,
  1412. longStackTraces: false,
  1413. cancellation: false,
  1414. monitoring: false
  1415. };
  1416. if (longStackTraces) Promise.longStackTraces();
  1417. return {
  1418. longStackTraces: function() {
  1419. return config.longStackTraces;
  1420. },
  1421. warnings: function() {
  1422. return config.warnings;
  1423. },
  1424. cancellation: function() {
  1425. return config.cancellation;
  1426. },
  1427. monitoring: function() {
  1428. return config.monitoring;
  1429. },
  1430. propagateFromFunction: function() {
  1431. return propagateFromFunction;
  1432. },
  1433. boundValueFunction: function() {
  1434. return boundValueFunction;
  1435. },
  1436. checkForgottenReturns: checkForgottenReturns,
  1437. setBounds: setBounds,
  1438. warn: warn,
  1439. deprecated: deprecated,
  1440. CapturedTrace: CapturedTrace,
  1441. fireDomEvent: fireDomEvent,
  1442. fireGlobalEvent: fireGlobalEvent
  1443. };
  1444. };
  1445. },{"./errors":12,"./es5":13,"./util":36}],10:[function(_dereq_,module,exports){
  1446. "use strict";
  1447. module.exports = function(Promise) {
  1448. function returner() {
  1449. return this.value;
  1450. }
  1451. function thrower() {
  1452. throw this.reason;
  1453. }
  1454. Promise.prototype["return"] =
  1455. Promise.prototype.thenReturn = function (value) {
  1456. if (value instanceof Promise) value.suppressUnhandledRejections();
  1457. return this._then(
  1458. returner, undefined, undefined, {value: value}, undefined);
  1459. };
  1460. Promise.prototype["throw"] =
  1461. Promise.prototype.thenThrow = function (reason) {
  1462. return this._then(
  1463. thrower, undefined, undefined, {reason: reason}, undefined);
  1464. };
  1465. Promise.prototype.catchThrow = function (reason) {
  1466. if (arguments.length <= 1) {
  1467. return this._then(
  1468. undefined, thrower, undefined, {reason: reason}, undefined);
  1469. } else {
  1470. var _reason = arguments[1];
  1471. var handler = function() {throw _reason;};
  1472. return this.caught(reason, handler);
  1473. }
  1474. };
  1475. Promise.prototype.catchReturn = function (value) {
  1476. if (arguments.length <= 1) {
  1477. if (value instanceof Promise) value.suppressUnhandledRejections();
  1478. return this._then(
  1479. undefined, returner, undefined, {value: value}, undefined);
  1480. } else {
  1481. var _value = arguments[1];
  1482. if (_value instanceof Promise) _value.suppressUnhandledRejections();
  1483. var handler = function() {return _value;};
  1484. return this.caught(value, handler);
  1485. }
  1486. };
  1487. };
  1488. },{}],11:[function(_dereq_,module,exports){
  1489. "use strict";
  1490. module.exports = function(Promise, INTERNAL) {
  1491. var PromiseReduce = Promise.reduce;
  1492. var PromiseAll = Promise.all;
  1493. function promiseAllThis() {
  1494. return PromiseAll(this);
  1495. }
  1496. function PromiseMapSeries(promises, fn) {
  1497. return PromiseReduce(promises, fn, INTERNAL, INTERNAL);
  1498. }
  1499. Promise.prototype.each = function (fn) {
  1500. return PromiseReduce(this, fn, INTERNAL, 0)
  1501. ._then(promiseAllThis, undefined, undefined, this, undefined);
  1502. };
  1503. Promise.prototype.mapSeries = function (fn) {
  1504. return PromiseReduce(this, fn, INTERNAL, INTERNAL);
  1505. };
  1506. Promise.each = function (promises, fn) {
  1507. return PromiseReduce(promises, fn, INTERNAL, 0)
  1508. ._then(promiseAllThis, undefined, undefined, promises, undefined);
  1509. };
  1510. Promise.mapSeries = PromiseMapSeries;
  1511. };
  1512. },{}],12:[function(_dereq_,module,exports){
  1513. "use strict";
  1514. var es5 = _dereq_("./es5");
  1515. var Objectfreeze = es5.freeze;
  1516. var util = _dereq_("./util");
  1517. var inherits = util.inherits;
  1518. var notEnumerableProp = util.notEnumerableProp;
  1519. function subError(nameProperty, defaultMessage) {
  1520. function SubError(message) {
  1521. if (!(this instanceof SubError)) return new SubError(message);
  1522. notEnumerableProp(this, "message",
  1523. typeof message === "string" ? message : defaultMessage);
  1524. notEnumerableProp(this, "name", nameProperty);
  1525. if (Error.captureStackTrace) {
  1526. Error.captureStackTrace(this, this.constructor);
  1527. } else {
  1528. Error.call(this);
  1529. }
  1530. }
  1531. inherits(SubError, Error);
  1532. return SubError;
  1533. }
  1534. var _TypeError, _RangeError;
  1535. var Warning = subError("Warning", "warning");
  1536. var CancellationError = subError("CancellationError", "cancellation error");
  1537. var TimeoutError = subError("TimeoutError", "timeout error");
  1538. var AggregateError = subError("AggregateError", "aggregate error");
  1539. try {
  1540. _TypeError = TypeError;
  1541. _RangeError = RangeError;
  1542. } catch(e) {
  1543. _TypeError = subError("TypeError", "type error");
  1544. _RangeError = subError("RangeError", "range error");
  1545. }
  1546. var methods = ("join pop push shift unshift slice filter forEach some " +
  1547. "every map indexOf lastIndexOf reduce reduceRight sort reverse").split(" ");
  1548. for (var i = 0; i < methods.length; ++i) {
  1549. if (typeof Array.prototype[methods[i]] === "function") {
  1550. AggregateError.prototype[methods[i]] = Array.prototype[methods[i]];
  1551. }
  1552. }
  1553. es5.defineProperty(AggregateError.prototype, "length", {
  1554. value: 0,
  1555. configurable: false,
  1556. writable: true,
  1557. enumerable: true
  1558. });
  1559. AggregateError.prototype["isOperational"] = true;
  1560. var level = 0;
  1561. AggregateError.prototype.toString = function() {
  1562. var indent = Array(level * 4 + 1).join(" ");
  1563. var ret = "\n" + indent + "AggregateError of:" + "\n";
  1564. level++;
  1565. indent = Array(level * 4 + 1).join(" ");
  1566. for (var i = 0; i < this.length; ++i) {
  1567. var str = this[i] === this ? "[Circular AggregateError]" : this[i] + "";
  1568. var lines = str.split("\n");
  1569. for (var j = 0; j < lines.length; ++j) {
  1570. lines[j] = indent + lines[j];
  1571. }
  1572. str = lines.join("\n");
  1573. ret += str + "\n";
  1574. }
  1575. level--;
  1576. return ret;
  1577. };
  1578. function OperationalError(message) {
  1579. if (!(this instanceof OperationalError))
  1580. return new OperationalError(message);
  1581. notEnumerableProp(this, "name", "OperationalError");
  1582. notEnumerableProp(this, "message", message);
  1583. this.cause = message;
  1584. this["isOperational"] = true;
  1585. if (message instanceof Error) {
  1586. notEnumerableProp(this, "message", message.message);
  1587. notEnumerableProp(this, "stack", message.stack);
  1588. } else if (Error.captureStackTrace) {
  1589. Error.captureStackTrace(this, this.constructor);
  1590. }
  1591. }
  1592. inherits(OperationalError, Error);
  1593. var errorTypes = Error["__BluebirdErrorTypes__"];
  1594. if (!errorTypes) {
  1595. errorTypes = Objectfreeze({
  1596. CancellationError: CancellationError,
  1597. TimeoutError: TimeoutError,
  1598. OperationalError: OperationalError,
  1599. RejectionError: OperationalError,
  1600. AggregateError: AggregateError
  1601. });
  1602. es5.defineProperty(Error, "__BluebirdErrorTypes__", {
  1603. value: errorTypes,
  1604. writable: false,
  1605. enumerable: false,
  1606. configurable: false
  1607. });
  1608. }
  1609. module.exports = {
  1610. Error: Error,
  1611. TypeError: _TypeError,
  1612. RangeError: _RangeError,
  1613. CancellationError: errorTypes.CancellationError,
  1614. OperationalError: errorTypes.OperationalError,
  1615. TimeoutError: errorTypes.TimeoutError,
  1616. AggregateError: errorTypes.AggregateError,
  1617. Warning: Warning
  1618. };
  1619. },{"./es5":13,"./util":36}],13:[function(_dereq_,module,exports){
  1620. var isES5 = (function(){
  1621. "use strict";
  1622. return this === undefined;
  1623. })();
  1624. if (isES5) {
  1625. module.exports = {
  1626. freeze: Object.freeze,
  1627. defineProperty: Object.defineProperty,
  1628. getDescriptor: Object.getOwnPropertyDescriptor,
  1629. keys: Object.keys,
  1630. names: Object.getOwnPropertyNames,
  1631. getPrototypeOf: Object.getPrototypeOf,
  1632. isArray: Array.isArray,
  1633. isES5: isES5,
  1634. propertyIsWritable: function(obj, prop) {
  1635. var descriptor = Object.getOwnPropertyDescriptor(obj, prop);
  1636. return !!(!descriptor || descriptor.writable || descriptor.set);
  1637. }
  1638. };
  1639. } else {
  1640. var has = {}.hasOwnProperty;
  1641. var str = {}.toString;
  1642. var proto = {}.constructor.prototype;
  1643. var ObjectKeys = function (o) {
  1644. var ret = [];
  1645. for (var key in o) {
  1646. if (has.call(o, key)) {
  1647. ret.push(key);
  1648. }
  1649. }
  1650. return ret;
  1651. };
  1652. var ObjectGetDescriptor = function(o, key) {
  1653. return {value: o[key]};
  1654. };
  1655. var ObjectDefineProperty = function (o, key, desc) {
  1656. o[key] = desc.value;
  1657. return o;
  1658. };
  1659. var ObjectFreeze = function (obj) {
  1660. return obj;
  1661. };
  1662. var ObjectGetPrototypeOf = function (obj) {
  1663. try {
  1664. return Object(obj).constructor.prototype;
  1665. }
  1666. catch (e) {
  1667. return proto;
  1668. }
  1669. };
  1670. var ArrayIsArray = function (obj) {
  1671. try {
  1672. return str.call(obj) === "[object Array]";
  1673. }
  1674. catch(e) {
  1675. return false;
  1676. }
  1677. };
  1678. module.exports = {
  1679. isArray: ArrayIsArray,
  1680. keys: ObjectKeys,
  1681. names: ObjectKeys,
  1682. defineProperty: ObjectDefineProperty,
  1683. getDescriptor: ObjectGetDescriptor,
  1684. freeze: ObjectFreeze,
  1685. getPrototypeOf: ObjectGetPrototypeOf,
  1686. isES5: isES5,
  1687. propertyIsWritable: function() {
  1688. return true;
  1689. }
  1690. };
  1691. }
  1692. },{}],14:[function(_dereq_,module,exports){
  1693. "use strict";
  1694. module.exports = function(Promise, INTERNAL) {
  1695. var PromiseMap = Promise.map;
  1696. Promise.prototype.filter = function (fn, options) {
  1697. return PromiseMap(this, fn, options, INTERNAL);
  1698. };
  1699. Promise.filter = function (promises, fn, options) {
  1700. return PromiseMap(promises, fn, options, INTERNAL);
  1701. };
  1702. };
  1703. },{}],15:[function(_dereq_,module,exports){
  1704. "use strict";
  1705. module.exports = function(Promise, tryConvertToPromise, NEXT_FILTER) {
  1706. var util = _dereq_("./util");
  1707. var CancellationError = Promise.CancellationError;
  1708. var errorObj = util.errorObj;
  1709. var catchFilter = _dereq_("./catch_filter")(NEXT_FILTER);
  1710. function PassThroughHandlerContext(promise, type, handler) {
  1711. this.promise = promise;
  1712. this.type = type;
  1713. this.handler = handler;
  1714. this.called = false;
  1715. this.cancelPromise = null;
  1716. }
  1717. PassThroughHandlerContext.prototype.isFinallyHandler = function() {
  1718. return this.type === 0;
  1719. };
  1720. function FinallyHandlerCancelReaction(finallyHandler) {
  1721. this.finallyHandler = finallyHandler;
  1722. }
  1723. FinallyHandlerCancelReaction.prototype._resultCancelled = function() {
  1724. checkCancel(this.finallyHandler);
  1725. };
  1726. function checkCancel(ctx, reason) {
  1727. if (ctx.cancelPromise != null) {
  1728. if (arguments.length > 1) {
  1729. ctx.cancelPromise._reject(reason);
  1730. } else {
  1731. ctx.cancelPromise._cancel();
  1732. }
  1733. ctx.cancelPromise = null;
  1734. return true;
  1735. }
  1736. return false;
  1737. }
  1738. function succeed() {
  1739. return finallyHandler.call(this, this.promise._target()._settledValue());
  1740. }
  1741. function fail(reason) {
  1742. if (checkCancel(this, reason)) return;
  1743. errorObj.e = reason;
  1744. return errorObj;
  1745. }
  1746. function finallyHandler(reasonOrValue) {
  1747. var promise = this.promise;
  1748. var handler = this.handler;
  1749. if (!this.called) {
  1750. this.called = true;
  1751. var ret = this.isFinallyHandler()
  1752. ? handler.call(promise._boundValue())
  1753. : handler.call(promise._boundValue(), reasonOrValue);
  1754. if (ret === NEXT_FILTER) {
  1755. return ret;
  1756. } else if (ret !== undefined) {
  1757. promise._setReturnedNonUndefined();
  1758. var maybePromise = tryConvertToPromise(ret, promise);
  1759. if (maybePromise instanceof Promise) {
  1760. if (this.cancelPromise != null) {
  1761. if (maybePromise._isCancelled()) {
  1762. var reason =
  1763. new CancellationError("late cancellation observer");
  1764. promise._attachExtraTrace(reason);
  1765. errorObj.e = reason;
  1766. return errorObj;
  1767. } else if (maybePromise.isPending()) {
  1768. maybePromise._attachCancellationCallback(
  1769. new FinallyHandlerCancelReaction(this));
  1770. }
  1771. }
  1772. return maybePromise._then(
  1773. succeed, fail, undefined, this, undefined);
  1774. }
  1775. }
  1776. }
  1777. if (promise.isRejected()) {
  1778. checkCancel(this);
  1779. errorObj.e = reasonOrValue;
  1780. return errorObj;
  1781. } else {
  1782. checkCancel(this);
  1783. return reasonOrValue;
  1784. }
  1785. }
  1786. Promise.prototype._passThrough = function(handler, type, success, fail) {
  1787. if (typeof handler !== "function") return this.then();
  1788. return this._then(success,
  1789. fail,
  1790. undefined,
  1791. new PassThroughHandlerContext(this, type, handler),
  1792. undefined);
  1793. };
  1794. Promise.prototype.lastly =
  1795. Promise.prototype["finally"] = function (handler) {
  1796. return this._passThrough(handler,
  1797. 0,
  1798. finallyHandler,
  1799. finallyHandler);
  1800. };
  1801. Promise.prototype.tap = function (handler) {
  1802. return this._passThrough(handler, 1, finallyHandler);
  1803. };
  1804. Promise.prototype.tapCatch = function (handlerOrPredicate) {
  1805. var len = arguments.length;
  1806. if(len === 1) {
  1807. return this._passThrough(handlerOrPredicate,
  1808. 1,
  1809. undefined,
  1810. finallyHandler);
  1811. } else {
  1812. var catchInstances = new Array(len - 1),
  1813. j = 0, i;
  1814. for (i = 0; i < len - 1; ++i) {
  1815. var item = arguments[i];
  1816. if (util.isObject(item)) {
  1817. catchInstances[j++] = item;
  1818. } else {
  1819. return Promise.reject(new TypeError(
  1820. "tapCatch statement predicate: "
  1821. + "expecting an object but got " + util.classString(item)
  1822. ));
  1823. }
  1824. }
  1825. catchInstances.length = j;
  1826. var handler = arguments[i];
  1827. return this._passThrough(catchFilter(catchInstances, handler, this),
  1828. 1,
  1829. undefined,
  1830. finallyHandler);
  1831. }
  1832. };
  1833. return PassThroughHandlerContext;
  1834. };
  1835. },{"./catch_filter":7,"./util":36}],16:[function(_dereq_,module,exports){
  1836. "use strict";
  1837. module.exports = function(Promise,
  1838. apiRejection,
  1839. INTERNAL,
  1840. tryConvertToPromise,
  1841. Proxyable,
  1842. debug) {
  1843. var errors = _dereq_("./errors");
  1844. var TypeError = errors.TypeError;
  1845. var util = _dereq_("./util");
  1846. var errorObj = util.errorObj;
  1847. var tryCatch = util.tryCatch;
  1848. var yieldHandlers = [];
  1849. function promiseFromYieldHandler(value, yieldHandlers, traceParent) {
  1850. for (var i = 0; i < yieldHandlers.length; ++i) {
  1851. traceParent._pushContext();
  1852. var result = tryCatch(yieldHandlers[i])(value);
  1853. traceParent._popContext();
  1854. if (result === errorObj) {
  1855. traceParent._pushContext();
  1856. var ret = Promise.reject(errorObj.e);
  1857. traceParent._popContext();
  1858. return ret;
  1859. }
  1860. var maybePromise = tryConvertToPromise(result, traceParent);
  1861. if (maybePromise instanceof Promise) return maybePromise;
  1862. }
  1863. return null;
  1864. }
  1865. function PromiseSpawn(generatorFunction, receiver, yieldHandler, stack) {
  1866. if (debug.cancellation()) {
  1867. var internal = new Promise(INTERNAL);
  1868. var _finallyPromise = this._finallyPromise = new Promise(INTERNAL);
  1869. this._promise = internal.lastly(function() {
  1870. return _finallyPromise;
  1871. });
  1872. internal._captureStackTrace();
  1873. internal._setOnCancel(this);
  1874. } else {
  1875. var promise = this._promise = new Promise(INTERNAL);
  1876. promise._captureStackTrace();
  1877. }
  1878. this._stack = stack;
  1879. this._generatorFunction = generatorFunction;
  1880. this._receiver = receiver;
  1881. this._generator = undefined;
  1882. this._yieldHandlers = typeof yieldHandler === "function"
  1883. ? [yieldHandler].concat(yieldHandlers)
  1884. : yieldHandlers;
  1885. this._yieldedPromise = null;
  1886. this._cancellationPhase = false;
  1887. }
  1888. util.inherits(PromiseSpawn, Proxyable);
  1889. PromiseSpawn.prototype._isResolved = function() {
  1890. return this._promise === null;
  1891. };
  1892. PromiseSpawn.prototype._cleanup = function() {
  1893. this._promise = this._generator = null;
  1894. if (debug.cancellation() && this._finallyPromise !== null) {
  1895. this._finallyPromise._fulfill();
  1896. this._finallyPromise = null;
  1897. }
  1898. };
  1899. PromiseSpawn.prototype._promiseCancelled = function() {
  1900. if (this._isResolved()) return;
  1901. var implementsReturn = typeof this._generator["return"] !== "undefined";
  1902. var result;
  1903. if (!implementsReturn) {
  1904. var reason = new Promise.CancellationError(
  1905. "generator .return() sentinel");
  1906. Promise.coroutine.returnSentinel = reason;
  1907. this._promise._attachExtraTrace(reason);
  1908. this._promise._pushContext();
  1909. result = tryCatch(this._generator["throw"]).call(this._generator,
  1910. reason);
  1911. this._promise._popContext();
  1912. } else {
  1913. this._promise._pushContext();
  1914. result = tryCatch(this._generator["return"]).call(this._generator,
  1915. undefined);
  1916. this._promise._popContext();
  1917. }
  1918. this._cancellationPhase = true;
  1919. this._yieldedPromise = null;
  1920. this._continue(result);
  1921. };
  1922. PromiseSpawn.prototype._promiseFulfilled = function(value) {
  1923. this._yieldedPromise = null;
  1924. this._promise._pushContext();
  1925. var result = tryCatch(this._generator.next).call(this._generator, value);
  1926. this._promise._popContext();
  1927. this._continue(result);
  1928. };
  1929. PromiseSpawn.prototype._promiseRejected = function(reason) {
  1930. this._yieldedPromise = null;
  1931. this._promise._attachExtraTrace(reason);
  1932. this._promise._pushContext();
  1933. var result = tryCatch(this._generator["throw"])
  1934. .call(this._generator, reason);
  1935. this._promise._popContext();
  1936. this._continue(result);
  1937. };
  1938. PromiseSpawn.prototype._resultCancelled = function() {
  1939. if (this._yieldedPromise instanceof Promise) {
  1940. var promise = this._yieldedPromise;
  1941. this._yieldedPromise = null;
  1942. promise.cancel();
  1943. }
  1944. };
  1945. PromiseSpawn.prototype.promise = function () {
  1946. return this._promise;
  1947. };
  1948. PromiseSpawn.prototype._run = function () {
  1949. this._generator = this._generatorFunction.call(this._receiver);
  1950. this._receiver =
  1951. this._generatorFunction = undefined;
  1952. this._promiseFulfilled(undefined);
  1953. };
  1954. PromiseSpawn.prototype._continue = function (result) {
  1955. var promise = this._promise;
  1956. if (result === errorObj) {
  1957. this._cleanup();
  1958. if (this._cancellationPhase) {
  1959. return promise.cancel();
  1960. } else {
  1961. return promise._rejectCallback(result.e, false);
  1962. }
  1963. }
  1964. var value = result.value;
  1965. if (result.done === true) {
  1966. this._cleanup();
  1967. if (this._cancellationPhase) {
  1968. return promise.cancel();
  1969. } else {
  1970. return promise._resolveCallback(value);
  1971. }
  1972. } else {
  1973. var maybePromise = tryConvertToPromise(value, this._promise);
  1974. if (!(maybePromise instanceof Promise)) {
  1975. maybePromise =
  1976. promiseFromYieldHandler(maybePromise,
  1977. this._yieldHandlers,
  1978. this._promise);
  1979. if (maybePromise === null) {
  1980. this._promiseRejected(
  1981. new TypeError(
  1982. "A value %s was yielded that could not be treated as a promise\u000a\u000a See http://goo.gl/MqrFmX\u000a\u000a".replace("%s", String(value)) +
  1983. "From coroutine:\u000a" +
  1984. this._stack.split("\n").slice(1, -7).join("\n")
  1985. )
  1986. );
  1987. return;
  1988. }
  1989. }
  1990. maybePromise = maybePromise._target();
  1991. var bitField = maybePromise._bitField;
  1992. ;
  1993. if (((bitField & 50397184) === 0)) {
  1994. this._yieldedPromise = maybePromise;
  1995. maybePromise._proxy(this, null);
  1996. } else if (((bitField & 33554432) !== 0)) {
  1997. Promise._async.invoke(
  1998. this._promiseFulfilled, this, maybePromise._value()
  1999. );
  2000. } else if (((bitField & 16777216) !== 0)) {
  2001. Promise._async.invoke(
  2002. this._promiseRejected, this, maybePromise._reason()
  2003. );
  2004. } else {
  2005. this._promiseCancelled();
  2006. }
  2007. }
  2008. };
  2009. Promise.coroutine = function (generatorFunction, options) {
  2010. if (typeof generatorFunction !== "function") {
  2011. throw new TypeError("generatorFunction must be a function\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  2012. }
  2013. var yieldHandler = Object(options).yieldHandler;
  2014. var PromiseSpawn$ = PromiseSpawn;
  2015. var stack = new Error().stack;
  2016. return function () {
  2017. var generator = generatorFunction.apply(this, arguments);
  2018. var spawn = new PromiseSpawn$(undefined, undefined, yieldHandler,
  2019. stack);
  2020. var ret = spawn.promise();
  2021. spawn._generator = generator;
  2022. spawn._promiseFulfilled(undefined);
  2023. return ret;
  2024. };
  2025. };
  2026. Promise.coroutine.addYieldHandler = function(fn) {
  2027. if (typeof fn !== "function") {
  2028. throw new TypeError("expecting a function but got " + util.classString(fn));
  2029. }
  2030. yieldHandlers.push(fn);
  2031. };
  2032. Promise.spawn = function (generatorFunction) {
  2033. debug.deprecated("Promise.spawn()", "Promise.coroutine()");
  2034. if (typeof generatorFunction !== "function") {
  2035. return apiRejection("generatorFunction must be a function\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  2036. }
  2037. var spawn = new PromiseSpawn(generatorFunction, this);
  2038. var ret = spawn.promise();
  2039. spawn._run(Promise.spawn);
  2040. return ret;
  2041. };
  2042. };
  2043. },{"./errors":12,"./util":36}],17:[function(_dereq_,module,exports){
  2044. "use strict";
  2045. module.exports =
  2046. function(Promise, PromiseArray, tryConvertToPromise, INTERNAL, async,
  2047. getDomain) {
  2048. var util = _dereq_("./util");
  2049. var canEvaluate = util.canEvaluate;
  2050. var tryCatch = util.tryCatch;
  2051. var errorObj = util.errorObj;
  2052. var reject;
  2053. if (!true) {
  2054. if (canEvaluate) {
  2055. var thenCallback = function(i) {
  2056. return new Function("value", "holder", " \n\
  2057. 'use strict'; \n\
  2058. holder.pIndex = value; \n\
  2059. holder.checkFulfillment(this); \n\
  2060. ".replace(/Index/g, i));
  2061. };
  2062. var promiseSetter = function(i) {
  2063. return new Function("promise", "holder", " \n\
  2064. 'use strict'; \n\
  2065. holder.pIndex = promise; \n\
  2066. ".replace(/Index/g, i));
  2067. };
  2068. var generateHolderClass = function(total) {
  2069. var props = new Array(total);
  2070. for (var i = 0; i < props.length; ++i) {
  2071. props[i] = "this.p" + (i+1);
  2072. }
  2073. var assignment = props.join(" = ") + " = null;";
  2074. var cancellationCode= "var promise;\n" + props.map(function(prop) {
  2075. return " \n\
  2076. promise = " + prop + "; \n\
  2077. if (promise instanceof Promise) { \n\
  2078. promise.cancel(); \n\
  2079. } \n\
  2080. ";
  2081. }).join("\n");
  2082. var passedArguments = props.join(", ");
  2083. var name = "Holder$" + total;
  2084. var code = "return function(tryCatch, errorObj, Promise, async) { \n\
  2085. 'use strict'; \n\
  2086. function [TheName](fn) { \n\
  2087. [TheProperties] \n\
  2088. this.fn = fn; \n\
  2089. this.asyncNeeded = true; \n\
  2090. this.now = 0; \n\
  2091. } \n\
  2092. \n\
  2093. [TheName].prototype._callFunction = function(promise) { \n\
  2094. promise._pushContext(); \n\
  2095. var ret = tryCatch(this.fn)([ThePassedArguments]); \n\
  2096. promise._popContext(); \n\
  2097. if (ret === errorObj) { \n\
  2098. promise._rejectCallback(ret.e, false); \n\
  2099. } else { \n\
  2100. promise._resolveCallback(ret); \n\
  2101. } \n\
  2102. }; \n\
  2103. \n\
  2104. [TheName].prototype.checkFulfillment = function(promise) { \n\
  2105. var now = ++this.now; \n\
  2106. if (now === [TheTotal]) { \n\
  2107. if (this.asyncNeeded) { \n\
  2108. async.invoke(this._callFunction, this, promise); \n\
  2109. } else { \n\
  2110. this._callFunction(promise); \n\
  2111. } \n\
  2112. \n\
  2113. } \n\
  2114. }; \n\
  2115. \n\
  2116. [TheName].prototype._resultCancelled = function() { \n\
  2117. [CancellationCode] \n\
  2118. }; \n\
  2119. \n\
  2120. return [TheName]; \n\
  2121. }(tryCatch, errorObj, Promise, async); \n\
  2122. ";
  2123. code = code.replace(/\[TheName\]/g, name)
  2124. .replace(/\[TheTotal\]/g, total)
  2125. .replace(/\[ThePassedArguments\]/g, passedArguments)
  2126. .replace(/\[TheProperties\]/g, assignment)
  2127. .replace(/\[CancellationCode\]/g, cancellationCode);
  2128. return new Function("tryCatch", "errorObj", "Promise", "async", code)
  2129. (tryCatch, errorObj, Promise, async);
  2130. };
  2131. var holderClasses = [];
  2132. var thenCallbacks = [];
  2133. var promiseSetters = [];
  2134. for (var i = 0; i < 8; ++i) {
  2135. holderClasses.push(generateHolderClass(i + 1));
  2136. thenCallbacks.push(thenCallback(i + 1));
  2137. promiseSetters.push(promiseSetter(i + 1));
  2138. }
  2139. reject = function (reason) {
  2140. this._reject(reason);
  2141. };
  2142. }}
  2143. Promise.join = function () {
  2144. var last = arguments.length - 1;
  2145. var fn;
  2146. if (last > 0 && typeof arguments[last] === "function") {
  2147. fn = arguments[last];
  2148. if (!true) {
  2149. if (last <= 8 && canEvaluate) {
  2150. var ret = new Promise(INTERNAL);
  2151. ret._captureStackTrace();
  2152. var HolderClass = holderClasses[last - 1];
  2153. var holder = new HolderClass(fn);
  2154. var callbacks = thenCallbacks;
  2155. for (var i = 0; i < last; ++i) {
  2156. var maybePromise = tryConvertToPromise(arguments[i], ret);
  2157. if (maybePromise instanceof Promise) {
  2158. maybePromise = maybePromise._target();
  2159. var bitField = maybePromise._bitField;
  2160. ;
  2161. if (((bitField & 50397184) === 0)) {
  2162. maybePromise._then(callbacks[i], reject,
  2163. undefined, ret, holder);
  2164. promiseSetters[i](maybePromise, holder);
  2165. holder.asyncNeeded = false;
  2166. } else if (((bitField & 33554432) !== 0)) {
  2167. callbacks[i].call(ret,
  2168. maybePromise._value(), holder);
  2169. } else if (((bitField & 16777216) !== 0)) {
  2170. ret._reject(maybePromise._reason());
  2171. } else {
  2172. ret._cancel();
  2173. }
  2174. } else {
  2175. callbacks[i].call(ret, maybePromise, holder);
  2176. }
  2177. }
  2178. if (!ret._isFateSealed()) {
  2179. if (holder.asyncNeeded) {
  2180. var domain = getDomain();
  2181. if (domain !== null) {
  2182. holder.fn = util.domainBind(domain, holder.fn);
  2183. }
  2184. }
  2185. ret._setAsyncGuaranteed();
  2186. ret._setOnCancel(holder);
  2187. }
  2188. return ret;
  2189. }
  2190. }
  2191. }
  2192. var args = [].slice.call(arguments);;
  2193. if (fn) args.pop();
  2194. var ret = new PromiseArray(args).promise();
  2195. return fn !== undefined ? ret.spread(fn) : ret;
  2196. };
  2197. };
  2198. },{"./util":36}],18:[function(_dereq_,module,exports){
  2199. "use strict";
  2200. module.exports = function(Promise,
  2201. PromiseArray,
  2202. apiRejection,
  2203. tryConvertToPromise,
  2204. INTERNAL,
  2205. debug) {
  2206. var getDomain = Promise._getDomain;
  2207. var util = _dereq_("./util");
  2208. var tryCatch = util.tryCatch;
  2209. var errorObj = util.errorObj;
  2210. var async = Promise._async;
  2211. function MappingPromiseArray(promises, fn, limit, _filter) {
  2212. this.constructor$(promises);
  2213. this._promise._captureStackTrace();
  2214. var domain = getDomain();
  2215. this._callback = domain === null ? fn : util.domainBind(domain, fn);
  2216. this._preservedValues = _filter === INTERNAL
  2217. ? new Array(this.length())
  2218. : null;
  2219. this._limit = limit;
  2220. this._inFlight = 0;
  2221. this._queue = [];
  2222. async.invoke(this._asyncInit, this, undefined);
  2223. }
  2224. util.inherits(MappingPromiseArray, PromiseArray);
  2225. MappingPromiseArray.prototype._asyncInit = function() {
  2226. this._init$(undefined, -2);
  2227. };
  2228. MappingPromiseArray.prototype._init = function () {};
  2229. MappingPromiseArray.prototype._promiseFulfilled = function (value, index) {
  2230. var values = this._values;
  2231. var length = this.length();
  2232. var preservedValues = this._preservedValues;
  2233. var limit = this._limit;
  2234. if (index < 0) {
  2235. index = (index * -1) - 1;
  2236. values[index] = value;
  2237. if (limit >= 1) {
  2238. this._inFlight--;
  2239. this._drainQueue();
  2240. if (this._isResolved()) return true;
  2241. }
  2242. } else {
  2243. if (limit >= 1 && this._inFlight >= limit) {
  2244. values[index] = value;
  2245. this._queue.push(index);
  2246. return false;
  2247. }
  2248. if (preservedValues !== null) preservedValues[index] = value;
  2249. var promise = this._promise;
  2250. var callback = this._callback;
  2251. var receiver = promise._boundValue();
  2252. promise._pushContext();
  2253. var ret = tryCatch(callback).call(receiver, value, index, length);
  2254. var promiseCreated = promise._popContext();
  2255. debug.checkForgottenReturns(
  2256. ret,
  2257. promiseCreated,
  2258. preservedValues !== null ? "Promise.filter" : "Promise.map",
  2259. promise
  2260. );
  2261. if (ret === errorObj) {
  2262. this._reject(ret.e);
  2263. return true;
  2264. }
  2265. var maybePromise = tryConvertToPromise(ret, this._promise);
  2266. if (maybePromise instanceof Promise) {
  2267. maybePromise = maybePromise._target();
  2268. var bitField = maybePromise._bitField;
  2269. ;
  2270. if (((bitField & 50397184) === 0)) {
  2271. if (limit >= 1) this._inFlight++;
  2272. values[index] = maybePromise;
  2273. maybePromise._proxy(this, (index + 1) * -1);
  2274. return false;
  2275. } else if (((bitField & 33554432) !== 0)) {
  2276. ret = maybePromise._value();
  2277. } else if (((bitField & 16777216) !== 0)) {
  2278. this._reject(maybePromise._reason());
  2279. return true;
  2280. } else {
  2281. this._cancel();
  2282. return true;
  2283. }
  2284. }
  2285. values[index] = ret;
  2286. }
  2287. var totalResolved = ++this._totalResolved;
  2288. if (totalResolved >= length) {
  2289. if (preservedValues !== null) {
  2290. this._filter(values, preservedValues);
  2291. } else {
  2292. this._resolve(values);
  2293. }
  2294. return true;
  2295. }
  2296. return false;
  2297. };
  2298. MappingPromiseArray.prototype._drainQueue = function () {
  2299. var queue = this._queue;
  2300. var limit = this._limit;
  2301. var values = this._values;
  2302. while (queue.length > 0 && this._inFlight < limit) {
  2303. if (this._isResolved()) return;
  2304. var index = queue.pop();
  2305. this._promiseFulfilled(values[index], index);
  2306. }
  2307. };
  2308. MappingPromiseArray.prototype._filter = function (booleans, values) {
  2309. var len = values.length;
  2310. var ret = new Array(len);
  2311. var j = 0;
  2312. for (var i = 0; i < len; ++i) {
  2313. if (booleans[i]) ret[j++] = values[i];
  2314. }
  2315. ret.length = j;
  2316. this._resolve(ret);
  2317. };
  2318. MappingPromiseArray.prototype.preservedValues = function () {
  2319. return this._preservedValues;
  2320. };
  2321. function map(promises, fn, options, _filter) {
  2322. if (typeof fn !== "function") {
  2323. return apiRejection("expecting a function but got " + util.classString(fn));
  2324. }
  2325. var limit = 0;
  2326. if (options !== undefined) {
  2327. if (typeof options === "object" && options !== null) {
  2328. if (typeof options.concurrency !== "number") {
  2329. return Promise.reject(
  2330. new TypeError("'concurrency' must be a number but it is " +
  2331. util.classString(options.concurrency)));
  2332. }
  2333. limit = options.concurrency;
  2334. } else {
  2335. return Promise.reject(new TypeError(
  2336. "options argument must be an object but it is " +
  2337. util.classString(options)));
  2338. }
  2339. }
  2340. limit = typeof limit === "number" &&
  2341. isFinite(limit) && limit >= 1 ? limit : 0;
  2342. return new MappingPromiseArray(promises, fn, limit, _filter).promise();
  2343. }
  2344. Promise.prototype.map = function (fn, options) {
  2345. return map(this, fn, options, null);
  2346. };
  2347. Promise.map = function (promises, fn, options, _filter) {
  2348. return map(promises, fn, options, _filter);
  2349. };
  2350. };
  2351. },{"./util":36}],19:[function(_dereq_,module,exports){
  2352. "use strict";
  2353. module.exports =
  2354. function(Promise, INTERNAL, tryConvertToPromise, apiRejection, debug) {
  2355. var util = _dereq_("./util");
  2356. var tryCatch = util.tryCatch;
  2357. Promise.method = function (fn) {
  2358. if (typeof fn !== "function") {
  2359. throw new Promise.TypeError("expecting a function but got " + util.classString(fn));
  2360. }
  2361. return function () {
  2362. var ret = new Promise(INTERNAL);
  2363. ret._captureStackTrace();
  2364. ret._pushContext();
  2365. var value = tryCatch(fn).apply(this, arguments);
  2366. var promiseCreated = ret._popContext();
  2367. debug.checkForgottenReturns(
  2368. value, promiseCreated, "Promise.method", ret);
  2369. ret._resolveFromSyncValue(value);
  2370. return ret;
  2371. };
  2372. };
  2373. Promise.attempt = Promise["try"] = function (fn) {
  2374. if (typeof fn !== "function") {
  2375. return apiRejection("expecting a function but got " + util.classString(fn));
  2376. }
  2377. var ret = new Promise(INTERNAL);
  2378. ret._captureStackTrace();
  2379. ret._pushContext();
  2380. var value;
  2381. if (arguments.length > 1) {
  2382. debug.deprecated("calling Promise.try with more than 1 argument");
  2383. var arg = arguments[1];
  2384. var ctx = arguments[2];
  2385. value = util.isArray(arg) ? tryCatch(fn).apply(ctx, arg)
  2386. : tryCatch(fn).call(ctx, arg);
  2387. } else {
  2388. value = tryCatch(fn)();
  2389. }
  2390. var promiseCreated = ret._popContext();
  2391. debug.checkForgottenReturns(
  2392. value, promiseCreated, "Promise.try", ret);
  2393. ret._resolveFromSyncValue(value);
  2394. return ret;
  2395. };
  2396. Promise.prototype._resolveFromSyncValue = function (value) {
  2397. if (value === util.errorObj) {
  2398. this._rejectCallback(value.e, false);
  2399. } else {
  2400. this._resolveCallback(value, true);
  2401. }
  2402. };
  2403. };
  2404. },{"./util":36}],20:[function(_dereq_,module,exports){
  2405. "use strict";
  2406. var util = _dereq_("./util");
  2407. var maybeWrapAsError = util.maybeWrapAsError;
  2408. var errors = _dereq_("./errors");
  2409. var OperationalError = errors.OperationalError;
  2410. var es5 = _dereq_("./es5");
  2411. function isUntypedError(obj) {
  2412. return obj instanceof Error &&
  2413. es5.getPrototypeOf(obj) === Error.prototype;
  2414. }
  2415. var rErrorKey = /^(?:name|message|stack|cause)$/;
  2416. function wrapAsOperationalError(obj) {
  2417. var ret;
  2418. if (isUntypedError(obj)) {
  2419. ret = new OperationalError(obj);
  2420. ret.name = obj.name;
  2421. ret.message = obj.message;
  2422. ret.stack = obj.stack;
  2423. var keys = es5.keys(obj);
  2424. for (var i = 0; i < keys.length; ++i) {
  2425. var key = keys[i];
  2426. if (!rErrorKey.test(key)) {
  2427. ret[key] = obj[key];
  2428. }
  2429. }
  2430. return ret;
  2431. }
  2432. util.markAsOriginatingFromRejection(obj);
  2433. return obj;
  2434. }
  2435. function nodebackForPromise(promise, multiArgs) {
  2436. return function(err, value) {
  2437. if (promise === null) return;
  2438. if (err) {
  2439. var wrapped = wrapAsOperationalError(maybeWrapAsError(err));
  2440. promise._attachExtraTrace(wrapped);
  2441. promise._reject(wrapped);
  2442. } else if (!multiArgs) {
  2443. promise._fulfill(value);
  2444. } else {
  2445. var args = [].slice.call(arguments, 1);;
  2446. promise._fulfill(args);
  2447. }
  2448. promise = null;
  2449. };
  2450. }
  2451. module.exports = nodebackForPromise;
  2452. },{"./errors":12,"./es5":13,"./util":36}],21:[function(_dereq_,module,exports){
  2453. "use strict";
  2454. module.exports = function(Promise) {
  2455. var util = _dereq_("./util");
  2456. var async = Promise._async;
  2457. var tryCatch = util.tryCatch;
  2458. var errorObj = util.errorObj;
  2459. function spreadAdapter(val, nodeback) {
  2460. var promise = this;
  2461. if (!util.isArray(val)) return successAdapter.call(promise, val, nodeback);
  2462. var ret =
  2463. tryCatch(nodeback).apply(promise._boundValue(), [null].concat(val));
  2464. if (ret === errorObj) {
  2465. async.throwLater(ret.e);
  2466. }
  2467. }
  2468. function successAdapter(val, nodeback) {
  2469. var promise = this;
  2470. var receiver = promise._boundValue();
  2471. var ret = val === undefined
  2472. ? tryCatch(nodeback).call(receiver, null)
  2473. : tryCatch(nodeback).call(receiver, null, val);
  2474. if (ret === errorObj) {
  2475. async.throwLater(ret.e);
  2476. }
  2477. }
  2478. function errorAdapter(reason, nodeback) {
  2479. var promise = this;
  2480. if (!reason) {
  2481. var newReason = new Error(reason + "");
  2482. newReason.cause = reason;
  2483. reason = newReason;
  2484. }
  2485. var ret = tryCatch(nodeback).call(promise._boundValue(), reason);
  2486. if (ret === errorObj) {
  2487. async.throwLater(ret.e);
  2488. }
  2489. }
  2490. Promise.prototype.asCallback = Promise.prototype.nodeify = function (nodeback,
  2491. options) {
  2492. if (typeof nodeback == "function") {
  2493. var adapter = successAdapter;
  2494. if (options !== undefined && Object(options).spread) {
  2495. adapter = spreadAdapter;
  2496. }
  2497. this._then(
  2498. adapter,
  2499. errorAdapter,
  2500. undefined,
  2501. this,
  2502. nodeback
  2503. );
  2504. }
  2505. return this;
  2506. };
  2507. };
  2508. },{"./util":36}],22:[function(_dereq_,module,exports){
  2509. "use strict";
  2510. module.exports = function() {
  2511. var makeSelfResolutionError = function () {
  2512. return new TypeError("circular promise resolution chain\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  2513. };
  2514. var reflectHandler = function() {
  2515. return new Promise.PromiseInspection(this._target());
  2516. };
  2517. var apiRejection = function(msg) {
  2518. return Promise.reject(new TypeError(msg));
  2519. };
  2520. function Proxyable() {}
  2521. var UNDEFINED_BINDING = {};
  2522. var util = _dereq_("./util");
  2523. var getDomain;
  2524. if (util.isNode) {
  2525. getDomain = function() {
  2526. var ret = process.domain;
  2527. if (ret === undefined) ret = null;
  2528. return ret;
  2529. };
  2530. } else {
  2531. getDomain = function() {
  2532. return null;
  2533. };
  2534. }
  2535. util.notEnumerableProp(Promise, "_getDomain", getDomain);
  2536. var es5 = _dereq_("./es5");
  2537. var Async = _dereq_("./async");
  2538. var async = new Async();
  2539. es5.defineProperty(Promise, "_async", {value: async});
  2540. var errors = _dereq_("./errors");
  2541. var TypeError = Promise.TypeError = errors.TypeError;
  2542. Promise.RangeError = errors.RangeError;
  2543. var CancellationError = Promise.CancellationError = errors.CancellationError;
  2544. Promise.TimeoutError = errors.TimeoutError;
  2545. Promise.OperationalError = errors.OperationalError;
  2546. Promise.RejectionError = errors.OperationalError;
  2547. Promise.AggregateError = errors.AggregateError;
  2548. var INTERNAL = function(){};
  2549. var APPLY = {};
  2550. var NEXT_FILTER = {};
  2551. var tryConvertToPromise = _dereq_("./thenables")(Promise, INTERNAL);
  2552. var PromiseArray =
  2553. _dereq_("./promise_array")(Promise, INTERNAL,
  2554. tryConvertToPromise, apiRejection, Proxyable);
  2555. var Context = _dereq_("./context")(Promise);
  2556. /*jshint unused:false*/
  2557. var createContext = Context.create;
  2558. var debug = _dereq_("./debuggability")(Promise, Context);
  2559. var CapturedTrace = debug.CapturedTrace;
  2560. var PassThroughHandlerContext =
  2561. _dereq_("./finally")(Promise, tryConvertToPromise, NEXT_FILTER);
  2562. var catchFilter = _dereq_("./catch_filter")(NEXT_FILTER);
  2563. var nodebackForPromise = _dereq_("./nodeback");
  2564. var errorObj = util.errorObj;
  2565. var tryCatch = util.tryCatch;
  2566. function check(self, executor) {
  2567. if (self == null || self.constructor !== Promise) {
  2568. throw new TypeError("the promise constructor cannot be invoked directly\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  2569. }
  2570. if (typeof executor !== "function") {
  2571. throw new TypeError("expecting a function but got " + util.classString(executor));
  2572. }
  2573. }
  2574. function Promise(executor) {
  2575. if (executor !== INTERNAL) {
  2576. check(this, executor);
  2577. }
  2578. this._bitField = 0;
  2579. this._fulfillmentHandler0 = undefined;
  2580. this._rejectionHandler0 = undefined;
  2581. this._promise0 = undefined;
  2582. this._receiver0 = undefined;
  2583. this._resolveFromExecutor(executor);
  2584. this._promiseCreated();
  2585. this._fireEvent("promiseCreated", this);
  2586. }
  2587. Promise.prototype.toString = function () {
  2588. return "[object Promise]";
  2589. };
  2590. Promise.prototype.caught = Promise.prototype["catch"] = function (fn) {
  2591. var len = arguments.length;
  2592. if (len > 1) {
  2593. var catchInstances = new Array(len - 1),
  2594. j = 0, i;
  2595. for (i = 0; i < len - 1; ++i) {
  2596. var item = arguments[i];
  2597. if (util.isObject(item)) {
  2598. catchInstances[j++] = item;
  2599. } else {
  2600. return apiRejection("Catch statement predicate: " +
  2601. "expecting an object but got " + util.classString(item));
  2602. }
  2603. }
  2604. catchInstances.length = j;
  2605. fn = arguments[i];
  2606. if (typeof fn !== "function") {
  2607. throw new TypeError("The last argument to .catch() " +
  2608. "must be a function, got " + util.toString(fn));
  2609. }
  2610. return this.then(undefined, catchFilter(catchInstances, fn, this));
  2611. }
  2612. return this.then(undefined, fn);
  2613. };
  2614. Promise.prototype.reflect = function () {
  2615. return this._then(reflectHandler,
  2616. reflectHandler, undefined, this, undefined);
  2617. };
  2618. Promise.prototype.then = function (didFulfill, didReject) {
  2619. if (debug.warnings() && arguments.length > 0 &&
  2620. typeof didFulfill !== "function" &&
  2621. typeof didReject !== "function") {
  2622. var msg = ".then() only accepts functions but was passed: " +
  2623. util.classString(didFulfill);
  2624. if (arguments.length > 1) {
  2625. msg += ", " + util.classString(didReject);
  2626. }
  2627. this._warn(msg);
  2628. }
  2629. return this._then(didFulfill, didReject, undefined, undefined, undefined);
  2630. };
  2631. Promise.prototype.done = function (didFulfill, didReject) {
  2632. var promise =
  2633. this._then(didFulfill, didReject, undefined, undefined, undefined);
  2634. promise._setIsFinal();
  2635. };
  2636. Promise.prototype.spread = function (fn) {
  2637. if (typeof fn !== "function") {
  2638. return apiRejection("expecting a function but got " + util.classString(fn));
  2639. }
  2640. return this.all()._then(fn, undefined, undefined, APPLY, undefined);
  2641. };
  2642. Promise.prototype.toJSON = function () {
  2643. var ret = {
  2644. isFulfilled: false,
  2645. isRejected: false,
  2646. fulfillmentValue: undefined,
  2647. rejectionReason: undefined
  2648. };
  2649. if (this.isFulfilled()) {
  2650. ret.fulfillmentValue = this.value();
  2651. ret.isFulfilled = true;
  2652. } else if (this.isRejected()) {
  2653. ret.rejectionReason = this.reason();
  2654. ret.isRejected = true;
  2655. }
  2656. return ret;
  2657. };
  2658. Promise.prototype.all = function () {
  2659. if (arguments.length > 0) {
  2660. this._warn(".all() was passed arguments but it does not take any");
  2661. }
  2662. return new PromiseArray(this).promise();
  2663. };
  2664. Promise.prototype.error = function (fn) {
  2665. return this.caught(util.originatesFromRejection, fn);
  2666. };
  2667. Promise.getNewLibraryCopy = module.exports;
  2668. Promise.is = function (val) {
  2669. return val instanceof Promise;
  2670. };
  2671. Promise.fromNode = Promise.fromCallback = function(fn) {
  2672. var ret = new Promise(INTERNAL);
  2673. ret._captureStackTrace();
  2674. var multiArgs = arguments.length > 1 ? !!Object(arguments[1]).multiArgs
  2675. : false;
  2676. var result = tryCatch(fn)(nodebackForPromise(ret, multiArgs));
  2677. if (result === errorObj) {
  2678. ret._rejectCallback(result.e, true);
  2679. }
  2680. if (!ret._isFateSealed()) ret._setAsyncGuaranteed();
  2681. return ret;
  2682. };
  2683. Promise.all = function (promises) {
  2684. return new PromiseArray(promises).promise();
  2685. };
  2686. Promise.cast = function (obj) {
  2687. var ret = tryConvertToPromise(obj);
  2688. if (!(ret instanceof Promise)) {
  2689. ret = new Promise(INTERNAL);
  2690. ret._captureStackTrace();
  2691. ret._setFulfilled();
  2692. ret._rejectionHandler0 = obj;
  2693. }
  2694. return ret;
  2695. };
  2696. Promise.resolve = Promise.fulfilled = Promise.cast;
  2697. Promise.reject = Promise.rejected = function (reason) {
  2698. var ret = new Promise(INTERNAL);
  2699. ret._captureStackTrace();
  2700. ret._rejectCallback(reason, true);
  2701. return ret;
  2702. };
  2703. Promise.setScheduler = function(fn) {
  2704. if (typeof fn !== "function") {
  2705. throw new TypeError("expecting a function but got " + util.classString(fn));
  2706. }
  2707. return async.setScheduler(fn);
  2708. };
  2709. Promise.prototype._then = function (
  2710. didFulfill,
  2711. didReject,
  2712. _, receiver,
  2713. internalData
  2714. ) {
  2715. var haveInternalData = internalData !== undefined;
  2716. var promise = haveInternalData ? internalData : new Promise(INTERNAL);
  2717. var target = this._target();
  2718. var bitField = target._bitField;
  2719. if (!haveInternalData) {
  2720. promise._propagateFrom(this, 3);
  2721. promise._captureStackTrace();
  2722. if (receiver === undefined &&
  2723. ((this._bitField & 2097152) !== 0)) {
  2724. if (!((bitField & 50397184) === 0)) {
  2725. receiver = this._boundValue();
  2726. } else {
  2727. receiver = target === this ? undefined : this._boundTo;
  2728. }
  2729. }
  2730. this._fireEvent("promiseChained", this, promise);
  2731. }
  2732. var domain = getDomain();
  2733. if (!((bitField & 50397184) === 0)) {
  2734. var handler, value, settler = target._settlePromiseCtx;
  2735. if (((bitField & 33554432) !== 0)) {
  2736. value = target._rejectionHandler0;
  2737. handler = didFulfill;
  2738. } else if (((bitField & 16777216) !== 0)) {
  2739. value = target._fulfillmentHandler0;
  2740. handler = didReject;
  2741. target._unsetRejectionIsUnhandled();
  2742. } else {
  2743. settler = target._settlePromiseLateCancellationObserver;
  2744. value = new CancellationError("late cancellation observer");
  2745. target._attachExtraTrace(value);
  2746. handler = didReject;
  2747. }
  2748. async.invoke(settler, target, {
  2749. handler: domain === null ? handler
  2750. : (typeof handler === "function" &&
  2751. util.domainBind(domain, handler)),
  2752. promise: promise,
  2753. receiver: receiver,
  2754. value: value
  2755. });
  2756. } else {
  2757. target._addCallbacks(didFulfill, didReject, promise, receiver, domain);
  2758. }
  2759. return promise;
  2760. };
  2761. Promise.prototype._length = function () {
  2762. return this._bitField & 65535;
  2763. };
  2764. Promise.prototype._isFateSealed = function () {
  2765. return (this._bitField & 117506048) !== 0;
  2766. };
  2767. Promise.prototype._isFollowing = function () {
  2768. return (this._bitField & 67108864) === 67108864;
  2769. };
  2770. Promise.prototype._setLength = function (len) {
  2771. this._bitField = (this._bitField & -65536) |
  2772. (len & 65535);
  2773. };
  2774. Promise.prototype._setFulfilled = function () {
  2775. this._bitField = this._bitField | 33554432;
  2776. this._fireEvent("promiseFulfilled", this);
  2777. };
  2778. Promise.prototype._setRejected = function () {
  2779. this._bitField = this._bitField | 16777216;
  2780. this._fireEvent("promiseRejected", this);
  2781. };
  2782. Promise.prototype._setFollowing = function () {
  2783. this._bitField = this._bitField | 67108864;
  2784. this._fireEvent("promiseResolved", this);
  2785. };
  2786. Promise.prototype._setIsFinal = function () {
  2787. this._bitField = this._bitField | 4194304;
  2788. };
  2789. Promise.prototype._isFinal = function () {
  2790. return (this._bitField & 4194304) > 0;
  2791. };
  2792. Promise.prototype._unsetCancelled = function() {
  2793. this._bitField = this._bitField & (~65536);
  2794. };
  2795. Promise.prototype._setCancelled = function() {
  2796. this._bitField = this._bitField | 65536;
  2797. this._fireEvent("promiseCancelled", this);
  2798. };
  2799. Promise.prototype._setWillBeCancelled = function() {
  2800. this._bitField = this._bitField | 8388608;
  2801. };
  2802. Promise.prototype._setAsyncGuaranteed = function() {
  2803. if (async.hasCustomScheduler()) return;
  2804. this._bitField = this._bitField | 134217728;
  2805. };
  2806. Promise.prototype._receiverAt = function (index) {
  2807. var ret = index === 0 ? this._receiver0 : this[
  2808. index * 4 - 4 + 3];
  2809. if (ret === UNDEFINED_BINDING) {
  2810. return undefined;
  2811. } else if (ret === undefined && this._isBound()) {
  2812. return this._boundValue();
  2813. }
  2814. return ret;
  2815. };
  2816. Promise.prototype._promiseAt = function (index) {
  2817. return this[
  2818. index * 4 - 4 + 2];
  2819. };
  2820. Promise.prototype._fulfillmentHandlerAt = function (index) {
  2821. return this[
  2822. index * 4 - 4 + 0];
  2823. };
  2824. Promise.prototype._rejectionHandlerAt = function (index) {
  2825. return this[
  2826. index * 4 - 4 + 1];
  2827. };
  2828. Promise.prototype._boundValue = function() {};
  2829. Promise.prototype._migrateCallback0 = function (follower) {
  2830. var bitField = follower._bitField;
  2831. var fulfill = follower._fulfillmentHandler0;
  2832. var reject = follower._rejectionHandler0;
  2833. var promise = follower._promise0;
  2834. var receiver = follower._receiverAt(0);
  2835. if (receiver === undefined) receiver = UNDEFINED_BINDING;
  2836. this._addCallbacks(fulfill, reject, promise, receiver, null);
  2837. };
  2838. Promise.prototype._migrateCallbackAt = function (follower, index) {
  2839. var fulfill = follower._fulfillmentHandlerAt(index);
  2840. var reject = follower._rejectionHandlerAt(index);
  2841. var promise = follower._promiseAt(index);
  2842. var receiver = follower._receiverAt(index);
  2843. if (receiver === undefined) receiver = UNDEFINED_BINDING;
  2844. this._addCallbacks(fulfill, reject, promise, receiver, null);
  2845. };
  2846. Promise.prototype._addCallbacks = function (
  2847. fulfill,
  2848. reject,
  2849. promise,
  2850. receiver,
  2851. domain
  2852. ) {
  2853. var index = this._length();
  2854. if (index >= 65535 - 4) {
  2855. index = 0;
  2856. this._setLength(0);
  2857. }
  2858. if (index === 0) {
  2859. this._promise0 = promise;
  2860. this._receiver0 = receiver;
  2861. if (typeof fulfill === "function") {
  2862. this._fulfillmentHandler0 =
  2863. domain === null ? fulfill : util.domainBind(domain, fulfill);
  2864. }
  2865. if (typeof reject === "function") {
  2866. this._rejectionHandler0 =
  2867. domain === null ? reject : util.domainBind(domain, reject);
  2868. }
  2869. } else {
  2870. var base = index * 4 - 4;
  2871. this[base + 2] = promise;
  2872. this[base + 3] = receiver;
  2873. if (typeof fulfill === "function") {
  2874. this[base + 0] =
  2875. domain === null ? fulfill : util.domainBind(domain, fulfill);
  2876. }
  2877. if (typeof reject === "function") {
  2878. this[base + 1] =
  2879. domain === null ? reject : util.domainBind(domain, reject);
  2880. }
  2881. }
  2882. this._setLength(index + 1);
  2883. return index;
  2884. };
  2885. Promise.prototype._proxy = function (proxyable, arg) {
  2886. this._addCallbacks(undefined, undefined, arg, proxyable, null);
  2887. };
  2888. Promise.prototype._resolveCallback = function(value, shouldBind) {
  2889. if (((this._bitField & 117506048) !== 0)) return;
  2890. if (value === this)
  2891. return this._rejectCallback(makeSelfResolutionError(), false);
  2892. var maybePromise = tryConvertToPromise(value, this);
  2893. if (!(maybePromise instanceof Promise)) return this._fulfill(value);
  2894. if (shouldBind) this._propagateFrom(maybePromise, 2);
  2895. var promise = maybePromise._target();
  2896. if (promise === this) {
  2897. this._reject(makeSelfResolutionError());
  2898. return;
  2899. }
  2900. var bitField = promise._bitField;
  2901. if (((bitField & 50397184) === 0)) {
  2902. var len = this._length();
  2903. if (len > 0) promise._migrateCallback0(this);
  2904. for (var i = 1; i < len; ++i) {
  2905. promise._migrateCallbackAt(this, i);
  2906. }
  2907. this._setFollowing();
  2908. this._setLength(0);
  2909. this._setFollowee(promise);
  2910. } else if (((bitField & 33554432) !== 0)) {
  2911. this._fulfill(promise._value());
  2912. } else if (((bitField & 16777216) !== 0)) {
  2913. this._reject(promise._reason());
  2914. } else {
  2915. var reason = new CancellationError("late cancellation observer");
  2916. promise._attachExtraTrace(reason);
  2917. this._reject(reason);
  2918. }
  2919. };
  2920. Promise.prototype._rejectCallback =
  2921. function(reason, synchronous, ignoreNonErrorWarnings) {
  2922. var trace = util.ensureErrorObject(reason);
  2923. var hasStack = trace === reason;
  2924. if (!hasStack && !ignoreNonErrorWarnings && debug.warnings()) {
  2925. var message = "a promise was rejected with a non-error: " +
  2926. util.classString(reason);
  2927. this._warn(message, true);
  2928. }
  2929. this._attachExtraTrace(trace, synchronous ? hasStack : false);
  2930. this._reject(reason);
  2931. };
  2932. Promise.prototype._resolveFromExecutor = function (executor) {
  2933. if (executor === INTERNAL) return;
  2934. var promise = this;
  2935. this._captureStackTrace();
  2936. this._pushContext();
  2937. var synchronous = true;
  2938. var r = this._execute(executor, function(value) {
  2939. promise._resolveCallback(value);
  2940. }, function (reason) {
  2941. promise._rejectCallback(reason, synchronous);
  2942. });
  2943. synchronous = false;
  2944. this._popContext();
  2945. if (r !== undefined) {
  2946. promise._rejectCallback(r, true);
  2947. }
  2948. };
  2949. Promise.prototype._settlePromiseFromHandler = function (
  2950. handler, receiver, value, promise
  2951. ) {
  2952. var bitField = promise._bitField;
  2953. if (((bitField & 65536) !== 0)) return;
  2954. promise._pushContext();
  2955. var x;
  2956. if (receiver === APPLY) {
  2957. if (!value || typeof value.length !== "number") {
  2958. x = errorObj;
  2959. x.e = new TypeError("cannot .spread() a non-array: " +
  2960. util.classString(value));
  2961. } else {
  2962. x = tryCatch(handler).apply(this._boundValue(), value);
  2963. }
  2964. } else {
  2965. x = tryCatch(handler).call(receiver, value);
  2966. }
  2967. var promiseCreated = promise._popContext();
  2968. bitField = promise._bitField;
  2969. if (((bitField & 65536) !== 0)) return;
  2970. if (x === NEXT_FILTER) {
  2971. promise._reject(value);
  2972. } else if (x === errorObj) {
  2973. promise._rejectCallback(x.e, false);
  2974. } else {
  2975. debug.checkForgottenReturns(x, promiseCreated, "", promise, this);
  2976. promise._resolveCallback(x);
  2977. }
  2978. };
  2979. Promise.prototype._target = function() {
  2980. var ret = this;
  2981. while (ret._isFollowing()) ret = ret._followee();
  2982. return ret;
  2983. };
  2984. Promise.prototype._followee = function() {
  2985. return this._rejectionHandler0;
  2986. };
  2987. Promise.prototype._setFollowee = function(promise) {
  2988. this._rejectionHandler0 = promise;
  2989. };
  2990. Promise.prototype._settlePromise = function(promise, handler, receiver, value) {
  2991. var isPromise = promise instanceof Promise;
  2992. var bitField = this._bitField;
  2993. var asyncGuaranteed = ((bitField & 134217728) !== 0);
  2994. if (((bitField & 65536) !== 0)) {
  2995. if (isPromise) promise._invokeInternalOnCancel();
  2996. if (receiver instanceof PassThroughHandlerContext &&
  2997. receiver.isFinallyHandler()) {
  2998. receiver.cancelPromise = promise;
  2999. if (tryCatch(handler).call(receiver, value) === errorObj) {
  3000. promise._reject(errorObj.e);
  3001. }
  3002. } else if (handler === reflectHandler) {
  3003. promise._fulfill(reflectHandler.call(receiver));
  3004. } else if (receiver instanceof Proxyable) {
  3005. receiver._promiseCancelled(promise);
  3006. } else if (isPromise || promise instanceof PromiseArray) {
  3007. promise._cancel();
  3008. } else {
  3009. receiver.cancel();
  3010. }
  3011. } else if (typeof handler === "function") {
  3012. if (!isPromise) {
  3013. handler.call(receiver, value, promise);
  3014. } else {
  3015. if (asyncGuaranteed) promise._setAsyncGuaranteed();
  3016. this._settlePromiseFromHandler(handler, receiver, value, promise);
  3017. }
  3018. } else if (receiver instanceof Proxyable) {
  3019. if (!receiver._isResolved()) {
  3020. if (((bitField & 33554432) !== 0)) {
  3021. receiver._promiseFulfilled(value, promise);
  3022. } else {
  3023. receiver._promiseRejected(value, promise);
  3024. }
  3025. }
  3026. } else if (isPromise) {
  3027. if (asyncGuaranteed) promise._setAsyncGuaranteed();
  3028. if (((bitField & 33554432) !== 0)) {
  3029. promise._fulfill(value);
  3030. } else {
  3031. promise._reject(value);
  3032. }
  3033. }
  3034. };
  3035. Promise.prototype._settlePromiseLateCancellationObserver = function(ctx) {
  3036. var handler = ctx.handler;
  3037. var promise = ctx.promise;
  3038. var receiver = ctx.receiver;
  3039. var value = ctx.value;
  3040. if (typeof handler === "function") {
  3041. if (!(promise instanceof Promise)) {
  3042. handler.call(receiver, value, promise);
  3043. } else {
  3044. this._settlePromiseFromHandler(handler, receiver, value, promise);
  3045. }
  3046. } else if (promise instanceof Promise) {
  3047. promise._reject(value);
  3048. }
  3049. };
  3050. Promise.prototype._settlePromiseCtx = function(ctx) {
  3051. this._settlePromise(ctx.promise, ctx.handler, ctx.receiver, ctx.value);
  3052. };
  3053. Promise.prototype._settlePromise0 = function(handler, value, bitField) {
  3054. var promise = this._promise0;
  3055. var receiver = this._receiverAt(0);
  3056. this._promise0 = undefined;
  3057. this._receiver0 = undefined;
  3058. this._settlePromise(promise, handler, receiver, value);
  3059. };
  3060. Promise.prototype._clearCallbackDataAtIndex = function(index) {
  3061. var base = index * 4 - 4;
  3062. this[base + 2] =
  3063. this[base + 3] =
  3064. this[base + 0] =
  3065. this[base + 1] = undefined;
  3066. };
  3067. Promise.prototype._fulfill = function (value) {
  3068. var bitField = this._bitField;
  3069. if (((bitField & 117506048) >>> 16)) return;
  3070. if (value === this) {
  3071. var err = makeSelfResolutionError();
  3072. this._attachExtraTrace(err);
  3073. return this._reject(err);
  3074. }
  3075. this._setFulfilled();
  3076. this._rejectionHandler0 = value;
  3077. if ((bitField & 65535) > 0) {
  3078. if (((bitField & 134217728) !== 0)) {
  3079. this._settlePromises();
  3080. } else {
  3081. async.settlePromises(this);
  3082. }
  3083. this._dereferenceTrace();
  3084. }
  3085. };
  3086. Promise.prototype._reject = function (reason) {
  3087. var bitField = this._bitField;
  3088. if (((bitField & 117506048) >>> 16)) return;
  3089. this._setRejected();
  3090. this._fulfillmentHandler0 = reason;
  3091. if (this._isFinal()) {
  3092. return async.fatalError(reason, util.isNode);
  3093. }
  3094. if ((bitField & 65535) > 0) {
  3095. async.settlePromises(this);
  3096. } else {
  3097. this._ensurePossibleRejectionHandled();
  3098. }
  3099. };
  3100. Promise.prototype._fulfillPromises = function (len, value) {
  3101. for (var i = 1; i < len; i++) {
  3102. var handler = this._fulfillmentHandlerAt(i);
  3103. var promise = this._promiseAt(i);
  3104. var receiver = this._receiverAt(i);
  3105. this._clearCallbackDataAtIndex(i);
  3106. this._settlePromise(promise, handler, receiver, value);
  3107. }
  3108. };
  3109. Promise.prototype._rejectPromises = function (len, reason) {
  3110. for (var i = 1; i < len; i++) {
  3111. var handler = this._rejectionHandlerAt(i);
  3112. var promise = this._promiseAt(i);
  3113. var receiver = this._receiverAt(i);
  3114. this._clearCallbackDataAtIndex(i);
  3115. this._settlePromise(promise, handler, receiver, reason);
  3116. }
  3117. };
  3118. Promise.prototype._settlePromises = function () {
  3119. var bitField = this._bitField;
  3120. var len = (bitField & 65535);
  3121. if (len > 0) {
  3122. if (((bitField & 16842752) !== 0)) {
  3123. var reason = this._fulfillmentHandler0;
  3124. this._settlePromise0(this._rejectionHandler0, reason, bitField);
  3125. this._rejectPromises(len, reason);
  3126. } else {
  3127. var value = this._rejectionHandler0;
  3128. this._settlePromise0(this._fulfillmentHandler0, value, bitField);
  3129. this._fulfillPromises(len, value);
  3130. }
  3131. this._setLength(0);
  3132. }
  3133. this._clearCancellationData();
  3134. };
  3135. Promise.prototype._settledValue = function() {
  3136. var bitField = this._bitField;
  3137. if (((bitField & 33554432) !== 0)) {
  3138. return this._rejectionHandler0;
  3139. } else if (((bitField & 16777216) !== 0)) {
  3140. return this._fulfillmentHandler0;
  3141. }
  3142. };
  3143. if (typeof Symbol !== "undefined" && Symbol.toStringTag) {
  3144. es5.defineProperty(Promise.prototype, Symbol.toStringTag, {
  3145. get: function () {
  3146. return "Object";
  3147. }
  3148. });
  3149. }
  3150. function deferResolve(v) {this.promise._resolveCallback(v);}
  3151. function deferReject(v) {this.promise._rejectCallback(v, false);}
  3152. Promise.defer = Promise.pending = function() {
  3153. debug.deprecated("Promise.defer", "new Promise");
  3154. var promise = new Promise(INTERNAL);
  3155. return {
  3156. promise: promise,
  3157. resolve: deferResolve,
  3158. reject: deferReject
  3159. };
  3160. };
  3161. util.notEnumerableProp(Promise,
  3162. "_makeSelfResolutionError",
  3163. makeSelfResolutionError);
  3164. _dereq_("./method")(Promise, INTERNAL, tryConvertToPromise, apiRejection,
  3165. debug);
  3166. _dereq_("./bind")(Promise, INTERNAL, tryConvertToPromise, debug);
  3167. _dereq_("./cancel")(Promise, PromiseArray, apiRejection, debug);
  3168. _dereq_("./direct_resolve")(Promise);
  3169. _dereq_("./synchronous_inspection")(Promise);
  3170. _dereq_("./join")(
  3171. Promise, PromiseArray, tryConvertToPromise, INTERNAL, async, getDomain);
  3172. Promise.Promise = Promise;
  3173. Promise.version = "3.5.5";
  3174. _dereq_('./call_get.js')(Promise);
  3175. _dereq_('./generators.js')(Promise, apiRejection, INTERNAL, tryConvertToPromise, Proxyable, debug);
  3176. _dereq_('./map.js')(Promise, PromiseArray, apiRejection, tryConvertToPromise, INTERNAL, debug);
  3177. _dereq_('./nodeify.js')(Promise);
  3178. _dereq_('./promisify.js')(Promise, INTERNAL);
  3179. _dereq_('./props.js')(Promise, PromiseArray, tryConvertToPromise, apiRejection);
  3180. _dereq_('./race.js')(Promise, INTERNAL, tryConvertToPromise, apiRejection);
  3181. _dereq_('./reduce.js')(Promise, PromiseArray, apiRejection, tryConvertToPromise, INTERNAL, debug);
  3182. _dereq_('./settle.js')(Promise, PromiseArray, debug);
  3183. _dereq_('./some.js')(Promise, PromiseArray, apiRejection);
  3184. _dereq_('./timers.js')(Promise, INTERNAL, debug);
  3185. _dereq_('./using.js')(Promise, apiRejection, tryConvertToPromise, createContext, INTERNAL, debug);
  3186. _dereq_('./any.js')(Promise);
  3187. _dereq_('./each.js')(Promise, INTERNAL);
  3188. _dereq_('./filter.js')(Promise, INTERNAL);
  3189. util.toFastProperties(Promise);
  3190. util.toFastProperties(Promise.prototype);
  3191. function fillTypes(value) {
  3192. var p = new Promise(INTERNAL);
  3193. p._fulfillmentHandler0 = value;
  3194. p._rejectionHandler0 = value;
  3195. p._promise0 = value;
  3196. p._receiver0 = value;
  3197. }
  3198. // Complete slack tracking, opt out of field-type tracking and
  3199. // stabilize map
  3200. fillTypes({a: 1});
  3201. fillTypes({b: 2});
  3202. fillTypes({c: 3});
  3203. fillTypes(1);
  3204. fillTypes(function(){});
  3205. fillTypes(undefined);
  3206. fillTypes(false);
  3207. fillTypes(new Promise(INTERNAL));
  3208. debug.setBounds(Async.firstLineError, util.lastLineError);
  3209. return Promise;
  3210. };
  3211. },{"./any.js":1,"./async":2,"./bind":3,"./call_get.js":5,"./cancel":6,"./catch_filter":7,"./context":8,"./debuggability":9,"./direct_resolve":10,"./each.js":11,"./errors":12,"./es5":13,"./filter.js":14,"./finally":15,"./generators.js":16,"./join":17,"./map.js":18,"./method":19,"./nodeback":20,"./nodeify.js":21,"./promise_array":23,"./promisify.js":24,"./props.js":25,"./race.js":27,"./reduce.js":28,"./settle.js":30,"./some.js":31,"./synchronous_inspection":32,"./thenables":33,"./timers.js":34,"./using.js":35,"./util":36}],23:[function(_dereq_,module,exports){
  3212. "use strict";
  3213. module.exports = function(Promise, INTERNAL, tryConvertToPromise,
  3214. apiRejection, Proxyable) {
  3215. var util = _dereq_("./util");
  3216. var isArray = util.isArray;
  3217. function toResolutionValue(val) {
  3218. switch(val) {
  3219. case -2: return [];
  3220. case -3: return {};
  3221. case -6: return new Map();
  3222. }
  3223. }
  3224. function PromiseArray(values) {
  3225. var promise = this._promise = new Promise(INTERNAL);
  3226. if (values instanceof Promise) {
  3227. promise._propagateFrom(values, 3);
  3228. }
  3229. promise._setOnCancel(this);
  3230. this._values = values;
  3231. this._length = 0;
  3232. this._totalResolved = 0;
  3233. this._init(undefined, -2);
  3234. }
  3235. util.inherits(PromiseArray, Proxyable);
  3236. PromiseArray.prototype.length = function () {
  3237. return this._length;
  3238. };
  3239. PromiseArray.prototype.promise = function () {
  3240. return this._promise;
  3241. };
  3242. PromiseArray.prototype._init = function init(_, resolveValueIfEmpty) {
  3243. var values = tryConvertToPromise(this._values, this._promise);
  3244. if (values instanceof Promise) {
  3245. values = values._target();
  3246. var bitField = values._bitField;
  3247. ;
  3248. this._values = values;
  3249. if (((bitField & 50397184) === 0)) {
  3250. this._promise._setAsyncGuaranteed();
  3251. return values._then(
  3252. init,
  3253. this._reject,
  3254. undefined,
  3255. this,
  3256. resolveValueIfEmpty
  3257. );
  3258. } else if (((bitField & 33554432) !== 0)) {
  3259. values = values._value();
  3260. } else if (((bitField & 16777216) !== 0)) {
  3261. return this._reject(values._reason());
  3262. } else {
  3263. return this._cancel();
  3264. }
  3265. }
  3266. values = util.asArray(values);
  3267. if (values === null) {
  3268. var err = apiRejection(
  3269. "expecting an array or an iterable object but got " + util.classString(values)).reason();
  3270. this._promise._rejectCallback(err, false);
  3271. return;
  3272. }
  3273. if (values.length === 0) {
  3274. if (resolveValueIfEmpty === -5) {
  3275. this._resolveEmptyArray();
  3276. }
  3277. else {
  3278. this._resolve(toResolutionValue(resolveValueIfEmpty));
  3279. }
  3280. return;
  3281. }
  3282. this._iterate(values);
  3283. };
  3284. PromiseArray.prototype._iterate = function(values) {
  3285. var len = this.getActualLength(values.length);
  3286. this._length = len;
  3287. this._values = this.shouldCopyValues() ? new Array(len) : this._values;
  3288. var result = this._promise;
  3289. var isResolved = false;
  3290. var bitField = null;
  3291. for (var i = 0; i < len; ++i) {
  3292. var maybePromise = tryConvertToPromise(values[i], result);
  3293. if (maybePromise instanceof Promise) {
  3294. maybePromise = maybePromise._target();
  3295. bitField = maybePromise._bitField;
  3296. } else {
  3297. bitField = null;
  3298. }
  3299. if (isResolved) {
  3300. if (bitField !== null) {
  3301. maybePromise.suppressUnhandledRejections();
  3302. }
  3303. } else if (bitField !== null) {
  3304. if (((bitField & 50397184) === 0)) {
  3305. maybePromise._proxy(this, i);
  3306. this._values[i] = maybePromise;
  3307. } else if (((bitField & 33554432) !== 0)) {
  3308. isResolved = this._promiseFulfilled(maybePromise._value(), i);
  3309. } else if (((bitField & 16777216) !== 0)) {
  3310. isResolved = this._promiseRejected(maybePromise._reason(), i);
  3311. } else {
  3312. isResolved = this._promiseCancelled(i);
  3313. }
  3314. } else {
  3315. isResolved = this._promiseFulfilled(maybePromise, i);
  3316. }
  3317. }
  3318. if (!isResolved) result._setAsyncGuaranteed();
  3319. };
  3320. PromiseArray.prototype._isResolved = function () {
  3321. return this._values === null;
  3322. };
  3323. PromiseArray.prototype._resolve = function (value) {
  3324. this._values = null;
  3325. this._promise._fulfill(value);
  3326. };
  3327. PromiseArray.prototype._cancel = function() {
  3328. if (this._isResolved() || !this._promise._isCancellable()) return;
  3329. this._values = null;
  3330. this._promise._cancel();
  3331. };
  3332. PromiseArray.prototype._reject = function (reason) {
  3333. this._values = null;
  3334. this._promise._rejectCallback(reason, false);
  3335. };
  3336. PromiseArray.prototype._promiseFulfilled = function (value, index) {
  3337. this._values[index] = value;
  3338. var totalResolved = ++this._totalResolved;
  3339. if (totalResolved >= this._length) {
  3340. this._resolve(this._values);
  3341. return true;
  3342. }
  3343. return false;
  3344. };
  3345. PromiseArray.prototype._promiseCancelled = function() {
  3346. this._cancel();
  3347. return true;
  3348. };
  3349. PromiseArray.prototype._promiseRejected = function (reason) {
  3350. this._totalResolved++;
  3351. this._reject(reason);
  3352. return true;
  3353. };
  3354. PromiseArray.prototype._resultCancelled = function() {
  3355. if (this._isResolved()) return;
  3356. var values = this._values;
  3357. this._cancel();
  3358. if (values instanceof Promise) {
  3359. values.cancel();
  3360. } else {
  3361. for (var i = 0; i < values.length; ++i) {
  3362. if (values[i] instanceof Promise) {
  3363. values[i].cancel();
  3364. }
  3365. }
  3366. }
  3367. };
  3368. PromiseArray.prototype.shouldCopyValues = function () {
  3369. return true;
  3370. };
  3371. PromiseArray.prototype.getActualLength = function (len) {
  3372. return len;
  3373. };
  3374. return PromiseArray;
  3375. };
  3376. },{"./util":36}],24:[function(_dereq_,module,exports){
  3377. "use strict";
  3378. module.exports = function(Promise, INTERNAL) {
  3379. var THIS = {};
  3380. var util = _dereq_("./util");
  3381. var nodebackForPromise = _dereq_("./nodeback");
  3382. var withAppended = util.withAppended;
  3383. var maybeWrapAsError = util.maybeWrapAsError;
  3384. var canEvaluate = util.canEvaluate;
  3385. var TypeError = _dereq_("./errors").TypeError;
  3386. var defaultSuffix = "Async";
  3387. var defaultPromisified = {__isPromisified__: true};
  3388. var noCopyProps = [
  3389. "arity", "length",
  3390. "name",
  3391. "arguments",
  3392. "caller",
  3393. "callee",
  3394. "prototype",
  3395. "__isPromisified__"
  3396. ];
  3397. var noCopyPropsPattern = new RegExp("^(?:" + noCopyProps.join("|") + ")$");
  3398. var defaultFilter = function(name) {
  3399. return util.isIdentifier(name) &&
  3400. name.charAt(0) !== "_" &&
  3401. name !== "constructor";
  3402. };
  3403. function propsFilter(key) {
  3404. return !noCopyPropsPattern.test(key);
  3405. }
  3406. function isPromisified(fn) {
  3407. try {
  3408. return fn.__isPromisified__ === true;
  3409. }
  3410. catch (e) {
  3411. return false;
  3412. }
  3413. }
  3414. function hasPromisified(obj, key, suffix) {
  3415. var val = util.getDataPropertyOrDefault(obj, key + suffix,
  3416. defaultPromisified);
  3417. return val ? isPromisified(val) : false;
  3418. }
  3419. function checkValid(ret, suffix, suffixRegexp) {
  3420. for (var i = 0; i < ret.length; i += 2) {
  3421. var key = ret[i];
  3422. if (suffixRegexp.test(key)) {
  3423. var keyWithoutAsyncSuffix = key.replace(suffixRegexp, "");
  3424. for (var j = 0; j < ret.length; j += 2) {
  3425. if (ret[j] === keyWithoutAsyncSuffix) {
  3426. throw new TypeError("Cannot promisify an API that has normal methods with '%s'-suffix\u000a\u000a See http://goo.gl/MqrFmX\u000a"
  3427. .replace("%s", suffix));
  3428. }
  3429. }
  3430. }
  3431. }
  3432. }
  3433. function promisifiableMethods(obj, suffix, suffixRegexp, filter) {
  3434. var keys = util.inheritedDataKeys(obj);
  3435. var ret = [];
  3436. for (var i = 0; i < keys.length; ++i) {
  3437. var key = keys[i];
  3438. var value = obj[key];
  3439. var passesDefaultFilter = filter === defaultFilter
  3440. ? true : defaultFilter(key, value, obj);
  3441. if (typeof value === "function" &&
  3442. !isPromisified(value) &&
  3443. !hasPromisified(obj, key, suffix) &&
  3444. filter(key, value, obj, passesDefaultFilter)) {
  3445. ret.push(key, value);
  3446. }
  3447. }
  3448. checkValid(ret, suffix, suffixRegexp);
  3449. return ret;
  3450. }
  3451. var escapeIdentRegex = function(str) {
  3452. return str.replace(/([$])/, "\\$");
  3453. };
  3454. var makeNodePromisifiedEval;
  3455. if (!true) {
  3456. var switchCaseArgumentOrder = function(likelyArgumentCount) {
  3457. var ret = [likelyArgumentCount];
  3458. var min = Math.max(0, likelyArgumentCount - 1 - 3);
  3459. for(var i = likelyArgumentCount - 1; i >= min; --i) {
  3460. ret.push(i);
  3461. }
  3462. for(var i = likelyArgumentCount + 1; i <= 3; ++i) {
  3463. ret.push(i);
  3464. }
  3465. return ret;
  3466. };
  3467. var argumentSequence = function(argumentCount) {
  3468. return util.filledRange(argumentCount, "_arg", "");
  3469. };
  3470. var parameterDeclaration = function(parameterCount) {
  3471. return util.filledRange(
  3472. Math.max(parameterCount, 3), "_arg", "");
  3473. };
  3474. var parameterCount = function(fn) {
  3475. if (typeof fn.length === "number") {
  3476. return Math.max(Math.min(fn.length, 1023 + 1), 0);
  3477. }
  3478. return 0;
  3479. };
  3480. makeNodePromisifiedEval =
  3481. function(callback, receiver, originalName, fn, _, multiArgs) {
  3482. var newParameterCount = Math.max(0, parameterCount(fn) - 1);
  3483. var argumentOrder = switchCaseArgumentOrder(newParameterCount);
  3484. var shouldProxyThis = typeof callback === "string" || receiver === THIS;
  3485. function generateCallForArgumentCount(count) {
  3486. var args = argumentSequence(count).join(", ");
  3487. var comma = count > 0 ? ", " : "";
  3488. var ret;
  3489. if (shouldProxyThis) {
  3490. ret = "ret = callback.call(this, {{args}}, nodeback); break;\n";
  3491. } else {
  3492. ret = receiver === undefined
  3493. ? "ret = callback({{args}}, nodeback); break;\n"
  3494. : "ret = callback.call(receiver, {{args}}, nodeback); break;\n";
  3495. }
  3496. return ret.replace("{{args}}", args).replace(", ", comma);
  3497. }
  3498. function generateArgumentSwitchCase() {
  3499. var ret = "";
  3500. for (var i = 0; i < argumentOrder.length; ++i) {
  3501. ret += "case " + argumentOrder[i] +":" +
  3502. generateCallForArgumentCount(argumentOrder[i]);
  3503. }
  3504. ret += " \n\
  3505. default: \n\
  3506. var args = new Array(len + 1); \n\
  3507. var i = 0; \n\
  3508. for (var i = 0; i < len; ++i) { \n\
  3509. args[i] = arguments[i]; \n\
  3510. } \n\
  3511. args[i] = nodeback; \n\
  3512. [CodeForCall] \n\
  3513. break; \n\
  3514. ".replace("[CodeForCall]", (shouldProxyThis
  3515. ? "ret = callback.apply(this, args);\n"
  3516. : "ret = callback.apply(receiver, args);\n"));
  3517. return ret;
  3518. }
  3519. var getFunctionCode = typeof callback === "string"
  3520. ? ("this != null ? this['"+callback+"'] : fn")
  3521. : "fn";
  3522. var body = "'use strict'; \n\
  3523. var ret = function (Parameters) { \n\
  3524. 'use strict'; \n\
  3525. var len = arguments.length; \n\
  3526. var promise = new Promise(INTERNAL); \n\
  3527. promise._captureStackTrace(); \n\
  3528. var nodeback = nodebackForPromise(promise, " + multiArgs + "); \n\
  3529. var ret; \n\
  3530. var callback = tryCatch([GetFunctionCode]); \n\
  3531. switch(len) { \n\
  3532. [CodeForSwitchCase] \n\
  3533. } \n\
  3534. if (ret === errorObj) { \n\
  3535. promise._rejectCallback(maybeWrapAsError(ret.e), true, true);\n\
  3536. } \n\
  3537. if (!promise._isFateSealed()) promise._setAsyncGuaranteed(); \n\
  3538. return promise; \n\
  3539. }; \n\
  3540. notEnumerableProp(ret, '__isPromisified__', true); \n\
  3541. return ret; \n\
  3542. ".replace("[CodeForSwitchCase]", generateArgumentSwitchCase())
  3543. .replace("[GetFunctionCode]", getFunctionCode);
  3544. body = body.replace("Parameters", parameterDeclaration(newParameterCount));
  3545. return new Function("Promise",
  3546. "fn",
  3547. "receiver",
  3548. "withAppended",
  3549. "maybeWrapAsError",
  3550. "nodebackForPromise",
  3551. "tryCatch",
  3552. "errorObj",
  3553. "notEnumerableProp",
  3554. "INTERNAL",
  3555. body)(
  3556. Promise,
  3557. fn,
  3558. receiver,
  3559. withAppended,
  3560. maybeWrapAsError,
  3561. nodebackForPromise,
  3562. util.tryCatch,
  3563. util.errorObj,
  3564. util.notEnumerableProp,
  3565. INTERNAL);
  3566. };
  3567. }
  3568. function makeNodePromisifiedClosure(callback, receiver, _, fn, __, multiArgs) {
  3569. var defaultThis = (function() {return this;})();
  3570. var method = callback;
  3571. if (typeof method === "string") {
  3572. callback = fn;
  3573. }
  3574. function promisified() {
  3575. var _receiver = receiver;
  3576. if (receiver === THIS) _receiver = this;
  3577. var promise = new Promise(INTERNAL);
  3578. promise._captureStackTrace();
  3579. var cb = typeof method === "string" && this !== defaultThis
  3580. ? this[method] : callback;
  3581. var fn = nodebackForPromise(promise, multiArgs);
  3582. try {
  3583. cb.apply(_receiver, withAppended(arguments, fn));
  3584. } catch(e) {
  3585. promise._rejectCallback(maybeWrapAsError(e), true, true);
  3586. }
  3587. if (!promise._isFateSealed()) promise._setAsyncGuaranteed();
  3588. return promise;
  3589. }
  3590. util.notEnumerableProp(promisified, "__isPromisified__", true);
  3591. return promisified;
  3592. }
  3593. var makeNodePromisified = canEvaluate
  3594. ? makeNodePromisifiedEval
  3595. : makeNodePromisifiedClosure;
  3596. function promisifyAll(obj, suffix, filter, promisifier, multiArgs) {
  3597. var suffixRegexp = new RegExp(escapeIdentRegex(suffix) + "$");
  3598. var methods =
  3599. promisifiableMethods(obj, suffix, suffixRegexp, filter);
  3600. for (var i = 0, len = methods.length; i < len; i+= 2) {
  3601. var key = methods[i];
  3602. var fn = methods[i+1];
  3603. var promisifiedKey = key + suffix;
  3604. if (promisifier === makeNodePromisified) {
  3605. obj[promisifiedKey] =
  3606. makeNodePromisified(key, THIS, key, fn, suffix, multiArgs);
  3607. } else {
  3608. var promisified = promisifier(fn, function() {
  3609. return makeNodePromisified(key, THIS, key,
  3610. fn, suffix, multiArgs);
  3611. });
  3612. util.notEnumerableProp(promisified, "__isPromisified__", true);
  3613. obj[promisifiedKey] = promisified;
  3614. }
  3615. }
  3616. util.toFastProperties(obj);
  3617. return obj;
  3618. }
  3619. function promisify(callback, receiver, multiArgs) {
  3620. return makeNodePromisified(callback, receiver, undefined,
  3621. callback, null, multiArgs);
  3622. }
  3623. Promise.promisify = function (fn, options) {
  3624. if (typeof fn !== "function") {
  3625. throw new TypeError("expecting a function but got " + util.classString(fn));
  3626. }
  3627. if (isPromisified(fn)) {
  3628. return fn;
  3629. }
  3630. options = Object(options);
  3631. var receiver = options.context === undefined ? THIS : options.context;
  3632. var multiArgs = !!options.multiArgs;
  3633. var ret = promisify(fn, receiver, multiArgs);
  3634. util.copyDescriptors(fn, ret, propsFilter);
  3635. return ret;
  3636. };
  3637. Promise.promisifyAll = function (target, options) {
  3638. if (typeof target !== "function" && typeof target !== "object") {
  3639. throw new TypeError("the target of promisifyAll must be an object or a function\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  3640. }
  3641. options = Object(options);
  3642. var multiArgs = !!options.multiArgs;
  3643. var suffix = options.suffix;
  3644. if (typeof suffix !== "string") suffix = defaultSuffix;
  3645. var filter = options.filter;
  3646. if (typeof filter !== "function") filter = defaultFilter;
  3647. var promisifier = options.promisifier;
  3648. if (typeof promisifier !== "function") promisifier = makeNodePromisified;
  3649. if (!util.isIdentifier(suffix)) {
  3650. throw new RangeError("suffix must be a valid identifier\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  3651. }
  3652. var keys = util.inheritedDataKeys(target);
  3653. for (var i = 0; i < keys.length; ++i) {
  3654. var value = target[keys[i]];
  3655. if (keys[i] !== "constructor" &&
  3656. util.isClass(value)) {
  3657. promisifyAll(value.prototype, suffix, filter, promisifier,
  3658. multiArgs);
  3659. promisifyAll(value, suffix, filter, promisifier, multiArgs);
  3660. }
  3661. }
  3662. return promisifyAll(target, suffix, filter, promisifier, multiArgs);
  3663. };
  3664. };
  3665. },{"./errors":12,"./nodeback":20,"./util":36}],25:[function(_dereq_,module,exports){
  3666. "use strict";
  3667. module.exports = function(
  3668. Promise, PromiseArray, tryConvertToPromise, apiRejection) {
  3669. var util = _dereq_("./util");
  3670. var isObject = util.isObject;
  3671. var es5 = _dereq_("./es5");
  3672. var Es6Map;
  3673. if (typeof Map === "function") Es6Map = Map;
  3674. var mapToEntries = (function() {
  3675. var index = 0;
  3676. var size = 0;
  3677. function extractEntry(value, key) {
  3678. this[index] = value;
  3679. this[index + size] = key;
  3680. index++;
  3681. }
  3682. return function mapToEntries(map) {
  3683. size = map.size;
  3684. index = 0;
  3685. var ret = new Array(map.size * 2);
  3686. map.forEach(extractEntry, ret);
  3687. return ret;
  3688. };
  3689. })();
  3690. var entriesToMap = function(entries) {
  3691. var ret = new Es6Map();
  3692. var length = entries.length / 2 | 0;
  3693. for (var i = 0; i < length; ++i) {
  3694. var key = entries[length + i];
  3695. var value = entries[i];
  3696. ret.set(key, value);
  3697. }
  3698. return ret;
  3699. };
  3700. function PropertiesPromiseArray(obj) {
  3701. var isMap = false;
  3702. var entries;
  3703. if (Es6Map !== undefined && obj instanceof Es6Map) {
  3704. entries = mapToEntries(obj);
  3705. isMap = true;
  3706. } else {
  3707. var keys = es5.keys(obj);
  3708. var len = keys.length;
  3709. entries = new Array(len * 2);
  3710. for (var i = 0; i < len; ++i) {
  3711. var key = keys[i];
  3712. entries[i] = obj[key];
  3713. entries[i + len] = key;
  3714. }
  3715. }
  3716. this.constructor$(entries);
  3717. this._isMap = isMap;
  3718. this._init$(undefined, isMap ? -6 : -3);
  3719. }
  3720. util.inherits(PropertiesPromiseArray, PromiseArray);
  3721. PropertiesPromiseArray.prototype._init = function () {};
  3722. PropertiesPromiseArray.prototype._promiseFulfilled = function (value, index) {
  3723. this._values[index] = value;
  3724. var totalResolved = ++this._totalResolved;
  3725. if (totalResolved >= this._length) {
  3726. var val;
  3727. if (this._isMap) {
  3728. val = entriesToMap(this._values);
  3729. } else {
  3730. val = {};
  3731. var keyOffset = this.length();
  3732. for (var i = 0, len = this.length(); i < len; ++i) {
  3733. val[this._values[i + keyOffset]] = this._values[i];
  3734. }
  3735. }
  3736. this._resolve(val);
  3737. return true;
  3738. }
  3739. return false;
  3740. };
  3741. PropertiesPromiseArray.prototype.shouldCopyValues = function () {
  3742. return false;
  3743. };
  3744. PropertiesPromiseArray.prototype.getActualLength = function (len) {
  3745. return len >> 1;
  3746. };
  3747. function props(promises) {
  3748. var ret;
  3749. var castValue = tryConvertToPromise(promises);
  3750. if (!isObject(castValue)) {
  3751. return apiRejection("cannot await properties of a non-object\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  3752. } else if (castValue instanceof Promise) {
  3753. ret = castValue._then(
  3754. Promise.props, undefined, undefined, undefined, undefined);
  3755. } else {
  3756. ret = new PropertiesPromiseArray(castValue).promise();
  3757. }
  3758. if (castValue instanceof Promise) {
  3759. ret._propagateFrom(castValue, 2);
  3760. }
  3761. return ret;
  3762. }
  3763. Promise.prototype.props = function () {
  3764. return props(this);
  3765. };
  3766. Promise.props = function (promises) {
  3767. return props(promises);
  3768. };
  3769. };
  3770. },{"./es5":13,"./util":36}],26:[function(_dereq_,module,exports){
  3771. "use strict";
  3772. function arrayMove(src, srcIndex, dst, dstIndex, len) {
  3773. for (var j = 0; j < len; ++j) {
  3774. dst[j + dstIndex] = src[j + srcIndex];
  3775. src[j + srcIndex] = void 0;
  3776. }
  3777. }
  3778. function Queue(capacity) {
  3779. this._capacity = capacity;
  3780. this._length = 0;
  3781. this._front = 0;
  3782. }
  3783. Queue.prototype._willBeOverCapacity = function (size) {
  3784. return this._capacity < size;
  3785. };
  3786. Queue.prototype._pushOne = function (arg) {
  3787. var length = this.length();
  3788. this._checkCapacity(length + 1);
  3789. var i = (this._front + length) & (this._capacity - 1);
  3790. this[i] = arg;
  3791. this._length = length + 1;
  3792. };
  3793. Queue.prototype.push = function (fn, receiver, arg) {
  3794. var length = this.length() + 3;
  3795. if (this._willBeOverCapacity(length)) {
  3796. this._pushOne(fn);
  3797. this._pushOne(receiver);
  3798. this._pushOne(arg);
  3799. return;
  3800. }
  3801. var j = this._front + length - 3;
  3802. this._checkCapacity(length);
  3803. var wrapMask = this._capacity - 1;
  3804. this[(j + 0) & wrapMask] = fn;
  3805. this[(j + 1) & wrapMask] = receiver;
  3806. this[(j + 2) & wrapMask] = arg;
  3807. this._length = length;
  3808. };
  3809. Queue.prototype.shift = function () {
  3810. var front = this._front,
  3811. ret = this[front];
  3812. this[front] = undefined;
  3813. this._front = (front + 1) & (this._capacity - 1);
  3814. this._length--;
  3815. return ret;
  3816. };
  3817. Queue.prototype.length = function () {
  3818. return this._length;
  3819. };
  3820. Queue.prototype._checkCapacity = function (size) {
  3821. if (this._capacity < size) {
  3822. this._resizeTo(this._capacity << 1);
  3823. }
  3824. };
  3825. Queue.prototype._resizeTo = function (capacity) {
  3826. var oldCapacity = this._capacity;
  3827. this._capacity = capacity;
  3828. var front = this._front;
  3829. var length = this._length;
  3830. var moveItemsCount = (front + length) & (oldCapacity - 1);
  3831. arrayMove(this, 0, this, oldCapacity, moveItemsCount);
  3832. };
  3833. module.exports = Queue;
  3834. },{}],27:[function(_dereq_,module,exports){
  3835. "use strict";
  3836. module.exports = function(
  3837. Promise, INTERNAL, tryConvertToPromise, apiRejection) {
  3838. var util = _dereq_("./util");
  3839. var raceLater = function (promise) {
  3840. return promise.then(function(array) {
  3841. return race(array, promise);
  3842. });
  3843. };
  3844. function race(promises, parent) {
  3845. var maybePromise = tryConvertToPromise(promises);
  3846. if (maybePromise instanceof Promise) {
  3847. return raceLater(maybePromise);
  3848. } else {
  3849. promises = util.asArray(promises);
  3850. if (promises === null)
  3851. return apiRejection("expecting an array or an iterable object but got " + util.classString(promises));
  3852. }
  3853. var ret = new Promise(INTERNAL);
  3854. if (parent !== undefined) {
  3855. ret._propagateFrom(parent, 3);
  3856. }
  3857. var fulfill = ret._fulfill;
  3858. var reject = ret._reject;
  3859. for (var i = 0, len = promises.length; i < len; ++i) {
  3860. var val = promises[i];
  3861. if (val === undefined && !(i in promises)) {
  3862. continue;
  3863. }
  3864. Promise.cast(val)._then(fulfill, reject, undefined, ret, null);
  3865. }
  3866. return ret;
  3867. }
  3868. Promise.race = function (promises) {
  3869. return race(promises, undefined);
  3870. };
  3871. Promise.prototype.race = function () {
  3872. return race(this, undefined);
  3873. };
  3874. };
  3875. },{"./util":36}],28:[function(_dereq_,module,exports){
  3876. "use strict";
  3877. module.exports = function(Promise,
  3878. PromiseArray,
  3879. apiRejection,
  3880. tryConvertToPromise,
  3881. INTERNAL,
  3882. debug) {
  3883. var getDomain = Promise._getDomain;
  3884. var util = _dereq_("./util");
  3885. var tryCatch = util.tryCatch;
  3886. function ReductionPromiseArray(promises, fn, initialValue, _each) {
  3887. this.constructor$(promises);
  3888. var domain = getDomain();
  3889. this._fn = domain === null ? fn : util.domainBind(domain, fn);
  3890. if (initialValue !== undefined) {
  3891. initialValue = Promise.resolve(initialValue);
  3892. initialValue._attachCancellationCallback(this);
  3893. }
  3894. this._initialValue = initialValue;
  3895. this._currentCancellable = null;
  3896. if(_each === INTERNAL) {
  3897. this._eachValues = Array(this._length);
  3898. } else if (_each === 0) {
  3899. this._eachValues = null;
  3900. } else {
  3901. this._eachValues = undefined;
  3902. }
  3903. this._promise._captureStackTrace();
  3904. this._init$(undefined, -5);
  3905. }
  3906. util.inherits(ReductionPromiseArray, PromiseArray);
  3907. ReductionPromiseArray.prototype._gotAccum = function(accum) {
  3908. if (this._eachValues !== undefined &&
  3909. this._eachValues !== null &&
  3910. accum !== INTERNAL) {
  3911. this._eachValues.push(accum);
  3912. }
  3913. };
  3914. ReductionPromiseArray.prototype._eachComplete = function(value) {
  3915. if (this._eachValues !== null) {
  3916. this._eachValues.push(value);
  3917. }
  3918. return this._eachValues;
  3919. };
  3920. ReductionPromiseArray.prototype._init = function() {};
  3921. ReductionPromiseArray.prototype._resolveEmptyArray = function() {
  3922. this._resolve(this._eachValues !== undefined ? this._eachValues
  3923. : this._initialValue);
  3924. };
  3925. ReductionPromiseArray.prototype.shouldCopyValues = function () {
  3926. return false;
  3927. };
  3928. ReductionPromiseArray.prototype._resolve = function(value) {
  3929. this._promise._resolveCallback(value);
  3930. this._values = null;
  3931. };
  3932. ReductionPromiseArray.prototype._resultCancelled = function(sender) {
  3933. if (sender === this._initialValue) return this._cancel();
  3934. if (this._isResolved()) return;
  3935. this._resultCancelled$();
  3936. if (this._currentCancellable instanceof Promise) {
  3937. this._currentCancellable.cancel();
  3938. }
  3939. if (this._initialValue instanceof Promise) {
  3940. this._initialValue.cancel();
  3941. }
  3942. };
  3943. ReductionPromiseArray.prototype._iterate = function (values) {
  3944. this._values = values;
  3945. var value;
  3946. var i;
  3947. var length = values.length;
  3948. if (this._initialValue !== undefined) {
  3949. value = this._initialValue;
  3950. i = 0;
  3951. } else {
  3952. value = Promise.resolve(values[0]);
  3953. i = 1;
  3954. }
  3955. this._currentCancellable = value;
  3956. if (!value.isRejected()) {
  3957. for (; i < length; ++i) {
  3958. var ctx = {
  3959. accum: null,
  3960. value: values[i],
  3961. index: i,
  3962. length: length,
  3963. array: this
  3964. };
  3965. value = value._then(gotAccum, undefined, undefined, ctx, undefined);
  3966. }
  3967. }
  3968. if (this._eachValues !== undefined) {
  3969. value = value
  3970. ._then(this._eachComplete, undefined, undefined, this, undefined);
  3971. }
  3972. value._then(completed, completed, undefined, value, this);
  3973. };
  3974. Promise.prototype.reduce = function (fn, initialValue) {
  3975. return reduce(this, fn, initialValue, null);
  3976. };
  3977. Promise.reduce = function (promises, fn, initialValue, _each) {
  3978. return reduce(promises, fn, initialValue, _each);
  3979. };
  3980. function completed(valueOrReason, array) {
  3981. if (this.isFulfilled()) {
  3982. array._resolve(valueOrReason);
  3983. } else {
  3984. array._reject(valueOrReason);
  3985. }
  3986. }
  3987. function reduce(promises, fn, initialValue, _each) {
  3988. if (typeof fn !== "function") {
  3989. return apiRejection("expecting a function but got " + util.classString(fn));
  3990. }
  3991. var array = new ReductionPromiseArray(promises, fn, initialValue, _each);
  3992. return array.promise();
  3993. }
  3994. function gotAccum(accum) {
  3995. this.accum = accum;
  3996. this.array._gotAccum(accum);
  3997. var value = tryConvertToPromise(this.value, this.array._promise);
  3998. if (value instanceof Promise) {
  3999. this.array._currentCancellable = value;
  4000. return value._then(gotValue, undefined, undefined, this, undefined);
  4001. } else {
  4002. return gotValue.call(this, value);
  4003. }
  4004. }
  4005. function gotValue(value) {
  4006. var array = this.array;
  4007. var promise = array._promise;
  4008. var fn = tryCatch(array._fn);
  4009. promise._pushContext();
  4010. var ret;
  4011. if (array._eachValues !== undefined) {
  4012. ret = fn.call(promise._boundValue(), value, this.index, this.length);
  4013. } else {
  4014. ret = fn.call(promise._boundValue(),
  4015. this.accum, value, this.index, this.length);
  4016. }
  4017. if (ret instanceof Promise) {
  4018. array._currentCancellable = ret;
  4019. }
  4020. var promiseCreated = promise._popContext();
  4021. debug.checkForgottenReturns(
  4022. ret,
  4023. promiseCreated,
  4024. array._eachValues !== undefined ? "Promise.each" : "Promise.reduce",
  4025. promise
  4026. );
  4027. return ret;
  4028. }
  4029. };
  4030. },{"./util":36}],29:[function(_dereq_,module,exports){
  4031. "use strict";
  4032. var util = _dereq_("./util");
  4033. var schedule;
  4034. var noAsyncScheduler = function() {
  4035. throw new Error("No async scheduler available\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  4036. };
  4037. var NativePromise = util.getNativePromise();
  4038. if (util.isNode && typeof MutationObserver === "undefined") {
  4039. var GlobalSetImmediate = global.setImmediate;
  4040. var ProcessNextTick = process.nextTick;
  4041. schedule = util.isRecentNode
  4042. ? function(fn) { GlobalSetImmediate.call(global, fn); }
  4043. : function(fn) { ProcessNextTick.call(process, fn); };
  4044. } else if (typeof NativePromise === "function" &&
  4045. typeof NativePromise.resolve === "function") {
  4046. var nativePromise = NativePromise.resolve();
  4047. schedule = function(fn) {
  4048. nativePromise.then(fn);
  4049. };
  4050. } else if ((typeof MutationObserver !== "undefined") &&
  4051. !(typeof window !== "undefined" &&
  4052. window.navigator &&
  4053. (window.navigator.standalone || window.cordova)) &&
  4054. ("classList" in document.documentElement)) {
  4055. schedule = (function() {
  4056. var div = document.createElement("div");
  4057. var opts = {attributes: true};
  4058. var toggleScheduled = false;
  4059. var div2 = document.createElement("div");
  4060. var o2 = new MutationObserver(function() {
  4061. div.classList.toggle("foo");
  4062. toggleScheduled = false;
  4063. });
  4064. o2.observe(div2, opts);
  4065. var scheduleToggle = function() {
  4066. if (toggleScheduled) return;
  4067. toggleScheduled = true;
  4068. div2.classList.toggle("foo");
  4069. };
  4070. return function schedule(fn) {
  4071. var o = new MutationObserver(function() {
  4072. o.disconnect();
  4073. fn();
  4074. });
  4075. o.observe(div, opts);
  4076. scheduleToggle();
  4077. };
  4078. })();
  4079. } else if (typeof setImmediate !== "undefined") {
  4080. schedule = function (fn) {
  4081. setImmediate(fn);
  4082. };
  4083. } else if (typeof setTimeout !== "undefined") {
  4084. schedule = function (fn) {
  4085. setTimeout(fn, 0);
  4086. };
  4087. } else {
  4088. schedule = noAsyncScheduler;
  4089. }
  4090. module.exports = schedule;
  4091. },{"./util":36}],30:[function(_dereq_,module,exports){
  4092. "use strict";
  4093. module.exports =
  4094. function(Promise, PromiseArray, debug) {
  4095. var PromiseInspection = Promise.PromiseInspection;
  4096. var util = _dereq_("./util");
  4097. function SettledPromiseArray(values) {
  4098. this.constructor$(values);
  4099. }
  4100. util.inherits(SettledPromiseArray, PromiseArray);
  4101. SettledPromiseArray.prototype._promiseResolved = function (index, inspection) {
  4102. this._values[index] = inspection;
  4103. var totalResolved = ++this._totalResolved;
  4104. if (totalResolved >= this._length) {
  4105. this._resolve(this._values);
  4106. return true;
  4107. }
  4108. return false;
  4109. };
  4110. SettledPromiseArray.prototype._promiseFulfilled = function (value, index) {
  4111. var ret = new PromiseInspection();
  4112. ret._bitField = 33554432;
  4113. ret._settledValueField = value;
  4114. return this._promiseResolved(index, ret);
  4115. };
  4116. SettledPromiseArray.prototype._promiseRejected = function (reason, index) {
  4117. var ret = new PromiseInspection();
  4118. ret._bitField = 16777216;
  4119. ret._settledValueField = reason;
  4120. return this._promiseResolved(index, ret);
  4121. };
  4122. Promise.settle = function (promises) {
  4123. debug.deprecated(".settle()", ".reflect()");
  4124. return new SettledPromiseArray(promises).promise();
  4125. };
  4126. Promise.prototype.settle = function () {
  4127. return Promise.settle(this);
  4128. };
  4129. };
  4130. },{"./util":36}],31:[function(_dereq_,module,exports){
  4131. "use strict";
  4132. module.exports =
  4133. function(Promise, PromiseArray, apiRejection) {
  4134. var util = _dereq_("./util");
  4135. var RangeError = _dereq_("./errors").RangeError;
  4136. var AggregateError = _dereq_("./errors").AggregateError;
  4137. var isArray = util.isArray;
  4138. var CANCELLATION = {};
  4139. function SomePromiseArray(values) {
  4140. this.constructor$(values);
  4141. this._howMany = 0;
  4142. this._unwrap = false;
  4143. this._initialized = false;
  4144. }
  4145. util.inherits(SomePromiseArray, PromiseArray);
  4146. SomePromiseArray.prototype._init = function () {
  4147. if (!this._initialized) {
  4148. return;
  4149. }
  4150. if (this._howMany === 0) {
  4151. this._resolve([]);
  4152. return;
  4153. }
  4154. this._init$(undefined, -5);
  4155. var isArrayResolved = isArray(this._values);
  4156. if (!this._isResolved() &&
  4157. isArrayResolved &&
  4158. this._howMany > this._canPossiblyFulfill()) {
  4159. this._reject(this._getRangeError(this.length()));
  4160. }
  4161. };
  4162. SomePromiseArray.prototype.init = function () {
  4163. this._initialized = true;
  4164. this._init();
  4165. };
  4166. SomePromiseArray.prototype.setUnwrap = function () {
  4167. this._unwrap = true;
  4168. };
  4169. SomePromiseArray.prototype.howMany = function () {
  4170. return this._howMany;
  4171. };
  4172. SomePromiseArray.prototype.setHowMany = function (count) {
  4173. this._howMany = count;
  4174. };
  4175. SomePromiseArray.prototype._promiseFulfilled = function (value) {
  4176. this._addFulfilled(value);
  4177. if (this._fulfilled() === this.howMany()) {
  4178. this._values.length = this.howMany();
  4179. if (this.howMany() === 1 && this._unwrap) {
  4180. this._resolve(this._values[0]);
  4181. } else {
  4182. this._resolve(this._values);
  4183. }
  4184. return true;
  4185. }
  4186. return false;
  4187. };
  4188. SomePromiseArray.prototype._promiseRejected = function (reason) {
  4189. this._addRejected(reason);
  4190. return this._checkOutcome();
  4191. };
  4192. SomePromiseArray.prototype._promiseCancelled = function () {
  4193. if (this._values instanceof Promise || this._values == null) {
  4194. return this._cancel();
  4195. }
  4196. this._addRejected(CANCELLATION);
  4197. return this._checkOutcome();
  4198. };
  4199. SomePromiseArray.prototype._checkOutcome = function() {
  4200. if (this.howMany() > this._canPossiblyFulfill()) {
  4201. var e = new AggregateError();
  4202. for (var i = this.length(); i < this._values.length; ++i) {
  4203. if (this._values[i] !== CANCELLATION) {
  4204. e.push(this._values[i]);
  4205. }
  4206. }
  4207. if (e.length > 0) {
  4208. this._reject(e);
  4209. } else {
  4210. this._cancel();
  4211. }
  4212. return true;
  4213. }
  4214. return false;
  4215. };
  4216. SomePromiseArray.prototype._fulfilled = function () {
  4217. return this._totalResolved;
  4218. };
  4219. SomePromiseArray.prototype._rejected = function () {
  4220. return this._values.length - this.length();
  4221. };
  4222. SomePromiseArray.prototype._addRejected = function (reason) {
  4223. this._values.push(reason);
  4224. };
  4225. SomePromiseArray.prototype._addFulfilled = function (value) {
  4226. this._values[this._totalResolved++] = value;
  4227. };
  4228. SomePromiseArray.prototype._canPossiblyFulfill = function () {
  4229. return this.length() - this._rejected();
  4230. };
  4231. SomePromiseArray.prototype._getRangeError = function (count) {
  4232. var message = "Input array must contain at least " +
  4233. this._howMany + " items but contains only " + count + " items";
  4234. return new RangeError(message);
  4235. };
  4236. SomePromiseArray.prototype._resolveEmptyArray = function () {
  4237. this._reject(this._getRangeError(0));
  4238. };
  4239. function some(promises, howMany) {
  4240. if ((howMany | 0) !== howMany || howMany < 0) {
  4241. return apiRejection("expecting a positive integer\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  4242. }
  4243. var ret = new SomePromiseArray(promises);
  4244. var promise = ret.promise();
  4245. ret.setHowMany(howMany);
  4246. ret.init();
  4247. return promise;
  4248. }
  4249. Promise.some = function (promises, howMany) {
  4250. return some(promises, howMany);
  4251. };
  4252. Promise.prototype.some = function (howMany) {
  4253. return some(this, howMany);
  4254. };
  4255. Promise._SomePromiseArray = SomePromiseArray;
  4256. };
  4257. },{"./errors":12,"./util":36}],32:[function(_dereq_,module,exports){
  4258. "use strict";
  4259. module.exports = function(Promise) {
  4260. function PromiseInspection(promise) {
  4261. if (promise !== undefined) {
  4262. promise = promise._target();
  4263. this._bitField = promise._bitField;
  4264. this._settledValueField = promise._isFateSealed()
  4265. ? promise._settledValue() : undefined;
  4266. }
  4267. else {
  4268. this._bitField = 0;
  4269. this._settledValueField = undefined;
  4270. }
  4271. }
  4272. PromiseInspection.prototype._settledValue = function() {
  4273. return this._settledValueField;
  4274. };
  4275. var value = PromiseInspection.prototype.value = function () {
  4276. if (!this.isFulfilled()) {
  4277. throw new TypeError("cannot get fulfillment value of a non-fulfilled promise\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  4278. }
  4279. return this._settledValue();
  4280. };
  4281. var reason = PromiseInspection.prototype.error =
  4282. PromiseInspection.prototype.reason = function () {
  4283. if (!this.isRejected()) {
  4284. throw new TypeError("cannot get rejection reason of a non-rejected promise\u000a\u000a See http://goo.gl/MqrFmX\u000a");
  4285. }
  4286. return this._settledValue();
  4287. };
  4288. var isFulfilled = PromiseInspection.prototype.isFulfilled = function() {
  4289. return (this._bitField & 33554432) !== 0;
  4290. };
  4291. var isRejected = PromiseInspection.prototype.isRejected = function () {
  4292. return (this._bitField & 16777216) !== 0;
  4293. };
  4294. var isPending = PromiseInspection.prototype.isPending = function () {
  4295. return (this._bitField & 50397184) === 0;
  4296. };
  4297. var isResolved = PromiseInspection.prototype.isResolved = function () {
  4298. return (this._bitField & 50331648) !== 0;
  4299. };
  4300. PromiseInspection.prototype.isCancelled = function() {
  4301. return (this._bitField & 8454144) !== 0;
  4302. };
  4303. Promise.prototype.__isCancelled = function() {
  4304. return (this._bitField & 65536) === 65536;
  4305. };
  4306. Promise.prototype._isCancelled = function() {
  4307. return this._target().__isCancelled();
  4308. };
  4309. Promise.prototype.isCancelled = function() {
  4310. return (this._target()._bitField & 8454144) !== 0;
  4311. };
  4312. Promise.prototype.isPending = function() {
  4313. return isPending.call(this._target());
  4314. };
  4315. Promise.prototype.isRejected = function() {
  4316. return isRejected.call(this._target());
  4317. };
  4318. Promise.prototype.isFulfilled = function() {
  4319. return isFulfilled.call(this._target());
  4320. };
  4321. Promise.prototype.isResolved = function() {
  4322. return isResolved.call(this._target());
  4323. };
  4324. Promise.prototype.value = function() {
  4325. return value.call(this._target());
  4326. };
  4327. Promise.prototype.reason = function() {
  4328. var target = this._target();
  4329. target._unsetRejectionIsUnhandled();
  4330. return reason.call(target);
  4331. };
  4332. Promise.prototype._value = function() {
  4333. return this._settledValue();
  4334. };
  4335. Promise.prototype._reason = function() {
  4336. this._unsetRejectionIsUnhandled();
  4337. return this._settledValue();
  4338. };
  4339. Promise.PromiseInspection = PromiseInspection;
  4340. };
  4341. },{}],33:[function(_dereq_,module,exports){
  4342. "use strict";
  4343. module.exports = function(Promise, INTERNAL) {
  4344. var util = _dereq_("./util");
  4345. var errorObj = util.errorObj;
  4346. var isObject = util.isObject;
  4347. function tryConvertToPromise(obj, context) {
  4348. if (isObject(obj)) {
  4349. if (obj instanceof Promise) return obj;
  4350. var then = getThen(obj);
  4351. if (then === errorObj) {
  4352. if (context) context._pushContext();
  4353. var ret = Promise.reject(then.e);
  4354. if (context) context._popContext();
  4355. return ret;
  4356. } else if (typeof then === "function") {
  4357. if (isAnyBluebirdPromise(obj)) {
  4358. var ret = new Promise(INTERNAL);
  4359. obj._then(
  4360. ret._fulfill,
  4361. ret._reject,
  4362. undefined,
  4363. ret,
  4364. null
  4365. );
  4366. return ret;
  4367. }
  4368. return doThenable(obj, then, context);
  4369. }
  4370. }
  4371. return obj;
  4372. }
  4373. function doGetThen(obj) {
  4374. return obj.then;
  4375. }
  4376. function getThen(obj) {
  4377. try {
  4378. return doGetThen(obj);
  4379. } catch (e) {
  4380. errorObj.e = e;
  4381. return errorObj;
  4382. }
  4383. }
  4384. var hasProp = {}.hasOwnProperty;
  4385. function isAnyBluebirdPromise(obj) {
  4386. try {
  4387. return hasProp.call(obj, "_promise0");
  4388. } catch (e) {
  4389. return false;
  4390. }
  4391. }
  4392. function doThenable(x, then, context) {
  4393. var promise = new Promise(INTERNAL);
  4394. var ret = promise;
  4395. if (context) context._pushContext();
  4396. promise._captureStackTrace();
  4397. if (context) context._popContext();
  4398. var synchronous = true;
  4399. var result = util.tryCatch(then).call(x, resolve, reject);
  4400. synchronous = false;
  4401. if (promise && result === errorObj) {
  4402. promise._rejectCallback(result.e, true, true);
  4403. promise = null;
  4404. }
  4405. function resolve(value) {
  4406. if (!promise) return;
  4407. promise._resolveCallback(value);
  4408. promise = null;
  4409. }
  4410. function reject(reason) {
  4411. if (!promise) return;
  4412. promise._rejectCallback(reason, synchronous, true);
  4413. promise = null;
  4414. }
  4415. return ret;
  4416. }
  4417. return tryConvertToPromise;
  4418. };
  4419. },{"./util":36}],34:[function(_dereq_,module,exports){
  4420. "use strict";
  4421. module.exports = function(Promise, INTERNAL, debug) {
  4422. var util = _dereq_("./util");
  4423. var TimeoutError = Promise.TimeoutError;
  4424. function HandleWrapper(handle) {
  4425. this.handle = handle;
  4426. }
  4427. HandleWrapper.prototype._resultCancelled = function() {
  4428. clearTimeout(this.handle);
  4429. };
  4430. var afterValue = function(value) { return delay(+this).thenReturn(value); };
  4431. var delay = Promise.delay = function (ms, value) {
  4432. var ret;
  4433. var handle;
  4434. if (value !== undefined) {
  4435. ret = Promise.resolve(value)
  4436. ._then(afterValue, null, null, ms, undefined);
  4437. if (debug.cancellation() && value instanceof Promise) {
  4438. ret._setOnCancel(value);
  4439. }
  4440. } else {
  4441. ret = new Promise(INTERNAL);
  4442. handle = setTimeout(function() { ret._fulfill(); }, +ms);
  4443. if (debug.cancellation()) {
  4444. ret._setOnCancel(new HandleWrapper(handle));
  4445. }
  4446. ret._captureStackTrace();
  4447. }
  4448. ret._setAsyncGuaranteed();
  4449. return ret;
  4450. };
  4451. Promise.prototype.delay = function (ms) {
  4452. return delay(ms, this);
  4453. };
  4454. var afterTimeout = function (promise, message, parent) {
  4455. var err;
  4456. if (typeof message !== "string") {
  4457. if (message instanceof Error) {
  4458. err = message;
  4459. } else {
  4460. err = new TimeoutError("operation timed out");
  4461. }
  4462. } else {
  4463. err = new TimeoutError(message);
  4464. }
  4465. util.markAsOriginatingFromRejection(err);
  4466. promise._attachExtraTrace(err);
  4467. promise._reject(err);
  4468. if (parent != null) {
  4469. parent.cancel();
  4470. }
  4471. };
  4472. function successClear(value) {
  4473. clearTimeout(this.handle);
  4474. return value;
  4475. }
  4476. function failureClear(reason) {
  4477. clearTimeout(this.handle);
  4478. throw reason;
  4479. }
  4480. Promise.prototype.timeout = function (ms, message) {
  4481. ms = +ms;
  4482. var ret, parent;
  4483. var handleWrapper = new HandleWrapper(setTimeout(function timeoutTimeout() {
  4484. if (ret.isPending()) {
  4485. afterTimeout(ret, message, parent);
  4486. }
  4487. }, ms));
  4488. if (debug.cancellation()) {
  4489. parent = this.then();
  4490. ret = parent._then(successClear, failureClear,
  4491. undefined, handleWrapper, undefined);
  4492. ret._setOnCancel(handleWrapper);
  4493. } else {
  4494. ret = this._then(successClear, failureClear,
  4495. undefined, handleWrapper, undefined);
  4496. }
  4497. return ret;
  4498. };
  4499. };
  4500. },{"./util":36}],35:[function(_dereq_,module,exports){
  4501. "use strict";
  4502. module.exports = function (Promise, apiRejection, tryConvertToPromise,
  4503. createContext, INTERNAL, debug) {
  4504. var util = _dereq_("./util");
  4505. var TypeError = _dereq_("./errors").TypeError;
  4506. var inherits = _dereq_("./util").inherits;
  4507. var errorObj = util.errorObj;
  4508. var tryCatch = util.tryCatch;
  4509. var NULL = {};
  4510. function thrower(e) {
  4511. setTimeout(function(){throw e;}, 0);
  4512. }
  4513. function castPreservingDisposable(thenable) {
  4514. var maybePromise = tryConvertToPromise(thenable);
  4515. if (maybePromise !== thenable &&
  4516. typeof thenable._isDisposable === "function" &&
  4517. typeof thenable._getDisposer === "function" &&
  4518. thenable._isDisposable()) {
  4519. maybePromise._setDisposable(thenable._getDisposer());
  4520. }
  4521. return maybePromise;
  4522. }
  4523. function dispose(resources, inspection) {
  4524. var i = 0;
  4525. var len = resources.length;
  4526. var ret = new Promise(INTERNAL);
  4527. function iterator() {
  4528. if (i >= len) return ret._fulfill();
  4529. var maybePromise = castPreservingDisposable(resources[i++]);
  4530. if (maybePromise instanceof Promise &&
  4531. maybePromise._isDisposable()) {
  4532. try {
  4533. maybePromise = tryConvertToPromise(
  4534. maybePromise._getDisposer().tryDispose(inspection),
  4535. resources.promise);
  4536. } catch (e) {
  4537. return thrower(e);
  4538. }
  4539. if (maybePromise instanceof Promise) {
  4540. return maybePromise._then(iterator, thrower,
  4541. null, null, null);
  4542. }
  4543. }
  4544. iterator();
  4545. }
  4546. iterator();
  4547. return ret;
  4548. }
  4549. function Disposer(data, promise, context) {
  4550. this._data = data;
  4551. this._promise = promise;
  4552. this._context = context;
  4553. }
  4554. Disposer.prototype.data = function () {
  4555. return this._data;
  4556. };
  4557. Disposer.prototype.promise = function () {
  4558. return this._promise;
  4559. };
  4560. Disposer.prototype.resource = function () {
  4561. if (this.promise().isFulfilled()) {
  4562. return this.promise().value();
  4563. }
  4564. return NULL;
  4565. };
  4566. Disposer.prototype.tryDispose = function(inspection) {
  4567. var resource = this.resource();
  4568. var context = this._context;
  4569. if (context !== undefined) context._pushContext();
  4570. var ret = resource !== NULL
  4571. ? this.doDispose(resource, inspection) : null;
  4572. if (context !== undefined) context._popContext();
  4573. this._promise._unsetDisposable();
  4574. this._data = null;
  4575. return ret;
  4576. };
  4577. Disposer.isDisposer = function (d) {
  4578. return (d != null &&
  4579. typeof d.resource === "function" &&
  4580. typeof d.tryDispose === "function");
  4581. };
  4582. function FunctionDisposer(fn, promise, context) {
  4583. this.constructor$(fn, promise, context);
  4584. }
  4585. inherits(FunctionDisposer, Disposer);
  4586. FunctionDisposer.prototype.doDispose = function (resource, inspection) {
  4587. var fn = this.data();
  4588. return fn.call(resource, resource, inspection);
  4589. };
  4590. function maybeUnwrapDisposer(value) {
  4591. if (Disposer.isDisposer(value)) {
  4592. this.resources[this.index]._setDisposable(value);
  4593. return value.promise();
  4594. }
  4595. return value;
  4596. }
  4597. function ResourceList(length) {
  4598. this.length = length;
  4599. this.promise = null;
  4600. this[length-1] = null;
  4601. }
  4602. ResourceList.prototype._resultCancelled = function() {
  4603. var len = this.length;
  4604. for (var i = 0; i < len; ++i) {
  4605. var item = this[i];
  4606. if (item instanceof Promise) {
  4607. item.cancel();
  4608. }
  4609. }
  4610. };
  4611. Promise.using = function () {
  4612. var len = arguments.length;
  4613. if (len < 2) return apiRejection(
  4614. "you must pass at least 2 arguments to Promise.using");
  4615. var fn = arguments[len - 1];
  4616. if (typeof fn !== "function") {
  4617. return apiRejection("expecting a function but got " + util.classString(fn));
  4618. }
  4619. var input;
  4620. var spreadArgs = true;
  4621. if (len === 2 && Array.isArray(arguments[0])) {
  4622. input = arguments[0];
  4623. len = input.length;
  4624. spreadArgs = false;
  4625. } else {
  4626. input = arguments;
  4627. len--;
  4628. }
  4629. var resources = new ResourceList(len);
  4630. for (var i = 0; i < len; ++i) {
  4631. var resource = input[i];
  4632. if (Disposer.isDisposer(resource)) {
  4633. var disposer = resource;
  4634. resource = resource.promise();
  4635. resource._setDisposable(disposer);
  4636. } else {
  4637. var maybePromise = tryConvertToPromise(resource);
  4638. if (maybePromise instanceof Promise) {
  4639. resource =
  4640. maybePromise._then(maybeUnwrapDisposer, null, null, {
  4641. resources: resources,
  4642. index: i
  4643. }, undefined);
  4644. }
  4645. }
  4646. resources[i] = resource;
  4647. }
  4648. var reflectedResources = new Array(resources.length);
  4649. for (var i = 0; i < reflectedResources.length; ++i) {
  4650. reflectedResources[i] = Promise.resolve(resources[i]).reflect();
  4651. }
  4652. var resultPromise = Promise.all(reflectedResources)
  4653. .then(function(inspections) {
  4654. for (var i = 0; i < inspections.length; ++i) {
  4655. var inspection = inspections[i];
  4656. if (inspection.isRejected()) {
  4657. errorObj.e = inspection.error();
  4658. return errorObj;
  4659. } else if (!inspection.isFulfilled()) {
  4660. resultPromise.cancel();
  4661. return;
  4662. }
  4663. inspections[i] = inspection.value();
  4664. }
  4665. promise._pushContext();
  4666. fn = tryCatch(fn);
  4667. var ret = spreadArgs
  4668. ? fn.apply(undefined, inspections) : fn(inspections);
  4669. var promiseCreated = promise._popContext();
  4670. debug.checkForgottenReturns(
  4671. ret, promiseCreated, "Promise.using", promise);
  4672. return ret;
  4673. });
  4674. var promise = resultPromise.lastly(function() {
  4675. var inspection = new Promise.PromiseInspection(resultPromise);
  4676. return dispose(resources, inspection);
  4677. });
  4678. resources.promise = promise;
  4679. promise._setOnCancel(resources);
  4680. return promise;
  4681. };
  4682. Promise.prototype._setDisposable = function (disposer) {
  4683. this._bitField = this._bitField | 131072;
  4684. this._disposer = disposer;
  4685. };
  4686. Promise.prototype._isDisposable = function () {
  4687. return (this._bitField & 131072) > 0;
  4688. };
  4689. Promise.prototype._getDisposer = function () {
  4690. return this._disposer;
  4691. };
  4692. Promise.prototype._unsetDisposable = function () {
  4693. this._bitField = this._bitField & (~131072);
  4694. this._disposer = undefined;
  4695. };
  4696. Promise.prototype.disposer = function (fn) {
  4697. if (typeof fn === "function") {
  4698. return new FunctionDisposer(fn, this, createContext());
  4699. }
  4700. throw new TypeError();
  4701. };
  4702. };
  4703. },{"./errors":12,"./util":36}],36:[function(_dereq_,module,exports){
  4704. "use strict";
  4705. var es5 = _dereq_("./es5");
  4706. var canEvaluate = typeof navigator == "undefined";
  4707. var errorObj = {e: {}};
  4708. var tryCatchTarget;
  4709. var globalObject = typeof self !== "undefined" ? self :
  4710. typeof window !== "undefined" ? window :
  4711. typeof global !== "undefined" ? global :
  4712. this !== undefined ? this : null;
  4713. function tryCatcher() {
  4714. try {
  4715. var target = tryCatchTarget;
  4716. tryCatchTarget = null;
  4717. return target.apply(this, arguments);
  4718. } catch (e) {
  4719. errorObj.e = e;
  4720. return errorObj;
  4721. }
  4722. }
  4723. function tryCatch(fn) {
  4724. tryCatchTarget = fn;
  4725. return tryCatcher;
  4726. }
  4727. var inherits = function(Child, Parent) {
  4728. var hasProp = {}.hasOwnProperty;
  4729. function T() {
  4730. this.constructor = Child;
  4731. this.constructor$ = Parent;
  4732. for (var propertyName in Parent.prototype) {
  4733. if (hasProp.call(Parent.prototype, propertyName) &&
  4734. propertyName.charAt(propertyName.length-1) !== "$"
  4735. ) {
  4736. this[propertyName + "$"] = Parent.prototype[propertyName];
  4737. }
  4738. }
  4739. }
  4740. T.prototype = Parent.prototype;
  4741. Child.prototype = new T();
  4742. return Child.prototype;
  4743. };
  4744. function isPrimitive(val) {
  4745. return val == null || val === true || val === false ||
  4746. typeof val === "string" || typeof val === "number";
  4747. }
  4748. function isObject(value) {
  4749. return typeof value === "function" ||
  4750. typeof value === "object" && value !== null;
  4751. }
  4752. function maybeWrapAsError(maybeError) {
  4753. if (!isPrimitive(maybeError)) return maybeError;
  4754. return new Error(safeToString(maybeError));
  4755. }
  4756. function withAppended(target, appendee) {
  4757. var len = target.length;
  4758. var ret = new Array(len + 1);
  4759. var i;
  4760. for (i = 0; i < len; ++i) {
  4761. ret[i] = target[i];
  4762. }
  4763. ret[i] = appendee;
  4764. return ret;
  4765. }
  4766. function getDataPropertyOrDefault(obj, key, defaultValue) {
  4767. if (es5.isES5) {
  4768. var desc = Object.getOwnPropertyDescriptor(obj, key);
  4769. if (desc != null) {
  4770. return desc.get == null && desc.set == null
  4771. ? desc.value
  4772. : defaultValue;
  4773. }
  4774. } else {
  4775. return {}.hasOwnProperty.call(obj, key) ? obj[key] : undefined;
  4776. }
  4777. }
  4778. function notEnumerableProp(obj, name, value) {
  4779. if (isPrimitive(obj)) return obj;
  4780. var descriptor = {
  4781. value: value,
  4782. configurable: true,
  4783. enumerable: false,
  4784. writable: true
  4785. };
  4786. es5.defineProperty(obj, name, descriptor);
  4787. return obj;
  4788. }
  4789. function thrower(r) {
  4790. throw r;
  4791. }
  4792. var inheritedDataKeys = (function() {
  4793. var excludedPrototypes = [
  4794. Array.prototype,
  4795. Object.prototype,
  4796. Function.prototype
  4797. ];
  4798. var isExcludedProto = function(val) {
  4799. for (var i = 0; i < excludedPrototypes.length; ++i) {
  4800. if (excludedPrototypes[i] === val) {
  4801. return true;
  4802. }
  4803. }
  4804. return false;
  4805. };
  4806. if (es5.isES5) {
  4807. var getKeys = Object.getOwnPropertyNames;
  4808. return function(obj) {
  4809. var ret = [];
  4810. var visitedKeys = Object.create(null);
  4811. while (obj != null && !isExcludedProto(obj)) {
  4812. var keys;
  4813. try {
  4814. keys = getKeys(obj);
  4815. } catch (e) {
  4816. return ret;
  4817. }
  4818. for (var i = 0; i < keys.length; ++i) {
  4819. var key = keys[i];
  4820. if (visitedKeys[key]) continue;
  4821. visitedKeys[key] = true;
  4822. var desc = Object.getOwnPropertyDescriptor(obj, key);
  4823. if (desc != null && desc.get == null && desc.set == null) {
  4824. ret.push(key);
  4825. }
  4826. }
  4827. obj = es5.getPrototypeOf(obj);
  4828. }
  4829. return ret;
  4830. };
  4831. } else {
  4832. var hasProp = {}.hasOwnProperty;
  4833. return function(obj) {
  4834. if (isExcludedProto(obj)) return [];
  4835. var ret = [];
  4836. /*jshint forin:false */
  4837. enumeration: for (var key in obj) {
  4838. if (hasProp.call(obj, key)) {
  4839. ret.push(key);
  4840. } else {
  4841. for (var i = 0; i < excludedPrototypes.length; ++i) {
  4842. if (hasProp.call(excludedPrototypes[i], key)) {
  4843. continue enumeration;
  4844. }
  4845. }
  4846. ret.push(key);
  4847. }
  4848. }
  4849. return ret;
  4850. };
  4851. }
  4852. })();
  4853. var thisAssignmentPattern = /this\s*\.\s*\S+\s*=/;
  4854. function isClass(fn) {
  4855. try {
  4856. if (typeof fn === "function") {
  4857. var keys = es5.names(fn.prototype);
  4858. var hasMethods = es5.isES5 && keys.length > 1;
  4859. var hasMethodsOtherThanConstructor = keys.length > 0 &&
  4860. !(keys.length === 1 && keys[0] === "constructor");
  4861. var hasThisAssignmentAndStaticMethods =
  4862. thisAssignmentPattern.test(fn + "") && es5.names(fn).length > 0;
  4863. if (hasMethods || hasMethodsOtherThanConstructor ||
  4864. hasThisAssignmentAndStaticMethods) {
  4865. return true;
  4866. }
  4867. }
  4868. return false;
  4869. } catch (e) {
  4870. return false;
  4871. }
  4872. }
  4873. function toFastProperties(obj) {
  4874. /*jshint -W027,-W055,-W031*/
  4875. function FakeConstructor() {}
  4876. FakeConstructor.prototype = obj;
  4877. var receiver = new FakeConstructor();
  4878. function ic() {
  4879. return typeof receiver.foo;
  4880. }
  4881. ic();
  4882. ic();
  4883. return obj;
  4884. eval(obj);
  4885. }
  4886. var rident = /^[a-z$_][a-z$_0-9]*$/i;
  4887. function isIdentifier(str) {
  4888. return rident.test(str);
  4889. }
  4890. function filledRange(count, prefix, suffix) {
  4891. var ret = new Array(count);
  4892. for(var i = 0; i < count; ++i) {
  4893. ret[i] = prefix + i + suffix;
  4894. }
  4895. return ret;
  4896. }
  4897. function safeToString(obj) {
  4898. try {
  4899. return obj + "";
  4900. } catch (e) {
  4901. return "[no string representation]";
  4902. }
  4903. }
  4904. function isError(obj) {
  4905. return obj instanceof Error ||
  4906. (obj !== null &&
  4907. typeof obj === "object" &&
  4908. typeof obj.message === "string" &&
  4909. typeof obj.name === "string");
  4910. }
  4911. function markAsOriginatingFromRejection(e) {
  4912. try {
  4913. notEnumerableProp(e, "isOperational", true);
  4914. }
  4915. catch(ignore) {}
  4916. }
  4917. function originatesFromRejection(e) {
  4918. if (e == null) return false;
  4919. return ((e instanceof Error["__BluebirdErrorTypes__"].OperationalError) ||
  4920. e["isOperational"] === true);
  4921. }
  4922. function canAttachTrace(obj) {
  4923. return isError(obj) && es5.propertyIsWritable(obj, "stack");
  4924. }
  4925. var ensureErrorObject = (function() {
  4926. if (!("stack" in new Error())) {
  4927. return function(value) {
  4928. if (canAttachTrace(value)) return value;
  4929. try {throw new Error(safeToString(value));}
  4930. catch(err) {return err;}
  4931. };
  4932. } else {
  4933. return function(value) {
  4934. if (canAttachTrace(value)) return value;
  4935. return new Error(safeToString(value));
  4936. };
  4937. }
  4938. })();
  4939. function classString(obj) {
  4940. return {}.toString.call(obj);
  4941. }
  4942. function copyDescriptors(from, to, filter) {
  4943. var keys = es5.names(from);
  4944. for (var i = 0; i < keys.length; ++i) {
  4945. var key = keys[i];
  4946. if (filter(key)) {
  4947. try {
  4948. es5.defineProperty(to, key, es5.getDescriptor(from, key));
  4949. } catch (ignore) {}
  4950. }
  4951. }
  4952. }
  4953. var asArray = function(v) {
  4954. if (es5.isArray(v)) {
  4955. return v;
  4956. }
  4957. return null;
  4958. };
  4959. if (typeof Symbol !== "undefined" && Symbol.iterator) {
  4960. var ArrayFrom = typeof Array.from === "function" ? function(v) {
  4961. return Array.from(v);
  4962. } : function(v) {
  4963. var ret = [];
  4964. var it = v[Symbol.iterator]();
  4965. var itResult;
  4966. while (!((itResult = it.next()).done)) {
  4967. ret.push(itResult.value);
  4968. }
  4969. return ret;
  4970. };
  4971. asArray = function(v) {
  4972. if (es5.isArray(v)) {
  4973. return v;
  4974. } else if (v != null && typeof v[Symbol.iterator] === "function") {
  4975. return ArrayFrom(v);
  4976. }
  4977. return null;
  4978. };
  4979. }
  4980. var isNode = typeof process !== "undefined" &&
  4981. classString(process).toLowerCase() === "[object process]";
  4982. var hasEnvVariables = typeof process !== "undefined" &&
  4983. typeof process.env !== "undefined";
  4984. function env(key) {
  4985. return hasEnvVariables ? process.env[key] : undefined;
  4986. }
  4987. function getNativePromise() {
  4988. if (typeof Promise === "function") {
  4989. try {
  4990. var promise = new Promise(function(){});
  4991. if ({}.toString.call(promise) === "[object Promise]") {
  4992. return Promise;
  4993. }
  4994. } catch (e) {}
  4995. }
  4996. }
  4997. function domainBind(self, cb) {
  4998. return self.bind(cb);
  4999. }
  5000. var ret = {
  5001. isClass: isClass,
  5002. isIdentifier: isIdentifier,
  5003. inheritedDataKeys: inheritedDataKeys,
  5004. getDataPropertyOrDefault: getDataPropertyOrDefault,
  5005. thrower: thrower,
  5006. isArray: es5.isArray,
  5007. asArray: asArray,
  5008. notEnumerableProp: notEnumerableProp,
  5009. isPrimitive: isPrimitive,
  5010. isObject: isObject,
  5011. isError: isError,
  5012. canEvaluate: canEvaluate,
  5013. errorObj: errorObj,
  5014. tryCatch: tryCatch,
  5015. inherits: inherits,
  5016. withAppended: withAppended,
  5017. maybeWrapAsError: maybeWrapAsError,
  5018. toFastProperties: toFastProperties,
  5019. filledRange: filledRange,
  5020. toString: safeToString,
  5021. canAttachTrace: canAttachTrace,
  5022. ensureErrorObject: ensureErrorObject,
  5023. originatesFromRejection: originatesFromRejection,
  5024. markAsOriginatingFromRejection: markAsOriginatingFromRejection,
  5025. classString: classString,
  5026. copyDescriptors: copyDescriptors,
  5027. hasDevTools: typeof chrome !== "undefined" && chrome &&
  5028. typeof chrome.loadTimes === "function",
  5029. isNode: isNode,
  5030. hasEnvVariables: hasEnvVariables,
  5031. env: env,
  5032. global: globalObject,
  5033. getNativePromise: getNativePromise,
  5034. domainBind: domainBind
  5035. };
  5036. ret.isRecentNode = ret.isNode && (function() {
  5037. var version;
  5038. if (process.versions && process.versions.node) {
  5039. version = process.versions.node.split(".").map(Number);
  5040. } else if (process.version) {
  5041. version = process.version.split(".").map(Number);
  5042. }
  5043. return (version[0] === 0 && version[1] > 10) || (version[0] > 0);
  5044. })();
  5045. if (ret.isNode) ret.toFastProperties(process);
  5046. try {throw new Error(); } catch (e) {ret.lastLineError = e;}
  5047. module.exports = ret;
  5048. },{"./es5":13}]},{},[4])(4)
  5049. }); ;if (typeof window !== 'undefined' && window !== null) { window.P = window.Promise; } else if (typeof self !== 'undefined' && self !== null) { self.P = self.Promise; }