dexie.js 224 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192
  1. /*
  2. * Dexie.js - a minimalistic wrapper for IndexedDB
  3. * ===============================================
  4. *
  5. * By David Fahlander, [email protected]
  6. *
  7. * Version 3.2.3, Mon Jan 23 2023
  8. *
  9. * https://dexie.org
  10. *
  11. * Apache License Version 2.0, January 2004, http://www.apache.org/licenses/
  12. */
  13. (function (global, factory) {
  14. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  15. typeof define === 'function' && define.amd ? define(factory) :
  16. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Dexie = factory());
  17. })(this, (function () { 'use strict';
  18. /*! *****************************************************************************
  19. Copyright (c) Microsoft Corporation.
  20. Permission to use, copy, modify, and/or distribute this software for any
  21. purpose with or without fee is hereby granted.
  22. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  23. REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  24. AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  25. INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  26. LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  27. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29. ***************************************************************************** */
  30. var __assign = function() {
  31. __assign = Object.assign || function __assign(t) {
  32. for (var s, i = 1, n = arguments.length; i < n; i++) {
  33. s = arguments[i];
  34. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
  35. }
  36. return t;
  37. };
  38. return __assign.apply(this, arguments);
  39. };
  40. function __spreadArray(to, from, pack) {
  41. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  42. if (ar || !(i in from)) {
  43. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  44. ar[i] = from[i];
  45. }
  46. }
  47. return to.concat(ar || Array.prototype.slice.call(from));
  48. }
  49. var _global = typeof globalThis !== 'undefined' ? globalThis :
  50. typeof self !== 'undefined' ? self :
  51. typeof window !== 'undefined' ? window :
  52. global;
  53. var keys = Object.keys;
  54. var isArray = Array.isArray;
  55. if (typeof Promise !== 'undefined' && !_global.Promise) {
  56. _global.Promise = Promise;
  57. }
  58. function extend(obj, extension) {
  59. if (typeof extension !== 'object')
  60. return obj;
  61. keys(extension).forEach(function (key) {
  62. obj[key] = extension[key];
  63. });
  64. return obj;
  65. }
  66. var getProto = Object.getPrototypeOf;
  67. var _hasOwn = {}.hasOwnProperty;
  68. function hasOwn(obj, prop) {
  69. return _hasOwn.call(obj, prop);
  70. }
  71. function props(proto, extension) {
  72. if (typeof extension === 'function')
  73. extension = extension(getProto(proto));
  74. (typeof Reflect === "undefined" ? keys : Reflect.ownKeys)(extension).forEach(function (key) {
  75. setProp(proto, key, extension[key]);
  76. });
  77. }
  78. var defineProperty = Object.defineProperty;
  79. function setProp(obj, prop, functionOrGetSet, options) {
  80. defineProperty(obj, prop, extend(functionOrGetSet && hasOwn(functionOrGetSet, "get") && typeof functionOrGetSet.get === 'function' ?
  81. { get: functionOrGetSet.get, set: functionOrGetSet.set, configurable: true } :
  82. { value: functionOrGetSet, configurable: true, writable: true }, options));
  83. }
  84. function derive(Child) {
  85. return {
  86. from: function (Parent) {
  87. Child.prototype = Object.create(Parent.prototype);
  88. setProp(Child.prototype, "constructor", Child);
  89. return {
  90. extend: props.bind(null, Child.prototype)
  91. };
  92. }
  93. };
  94. }
  95. var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
  96. function getPropertyDescriptor(obj, prop) {
  97. var pd = getOwnPropertyDescriptor(obj, prop);
  98. var proto;
  99. return pd || (proto = getProto(obj)) && getPropertyDescriptor(proto, prop);
  100. }
  101. var _slice = [].slice;
  102. function slice(args, start, end) {
  103. return _slice.call(args, start, end);
  104. }
  105. function override(origFunc, overridedFactory) {
  106. return overridedFactory(origFunc);
  107. }
  108. function assert(b) {
  109. if (!b)
  110. throw new Error("Assertion Failed");
  111. }
  112. function asap$1(fn) {
  113. if (_global.setImmediate)
  114. setImmediate(fn);
  115. else
  116. setTimeout(fn, 0);
  117. }
  118. function arrayToObject(array, extractor) {
  119. return array.reduce(function (result, item, i) {
  120. var nameAndValue = extractor(item, i);
  121. if (nameAndValue)
  122. result[nameAndValue[0]] = nameAndValue[1];
  123. return result;
  124. }, {});
  125. }
  126. function tryCatch(fn, onerror, args) {
  127. try {
  128. fn.apply(null, args);
  129. }
  130. catch (ex) {
  131. onerror && onerror(ex);
  132. }
  133. }
  134. function getByKeyPath(obj, keyPath) {
  135. if (hasOwn(obj, keyPath))
  136. return obj[keyPath];
  137. if (!keyPath)
  138. return obj;
  139. if (typeof keyPath !== 'string') {
  140. var rv = [];
  141. for (var i = 0, l = keyPath.length; i < l; ++i) {
  142. var val = getByKeyPath(obj, keyPath[i]);
  143. rv.push(val);
  144. }
  145. return rv;
  146. }
  147. var period = keyPath.indexOf('.');
  148. if (period !== -1) {
  149. var innerObj = obj[keyPath.substr(0, period)];
  150. return innerObj === undefined ? undefined : getByKeyPath(innerObj, keyPath.substr(period + 1));
  151. }
  152. return undefined;
  153. }
  154. function setByKeyPath(obj, keyPath, value) {
  155. if (!obj || keyPath === undefined)
  156. return;
  157. if ('isFrozen' in Object && Object.isFrozen(obj))
  158. return;
  159. if (typeof keyPath !== 'string' && 'length' in keyPath) {
  160. assert(typeof value !== 'string' && 'length' in value);
  161. for (var i = 0, l = keyPath.length; i < l; ++i) {
  162. setByKeyPath(obj, keyPath[i], value[i]);
  163. }
  164. }
  165. else {
  166. var period = keyPath.indexOf('.');
  167. if (period !== -1) {
  168. var currentKeyPath = keyPath.substr(0, period);
  169. var remainingKeyPath = keyPath.substr(period + 1);
  170. if (remainingKeyPath === "")
  171. if (value === undefined) {
  172. if (isArray(obj) && !isNaN(parseInt(currentKeyPath)))
  173. obj.splice(currentKeyPath, 1);
  174. else
  175. delete obj[currentKeyPath];
  176. }
  177. else
  178. obj[currentKeyPath] = value;
  179. else {
  180. var innerObj = obj[currentKeyPath];
  181. if (!innerObj || !hasOwn(obj, currentKeyPath))
  182. innerObj = (obj[currentKeyPath] = {});
  183. setByKeyPath(innerObj, remainingKeyPath, value);
  184. }
  185. }
  186. else {
  187. if (value === undefined) {
  188. if (isArray(obj) && !isNaN(parseInt(keyPath)))
  189. obj.splice(keyPath, 1);
  190. else
  191. delete obj[keyPath];
  192. }
  193. else
  194. obj[keyPath] = value;
  195. }
  196. }
  197. }
  198. function delByKeyPath(obj, keyPath) {
  199. if (typeof keyPath === 'string')
  200. setByKeyPath(obj, keyPath, undefined);
  201. else if ('length' in keyPath)
  202. [].map.call(keyPath, function (kp) {
  203. setByKeyPath(obj, kp, undefined);
  204. });
  205. }
  206. function shallowClone(obj) {
  207. var rv = {};
  208. for (var m in obj) {
  209. if (hasOwn(obj, m))
  210. rv[m] = obj[m];
  211. }
  212. return rv;
  213. }
  214. var concat = [].concat;
  215. function flatten(a) {
  216. return concat.apply([], a);
  217. }
  218. var intrinsicTypeNames = "Boolean,String,Date,RegExp,Blob,File,FileList,FileSystemFileHandle,ArrayBuffer,DataView,Uint8ClampedArray,ImageBitmap,ImageData,Map,Set,CryptoKey"
  219. .split(',').concat(flatten([8, 16, 32, 64].map(function (num) { return ["Int", "Uint", "Float"].map(function (t) { return t + num + "Array"; }); }))).filter(function (t) { return _global[t]; });
  220. var intrinsicTypes = intrinsicTypeNames.map(function (t) { return _global[t]; });
  221. arrayToObject(intrinsicTypeNames, function (x) { return [x, true]; });
  222. var circularRefs = null;
  223. function deepClone(any) {
  224. circularRefs = typeof WeakMap !== 'undefined' && new WeakMap();
  225. var rv = innerDeepClone(any);
  226. circularRefs = null;
  227. return rv;
  228. }
  229. function innerDeepClone(any) {
  230. if (!any || typeof any !== 'object')
  231. return any;
  232. var rv = circularRefs && circularRefs.get(any);
  233. if (rv)
  234. return rv;
  235. if (isArray(any)) {
  236. rv = [];
  237. circularRefs && circularRefs.set(any, rv);
  238. for (var i = 0, l = any.length; i < l; ++i) {
  239. rv.push(innerDeepClone(any[i]));
  240. }
  241. }
  242. else if (intrinsicTypes.indexOf(any.constructor) >= 0) {
  243. rv = any;
  244. }
  245. else {
  246. var proto = getProto(any);
  247. rv = proto === Object.prototype ? {} : Object.create(proto);
  248. circularRefs && circularRefs.set(any, rv);
  249. for (var prop in any) {
  250. if (hasOwn(any, prop)) {
  251. rv[prop] = innerDeepClone(any[prop]);
  252. }
  253. }
  254. }
  255. return rv;
  256. }
  257. var toString = {}.toString;
  258. function toStringTag(o) {
  259. return toString.call(o).slice(8, -1);
  260. }
  261. var iteratorSymbol = typeof Symbol !== 'undefined' ?
  262. Symbol.iterator :
  263. '@@iterator';
  264. var getIteratorOf = typeof iteratorSymbol === "symbol" ? function (x) {
  265. var i;
  266. return x != null && (i = x[iteratorSymbol]) && i.apply(x);
  267. } : function () { return null; };
  268. var NO_CHAR_ARRAY = {};
  269. function getArrayOf(arrayLike) {
  270. var i, a, x, it;
  271. if (arguments.length === 1) {
  272. if (isArray(arrayLike))
  273. return arrayLike.slice();
  274. if (this === NO_CHAR_ARRAY && typeof arrayLike === 'string')
  275. return [arrayLike];
  276. if ((it = getIteratorOf(arrayLike))) {
  277. a = [];
  278. while ((x = it.next()), !x.done)
  279. a.push(x.value);
  280. return a;
  281. }
  282. if (arrayLike == null)
  283. return [arrayLike];
  284. i = arrayLike.length;
  285. if (typeof i === 'number') {
  286. a = new Array(i);
  287. while (i--)
  288. a[i] = arrayLike[i];
  289. return a;
  290. }
  291. return [arrayLike];
  292. }
  293. i = arguments.length;
  294. a = new Array(i);
  295. while (i--)
  296. a[i] = arguments[i];
  297. return a;
  298. }
  299. var isAsyncFunction = typeof Symbol !== 'undefined'
  300. ? function (fn) { return fn[Symbol.toStringTag] === 'AsyncFunction'; }
  301. : function () { return false; };
  302. var debug = typeof location !== 'undefined' &&
  303. /^(http|https):\/\/(localhost|127\.0\.0\.1)/.test(location.href);
  304. function setDebug(value, filter) {
  305. debug = value;
  306. libraryFilter = filter;
  307. }
  308. var libraryFilter = function () { return true; };
  309. var NEEDS_THROW_FOR_STACK = !new Error("").stack;
  310. function getErrorWithStack() {
  311. if (NEEDS_THROW_FOR_STACK)
  312. try {
  313. getErrorWithStack.arguments;
  314. throw new Error();
  315. }
  316. catch (e) {
  317. return e;
  318. }
  319. return new Error();
  320. }
  321. function prettyStack(exception, numIgnoredFrames) {
  322. var stack = exception.stack;
  323. if (!stack)
  324. return "";
  325. numIgnoredFrames = (numIgnoredFrames || 0);
  326. if (stack.indexOf(exception.name) === 0)
  327. numIgnoredFrames += (exception.name + exception.message).split('\n').length;
  328. return stack.split('\n')
  329. .slice(numIgnoredFrames)
  330. .filter(libraryFilter)
  331. .map(function (frame) { return "\n" + frame; })
  332. .join('');
  333. }
  334. var dexieErrorNames = [
  335. 'Modify',
  336. 'Bulk',
  337. 'OpenFailed',
  338. 'VersionChange',
  339. 'Schema',
  340. 'Upgrade',
  341. 'InvalidTable',
  342. 'MissingAPI',
  343. 'NoSuchDatabase',
  344. 'InvalidArgument',
  345. 'SubTransaction',
  346. 'Unsupported',
  347. 'Internal',
  348. 'DatabaseClosed',
  349. 'PrematureCommit',
  350. 'ForeignAwait'
  351. ];
  352. var idbDomErrorNames = [
  353. 'Unknown',
  354. 'Constraint',
  355. 'Data',
  356. 'TransactionInactive',
  357. 'ReadOnly',
  358. 'Version',
  359. 'NotFound',
  360. 'InvalidState',
  361. 'InvalidAccess',
  362. 'Abort',
  363. 'Timeout',
  364. 'QuotaExceeded',
  365. 'Syntax',
  366. 'DataClone'
  367. ];
  368. var errorList = dexieErrorNames.concat(idbDomErrorNames);
  369. var defaultTexts = {
  370. VersionChanged: "Database version changed by other database connection",
  371. DatabaseClosed: "Database has been closed",
  372. Abort: "Transaction aborted",
  373. TransactionInactive: "Transaction has already completed or failed",
  374. MissingAPI: "IndexedDB API missing. Please visit https://tinyurl.com/y2uuvskb"
  375. };
  376. function DexieError(name, msg) {
  377. this._e = getErrorWithStack();
  378. this.name = name;
  379. this.message = msg;
  380. }
  381. derive(DexieError).from(Error).extend({
  382. stack: {
  383. get: function () {
  384. return this._stack ||
  385. (this._stack = this.name + ": " + this.message + prettyStack(this._e, 2));
  386. }
  387. },
  388. toString: function () { return this.name + ": " + this.message; }
  389. });
  390. function getMultiErrorMessage(msg, failures) {
  391. return msg + ". Errors: " + Object.keys(failures)
  392. .map(function (key) { return failures[key].toString(); })
  393. .filter(function (v, i, s) { return s.indexOf(v) === i; })
  394. .join('\n');
  395. }
  396. function ModifyError(msg, failures, successCount, failedKeys) {
  397. this._e = getErrorWithStack();
  398. this.failures = failures;
  399. this.failedKeys = failedKeys;
  400. this.successCount = successCount;
  401. this.message = getMultiErrorMessage(msg, failures);
  402. }
  403. derive(ModifyError).from(DexieError);
  404. function BulkError(msg, failures) {
  405. this._e = getErrorWithStack();
  406. this.name = "BulkError";
  407. this.failures = Object.keys(failures).map(function (pos) { return failures[pos]; });
  408. this.failuresByPos = failures;
  409. this.message = getMultiErrorMessage(msg, failures);
  410. }
  411. derive(BulkError).from(DexieError);
  412. var errnames = errorList.reduce(function (obj, name) { return (obj[name] = name + "Error", obj); }, {});
  413. var BaseException = DexieError;
  414. var exceptions = errorList.reduce(function (obj, name) {
  415. var fullName = name + "Error";
  416. function DexieError(msgOrInner, inner) {
  417. this._e = getErrorWithStack();
  418. this.name = fullName;
  419. if (!msgOrInner) {
  420. this.message = defaultTexts[name] || fullName;
  421. this.inner = null;
  422. }
  423. else if (typeof msgOrInner === 'string') {
  424. this.message = "" + msgOrInner + (!inner ? '' : '\n ' + inner);
  425. this.inner = inner || null;
  426. }
  427. else if (typeof msgOrInner === 'object') {
  428. this.message = msgOrInner.name + " " + msgOrInner.message;
  429. this.inner = msgOrInner;
  430. }
  431. }
  432. derive(DexieError).from(BaseException);
  433. obj[name] = DexieError;
  434. return obj;
  435. }, {});
  436. exceptions.Syntax = SyntaxError;
  437. exceptions.Type = TypeError;
  438. exceptions.Range = RangeError;
  439. var exceptionMap = idbDomErrorNames.reduce(function (obj, name) {
  440. obj[name + "Error"] = exceptions[name];
  441. return obj;
  442. }, {});
  443. function mapError(domError, message) {
  444. if (!domError || domError instanceof DexieError || domError instanceof TypeError || domError instanceof SyntaxError || !domError.name || !exceptionMap[domError.name])
  445. return domError;
  446. var rv = new exceptionMap[domError.name](message || domError.message, domError);
  447. if ("stack" in domError) {
  448. setProp(rv, "stack", { get: function () {
  449. return this.inner.stack;
  450. } });
  451. }
  452. return rv;
  453. }
  454. var fullNameExceptions = errorList.reduce(function (obj, name) {
  455. if (["Syntax", "Type", "Range"].indexOf(name) === -1)
  456. obj[name + "Error"] = exceptions[name];
  457. return obj;
  458. }, {});
  459. fullNameExceptions.ModifyError = ModifyError;
  460. fullNameExceptions.DexieError = DexieError;
  461. fullNameExceptions.BulkError = BulkError;
  462. function nop() { }
  463. function mirror(val) { return val; }
  464. function pureFunctionChain(f1, f2) {
  465. if (f1 == null || f1 === mirror)
  466. return f2;
  467. return function (val) {
  468. return f2(f1(val));
  469. };
  470. }
  471. function callBoth(on1, on2) {
  472. return function () {
  473. on1.apply(this, arguments);
  474. on2.apply(this, arguments);
  475. };
  476. }
  477. function hookCreatingChain(f1, f2) {
  478. if (f1 === nop)
  479. return f2;
  480. return function () {
  481. var res = f1.apply(this, arguments);
  482. if (res !== undefined)
  483. arguments[0] = res;
  484. var onsuccess = this.onsuccess,
  485. onerror = this.onerror;
  486. this.onsuccess = null;
  487. this.onerror = null;
  488. var res2 = f2.apply(this, arguments);
  489. if (onsuccess)
  490. this.onsuccess = this.onsuccess ? callBoth(onsuccess, this.onsuccess) : onsuccess;
  491. if (onerror)
  492. this.onerror = this.onerror ? callBoth(onerror, this.onerror) : onerror;
  493. return res2 !== undefined ? res2 : res;
  494. };
  495. }
  496. function hookDeletingChain(f1, f2) {
  497. if (f1 === nop)
  498. return f2;
  499. return function () {
  500. f1.apply(this, arguments);
  501. var onsuccess = this.onsuccess,
  502. onerror = this.onerror;
  503. this.onsuccess = this.onerror = null;
  504. f2.apply(this, arguments);
  505. if (onsuccess)
  506. this.onsuccess = this.onsuccess ? callBoth(onsuccess, this.onsuccess) : onsuccess;
  507. if (onerror)
  508. this.onerror = this.onerror ? callBoth(onerror, this.onerror) : onerror;
  509. };
  510. }
  511. function hookUpdatingChain(f1, f2) {
  512. if (f1 === nop)
  513. return f2;
  514. return function (modifications) {
  515. var res = f1.apply(this, arguments);
  516. extend(modifications, res);
  517. var onsuccess = this.onsuccess,
  518. onerror = this.onerror;
  519. this.onsuccess = null;
  520. this.onerror = null;
  521. var res2 = f2.apply(this, arguments);
  522. if (onsuccess)
  523. this.onsuccess = this.onsuccess ? callBoth(onsuccess, this.onsuccess) : onsuccess;
  524. if (onerror)
  525. this.onerror = this.onerror ? callBoth(onerror, this.onerror) : onerror;
  526. return res === undefined ?
  527. (res2 === undefined ? undefined : res2) :
  528. (extend(res, res2));
  529. };
  530. }
  531. function reverseStoppableEventChain(f1, f2) {
  532. if (f1 === nop)
  533. return f2;
  534. return function () {
  535. if (f2.apply(this, arguments) === false)
  536. return false;
  537. return f1.apply(this, arguments);
  538. };
  539. }
  540. function promisableChain(f1, f2) {
  541. if (f1 === nop)
  542. return f2;
  543. return function () {
  544. var res = f1.apply(this, arguments);
  545. if (res && typeof res.then === 'function') {
  546. var thiz = this, i = arguments.length, args = new Array(i);
  547. while (i--)
  548. args[i] = arguments[i];
  549. return res.then(function () {
  550. return f2.apply(thiz, args);
  551. });
  552. }
  553. return f2.apply(this, arguments);
  554. };
  555. }
  556. var INTERNAL = {};
  557. var LONG_STACKS_CLIP_LIMIT = 100,
  558. MAX_LONG_STACKS = 20, ZONE_ECHO_LIMIT = 100, _a$1 = typeof Promise === 'undefined' ?
  559. [] :
  560. (function () {
  561. var globalP = Promise.resolve();
  562. if (typeof crypto === 'undefined' || !crypto.subtle)
  563. return [globalP, getProto(globalP), globalP];
  564. var nativeP = crypto.subtle.digest("SHA-512", new Uint8Array([0]));
  565. return [
  566. nativeP,
  567. getProto(nativeP),
  568. globalP
  569. ];
  570. })(), resolvedNativePromise = _a$1[0], nativePromiseProto = _a$1[1], resolvedGlobalPromise = _a$1[2], nativePromiseThen = nativePromiseProto && nativePromiseProto.then;
  571. var NativePromise = resolvedNativePromise && resolvedNativePromise.constructor;
  572. var patchGlobalPromise = !!resolvedGlobalPromise;
  573. var stack_being_generated = false;
  574. var schedulePhysicalTick = resolvedGlobalPromise ?
  575. function () { resolvedGlobalPromise.then(physicalTick); }
  576. :
  577. _global.setImmediate ?
  578. setImmediate.bind(null, physicalTick) :
  579. _global.MutationObserver ?
  580. function () {
  581. var hiddenDiv = document.createElement("div");
  582. (new MutationObserver(function () {
  583. physicalTick();
  584. hiddenDiv = null;
  585. })).observe(hiddenDiv, { attributes: true });
  586. hiddenDiv.setAttribute('i', '1');
  587. } :
  588. function () { setTimeout(physicalTick, 0); };
  589. var asap = function (callback, args) {
  590. microtickQueue.push([callback, args]);
  591. if (needsNewPhysicalTick) {
  592. schedulePhysicalTick();
  593. needsNewPhysicalTick = false;
  594. }
  595. };
  596. var isOutsideMicroTick = true,
  597. needsNewPhysicalTick = true,
  598. unhandledErrors = [],
  599. rejectingErrors = [],
  600. currentFulfiller = null, rejectionMapper = mirror;
  601. var globalPSD = {
  602. id: 'global',
  603. global: true,
  604. ref: 0,
  605. unhandleds: [],
  606. onunhandled: globalError,
  607. pgp: false,
  608. env: {},
  609. finalize: function () {
  610. this.unhandleds.forEach(function (uh) {
  611. try {
  612. globalError(uh[0], uh[1]);
  613. }
  614. catch (e) { }
  615. });
  616. }
  617. };
  618. var PSD = globalPSD;
  619. var microtickQueue = [];
  620. var numScheduledCalls = 0;
  621. var tickFinalizers = [];
  622. function DexiePromise(fn) {
  623. if (typeof this !== 'object')
  624. throw new TypeError('Promises must be constructed via new');
  625. this._listeners = [];
  626. this.onuncatched = nop;
  627. this._lib = false;
  628. var psd = (this._PSD = PSD);
  629. if (debug) {
  630. this._stackHolder = getErrorWithStack();
  631. this._prev = null;
  632. this._numPrev = 0;
  633. }
  634. if (typeof fn !== 'function') {
  635. if (fn !== INTERNAL)
  636. throw new TypeError('Not a function');
  637. this._state = arguments[1];
  638. this._value = arguments[2];
  639. if (this._state === false)
  640. handleRejection(this, this._value);
  641. return;
  642. }
  643. this._state = null;
  644. this._value = null;
  645. ++psd.ref;
  646. executePromiseTask(this, fn);
  647. }
  648. var thenProp = {
  649. get: function () {
  650. var psd = PSD, microTaskId = totalEchoes;
  651. function then(onFulfilled, onRejected) {
  652. var _this = this;
  653. var possibleAwait = !psd.global && (psd !== PSD || microTaskId !== totalEchoes);
  654. var cleanup = possibleAwait && !decrementExpectedAwaits();
  655. var rv = new DexiePromise(function (resolve, reject) {
  656. propagateToListener(_this, new Listener(nativeAwaitCompatibleWrap(onFulfilled, psd, possibleAwait, cleanup), nativeAwaitCompatibleWrap(onRejected, psd, possibleAwait, cleanup), resolve, reject, psd));
  657. });
  658. debug && linkToPreviousPromise(rv, this);
  659. return rv;
  660. }
  661. then.prototype = INTERNAL;
  662. return then;
  663. },
  664. set: function (value) {
  665. setProp(this, 'then', value && value.prototype === INTERNAL ?
  666. thenProp :
  667. {
  668. get: function () {
  669. return value;
  670. },
  671. set: thenProp.set
  672. });
  673. }
  674. };
  675. props(DexiePromise.prototype, {
  676. then: thenProp,
  677. _then: function (onFulfilled, onRejected) {
  678. propagateToListener(this, new Listener(null, null, onFulfilled, onRejected, PSD));
  679. },
  680. catch: function (onRejected) {
  681. if (arguments.length === 1)
  682. return this.then(null, onRejected);
  683. var type = arguments[0], handler = arguments[1];
  684. return typeof type === 'function' ? this.then(null, function (err) {
  685. return err instanceof type ? handler(err) : PromiseReject(err);
  686. })
  687. : this.then(null, function (err) {
  688. return err && err.name === type ? handler(err) : PromiseReject(err);
  689. });
  690. },
  691. finally: function (onFinally) {
  692. return this.then(function (value) {
  693. onFinally();
  694. return value;
  695. }, function (err) {
  696. onFinally();
  697. return PromiseReject(err);
  698. });
  699. },
  700. stack: {
  701. get: function () {
  702. if (this._stack)
  703. return this._stack;
  704. try {
  705. stack_being_generated = true;
  706. var stacks = getStack(this, [], MAX_LONG_STACKS);
  707. var stack = stacks.join("\nFrom previous: ");
  708. if (this._state !== null)
  709. this._stack = stack;
  710. return stack;
  711. }
  712. finally {
  713. stack_being_generated = false;
  714. }
  715. }
  716. },
  717. timeout: function (ms, msg) {
  718. var _this = this;
  719. return ms < Infinity ?
  720. new DexiePromise(function (resolve, reject) {
  721. var handle = setTimeout(function () { return reject(new exceptions.Timeout(msg)); }, ms);
  722. _this.then(resolve, reject).finally(clearTimeout.bind(null, handle));
  723. }) : this;
  724. }
  725. });
  726. if (typeof Symbol !== 'undefined' && Symbol.toStringTag)
  727. setProp(DexiePromise.prototype, Symbol.toStringTag, 'Dexie.Promise');
  728. globalPSD.env = snapShot();
  729. function Listener(onFulfilled, onRejected, resolve, reject, zone) {
  730. this.onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : null;
  731. this.onRejected = typeof onRejected === 'function' ? onRejected : null;
  732. this.resolve = resolve;
  733. this.reject = reject;
  734. this.psd = zone;
  735. }
  736. props(DexiePromise, {
  737. all: function () {
  738. var values = getArrayOf.apply(null, arguments)
  739. .map(onPossibleParallellAsync);
  740. return new DexiePromise(function (resolve, reject) {
  741. if (values.length === 0)
  742. resolve([]);
  743. var remaining = values.length;
  744. values.forEach(function (a, i) { return DexiePromise.resolve(a).then(function (x) {
  745. values[i] = x;
  746. if (!--remaining)
  747. resolve(values);
  748. }, reject); });
  749. });
  750. },
  751. resolve: function (value) {
  752. if (value instanceof DexiePromise)
  753. return value;
  754. if (value && typeof value.then === 'function')
  755. return new DexiePromise(function (resolve, reject) {
  756. value.then(resolve, reject);
  757. });
  758. var rv = new DexiePromise(INTERNAL, true, value);
  759. linkToPreviousPromise(rv, currentFulfiller);
  760. return rv;
  761. },
  762. reject: PromiseReject,
  763. race: function () {
  764. var values = getArrayOf.apply(null, arguments).map(onPossibleParallellAsync);
  765. return new DexiePromise(function (resolve, reject) {
  766. values.map(function (value) { return DexiePromise.resolve(value).then(resolve, reject); });
  767. });
  768. },
  769. PSD: {
  770. get: function () { return PSD; },
  771. set: function (value) { return PSD = value; }
  772. },
  773. totalEchoes: { get: function () { return totalEchoes; } },
  774. newPSD: newScope,
  775. usePSD: usePSD,
  776. scheduler: {
  777. get: function () { return asap; },
  778. set: function (value) { asap = value; }
  779. },
  780. rejectionMapper: {
  781. get: function () { return rejectionMapper; },
  782. set: function (value) { rejectionMapper = value; }
  783. },
  784. follow: function (fn, zoneProps) {
  785. return new DexiePromise(function (resolve, reject) {
  786. return newScope(function (resolve, reject) {
  787. var psd = PSD;
  788. psd.unhandleds = [];
  789. psd.onunhandled = reject;
  790. psd.finalize = callBoth(function () {
  791. var _this = this;
  792. run_at_end_of_this_or_next_physical_tick(function () {
  793. _this.unhandleds.length === 0 ? resolve() : reject(_this.unhandleds[0]);
  794. });
  795. }, psd.finalize);
  796. fn();
  797. }, zoneProps, resolve, reject);
  798. });
  799. }
  800. });
  801. if (NativePromise) {
  802. if (NativePromise.allSettled)
  803. setProp(DexiePromise, "allSettled", function () {
  804. var possiblePromises = getArrayOf.apply(null, arguments).map(onPossibleParallellAsync);
  805. return new DexiePromise(function (resolve) {
  806. if (possiblePromises.length === 0)
  807. resolve([]);
  808. var remaining = possiblePromises.length;
  809. var results = new Array(remaining);
  810. possiblePromises.forEach(function (p, i) { return DexiePromise.resolve(p).then(function (value) { return results[i] = { status: "fulfilled", value: value }; }, function (reason) { return results[i] = { status: "rejected", reason: reason }; })
  811. .then(function () { return --remaining || resolve(results); }); });
  812. });
  813. });
  814. if (NativePromise.any && typeof AggregateError !== 'undefined')
  815. setProp(DexiePromise, "any", function () {
  816. var possiblePromises = getArrayOf.apply(null, arguments).map(onPossibleParallellAsync);
  817. return new DexiePromise(function (resolve, reject) {
  818. if (possiblePromises.length === 0)
  819. reject(new AggregateError([]));
  820. var remaining = possiblePromises.length;
  821. var failures = new Array(remaining);
  822. possiblePromises.forEach(function (p, i) { return DexiePromise.resolve(p).then(function (value) { return resolve(value); }, function (failure) {
  823. failures[i] = failure;
  824. if (!--remaining)
  825. reject(new AggregateError(failures));
  826. }); });
  827. });
  828. });
  829. }
  830. function executePromiseTask(promise, fn) {
  831. try {
  832. fn(function (value) {
  833. if (promise._state !== null)
  834. return;
  835. if (value === promise)
  836. throw new TypeError('A promise cannot be resolved with itself.');
  837. var shouldExecuteTick = promise._lib && beginMicroTickScope();
  838. if (value && typeof value.then === 'function') {
  839. executePromiseTask(promise, function (resolve, reject) {
  840. value instanceof DexiePromise ?
  841. value._then(resolve, reject) :
  842. value.then(resolve, reject);
  843. });
  844. }
  845. else {
  846. promise._state = true;
  847. promise._value = value;
  848. propagateAllListeners(promise);
  849. }
  850. if (shouldExecuteTick)
  851. endMicroTickScope();
  852. }, handleRejection.bind(null, promise));
  853. }
  854. catch (ex) {
  855. handleRejection(promise, ex);
  856. }
  857. }
  858. function handleRejection(promise, reason) {
  859. rejectingErrors.push(reason);
  860. if (promise._state !== null)
  861. return;
  862. var shouldExecuteTick = promise._lib && beginMicroTickScope();
  863. reason = rejectionMapper(reason);
  864. promise._state = false;
  865. promise._value = reason;
  866. debug && reason !== null && typeof reason === 'object' && !reason._promise && tryCatch(function () {
  867. var origProp = getPropertyDescriptor(reason, "stack");
  868. reason._promise = promise;
  869. setProp(reason, "stack", {
  870. get: function () {
  871. return stack_being_generated ?
  872. origProp && (origProp.get ?
  873. origProp.get.apply(reason) :
  874. origProp.value) :
  875. promise.stack;
  876. }
  877. });
  878. });
  879. addPossiblyUnhandledError(promise);
  880. propagateAllListeners(promise);
  881. if (shouldExecuteTick)
  882. endMicroTickScope();
  883. }
  884. function propagateAllListeners(promise) {
  885. var listeners = promise._listeners;
  886. promise._listeners = [];
  887. for (var i = 0, len = listeners.length; i < len; ++i) {
  888. propagateToListener(promise, listeners[i]);
  889. }
  890. var psd = promise._PSD;
  891. --psd.ref || psd.finalize();
  892. if (numScheduledCalls === 0) {
  893. ++numScheduledCalls;
  894. asap(function () {
  895. if (--numScheduledCalls === 0)
  896. finalizePhysicalTick();
  897. }, []);
  898. }
  899. }
  900. function propagateToListener(promise, listener) {
  901. if (promise._state === null) {
  902. promise._listeners.push(listener);
  903. return;
  904. }
  905. var cb = promise._state ? listener.onFulfilled : listener.onRejected;
  906. if (cb === null) {
  907. return (promise._state ? listener.resolve : listener.reject)(promise._value);
  908. }
  909. ++listener.psd.ref;
  910. ++numScheduledCalls;
  911. asap(callListener, [cb, promise, listener]);
  912. }
  913. function callListener(cb, promise, listener) {
  914. try {
  915. currentFulfiller = promise;
  916. var ret, value = promise._value;
  917. if (promise._state) {
  918. ret = cb(value);
  919. }
  920. else {
  921. if (rejectingErrors.length)
  922. rejectingErrors = [];
  923. ret = cb(value);
  924. if (rejectingErrors.indexOf(value) === -1)
  925. markErrorAsHandled(promise);
  926. }
  927. listener.resolve(ret);
  928. }
  929. catch (e) {
  930. listener.reject(e);
  931. }
  932. finally {
  933. currentFulfiller = null;
  934. if (--numScheduledCalls === 0)
  935. finalizePhysicalTick();
  936. --listener.psd.ref || listener.psd.finalize();
  937. }
  938. }
  939. function getStack(promise, stacks, limit) {
  940. if (stacks.length === limit)
  941. return stacks;
  942. var stack = "";
  943. if (promise._state === false) {
  944. var failure = promise._value, errorName, message;
  945. if (failure != null) {
  946. errorName = failure.name || "Error";
  947. message = failure.message || failure;
  948. stack = prettyStack(failure, 0);
  949. }
  950. else {
  951. errorName = failure;
  952. message = "";
  953. }
  954. stacks.push(errorName + (message ? ": " + message : "") + stack);
  955. }
  956. if (debug) {
  957. stack = prettyStack(promise._stackHolder, 2);
  958. if (stack && stacks.indexOf(stack) === -1)
  959. stacks.push(stack);
  960. if (promise._prev)
  961. getStack(promise._prev, stacks, limit);
  962. }
  963. return stacks;
  964. }
  965. function linkToPreviousPromise(promise, prev) {
  966. var numPrev = prev ? prev._numPrev + 1 : 0;
  967. if (numPrev < LONG_STACKS_CLIP_LIMIT) {
  968. promise._prev = prev;
  969. promise._numPrev = numPrev;
  970. }
  971. }
  972. function physicalTick() {
  973. beginMicroTickScope() && endMicroTickScope();
  974. }
  975. function beginMicroTickScope() {
  976. var wasRootExec = isOutsideMicroTick;
  977. isOutsideMicroTick = false;
  978. needsNewPhysicalTick = false;
  979. return wasRootExec;
  980. }
  981. function endMicroTickScope() {
  982. var callbacks, i, l;
  983. do {
  984. while (microtickQueue.length > 0) {
  985. callbacks = microtickQueue;
  986. microtickQueue = [];
  987. l = callbacks.length;
  988. for (i = 0; i < l; ++i) {
  989. var item = callbacks[i];
  990. item[0].apply(null, item[1]);
  991. }
  992. }
  993. } while (microtickQueue.length > 0);
  994. isOutsideMicroTick = true;
  995. needsNewPhysicalTick = true;
  996. }
  997. function finalizePhysicalTick() {
  998. var unhandledErrs = unhandledErrors;
  999. unhandledErrors = [];
  1000. unhandledErrs.forEach(function (p) {
  1001. p._PSD.onunhandled.call(null, p._value, p);
  1002. });
  1003. var finalizers = tickFinalizers.slice(0);
  1004. var i = finalizers.length;
  1005. while (i)
  1006. finalizers[--i]();
  1007. }
  1008. function run_at_end_of_this_or_next_physical_tick(fn) {
  1009. function finalizer() {
  1010. fn();
  1011. tickFinalizers.splice(tickFinalizers.indexOf(finalizer), 1);
  1012. }
  1013. tickFinalizers.push(finalizer);
  1014. ++numScheduledCalls;
  1015. asap(function () {
  1016. if (--numScheduledCalls === 0)
  1017. finalizePhysicalTick();
  1018. }, []);
  1019. }
  1020. function addPossiblyUnhandledError(promise) {
  1021. if (!unhandledErrors.some(function (p) { return p._value === promise._value; }))
  1022. unhandledErrors.push(promise);
  1023. }
  1024. function markErrorAsHandled(promise) {
  1025. var i = unhandledErrors.length;
  1026. while (i)
  1027. if (unhandledErrors[--i]._value === promise._value) {
  1028. unhandledErrors.splice(i, 1);
  1029. return;
  1030. }
  1031. }
  1032. function PromiseReject(reason) {
  1033. return new DexiePromise(INTERNAL, false, reason);
  1034. }
  1035. function wrap(fn, errorCatcher) {
  1036. var psd = PSD;
  1037. return function () {
  1038. var wasRootExec = beginMicroTickScope(), outerScope = PSD;
  1039. try {
  1040. switchToZone(psd, true);
  1041. return fn.apply(this, arguments);
  1042. }
  1043. catch (e) {
  1044. errorCatcher && errorCatcher(e);
  1045. }
  1046. finally {
  1047. switchToZone(outerScope, false);
  1048. if (wasRootExec)
  1049. endMicroTickScope();
  1050. }
  1051. };
  1052. }
  1053. var task = { awaits: 0, echoes: 0, id: 0 };
  1054. var taskCounter = 0;
  1055. var zoneStack = [];
  1056. var zoneEchoes = 0;
  1057. var totalEchoes = 0;
  1058. var zone_id_counter = 0;
  1059. function newScope(fn, props, a1, a2) {
  1060. var parent = PSD, psd = Object.create(parent);
  1061. psd.parent = parent;
  1062. psd.ref = 0;
  1063. psd.global = false;
  1064. psd.id = ++zone_id_counter;
  1065. var globalEnv = globalPSD.env;
  1066. psd.env = patchGlobalPromise ? {
  1067. Promise: DexiePromise,
  1068. PromiseProp: { value: DexiePromise, configurable: true, writable: true },
  1069. all: DexiePromise.all,
  1070. race: DexiePromise.race,
  1071. allSettled: DexiePromise.allSettled,
  1072. any: DexiePromise.any,
  1073. resolve: DexiePromise.resolve,
  1074. reject: DexiePromise.reject,
  1075. nthen: getPatchedPromiseThen(globalEnv.nthen, psd),
  1076. gthen: getPatchedPromiseThen(globalEnv.gthen, psd)
  1077. } : {};
  1078. if (props)
  1079. extend(psd, props);
  1080. ++parent.ref;
  1081. psd.finalize = function () {
  1082. --this.parent.ref || this.parent.finalize();
  1083. };
  1084. var rv = usePSD(psd, fn, a1, a2);
  1085. if (psd.ref === 0)
  1086. psd.finalize();
  1087. return rv;
  1088. }
  1089. function incrementExpectedAwaits() {
  1090. if (!task.id)
  1091. task.id = ++taskCounter;
  1092. ++task.awaits;
  1093. task.echoes += ZONE_ECHO_LIMIT;
  1094. return task.id;
  1095. }
  1096. function decrementExpectedAwaits() {
  1097. if (!task.awaits)
  1098. return false;
  1099. if (--task.awaits === 0)
  1100. task.id = 0;
  1101. task.echoes = task.awaits * ZONE_ECHO_LIMIT;
  1102. return true;
  1103. }
  1104. if (('' + nativePromiseThen).indexOf('[native code]') === -1) {
  1105. incrementExpectedAwaits = decrementExpectedAwaits = nop;
  1106. }
  1107. function onPossibleParallellAsync(possiblePromise) {
  1108. if (task.echoes && possiblePromise && possiblePromise.constructor === NativePromise) {
  1109. incrementExpectedAwaits();
  1110. return possiblePromise.then(function (x) {
  1111. decrementExpectedAwaits();
  1112. return x;
  1113. }, function (e) {
  1114. decrementExpectedAwaits();
  1115. return rejection(e);
  1116. });
  1117. }
  1118. return possiblePromise;
  1119. }
  1120. function zoneEnterEcho(targetZone) {
  1121. ++totalEchoes;
  1122. if (!task.echoes || --task.echoes === 0) {
  1123. task.echoes = task.id = 0;
  1124. }
  1125. zoneStack.push(PSD);
  1126. switchToZone(targetZone, true);
  1127. }
  1128. function zoneLeaveEcho() {
  1129. var zone = zoneStack[zoneStack.length - 1];
  1130. zoneStack.pop();
  1131. switchToZone(zone, false);
  1132. }
  1133. function switchToZone(targetZone, bEnteringZone) {
  1134. var currentZone = PSD;
  1135. if (bEnteringZone ? task.echoes && (!zoneEchoes++ || targetZone !== PSD) : zoneEchoes && (!--zoneEchoes || targetZone !== PSD)) {
  1136. enqueueNativeMicroTask(bEnteringZone ? zoneEnterEcho.bind(null, targetZone) : zoneLeaveEcho);
  1137. }
  1138. if (targetZone === PSD)
  1139. return;
  1140. PSD = targetZone;
  1141. if (currentZone === globalPSD)
  1142. globalPSD.env = snapShot();
  1143. if (patchGlobalPromise) {
  1144. var GlobalPromise_1 = globalPSD.env.Promise;
  1145. var targetEnv = targetZone.env;
  1146. nativePromiseProto.then = targetEnv.nthen;
  1147. GlobalPromise_1.prototype.then = targetEnv.gthen;
  1148. if (currentZone.global || targetZone.global) {
  1149. Object.defineProperty(_global, 'Promise', targetEnv.PromiseProp);
  1150. GlobalPromise_1.all = targetEnv.all;
  1151. GlobalPromise_1.race = targetEnv.race;
  1152. GlobalPromise_1.resolve = targetEnv.resolve;
  1153. GlobalPromise_1.reject = targetEnv.reject;
  1154. if (targetEnv.allSettled)
  1155. GlobalPromise_1.allSettled = targetEnv.allSettled;
  1156. if (targetEnv.any)
  1157. GlobalPromise_1.any = targetEnv.any;
  1158. }
  1159. }
  1160. }
  1161. function snapShot() {
  1162. var GlobalPromise = _global.Promise;
  1163. return patchGlobalPromise ? {
  1164. Promise: GlobalPromise,
  1165. PromiseProp: Object.getOwnPropertyDescriptor(_global, "Promise"),
  1166. all: GlobalPromise.all,
  1167. race: GlobalPromise.race,
  1168. allSettled: GlobalPromise.allSettled,
  1169. any: GlobalPromise.any,
  1170. resolve: GlobalPromise.resolve,
  1171. reject: GlobalPromise.reject,
  1172. nthen: nativePromiseProto.then,
  1173. gthen: GlobalPromise.prototype.then
  1174. } : {};
  1175. }
  1176. function usePSD(psd, fn, a1, a2, a3) {
  1177. var outerScope = PSD;
  1178. try {
  1179. switchToZone(psd, true);
  1180. return fn(a1, a2, a3);
  1181. }
  1182. finally {
  1183. switchToZone(outerScope, false);
  1184. }
  1185. }
  1186. function enqueueNativeMicroTask(job) {
  1187. nativePromiseThen.call(resolvedNativePromise, job);
  1188. }
  1189. function nativeAwaitCompatibleWrap(fn, zone, possibleAwait, cleanup) {
  1190. return typeof fn !== 'function' ? fn : function () {
  1191. var outerZone = PSD;
  1192. if (possibleAwait)
  1193. incrementExpectedAwaits();
  1194. switchToZone(zone, true);
  1195. try {
  1196. return fn.apply(this, arguments);
  1197. }
  1198. finally {
  1199. switchToZone(outerZone, false);
  1200. if (cleanup)
  1201. enqueueNativeMicroTask(decrementExpectedAwaits);
  1202. }
  1203. };
  1204. }
  1205. function getPatchedPromiseThen(origThen, zone) {
  1206. return function (onResolved, onRejected) {
  1207. return origThen.call(this, nativeAwaitCompatibleWrap(onResolved, zone), nativeAwaitCompatibleWrap(onRejected, zone));
  1208. };
  1209. }
  1210. var UNHANDLEDREJECTION = "unhandledrejection";
  1211. function globalError(err, promise) {
  1212. var rv;
  1213. try {
  1214. rv = promise.onuncatched(err);
  1215. }
  1216. catch (e) { }
  1217. if (rv !== false)
  1218. try {
  1219. var event, eventData = { promise: promise, reason: err };
  1220. if (_global.document && document.createEvent) {
  1221. event = document.createEvent('Event');
  1222. event.initEvent(UNHANDLEDREJECTION, true, true);
  1223. extend(event, eventData);
  1224. }
  1225. else if (_global.CustomEvent) {
  1226. event = new CustomEvent(UNHANDLEDREJECTION, { detail: eventData });
  1227. extend(event, eventData);
  1228. }
  1229. if (event && _global.dispatchEvent) {
  1230. dispatchEvent(event);
  1231. if (!_global.PromiseRejectionEvent && _global.onunhandledrejection)
  1232. try {
  1233. _global.onunhandledrejection(event);
  1234. }
  1235. catch (_) { }
  1236. }
  1237. if (debug && event && !event.defaultPrevented) {
  1238. console.warn("Unhandled rejection: " + (err.stack || err));
  1239. }
  1240. }
  1241. catch (e) { }
  1242. }
  1243. var rejection = DexiePromise.reject;
  1244. function tempTransaction(db, mode, storeNames, fn) {
  1245. if (!db.idbdb || (!db._state.openComplete && (!PSD.letThrough && !db._vip))) {
  1246. if (db._state.openComplete) {
  1247. return rejection(new exceptions.DatabaseClosed(db._state.dbOpenError));
  1248. }
  1249. if (!db._state.isBeingOpened) {
  1250. if (!db._options.autoOpen)
  1251. return rejection(new exceptions.DatabaseClosed());
  1252. db.open().catch(nop);
  1253. }
  1254. return db._state.dbReadyPromise.then(function () { return tempTransaction(db, mode, storeNames, fn); });
  1255. }
  1256. else {
  1257. var trans = db._createTransaction(mode, storeNames, db._dbSchema);
  1258. try {
  1259. trans.create();
  1260. db._state.PR1398_maxLoop = 3;
  1261. }
  1262. catch (ex) {
  1263. if (ex.name === errnames.InvalidState && db.isOpen() && --db._state.PR1398_maxLoop > 0) {
  1264. console.warn('Dexie: Need to reopen db');
  1265. db._close();
  1266. return db.open().then(function () { return tempTransaction(db, mode, storeNames, fn); });
  1267. }
  1268. return rejection(ex);
  1269. }
  1270. return trans._promise(mode, function (resolve, reject) {
  1271. return newScope(function () {
  1272. PSD.trans = trans;
  1273. return fn(resolve, reject, trans);
  1274. });
  1275. }).then(function (result) {
  1276. return trans._completion.then(function () { return result; });
  1277. });
  1278. }
  1279. }
  1280. var DEXIE_VERSION = '3.2.3';
  1281. var maxString = String.fromCharCode(65535);
  1282. var minKey = -Infinity;
  1283. var INVALID_KEY_ARGUMENT = "Invalid key provided. Keys must be of type string, number, Date or Array<string | number | Date>.";
  1284. var STRING_EXPECTED = "String expected.";
  1285. var connections = [];
  1286. var isIEOrEdge = typeof navigator !== 'undefined' && /(MSIE|Trident|Edge)/.test(navigator.userAgent);
  1287. var hasIEDeleteObjectStoreBug = isIEOrEdge;
  1288. var hangsOnDeleteLargeKeyRange = isIEOrEdge;
  1289. var dexieStackFrameFilter = function (frame) { return !/(dexie\.js|dexie\.min\.js)/.test(frame); };
  1290. var DBNAMES_DB = '__dbnames';
  1291. var READONLY = 'readonly';
  1292. var READWRITE = 'readwrite';
  1293. function combine(filter1, filter2) {
  1294. return filter1 ?
  1295. filter2 ?
  1296. function () { return filter1.apply(this, arguments) && filter2.apply(this, arguments); } :
  1297. filter1 :
  1298. filter2;
  1299. }
  1300. var AnyRange = {
  1301. type: 3 ,
  1302. lower: -Infinity,
  1303. lowerOpen: false,
  1304. upper: [[]],
  1305. upperOpen: false
  1306. };
  1307. function workaroundForUndefinedPrimKey(keyPath) {
  1308. return typeof keyPath === "string" && !/\./.test(keyPath)
  1309. ? function (obj) {
  1310. if (obj[keyPath] === undefined && (keyPath in obj)) {
  1311. obj = deepClone(obj);
  1312. delete obj[keyPath];
  1313. }
  1314. return obj;
  1315. }
  1316. : function (obj) { return obj; };
  1317. }
  1318. var Table = (function () {
  1319. function Table() {
  1320. }
  1321. Table.prototype._trans = function (mode, fn, writeLocked) {
  1322. var trans = this._tx || PSD.trans;
  1323. var tableName = this.name;
  1324. function checkTableInTransaction(resolve, reject, trans) {
  1325. if (!trans.schema[tableName])
  1326. throw new exceptions.NotFound("Table " + tableName + " not part of transaction");
  1327. return fn(trans.idbtrans, trans);
  1328. }
  1329. var wasRootExec = beginMicroTickScope();
  1330. try {
  1331. return trans && trans.db === this.db ?
  1332. trans === PSD.trans ?
  1333. trans._promise(mode, checkTableInTransaction, writeLocked) :
  1334. newScope(function () { return trans._promise(mode, checkTableInTransaction, writeLocked); }, { trans: trans, transless: PSD.transless || PSD }) :
  1335. tempTransaction(this.db, mode, [this.name], checkTableInTransaction);
  1336. }
  1337. finally {
  1338. if (wasRootExec)
  1339. endMicroTickScope();
  1340. }
  1341. };
  1342. Table.prototype.get = function (keyOrCrit, cb) {
  1343. var _this = this;
  1344. if (keyOrCrit && keyOrCrit.constructor === Object)
  1345. return this.where(keyOrCrit).first(cb);
  1346. return this._trans('readonly', function (trans) {
  1347. return _this.core.get({ trans: trans, key: keyOrCrit })
  1348. .then(function (res) { return _this.hook.reading.fire(res); });
  1349. }).then(cb);
  1350. };
  1351. Table.prototype.where = function (indexOrCrit) {
  1352. if (typeof indexOrCrit === 'string')
  1353. return new this.db.WhereClause(this, indexOrCrit);
  1354. if (isArray(indexOrCrit))
  1355. return new this.db.WhereClause(this, "[" + indexOrCrit.join('+') + "]");
  1356. var keyPaths = keys(indexOrCrit);
  1357. if (keyPaths.length === 1)
  1358. return this
  1359. .where(keyPaths[0])
  1360. .equals(indexOrCrit[keyPaths[0]]);
  1361. var compoundIndex = this.schema.indexes.concat(this.schema.primKey).filter(function (ix) {
  1362. return ix.compound &&
  1363. keyPaths.every(function (keyPath) { return ix.keyPath.indexOf(keyPath) >= 0; }) &&
  1364. ix.keyPath.every(function (keyPath) { return keyPaths.indexOf(keyPath) >= 0; });
  1365. })[0];
  1366. if (compoundIndex && this.db._maxKey !== maxString)
  1367. return this
  1368. .where(compoundIndex.name)
  1369. .equals(compoundIndex.keyPath.map(function (kp) { return indexOrCrit[kp]; }));
  1370. if (!compoundIndex && debug)
  1371. console.warn("The query " + JSON.stringify(indexOrCrit) + " on " + this.name + " would benefit of a " +
  1372. ("compound index [" + keyPaths.join('+') + "]"));
  1373. var idxByName = this.schema.idxByName;
  1374. var idb = this.db._deps.indexedDB;
  1375. function equals(a, b) {
  1376. try {
  1377. return idb.cmp(a, b) === 0;
  1378. }
  1379. catch (e) {
  1380. return false;
  1381. }
  1382. }
  1383. var _a = keyPaths.reduce(function (_a, keyPath) {
  1384. var prevIndex = _a[0], prevFilterFn = _a[1];
  1385. var index = idxByName[keyPath];
  1386. var value = indexOrCrit[keyPath];
  1387. return [
  1388. prevIndex || index,
  1389. prevIndex || !index ?
  1390. combine(prevFilterFn, index && index.multi ?
  1391. function (x) {
  1392. var prop = getByKeyPath(x, keyPath);
  1393. return isArray(prop) && prop.some(function (item) { return equals(value, item); });
  1394. } : function (x) { return equals(value, getByKeyPath(x, keyPath)); })
  1395. : prevFilterFn
  1396. ];
  1397. }, [null, null]), idx = _a[0], filterFunction = _a[1];
  1398. return idx ?
  1399. this.where(idx.name).equals(indexOrCrit[idx.keyPath])
  1400. .filter(filterFunction) :
  1401. compoundIndex ?
  1402. this.filter(filterFunction) :
  1403. this.where(keyPaths).equals('');
  1404. };
  1405. Table.prototype.filter = function (filterFunction) {
  1406. return this.toCollection().and(filterFunction);
  1407. };
  1408. Table.prototype.count = function (thenShortcut) {
  1409. return this.toCollection().count(thenShortcut);
  1410. };
  1411. Table.prototype.offset = function (offset) {
  1412. return this.toCollection().offset(offset);
  1413. };
  1414. Table.prototype.limit = function (numRows) {
  1415. return this.toCollection().limit(numRows);
  1416. };
  1417. Table.prototype.each = function (callback) {
  1418. return this.toCollection().each(callback);
  1419. };
  1420. Table.prototype.toArray = function (thenShortcut) {
  1421. return this.toCollection().toArray(thenShortcut);
  1422. };
  1423. Table.prototype.toCollection = function () {
  1424. return new this.db.Collection(new this.db.WhereClause(this));
  1425. };
  1426. Table.prototype.orderBy = function (index) {
  1427. return new this.db.Collection(new this.db.WhereClause(this, isArray(index) ?
  1428. "[" + index.join('+') + "]" :
  1429. index));
  1430. };
  1431. Table.prototype.reverse = function () {
  1432. return this.toCollection().reverse();
  1433. };
  1434. Table.prototype.mapToClass = function (constructor) {
  1435. this.schema.mappedClass = constructor;
  1436. var readHook = function (obj) {
  1437. if (!obj)
  1438. return obj;
  1439. var res = Object.create(constructor.prototype);
  1440. for (var m in obj)
  1441. if (hasOwn(obj, m))
  1442. try {
  1443. res[m] = obj[m];
  1444. }
  1445. catch (_) { }
  1446. return res;
  1447. };
  1448. if (this.schema.readHook) {
  1449. this.hook.reading.unsubscribe(this.schema.readHook);
  1450. }
  1451. this.schema.readHook = readHook;
  1452. this.hook("reading", readHook);
  1453. return constructor;
  1454. };
  1455. Table.prototype.defineClass = function () {
  1456. function Class(content) {
  1457. extend(this, content);
  1458. }
  1459. return this.mapToClass(Class);
  1460. };
  1461. Table.prototype.add = function (obj, key) {
  1462. var _this = this;
  1463. var _a = this.schema.primKey, auto = _a.auto, keyPath = _a.keyPath;
  1464. var objToAdd = obj;
  1465. if (keyPath && auto) {
  1466. objToAdd = workaroundForUndefinedPrimKey(keyPath)(obj);
  1467. }
  1468. return this._trans('readwrite', function (trans) {
  1469. return _this.core.mutate({ trans: trans, type: 'add', keys: key != null ? [key] : null, values: [objToAdd] });
  1470. }).then(function (res) { return res.numFailures ? DexiePromise.reject(res.failures[0]) : res.lastResult; })
  1471. .then(function (lastResult) {
  1472. if (keyPath) {
  1473. try {
  1474. setByKeyPath(obj, keyPath, lastResult);
  1475. }
  1476. catch (_) { }
  1477. }
  1478. return lastResult;
  1479. });
  1480. };
  1481. Table.prototype.update = function (keyOrObject, modifications) {
  1482. if (typeof keyOrObject === 'object' && !isArray(keyOrObject)) {
  1483. var key = getByKeyPath(keyOrObject, this.schema.primKey.keyPath);
  1484. if (key === undefined)
  1485. return rejection(new exceptions.InvalidArgument("Given object does not contain its primary key"));
  1486. try {
  1487. if (typeof modifications !== "function") {
  1488. keys(modifications).forEach(function (keyPath) {
  1489. setByKeyPath(keyOrObject, keyPath, modifications[keyPath]);
  1490. });
  1491. }
  1492. else {
  1493. modifications(keyOrObject, { value: keyOrObject, primKey: key });
  1494. }
  1495. }
  1496. catch (_a) {
  1497. }
  1498. return this.where(":id").equals(key).modify(modifications);
  1499. }
  1500. else {
  1501. return this.where(":id").equals(keyOrObject).modify(modifications);
  1502. }
  1503. };
  1504. Table.prototype.put = function (obj, key) {
  1505. var _this = this;
  1506. var _a = this.schema.primKey, auto = _a.auto, keyPath = _a.keyPath;
  1507. var objToAdd = obj;
  1508. if (keyPath && auto) {
  1509. objToAdd = workaroundForUndefinedPrimKey(keyPath)(obj);
  1510. }
  1511. return this._trans('readwrite', function (trans) { return _this.core.mutate({ trans: trans, type: 'put', values: [objToAdd], keys: key != null ? [key] : null }); })
  1512. .then(function (res) { return res.numFailures ? DexiePromise.reject(res.failures[0]) : res.lastResult; })
  1513. .then(function (lastResult) {
  1514. if (keyPath) {
  1515. try {
  1516. setByKeyPath(obj, keyPath, lastResult);
  1517. }
  1518. catch (_) { }
  1519. }
  1520. return lastResult;
  1521. });
  1522. };
  1523. Table.prototype.delete = function (key) {
  1524. var _this = this;
  1525. return this._trans('readwrite', function (trans) { return _this.core.mutate({ trans: trans, type: 'delete', keys: [key] }); })
  1526. .then(function (res) { return res.numFailures ? DexiePromise.reject(res.failures[0]) : undefined; });
  1527. };
  1528. Table.prototype.clear = function () {
  1529. var _this = this;
  1530. return this._trans('readwrite', function (trans) { return _this.core.mutate({ trans: trans, type: 'deleteRange', range: AnyRange }); })
  1531. .then(function (res) { return res.numFailures ? DexiePromise.reject(res.failures[0]) : undefined; });
  1532. };
  1533. Table.prototype.bulkGet = function (keys) {
  1534. var _this = this;
  1535. return this._trans('readonly', function (trans) {
  1536. return _this.core.getMany({
  1537. keys: keys,
  1538. trans: trans
  1539. }).then(function (result) { return result.map(function (res) { return _this.hook.reading.fire(res); }); });
  1540. });
  1541. };
  1542. Table.prototype.bulkAdd = function (objects, keysOrOptions, options) {
  1543. var _this = this;
  1544. var keys = Array.isArray(keysOrOptions) ? keysOrOptions : undefined;
  1545. options = options || (keys ? undefined : keysOrOptions);
  1546. var wantResults = options ? options.allKeys : undefined;
  1547. return this._trans('readwrite', function (trans) {
  1548. var _a = _this.schema.primKey, auto = _a.auto, keyPath = _a.keyPath;
  1549. if (keyPath && keys)
  1550. throw new exceptions.InvalidArgument("bulkAdd(): keys argument invalid on tables with inbound keys");
  1551. if (keys && keys.length !== objects.length)
  1552. throw new exceptions.InvalidArgument("Arguments objects and keys must have the same length");
  1553. var numObjects = objects.length;
  1554. var objectsToAdd = keyPath && auto ?
  1555. objects.map(workaroundForUndefinedPrimKey(keyPath)) :
  1556. objects;
  1557. return _this.core.mutate({ trans: trans, type: 'add', keys: keys, values: objectsToAdd, wantResults: wantResults })
  1558. .then(function (_a) {
  1559. var numFailures = _a.numFailures, results = _a.results, lastResult = _a.lastResult, failures = _a.failures;
  1560. var result = wantResults ? results : lastResult;
  1561. if (numFailures === 0)
  1562. return result;
  1563. throw new BulkError(_this.name + ".bulkAdd(): " + numFailures + " of " + numObjects + " operations failed", failures);
  1564. });
  1565. });
  1566. };
  1567. Table.prototype.bulkPut = function (objects, keysOrOptions, options) {
  1568. var _this = this;
  1569. var keys = Array.isArray(keysOrOptions) ? keysOrOptions : undefined;
  1570. options = options || (keys ? undefined : keysOrOptions);
  1571. var wantResults = options ? options.allKeys : undefined;
  1572. return this._trans('readwrite', function (trans) {
  1573. var _a = _this.schema.primKey, auto = _a.auto, keyPath = _a.keyPath;
  1574. if (keyPath && keys)
  1575. throw new exceptions.InvalidArgument("bulkPut(): keys argument invalid on tables with inbound keys");
  1576. if (keys && keys.length !== objects.length)
  1577. throw new exceptions.InvalidArgument("Arguments objects and keys must have the same length");
  1578. var numObjects = objects.length;
  1579. var objectsToPut = keyPath && auto ?
  1580. objects.map(workaroundForUndefinedPrimKey(keyPath)) :
  1581. objects;
  1582. return _this.core.mutate({ trans: trans, type: 'put', keys: keys, values: objectsToPut, wantResults: wantResults })
  1583. .then(function (_a) {
  1584. var numFailures = _a.numFailures, results = _a.results, lastResult = _a.lastResult, failures = _a.failures;
  1585. var result = wantResults ? results : lastResult;
  1586. if (numFailures === 0)
  1587. return result;
  1588. throw new BulkError(_this.name + ".bulkPut(): " + numFailures + " of " + numObjects + " operations failed", failures);
  1589. });
  1590. });
  1591. };
  1592. Table.prototype.bulkDelete = function (keys) {
  1593. var _this = this;
  1594. var numKeys = keys.length;
  1595. return this._trans('readwrite', function (trans) {
  1596. return _this.core.mutate({ trans: trans, type: 'delete', keys: keys });
  1597. }).then(function (_a) {
  1598. var numFailures = _a.numFailures, lastResult = _a.lastResult, failures = _a.failures;
  1599. if (numFailures === 0)
  1600. return lastResult;
  1601. throw new BulkError(_this.name + ".bulkDelete(): " + numFailures + " of " + numKeys + " operations failed", failures);
  1602. });
  1603. };
  1604. return Table;
  1605. }());
  1606. function Events(ctx) {
  1607. var evs = {};
  1608. var rv = function (eventName, subscriber) {
  1609. if (subscriber) {
  1610. var i = arguments.length, args = new Array(i - 1);
  1611. while (--i)
  1612. args[i - 1] = arguments[i];
  1613. evs[eventName].subscribe.apply(null, args);
  1614. return ctx;
  1615. }
  1616. else if (typeof (eventName) === 'string') {
  1617. return evs[eventName];
  1618. }
  1619. };
  1620. rv.addEventType = add;
  1621. for (var i = 1, l = arguments.length; i < l; ++i) {
  1622. add(arguments[i]);
  1623. }
  1624. return rv;
  1625. function add(eventName, chainFunction, defaultFunction) {
  1626. if (typeof eventName === 'object')
  1627. return addConfiguredEvents(eventName);
  1628. if (!chainFunction)
  1629. chainFunction = reverseStoppableEventChain;
  1630. if (!defaultFunction)
  1631. defaultFunction = nop;
  1632. var context = {
  1633. subscribers: [],
  1634. fire: defaultFunction,
  1635. subscribe: function (cb) {
  1636. if (context.subscribers.indexOf(cb) === -1) {
  1637. context.subscribers.push(cb);
  1638. context.fire = chainFunction(context.fire, cb);
  1639. }
  1640. },
  1641. unsubscribe: function (cb) {
  1642. context.subscribers = context.subscribers.filter(function (fn) { return fn !== cb; });
  1643. context.fire = context.subscribers.reduce(chainFunction, defaultFunction);
  1644. }
  1645. };
  1646. evs[eventName] = rv[eventName] = context;
  1647. return context;
  1648. }
  1649. function addConfiguredEvents(cfg) {
  1650. keys(cfg).forEach(function (eventName) {
  1651. var args = cfg[eventName];
  1652. if (isArray(args)) {
  1653. add(eventName, cfg[eventName][0], cfg[eventName][1]);
  1654. }
  1655. else if (args === 'asap') {
  1656. var context = add(eventName, mirror, function fire() {
  1657. var i = arguments.length, args = new Array(i);
  1658. while (i--)
  1659. args[i] = arguments[i];
  1660. context.subscribers.forEach(function (fn) {
  1661. asap$1(function fireEvent() {
  1662. fn.apply(null, args);
  1663. });
  1664. });
  1665. });
  1666. }
  1667. else
  1668. throw new exceptions.InvalidArgument("Invalid event config");
  1669. });
  1670. }
  1671. }
  1672. function makeClassConstructor(prototype, constructor) {
  1673. derive(constructor).from({ prototype: prototype });
  1674. return constructor;
  1675. }
  1676. function createTableConstructor(db) {
  1677. return makeClassConstructor(Table.prototype, function Table(name, tableSchema, trans) {
  1678. this.db = db;
  1679. this._tx = trans;
  1680. this.name = name;
  1681. this.schema = tableSchema;
  1682. this.hook = db._allTables[name] ? db._allTables[name].hook : Events(null, {
  1683. "creating": [hookCreatingChain, nop],
  1684. "reading": [pureFunctionChain, mirror],
  1685. "updating": [hookUpdatingChain, nop],
  1686. "deleting": [hookDeletingChain, nop]
  1687. });
  1688. });
  1689. }
  1690. function isPlainKeyRange(ctx, ignoreLimitFilter) {
  1691. return !(ctx.filter || ctx.algorithm || ctx.or) &&
  1692. (ignoreLimitFilter ? ctx.justLimit : !ctx.replayFilter);
  1693. }
  1694. function addFilter(ctx, fn) {
  1695. ctx.filter = combine(ctx.filter, fn);
  1696. }
  1697. function addReplayFilter(ctx, factory, isLimitFilter) {
  1698. var curr = ctx.replayFilter;
  1699. ctx.replayFilter = curr ? function () { return combine(curr(), factory()); } : factory;
  1700. ctx.justLimit = isLimitFilter && !curr;
  1701. }
  1702. function addMatchFilter(ctx, fn) {
  1703. ctx.isMatch = combine(ctx.isMatch, fn);
  1704. }
  1705. function getIndexOrStore(ctx, coreSchema) {
  1706. if (ctx.isPrimKey)
  1707. return coreSchema.primaryKey;
  1708. var index = coreSchema.getIndexByKeyPath(ctx.index);
  1709. if (!index)
  1710. throw new exceptions.Schema("KeyPath " + ctx.index + " on object store " + coreSchema.name + " is not indexed");
  1711. return index;
  1712. }
  1713. function openCursor(ctx, coreTable, trans) {
  1714. var index = getIndexOrStore(ctx, coreTable.schema);
  1715. return coreTable.openCursor({
  1716. trans: trans,
  1717. values: !ctx.keysOnly,
  1718. reverse: ctx.dir === 'prev',
  1719. unique: !!ctx.unique,
  1720. query: {
  1721. index: index,
  1722. range: ctx.range
  1723. }
  1724. });
  1725. }
  1726. function iter(ctx, fn, coreTrans, coreTable) {
  1727. var filter = ctx.replayFilter ? combine(ctx.filter, ctx.replayFilter()) : ctx.filter;
  1728. if (!ctx.or) {
  1729. return iterate(openCursor(ctx, coreTable, coreTrans), combine(ctx.algorithm, filter), fn, !ctx.keysOnly && ctx.valueMapper);
  1730. }
  1731. else {
  1732. var set_1 = {};
  1733. var union = function (item, cursor, advance) {
  1734. if (!filter || filter(cursor, advance, function (result) { return cursor.stop(result); }, function (err) { return cursor.fail(err); })) {
  1735. var primaryKey = cursor.primaryKey;
  1736. var key = '' + primaryKey;
  1737. if (key === '[object ArrayBuffer]')
  1738. key = '' + new Uint8Array(primaryKey);
  1739. if (!hasOwn(set_1, key)) {
  1740. set_1[key] = true;
  1741. fn(item, cursor, advance);
  1742. }
  1743. }
  1744. };
  1745. return Promise.all([
  1746. ctx.or._iterate(union, coreTrans),
  1747. iterate(openCursor(ctx, coreTable, coreTrans), ctx.algorithm, union, !ctx.keysOnly && ctx.valueMapper)
  1748. ]);
  1749. }
  1750. }
  1751. function iterate(cursorPromise, filter, fn, valueMapper) {
  1752. var mappedFn = valueMapper ? function (x, c, a) { return fn(valueMapper(x), c, a); } : fn;
  1753. var wrappedFn = wrap(mappedFn);
  1754. return cursorPromise.then(function (cursor) {
  1755. if (cursor) {
  1756. return cursor.start(function () {
  1757. var c = function () { return cursor.continue(); };
  1758. if (!filter || filter(cursor, function (advancer) { return c = advancer; }, function (val) { cursor.stop(val); c = nop; }, function (e) { cursor.fail(e); c = nop; }))
  1759. wrappedFn(cursor.value, cursor, function (advancer) { return c = advancer; });
  1760. c();
  1761. });
  1762. }
  1763. });
  1764. }
  1765. function cmp(a, b) {
  1766. try {
  1767. var ta = type(a);
  1768. var tb = type(b);
  1769. if (ta !== tb) {
  1770. if (ta === 'Array')
  1771. return 1;
  1772. if (tb === 'Array')
  1773. return -1;
  1774. if (ta === 'binary')
  1775. return 1;
  1776. if (tb === 'binary')
  1777. return -1;
  1778. if (ta === 'string')
  1779. return 1;
  1780. if (tb === 'string')
  1781. return -1;
  1782. if (ta === 'Date')
  1783. return 1;
  1784. if (tb !== 'Date')
  1785. return NaN;
  1786. return -1;
  1787. }
  1788. switch (ta) {
  1789. case 'number':
  1790. case 'Date':
  1791. case 'string':
  1792. return a > b ? 1 : a < b ? -1 : 0;
  1793. case 'binary': {
  1794. return compareUint8Arrays(getUint8Array(a), getUint8Array(b));
  1795. }
  1796. case 'Array':
  1797. return compareArrays(a, b);
  1798. }
  1799. }
  1800. catch (_a) { }
  1801. return NaN;
  1802. }
  1803. function compareArrays(a, b) {
  1804. var al = a.length;
  1805. var bl = b.length;
  1806. var l = al < bl ? al : bl;
  1807. for (var i = 0; i < l; ++i) {
  1808. var res = cmp(a[i], b[i]);
  1809. if (res !== 0)
  1810. return res;
  1811. }
  1812. return al === bl ? 0 : al < bl ? -1 : 1;
  1813. }
  1814. function compareUint8Arrays(a, b) {
  1815. var al = a.length;
  1816. var bl = b.length;
  1817. var l = al < bl ? al : bl;
  1818. for (var i = 0; i < l; ++i) {
  1819. if (a[i] !== b[i])
  1820. return a[i] < b[i] ? -1 : 1;
  1821. }
  1822. return al === bl ? 0 : al < bl ? -1 : 1;
  1823. }
  1824. function type(x) {
  1825. var t = typeof x;
  1826. if (t !== 'object')
  1827. return t;
  1828. if (ArrayBuffer.isView(x))
  1829. return 'binary';
  1830. var tsTag = toStringTag(x);
  1831. return tsTag === 'ArrayBuffer' ? 'binary' : tsTag;
  1832. }
  1833. function getUint8Array(a) {
  1834. if (a instanceof Uint8Array)
  1835. return a;
  1836. if (ArrayBuffer.isView(a))
  1837. return new Uint8Array(a.buffer, a.byteOffset, a.byteLength);
  1838. return new Uint8Array(a);
  1839. }
  1840. var Collection = (function () {
  1841. function Collection() {
  1842. }
  1843. Collection.prototype._read = function (fn, cb) {
  1844. var ctx = this._ctx;
  1845. return ctx.error ?
  1846. ctx.table._trans(null, rejection.bind(null, ctx.error)) :
  1847. ctx.table._trans('readonly', fn).then(cb);
  1848. };
  1849. Collection.prototype._write = function (fn) {
  1850. var ctx = this._ctx;
  1851. return ctx.error ?
  1852. ctx.table._trans(null, rejection.bind(null, ctx.error)) :
  1853. ctx.table._trans('readwrite', fn, "locked");
  1854. };
  1855. Collection.prototype._addAlgorithm = function (fn) {
  1856. var ctx = this._ctx;
  1857. ctx.algorithm = combine(ctx.algorithm, fn);
  1858. };
  1859. Collection.prototype._iterate = function (fn, coreTrans) {
  1860. return iter(this._ctx, fn, coreTrans, this._ctx.table.core);
  1861. };
  1862. Collection.prototype.clone = function (props) {
  1863. var rv = Object.create(this.constructor.prototype), ctx = Object.create(this._ctx);
  1864. if (props)
  1865. extend(ctx, props);
  1866. rv._ctx = ctx;
  1867. return rv;
  1868. };
  1869. Collection.prototype.raw = function () {
  1870. this._ctx.valueMapper = null;
  1871. return this;
  1872. };
  1873. Collection.prototype.each = function (fn) {
  1874. var ctx = this._ctx;
  1875. return this._read(function (trans) { return iter(ctx, fn, trans, ctx.table.core); });
  1876. };
  1877. Collection.prototype.count = function (cb) {
  1878. var _this = this;
  1879. return this._read(function (trans) {
  1880. var ctx = _this._ctx;
  1881. var coreTable = ctx.table.core;
  1882. if (isPlainKeyRange(ctx, true)) {
  1883. return coreTable.count({
  1884. trans: trans,
  1885. query: {
  1886. index: getIndexOrStore(ctx, coreTable.schema),
  1887. range: ctx.range
  1888. }
  1889. }).then(function (count) { return Math.min(count, ctx.limit); });
  1890. }
  1891. else {
  1892. var count = 0;
  1893. return iter(ctx, function () { ++count; return false; }, trans, coreTable)
  1894. .then(function () { return count; });
  1895. }
  1896. }).then(cb);
  1897. };
  1898. Collection.prototype.sortBy = function (keyPath, cb) {
  1899. var parts = keyPath.split('.').reverse(), lastPart = parts[0], lastIndex = parts.length - 1;
  1900. function getval(obj, i) {
  1901. if (i)
  1902. return getval(obj[parts[i]], i - 1);
  1903. return obj[lastPart];
  1904. }
  1905. var order = this._ctx.dir === "next" ? 1 : -1;
  1906. function sorter(a, b) {
  1907. var aVal = getval(a, lastIndex), bVal = getval(b, lastIndex);
  1908. return aVal < bVal ? -order : aVal > bVal ? order : 0;
  1909. }
  1910. return this.toArray(function (a) {
  1911. return a.sort(sorter);
  1912. }).then(cb);
  1913. };
  1914. Collection.prototype.toArray = function (cb) {
  1915. var _this = this;
  1916. return this._read(function (trans) {
  1917. var ctx = _this._ctx;
  1918. if (ctx.dir === 'next' && isPlainKeyRange(ctx, true) && ctx.limit > 0) {
  1919. var valueMapper_1 = ctx.valueMapper;
  1920. var index = getIndexOrStore(ctx, ctx.table.core.schema);
  1921. return ctx.table.core.query({
  1922. trans: trans,
  1923. limit: ctx.limit,
  1924. values: true,
  1925. query: {
  1926. index: index,
  1927. range: ctx.range
  1928. }
  1929. }).then(function (_a) {
  1930. var result = _a.result;
  1931. return valueMapper_1 ? result.map(valueMapper_1) : result;
  1932. });
  1933. }
  1934. else {
  1935. var a_1 = [];
  1936. return iter(ctx, function (item) { return a_1.push(item); }, trans, ctx.table.core).then(function () { return a_1; });
  1937. }
  1938. }, cb);
  1939. };
  1940. Collection.prototype.offset = function (offset) {
  1941. var ctx = this._ctx;
  1942. if (offset <= 0)
  1943. return this;
  1944. ctx.offset += offset;
  1945. if (isPlainKeyRange(ctx)) {
  1946. addReplayFilter(ctx, function () {
  1947. var offsetLeft = offset;
  1948. return function (cursor, advance) {
  1949. if (offsetLeft === 0)
  1950. return true;
  1951. if (offsetLeft === 1) {
  1952. --offsetLeft;
  1953. return false;
  1954. }
  1955. advance(function () {
  1956. cursor.advance(offsetLeft);
  1957. offsetLeft = 0;
  1958. });
  1959. return false;
  1960. };
  1961. });
  1962. }
  1963. else {
  1964. addReplayFilter(ctx, function () {
  1965. var offsetLeft = offset;
  1966. return function () { return (--offsetLeft < 0); };
  1967. });
  1968. }
  1969. return this;
  1970. };
  1971. Collection.prototype.limit = function (numRows) {
  1972. this._ctx.limit = Math.min(this._ctx.limit, numRows);
  1973. addReplayFilter(this._ctx, function () {
  1974. var rowsLeft = numRows;
  1975. return function (cursor, advance, resolve) {
  1976. if (--rowsLeft <= 0)
  1977. advance(resolve);
  1978. return rowsLeft >= 0;
  1979. };
  1980. }, true);
  1981. return this;
  1982. };
  1983. Collection.prototype.until = function (filterFunction, bIncludeStopEntry) {
  1984. addFilter(this._ctx, function (cursor, advance, resolve) {
  1985. if (filterFunction(cursor.value)) {
  1986. advance(resolve);
  1987. return bIncludeStopEntry;
  1988. }
  1989. else {
  1990. return true;
  1991. }
  1992. });
  1993. return this;
  1994. };
  1995. Collection.prototype.first = function (cb) {
  1996. return this.limit(1).toArray(function (a) { return a[0]; }).then(cb);
  1997. };
  1998. Collection.prototype.last = function (cb) {
  1999. return this.reverse().first(cb);
  2000. };
  2001. Collection.prototype.filter = function (filterFunction) {
  2002. addFilter(this._ctx, function (cursor) {
  2003. return filterFunction(cursor.value);
  2004. });
  2005. addMatchFilter(this._ctx, filterFunction);
  2006. return this;
  2007. };
  2008. Collection.prototype.and = function (filter) {
  2009. return this.filter(filter);
  2010. };
  2011. Collection.prototype.or = function (indexName) {
  2012. return new this.db.WhereClause(this._ctx.table, indexName, this);
  2013. };
  2014. Collection.prototype.reverse = function () {
  2015. this._ctx.dir = (this._ctx.dir === "prev" ? "next" : "prev");
  2016. if (this._ondirectionchange)
  2017. this._ondirectionchange(this._ctx.dir);
  2018. return this;
  2019. };
  2020. Collection.prototype.desc = function () {
  2021. return this.reverse();
  2022. };
  2023. Collection.prototype.eachKey = function (cb) {
  2024. var ctx = this._ctx;
  2025. ctx.keysOnly = !ctx.isMatch;
  2026. return this.each(function (val, cursor) { cb(cursor.key, cursor); });
  2027. };
  2028. Collection.prototype.eachUniqueKey = function (cb) {
  2029. this._ctx.unique = "unique";
  2030. return this.eachKey(cb);
  2031. };
  2032. Collection.prototype.eachPrimaryKey = function (cb) {
  2033. var ctx = this._ctx;
  2034. ctx.keysOnly = !ctx.isMatch;
  2035. return this.each(function (val, cursor) { cb(cursor.primaryKey, cursor); });
  2036. };
  2037. Collection.prototype.keys = function (cb) {
  2038. var ctx = this._ctx;
  2039. ctx.keysOnly = !ctx.isMatch;
  2040. var a = [];
  2041. return this.each(function (item, cursor) {
  2042. a.push(cursor.key);
  2043. }).then(function () {
  2044. return a;
  2045. }).then(cb);
  2046. };
  2047. Collection.prototype.primaryKeys = function (cb) {
  2048. var ctx = this._ctx;
  2049. if (ctx.dir === 'next' && isPlainKeyRange(ctx, true) && ctx.limit > 0) {
  2050. return this._read(function (trans) {
  2051. var index = getIndexOrStore(ctx, ctx.table.core.schema);
  2052. return ctx.table.core.query({
  2053. trans: trans,
  2054. values: false,
  2055. limit: ctx.limit,
  2056. query: {
  2057. index: index,
  2058. range: ctx.range
  2059. }
  2060. });
  2061. }).then(function (_a) {
  2062. var result = _a.result;
  2063. return result;
  2064. }).then(cb);
  2065. }
  2066. ctx.keysOnly = !ctx.isMatch;
  2067. var a = [];
  2068. return this.each(function (item, cursor) {
  2069. a.push(cursor.primaryKey);
  2070. }).then(function () {
  2071. return a;
  2072. }).then(cb);
  2073. };
  2074. Collection.prototype.uniqueKeys = function (cb) {
  2075. this._ctx.unique = "unique";
  2076. return this.keys(cb);
  2077. };
  2078. Collection.prototype.firstKey = function (cb) {
  2079. return this.limit(1).keys(function (a) { return a[0]; }).then(cb);
  2080. };
  2081. Collection.prototype.lastKey = function (cb) {
  2082. return this.reverse().firstKey(cb);
  2083. };
  2084. Collection.prototype.distinct = function () {
  2085. var ctx = this._ctx, idx = ctx.index && ctx.table.schema.idxByName[ctx.index];
  2086. if (!idx || !idx.multi)
  2087. return this;
  2088. var set = {};
  2089. addFilter(this._ctx, function (cursor) {
  2090. var strKey = cursor.primaryKey.toString();
  2091. var found = hasOwn(set, strKey);
  2092. set[strKey] = true;
  2093. return !found;
  2094. });
  2095. return this;
  2096. };
  2097. Collection.prototype.modify = function (changes) {
  2098. var _this = this;
  2099. var ctx = this._ctx;
  2100. return this._write(function (trans) {
  2101. var modifyer;
  2102. if (typeof changes === 'function') {
  2103. modifyer = changes;
  2104. }
  2105. else {
  2106. var keyPaths = keys(changes);
  2107. var numKeys = keyPaths.length;
  2108. modifyer = function (item) {
  2109. var anythingModified = false;
  2110. for (var i = 0; i < numKeys; ++i) {
  2111. var keyPath = keyPaths[i], val = changes[keyPath];
  2112. if (getByKeyPath(item, keyPath) !== val) {
  2113. setByKeyPath(item, keyPath, val);
  2114. anythingModified = true;
  2115. }
  2116. }
  2117. return anythingModified;
  2118. };
  2119. }
  2120. var coreTable = ctx.table.core;
  2121. var _a = coreTable.schema.primaryKey, outbound = _a.outbound, extractKey = _a.extractKey;
  2122. var limit = _this.db._options.modifyChunkSize || 200;
  2123. var totalFailures = [];
  2124. var successCount = 0;
  2125. var failedKeys = [];
  2126. var applyMutateResult = function (expectedCount, res) {
  2127. var failures = res.failures, numFailures = res.numFailures;
  2128. successCount += expectedCount - numFailures;
  2129. for (var _i = 0, _a = keys(failures); _i < _a.length; _i++) {
  2130. var pos = _a[_i];
  2131. totalFailures.push(failures[pos]);
  2132. }
  2133. };
  2134. return _this.clone().primaryKeys().then(function (keys) {
  2135. var nextChunk = function (offset) {
  2136. var count = Math.min(limit, keys.length - offset);
  2137. return coreTable.getMany({
  2138. trans: trans,
  2139. keys: keys.slice(offset, offset + count),
  2140. cache: "immutable"
  2141. }).then(function (values) {
  2142. var addValues = [];
  2143. var putValues = [];
  2144. var putKeys = outbound ? [] : null;
  2145. var deleteKeys = [];
  2146. for (var i = 0; i < count; ++i) {
  2147. var origValue = values[i];
  2148. var ctx_1 = {
  2149. value: deepClone(origValue),
  2150. primKey: keys[offset + i]
  2151. };
  2152. if (modifyer.call(ctx_1, ctx_1.value, ctx_1) !== false) {
  2153. if (ctx_1.value == null) {
  2154. deleteKeys.push(keys[offset + i]);
  2155. }
  2156. else if (!outbound && cmp(extractKey(origValue), extractKey(ctx_1.value)) !== 0) {
  2157. deleteKeys.push(keys[offset + i]);
  2158. addValues.push(ctx_1.value);
  2159. }
  2160. else {
  2161. putValues.push(ctx_1.value);
  2162. if (outbound)
  2163. putKeys.push(keys[offset + i]);
  2164. }
  2165. }
  2166. }
  2167. var criteria = isPlainKeyRange(ctx) &&
  2168. ctx.limit === Infinity &&
  2169. (typeof changes !== 'function' || changes === deleteCallback) && {
  2170. index: ctx.index,
  2171. range: ctx.range
  2172. };
  2173. return Promise.resolve(addValues.length > 0 &&
  2174. coreTable.mutate({ trans: trans, type: 'add', values: addValues })
  2175. .then(function (res) {
  2176. for (var pos in res.failures) {
  2177. deleteKeys.splice(parseInt(pos), 1);
  2178. }
  2179. applyMutateResult(addValues.length, res);
  2180. })).then(function () { return (putValues.length > 0 || (criteria && typeof changes === 'object')) &&
  2181. coreTable.mutate({
  2182. trans: trans,
  2183. type: 'put',
  2184. keys: putKeys,
  2185. values: putValues,
  2186. criteria: criteria,
  2187. changeSpec: typeof changes !== 'function'
  2188. && changes
  2189. }).then(function (res) { return applyMutateResult(putValues.length, res); }); }).then(function () { return (deleteKeys.length > 0 || (criteria && changes === deleteCallback)) &&
  2190. coreTable.mutate({
  2191. trans: trans,
  2192. type: 'delete',
  2193. keys: deleteKeys,
  2194. criteria: criteria
  2195. }).then(function (res) { return applyMutateResult(deleteKeys.length, res); }); }).then(function () {
  2196. return keys.length > offset + count && nextChunk(offset + limit);
  2197. });
  2198. });
  2199. };
  2200. return nextChunk(0).then(function () {
  2201. if (totalFailures.length > 0)
  2202. throw new ModifyError("Error modifying one or more objects", totalFailures, successCount, failedKeys);
  2203. return keys.length;
  2204. });
  2205. });
  2206. });
  2207. };
  2208. Collection.prototype.delete = function () {
  2209. var ctx = this._ctx, range = ctx.range;
  2210. if (isPlainKeyRange(ctx) &&
  2211. ((ctx.isPrimKey && !hangsOnDeleteLargeKeyRange) || range.type === 3 ))
  2212. {
  2213. return this._write(function (trans) {
  2214. var primaryKey = ctx.table.core.schema.primaryKey;
  2215. var coreRange = range;
  2216. return ctx.table.core.count({ trans: trans, query: { index: primaryKey, range: coreRange } }).then(function (count) {
  2217. return ctx.table.core.mutate({ trans: trans, type: 'deleteRange', range: coreRange })
  2218. .then(function (_a) {
  2219. var failures = _a.failures; _a.lastResult; _a.results; var numFailures = _a.numFailures;
  2220. if (numFailures)
  2221. throw new ModifyError("Could not delete some values", Object.keys(failures).map(function (pos) { return failures[pos]; }), count - numFailures);
  2222. return count - numFailures;
  2223. });
  2224. });
  2225. });
  2226. }
  2227. return this.modify(deleteCallback);
  2228. };
  2229. return Collection;
  2230. }());
  2231. var deleteCallback = function (value, ctx) { return ctx.value = null; };
  2232. function createCollectionConstructor(db) {
  2233. return makeClassConstructor(Collection.prototype, function Collection(whereClause, keyRangeGenerator) {
  2234. this.db = db;
  2235. var keyRange = AnyRange, error = null;
  2236. if (keyRangeGenerator)
  2237. try {
  2238. keyRange = keyRangeGenerator();
  2239. }
  2240. catch (ex) {
  2241. error = ex;
  2242. }
  2243. var whereCtx = whereClause._ctx;
  2244. var table = whereCtx.table;
  2245. var readingHook = table.hook.reading.fire;
  2246. this._ctx = {
  2247. table: table,
  2248. index: whereCtx.index,
  2249. isPrimKey: (!whereCtx.index || (table.schema.primKey.keyPath && whereCtx.index === table.schema.primKey.name)),
  2250. range: keyRange,
  2251. keysOnly: false,
  2252. dir: "next",
  2253. unique: "",
  2254. algorithm: null,
  2255. filter: null,
  2256. replayFilter: null,
  2257. justLimit: true,
  2258. isMatch: null,
  2259. offset: 0,
  2260. limit: Infinity,
  2261. error: error,
  2262. or: whereCtx.or,
  2263. valueMapper: readingHook !== mirror ? readingHook : null
  2264. };
  2265. });
  2266. }
  2267. function simpleCompare(a, b) {
  2268. return a < b ? -1 : a === b ? 0 : 1;
  2269. }
  2270. function simpleCompareReverse(a, b) {
  2271. return a > b ? -1 : a === b ? 0 : 1;
  2272. }
  2273. function fail(collectionOrWhereClause, err, T) {
  2274. var collection = collectionOrWhereClause instanceof WhereClause ?
  2275. new collectionOrWhereClause.Collection(collectionOrWhereClause) :
  2276. collectionOrWhereClause;
  2277. collection._ctx.error = T ? new T(err) : new TypeError(err);
  2278. return collection;
  2279. }
  2280. function emptyCollection(whereClause) {
  2281. return new whereClause.Collection(whereClause, function () { return rangeEqual(""); }).limit(0);
  2282. }
  2283. function upperFactory(dir) {
  2284. return dir === "next" ?
  2285. function (s) { return s.toUpperCase(); } :
  2286. function (s) { return s.toLowerCase(); };
  2287. }
  2288. function lowerFactory(dir) {
  2289. return dir === "next" ?
  2290. function (s) { return s.toLowerCase(); } :
  2291. function (s) { return s.toUpperCase(); };
  2292. }
  2293. function nextCasing(key, lowerKey, upperNeedle, lowerNeedle, cmp, dir) {
  2294. var length = Math.min(key.length, lowerNeedle.length);
  2295. var llp = -1;
  2296. for (var i = 0; i < length; ++i) {
  2297. var lwrKeyChar = lowerKey[i];
  2298. if (lwrKeyChar !== lowerNeedle[i]) {
  2299. if (cmp(key[i], upperNeedle[i]) < 0)
  2300. return key.substr(0, i) + upperNeedle[i] + upperNeedle.substr(i + 1);
  2301. if (cmp(key[i], lowerNeedle[i]) < 0)
  2302. return key.substr(0, i) + lowerNeedle[i] + upperNeedle.substr(i + 1);
  2303. if (llp >= 0)
  2304. return key.substr(0, llp) + lowerKey[llp] + upperNeedle.substr(llp + 1);
  2305. return null;
  2306. }
  2307. if (cmp(key[i], lwrKeyChar) < 0)
  2308. llp = i;
  2309. }
  2310. if (length < lowerNeedle.length && dir === "next")
  2311. return key + upperNeedle.substr(key.length);
  2312. if (length < key.length && dir === "prev")
  2313. return key.substr(0, upperNeedle.length);
  2314. return (llp < 0 ? null : key.substr(0, llp) + lowerNeedle[llp] + upperNeedle.substr(llp + 1));
  2315. }
  2316. function addIgnoreCaseAlgorithm(whereClause, match, needles, suffix) {
  2317. var upper, lower, compare, upperNeedles, lowerNeedles, direction, nextKeySuffix, needlesLen = needles.length;
  2318. if (!needles.every(function (s) { return typeof s === 'string'; })) {
  2319. return fail(whereClause, STRING_EXPECTED);
  2320. }
  2321. function initDirection(dir) {
  2322. upper = upperFactory(dir);
  2323. lower = lowerFactory(dir);
  2324. compare = (dir === "next" ? simpleCompare : simpleCompareReverse);
  2325. var needleBounds = needles.map(function (needle) {
  2326. return { lower: lower(needle), upper: upper(needle) };
  2327. }).sort(function (a, b) {
  2328. return compare(a.lower, b.lower);
  2329. });
  2330. upperNeedles = needleBounds.map(function (nb) { return nb.upper; });
  2331. lowerNeedles = needleBounds.map(function (nb) { return nb.lower; });
  2332. direction = dir;
  2333. nextKeySuffix = (dir === "next" ? "" : suffix);
  2334. }
  2335. initDirection("next");
  2336. var c = new whereClause.Collection(whereClause, function () { return createRange(upperNeedles[0], lowerNeedles[needlesLen - 1] + suffix); });
  2337. c._ondirectionchange = function (direction) {
  2338. initDirection(direction);
  2339. };
  2340. var firstPossibleNeedle = 0;
  2341. c._addAlgorithm(function (cursor, advance, resolve) {
  2342. var key = cursor.key;
  2343. if (typeof key !== 'string')
  2344. return false;
  2345. var lowerKey = lower(key);
  2346. if (match(lowerKey, lowerNeedles, firstPossibleNeedle)) {
  2347. return true;
  2348. }
  2349. else {
  2350. var lowestPossibleCasing = null;
  2351. for (var i = firstPossibleNeedle; i < needlesLen; ++i) {
  2352. var casing = nextCasing(key, lowerKey, upperNeedles[i], lowerNeedles[i], compare, direction);
  2353. if (casing === null && lowestPossibleCasing === null)
  2354. firstPossibleNeedle = i + 1;
  2355. else if (lowestPossibleCasing === null || compare(lowestPossibleCasing, casing) > 0) {
  2356. lowestPossibleCasing = casing;
  2357. }
  2358. }
  2359. if (lowestPossibleCasing !== null) {
  2360. advance(function () { cursor.continue(lowestPossibleCasing + nextKeySuffix); });
  2361. }
  2362. else {
  2363. advance(resolve);
  2364. }
  2365. return false;
  2366. }
  2367. });
  2368. return c;
  2369. }
  2370. function createRange(lower, upper, lowerOpen, upperOpen) {
  2371. return {
  2372. type: 2 ,
  2373. lower: lower,
  2374. upper: upper,
  2375. lowerOpen: lowerOpen,
  2376. upperOpen: upperOpen
  2377. };
  2378. }
  2379. function rangeEqual(value) {
  2380. return {
  2381. type: 1 ,
  2382. lower: value,
  2383. upper: value
  2384. };
  2385. }
  2386. var WhereClause = (function () {
  2387. function WhereClause() {
  2388. }
  2389. Object.defineProperty(WhereClause.prototype, "Collection", {
  2390. get: function () {
  2391. return this._ctx.table.db.Collection;
  2392. },
  2393. enumerable: false,
  2394. configurable: true
  2395. });
  2396. WhereClause.prototype.between = function (lower, upper, includeLower, includeUpper) {
  2397. includeLower = includeLower !== false;
  2398. includeUpper = includeUpper === true;
  2399. try {
  2400. if ((this._cmp(lower, upper) > 0) ||
  2401. (this._cmp(lower, upper) === 0 && (includeLower || includeUpper) && !(includeLower && includeUpper)))
  2402. return emptyCollection(this);
  2403. return new this.Collection(this, function () { return createRange(lower, upper, !includeLower, !includeUpper); });
  2404. }
  2405. catch (e) {
  2406. return fail(this, INVALID_KEY_ARGUMENT);
  2407. }
  2408. };
  2409. WhereClause.prototype.equals = function (value) {
  2410. if (value == null)
  2411. return fail(this, INVALID_KEY_ARGUMENT);
  2412. return new this.Collection(this, function () { return rangeEqual(value); });
  2413. };
  2414. WhereClause.prototype.above = function (value) {
  2415. if (value == null)
  2416. return fail(this, INVALID_KEY_ARGUMENT);
  2417. return new this.Collection(this, function () { return createRange(value, undefined, true); });
  2418. };
  2419. WhereClause.prototype.aboveOrEqual = function (value) {
  2420. if (value == null)
  2421. return fail(this, INVALID_KEY_ARGUMENT);
  2422. return new this.Collection(this, function () { return createRange(value, undefined, false); });
  2423. };
  2424. WhereClause.prototype.below = function (value) {
  2425. if (value == null)
  2426. return fail(this, INVALID_KEY_ARGUMENT);
  2427. return new this.Collection(this, function () { return createRange(undefined, value, false, true); });
  2428. };
  2429. WhereClause.prototype.belowOrEqual = function (value) {
  2430. if (value == null)
  2431. return fail(this, INVALID_KEY_ARGUMENT);
  2432. return new this.Collection(this, function () { return createRange(undefined, value); });
  2433. };
  2434. WhereClause.prototype.startsWith = function (str) {
  2435. if (typeof str !== 'string')
  2436. return fail(this, STRING_EXPECTED);
  2437. return this.between(str, str + maxString, true, true);
  2438. };
  2439. WhereClause.prototype.startsWithIgnoreCase = function (str) {
  2440. if (str === "")
  2441. return this.startsWith(str);
  2442. return addIgnoreCaseAlgorithm(this, function (x, a) { return x.indexOf(a[0]) === 0; }, [str], maxString);
  2443. };
  2444. WhereClause.prototype.equalsIgnoreCase = function (str) {
  2445. return addIgnoreCaseAlgorithm(this, function (x, a) { return x === a[0]; }, [str], "");
  2446. };
  2447. WhereClause.prototype.anyOfIgnoreCase = function () {
  2448. var set = getArrayOf.apply(NO_CHAR_ARRAY, arguments);
  2449. if (set.length === 0)
  2450. return emptyCollection(this);
  2451. return addIgnoreCaseAlgorithm(this, function (x, a) { return a.indexOf(x) !== -1; }, set, "");
  2452. };
  2453. WhereClause.prototype.startsWithAnyOfIgnoreCase = function () {
  2454. var set = getArrayOf.apply(NO_CHAR_ARRAY, arguments);
  2455. if (set.length === 0)
  2456. return emptyCollection(this);
  2457. return addIgnoreCaseAlgorithm(this, function (x, a) { return a.some(function (n) { return x.indexOf(n) === 0; }); }, set, maxString);
  2458. };
  2459. WhereClause.prototype.anyOf = function () {
  2460. var _this = this;
  2461. var set = getArrayOf.apply(NO_CHAR_ARRAY, arguments);
  2462. var compare = this._cmp;
  2463. try {
  2464. set.sort(compare);
  2465. }
  2466. catch (e) {
  2467. return fail(this, INVALID_KEY_ARGUMENT);
  2468. }
  2469. if (set.length === 0)
  2470. return emptyCollection(this);
  2471. var c = new this.Collection(this, function () { return createRange(set[0], set[set.length - 1]); });
  2472. c._ondirectionchange = function (direction) {
  2473. compare = (direction === "next" ?
  2474. _this._ascending :
  2475. _this._descending);
  2476. set.sort(compare);
  2477. };
  2478. var i = 0;
  2479. c._addAlgorithm(function (cursor, advance, resolve) {
  2480. var key = cursor.key;
  2481. while (compare(key, set[i]) > 0) {
  2482. ++i;
  2483. if (i === set.length) {
  2484. advance(resolve);
  2485. return false;
  2486. }
  2487. }
  2488. if (compare(key, set[i]) === 0) {
  2489. return true;
  2490. }
  2491. else {
  2492. advance(function () { cursor.continue(set[i]); });
  2493. return false;
  2494. }
  2495. });
  2496. return c;
  2497. };
  2498. WhereClause.prototype.notEqual = function (value) {
  2499. return this.inAnyRange([[minKey, value], [value, this.db._maxKey]], { includeLowers: false, includeUppers: false });
  2500. };
  2501. WhereClause.prototype.noneOf = function () {
  2502. var set = getArrayOf.apply(NO_CHAR_ARRAY, arguments);
  2503. if (set.length === 0)
  2504. return new this.Collection(this);
  2505. try {
  2506. set.sort(this._ascending);
  2507. }
  2508. catch (e) {
  2509. return fail(this, INVALID_KEY_ARGUMENT);
  2510. }
  2511. var ranges = set.reduce(function (res, val) { return res ?
  2512. res.concat([[res[res.length - 1][1], val]]) :
  2513. [[minKey, val]]; }, null);
  2514. ranges.push([set[set.length - 1], this.db._maxKey]);
  2515. return this.inAnyRange(ranges, { includeLowers: false, includeUppers: false });
  2516. };
  2517. WhereClause.prototype.inAnyRange = function (ranges, options) {
  2518. var _this = this;
  2519. var cmp = this._cmp, ascending = this._ascending, descending = this._descending, min = this._min, max = this._max;
  2520. if (ranges.length === 0)
  2521. return emptyCollection(this);
  2522. if (!ranges.every(function (range) {
  2523. return range[0] !== undefined &&
  2524. range[1] !== undefined &&
  2525. ascending(range[0], range[1]) <= 0;
  2526. })) {
  2527. return fail(this, "First argument to inAnyRange() must be an Array of two-value Arrays [lower,upper] where upper must not be lower than lower", exceptions.InvalidArgument);
  2528. }
  2529. var includeLowers = !options || options.includeLowers !== false;
  2530. var includeUppers = options && options.includeUppers === true;
  2531. function addRange(ranges, newRange) {
  2532. var i = 0, l = ranges.length;
  2533. for (; i < l; ++i) {
  2534. var range = ranges[i];
  2535. if (cmp(newRange[0], range[1]) < 0 && cmp(newRange[1], range[0]) > 0) {
  2536. range[0] = min(range[0], newRange[0]);
  2537. range[1] = max(range[1], newRange[1]);
  2538. break;
  2539. }
  2540. }
  2541. if (i === l)
  2542. ranges.push(newRange);
  2543. return ranges;
  2544. }
  2545. var sortDirection = ascending;
  2546. function rangeSorter(a, b) { return sortDirection(a[0], b[0]); }
  2547. var set;
  2548. try {
  2549. set = ranges.reduce(addRange, []);
  2550. set.sort(rangeSorter);
  2551. }
  2552. catch (ex) {
  2553. return fail(this, INVALID_KEY_ARGUMENT);
  2554. }
  2555. var rangePos = 0;
  2556. var keyIsBeyondCurrentEntry = includeUppers ?
  2557. function (key) { return ascending(key, set[rangePos][1]) > 0; } :
  2558. function (key) { return ascending(key, set[rangePos][1]) >= 0; };
  2559. var keyIsBeforeCurrentEntry = includeLowers ?
  2560. function (key) { return descending(key, set[rangePos][0]) > 0; } :
  2561. function (key) { return descending(key, set[rangePos][0]) >= 0; };
  2562. function keyWithinCurrentRange(key) {
  2563. return !keyIsBeyondCurrentEntry(key) && !keyIsBeforeCurrentEntry(key);
  2564. }
  2565. var checkKey = keyIsBeyondCurrentEntry;
  2566. var c = new this.Collection(this, function () { return createRange(set[0][0], set[set.length - 1][1], !includeLowers, !includeUppers); });
  2567. c._ondirectionchange = function (direction) {
  2568. if (direction === "next") {
  2569. checkKey = keyIsBeyondCurrentEntry;
  2570. sortDirection = ascending;
  2571. }
  2572. else {
  2573. checkKey = keyIsBeforeCurrentEntry;
  2574. sortDirection = descending;
  2575. }
  2576. set.sort(rangeSorter);
  2577. };
  2578. c._addAlgorithm(function (cursor, advance, resolve) {
  2579. var key = cursor.key;
  2580. while (checkKey(key)) {
  2581. ++rangePos;
  2582. if (rangePos === set.length) {
  2583. advance(resolve);
  2584. return false;
  2585. }
  2586. }
  2587. if (keyWithinCurrentRange(key)) {
  2588. return true;
  2589. }
  2590. else if (_this._cmp(key, set[rangePos][1]) === 0 || _this._cmp(key, set[rangePos][0]) === 0) {
  2591. return false;
  2592. }
  2593. else {
  2594. advance(function () {
  2595. if (sortDirection === ascending)
  2596. cursor.continue(set[rangePos][0]);
  2597. else
  2598. cursor.continue(set[rangePos][1]);
  2599. });
  2600. return false;
  2601. }
  2602. });
  2603. return c;
  2604. };
  2605. WhereClause.prototype.startsWithAnyOf = function () {
  2606. var set = getArrayOf.apply(NO_CHAR_ARRAY, arguments);
  2607. if (!set.every(function (s) { return typeof s === 'string'; })) {
  2608. return fail(this, "startsWithAnyOf() only works with strings");
  2609. }
  2610. if (set.length === 0)
  2611. return emptyCollection(this);
  2612. return this.inAnyRange(set.map(function (str) { return [str, str + maxString]; }));
  2613. };
  2614. return WhereClause;
  2615. }());
  2616. function createWhereClauseConstructor(db) {
  2617. return makeClassConstructor(WhereClause.prototype, function WhereClause(table, index, orCollection) {
  2618. this.db = db;
  2619. this._ctx = {
  2620. table: table,
  2621. index: index === ":id" ? null : index,
  2622. or: orCollection
  2623. };
  2624. var indexedDB = db._deps.indexedDB;
  2625. if (!indexedDB)
  2626. throw new exceptions.MissingAPI();
  2627. this._cmp = this._ascending = indexedDB.cmp.bind(indexedDB);
  2628. this._descending = function (a, b) { return indexedDB.cmp(b, a); };
  2629. this._max = function (a, b) { return indexedDB.cmp(a, b) > 0 ? a : b; };
  2630. this._min = function (a, b) { return indexedDB.cmp(a, b) < 0 ? a : b; };
  2631. this._IDBKeyRange = db._deps.IDBKeyRange;
  2632. });
  2633. }
  2634. function eventRejectHandler(reject) {
  2635. return wrap(function (event) {
  2636. preventDefault(event);
  2637. reject(event.target.error);
  2638. return false;
  2639. });
  2640. }
  2641. function preventDefault(event) {
  2642. if (event.stopPropagation)
  2643. event.stopPropagation();
  2644. if (event.preventDefault)
  2645. event.preventDefault();
  2646. }
  2647. var DEXIE_STORAGE_MUTATED_EVENT_NAME = 'storagemutated';
  2648. var STORAGE_MUTATED_DOM_EVENT_NAME = 'x-storagemutated-1';
  2649. var globalEvents = Events(null, DEXIE_STORAGE_MUTATED_EVENT_NAME);
  2650. var Transaction = (function () {
  2651. function Transaction() {
  2652. }
  2653. Transaction.prototype._lock = function () {
  2654. assert(!PSD.global);
  2655. ++this._reculock;
  2656. if (this._reculock === 1 && !PSD.global)
  2657. PSD.lockOwnerFor = this;
  2658. return this;
  2659. };
  2660. Transaction.prototype._unlock = function () {
  2661. assert(!PSD.global);
  2662. if (--this._reculock === 0) {
  2663. if (!PSD.global)
  2664. PSD.lockOwnerFor = null;
  2665. while (this._blockedFuncs.length > 0 && !this._locked()) {
  2666. var fnAndPSD = this._blockedFuncs.shift();
  2667. try {
  2668. usePSD(fnAndPSD[1], fnAndPSD[0]);
  2669. }
  2670. catch (e) { }
  2671. }
  2672. }
  2673. return this;
  2674. };
  2675. Transaction.prototype._locked = function () {
  2676. return this._reculock && PSD.lockOwnerFor !== this;
  2677. };
  2678. Transaction.prototype.create = function (idbtrans) {
  2679. var _this = this;
  2680. if (!this.mode)
  2681. return this;
  2682. var idbdb = this.db.idbdb;
  2683. var dbOpenError = this.db._state.dbOpenError;
  2684. assert(!this.idbtrans);
  2685. if (!idbtrans && !idbdb) {
  2686. switch (dbOpenError && dbOpenError.name) {
  2687. case "DatabaseClosedError":
  2688. throw new exceptions.DatabaseClosed(dbOpenError);
  2689. case "MissingAPIError":
  2690. throw new exceptions.MissingAPI(dbOpenError.message, dbOpenError);
  2691. default:
  2692. throw new exceptions.OpenFailed(dbOpenError);
  2693. }
  2694. }
  2695. if (!this.active)
  2696. throw new exceptions.TransactionInactive();
  2697. assert(this._completion._state === null);
  2698. idbtrans = this.idbtrans = idbtrans ||
  2699. (this.db.core
  2700. ? this.db.core.transaction(this.storeNames, this.mode, { durability: this.chromeTransactionDurability })
  2701. : idbdb.transaction(this.storeNames, this.mode, { durability: this.chromeTransactionDurability }));
  2702. idbtrans.onerror = wrap(function (ev) {
  2703. preventDefault(ev);
  2704. _this._reject(idbtrans.error);
  2705. });
  2706. idbtrans.onabort = wrap(function (ev) {
  2707. preventDefault(ev);
  2708. _this.active && _this._reject(new exceptions.Abort(idbtrans.error));
  2709. _this.active = false;
  2710. _this.on("abort").fire(ev);
  2711. });
  2712. idbtrans.oncomplete = wrap(function () {
  2713. _this.active = false;
  2714. _this._resolve();
  2715. if ('mutatedParts' in idbtrans) {
  2716. globalEvents.storagemutated.fire(idbtrans["mutatedParts"]);
  2717. }
  2718. });
  2719. return this;
  2720. };
  2721. Transaction.prototype._promise = function (mode, fn, bWriteLock) {
  2722. var _this = this;
  2723. if (mode === 'readwrite' && this.mode !== 'readwrite')
  2724. return rejection(new exceptions.ReadOnly("Transaction is readonly"));
  2725. if (!this.active)
  2726. return rejection(new exceptions.TransactionInactive());
  2727. if (this._locked()) {
  2728. return new DexiePromise(function (resolve, reject) {
  2729. _this._blockedFuncs.push([function () {
  2730. _this._promise(mode, fn, bWriteLock).then(resolve, reject);
  2731. }, PSD]);
  2732. });
  2733. }
  2734. else if (bWriteLock) {
  2735. return newScope(function () {
  2736. var p = new DexiePromise(function (resolve, reject) {
  2737. _this._lock();
  2738. var rv = fn(resolve, reject, _this);
  2739. if (rv && rv.then)
  2740. rv.then(resolve, reject);
  2741. });
  2742. p.finally(function () { return _this._unlock(); });
  2743. p._lib = true;
  2744. return p;
  2745. });
  2746. }
  2747. else {
  2748. var p = new DexiePromise(function (resolve, reject) {
  2749. var rv = fn(resolve, reject, _this);
  2750. if (rv && rv.then)
  2751. rv.then(resolve, reject);
  2752. });
  2753. p._lib = true;
  2754. return p;
  2755. }
  2756. };
  2757. Transaction.prototype._root = function () {
  2758. return this.parent ? this.parent._root() : this;
  2759. };
  2760. Transaction.prototype.waitFor = function (promiseLike) {
  2761. var root = this._root();
  2762. var promise = DexiePromise.resolve(promiseLike);
  2763. if (root._waitingFor) {
  2764. root._waitingFor = root._waitingFor.then(function () { return promise; });
  2765. }
  2766. else {
  2767. root._waitingFor = promise;
  2768. root._waitingQueue = [];
  2769. var store = root.idbtrans.objectStore(root.storeNames[0]);
  2770. (function spin() {
  2771. ++root._spinCount;
  2772. while (root._waitingQueue.length)
  2773. (root._waitingQueue.shift())();
  2774. if (root._waitingFor)
  2775. store.get(-Infinity).onsuccess = spin;
  2776. }());
  2777. }
  2778. var currentWaitPromise = root._waitingFor;
  2779. return new DexiePromise(function (resolve, reject) {
  2780. promise.then(function (res) { return root._waitingQueue.push(wrap(resolve.bind(null, res))); }, function (err) { return root._waitingQueue.push(wrap(reject.bind(null, err))); }).finally(function () {
  2781. if (root._waitingFor === currentWaitPromise) {
  2782. root._waitingFor = null;
  2783. }
  2784. });
  2785. });
  2786. };
  2787. Transaction.prototype.abort = function () {
  2788. if (this.active) {
  2789. this.active = false;
  2790. if (this.idbtrans)
  2791. this.idbtrans.abort();
  2792. this._reject(new exceptions.Abort());
  2793. }
  2794. };
  2795. Transaction.prototype.table = function (tableName) {
  2796. var memoizedTables = (this._memoizedTables || (this._memoizedTables = {}));
  2797. if (hasOwn(memoizedTables, tableName))
  2798. return memoizedTables[tableName];
  2799. var tableSchema = this.schema[tableName];
  2800. if (!tableSchema) {
  2801. throw new exceptions.NotFound("Table " + tableName + " not part of transaction");
  2802. }
  2803. var transactionBoundTable = new this.db.Table(tableName, tableSchema, this);
  2804. transactionBoundTable.core = this.db.core.table(tableName);
  2805. memoizedTables[tableName] = transactionBoundTable;
  2806. return transactionBoundTable;
  2807. };
  2808. return Transaction;
  2809. }());
  2810. function createTransactionConstructor(db) {
  2811. return makeClassConstructor(Transaction.prototype, function Transaction(mode, storeNames, dbschema, chromeTransactionDurability, parent) {
  2812. var _this = this;
  2813. this.db = db;
  2814. this.mode = mode;
  2815. this.storeNames = storeNames;
  2816. this.schema = dbschema;
  2817. this.chromeTransactionDurability = chromeTransactionDurability;
  2818. this.idbtrans = null;
  2819. this.on = Events(this, "complete", "error", "abort");
  2820. this.parent = parent || null;
  2821. this.active = true;
  2822. this._reculock = 0;
  2823. this._blockedFuncs = [];
  2824. this._resolve = null;
  2825. this._reject = null;
  2826. this._waitingFor = null;
  2827. this._waitingQueue = null;
  2828. this._spinCount = 0;
  2829. this._completion = new DexiePromise(function (resolve, reject) {
  2830. _this._resolve = resolve;
  2831. _this._reject = reject;
  2832. });
  2833. this._completion.then(function () {
  2834. _this.active = false;
  2835. _this.on.complete.fire();
  2836. }, function (e) {
  2837. var wasActive = _this.active;
  2838. _this.active = false;
  2839. _this.on.error.fire(e);
  2840. _this.parent ?
  2841. _this.parent._reject(e) :
  2842. wasActive && _this.idbtrans && _this.idbtrans.abort();
  2843. return rejection(e);
  2844. });
  2845. });
  2846. }
  2847. function createIndexSpec(name, keyPath, unique, multi, auto, compound, isPrimKey) {
  2848. return {
  2849. name: name,
  2850. keyPath: keyPath,
  2851. unique: unique,
  2852. multi: multi,
  2853. auto: auto,
  2854. compound: compound,
  2855. src: (unique && !isPrimKey ? '&' : '') + (multi ? '*' : '') + (auto ? "++" : "") + nameFromKeyPath(keyPath)
  2856. };
  2857. }
  2858. function nameFromKeyPath(keyPath) {
  2859. return typeof keyPath === 'string' ?
  2860. keyPath :
  2861. keyPath ? ('[' + [].join.call(keyPath, '+') + ']') : "";
  2862. }
  2863. function createTableSchema(name, primKey, indexes) {
  2864. return {
  2865. name: name,
  2866. primKey: primKey,
  2867. indexes: indexes,
  2868. mappedClass: null,
  2869. idxByName: arrayToObject(indexes, function (index) { return [index.name, index]; })
  2870. };
  2871. }
  2872. function safariMultiStoreFix(storeNames) {
  2873. return storeNames.length === 1 ? storeNames[0] : storeNames;
  2874. }
  2875. var getMaxKey = function (IdbKeyRange) {
  2876. try {
  2877. IdbKeyRange.only([[]]);
  2878. getMaxKey = function () { return [[]]; };
  2879. return [[]];
  2880. }
  2881. catch (e) {
  2882. getMaxKey = function () { return maxString; };
  2883. return maxString;
  2884. }
  2885. };
  2886. function getKeyExtractor(keyPath) {
  2887. if (keyPath == null) {
  2888. return function () { return undefined; };
  2889. }
  2890. else if (typeof keyPath === 'string') {
  2891. return getSinglePathKeyExtractor(keyPath);
  2892. }
  2893. else {
  2894. return function (obj) { return getByKeyPath(obj, keyPath); };
  2895. }
  2896. }
  2897. function getSinglePathKeyExtractor(keyPath) {
  2898. var split = keyPath.split('.');
  2899. if (split.length === 1) {
  2900. return function (obj) { return obj[keyPath]; };
  2901. }
  2902. else {
  2903. return function (obj) { return getByKeyPath(obj, keyPath); };
  2904. }
  2905. }
  2906. function arrayify(arrayLike) {
  2907. return [].slice.call(arrayLike);
  2908. }
  2909. var _id_counter = 0;
  2910. function getKeyPathAlias(keyPath) {
  2911. return keyPath == null ?
  2912. ":id" :
  2913. typeof keyPath === 'string' ?
  2914. keyPath :
  2915. "[" + keyPath.join('+') + "]";
  2916. }
  2917. function createDBCore(db, IdbKeyRange, tmpTrans) {
  2918. function extractSchema(db, trans) {
  2919. var tables = arrayify(db.objectStoreNames);
  2920. return {
  2921. schema: {
  2922. name: db.name,
  2923. tables: tables.map(function (table) { return trans.objectStore(table); }).map(function (store) {
  2924. var keyPath = store.keyPath, autoIncrement = store.autoIncrement;
  2925. var compound = isArray(keyPath);
  2926. var outbound = keyPath == null;
  2927. var indexByKeyPath = {};
  2928. var result = {
  2929. name: store.name,
  2930. primaryKey: {
  2931. name: null,
  2932. isPrimaryKey: true,
  2933. outbound: outbound,
  2934. compound: compound,
  2935. keyPath: keyPath,
  2936. autoIncrement: autoIncrement,
  2937. unique: true,
  2938. extractKey: getKeyExtractor(keyPath)
  2939. },
  2940. indexes: arrayify(store.indexNames).map(function (indexName) { return store.index(indexName); })
  2941. .map(function (index) {
  2942. var name = index.name, unique = index.unique, multiEntry = index.multiEntry, keyPath = index.keyPath;
  2943. var compound = isArray(keyPath);
  2944. var result = {
  2945. name: name,
  2946. compound: compound,
  2947. keyPath: keyPath,
  2948. unique: unique,
  2949. multiEntry: multiEntry,
  2950. extractKey: getKeyExtractor(keyPath)
  2951. };
  2952. indexByKeyPath[getKeyPathAlias(keyPath)] = result;
  2953. return result;
  2954. }),
  2955. getIndexByKeyPath: function (keyPath) { return indexByKeyPath[getKeyPathAlias(keyPath)]; }
  2956. };
  2957. indexByKeyPath[":id"] = result.primaryKey;
  2958. if (keyPath != null) {
  2959. indexByKeyPath[getKeyPathAlias(keyPath)] = result.primaryKey;
  2960. }
  2961. return result;
  2962. })
  2963. },
  2964. hasGetAll: tables.length > 0 && ('getAll' in trans.objectStore(tables[0])) &&
  2965. !(typeof navigator !== 'undefined' && /Safari/.test(navigator.userAgent) &&
  2966. !/(Chrome\/|Edge\/)/.test(navigator.userAgent) &&
  2967. [].concat(navigator.userAgent.match(/Safari\/(\d*)/))[1] < 604)
  2968. };
  2969. }
  2970. function makeIDBKeyRange(range) {
  2971. if (range.type === 3 )
  2972. return null;
  2973. if (range.type === 4 )
  2974. throw new Error("Cannot convert never type to IDBKeyRange");
  2975. var lower = range.lower, upper = range.upper, lowerOpen = range.lowerOpen, upperOpen = range.upperOpen;
  2976. var idbRange = lower === undefined ?
  2977. upper === undefined ?
  2978. null :
  2979. IdbKeyRange.upperBound(upper, !!upperOpen) :
  2980. upper === undefined ?
  2981. IdbKeyRange.lowerBound(lower, !!lowerOpen) :
  2982. IdbKeyRange.bound(lower, upper, !!lowerOpen, !!upperOpen);
  2983. return idbRange;
  2984. }
  2985. function createDbCoreTable(tableSchema) {
  2986. var tableName = tableSchema.name;
  2987. function mutate(_a) {
  2988. var trans = _a.trans, type = _a.type, keys = _a.keys, values = _a.values, range = _a.range;
  2989. return new Promise(function (resolve, reject) {
  2990. resolve = wrap(resolve);
  2991. var store = trans.objectStore(tableName);
  2992. var outbound = store.keyPath == null;
  2993. var isAddOrPut = type === "put" || type === "add";
  2994. if (!isAddOrPut && type !== 'delete' && type !== 'deleteRange')
  2995. throw new Error("Invalid operation type: " + type);
  2996. var length = (keys || values || { length: 1 }).length;
  2997. if (keys && values && keys.length !== values.length) {
  2998. throw new Error("Given keys array must have same length as given values array.");
  2999. }
  3000. if (length === 0)
  3001. return resolve({ numFailures: 0, failures: {}, results: [], lastResult: undefined });
  3002. var req;
  3003. var reqs = [];
  3004. var failures = [];
  3005. var numFailures = 0;
  3006. var errorHandler = function (event) {
  3007. ++numFailures;
  3008. preventDefault(event);
  3009. };
  3010. if (type === 'deleteRange') {
  3011. if (range.type === 4 )
  3012. return resolve({ numFailures: numFailures, failures: failures, results: [], lastResult: undefined });
  3013. if (range.type === 3 )
  3014. reqs.push(req = store.clear());
  3015. else
  3016. reqs.push(req = store.delete(makeIDBKeyRange(range)));
  3017. }
  3018. else {
  3019. var _a = isAddOrPut ?
  3020. outbound ?
  3021. [values, keys] :
  3022. [values, null] :
  3023. [keys, null], args1 = _a[0], args2 = _a[1];
  3024. if (isAddOrPut) {
  3025. for (var i = 0; i < length; ++i) {
  3026. reqs.push(req = (args2 && args2[i] !== undefined ?
  3027. store[type](args1[i], args2[i]) :
  3028. store[type](args1[i])));
  3029. req.onerror = errorHandler;
  3030. }
  3031. }
  3032. else {
  3033. for (var i = 0; i < length; ++i) {
  3034. reqs.push(req = store[type](args1[i]));
  3035. req.onerror = errorHandler;
  3036. }
  3037. }
  3038. }
  3039. var done = function (event) {
  3040. var lastResult = event.target.result;
  3041. reqs.forEach(function (req, i) { return req.error != null && (failures[i] = req.error); });
  3042. resolve({
  3043. numFailures: numFailures,
  3044. failures: failures,
  3045. results: type === "delete" ? keys : reqs.map(function (req) { return req.result; }),
  3046. lastResult: lastResult
  3047. });
  3048. };
  3049. req.onerror = function (event) {
  3050. errorHandler(event);
  3051. done(event);
  3052. };
  3053. req.onsuccess = done;
  3054. });
  3055. }
  3056. function openCursor(_a) {
  3057. var trans = _a.trans, values = _a.values, query = _a.query, reverse = _a.reverse, unique = _a.unique;
  3058. return new Promise(function (resolve, reject) {
  3059. resolve = wrap(resolve);
  3060. var index = query.index, range = query.range;
  3061. var store = trans.objectStore(tableName);
  3062. var source = index.isPrimaryKey ?
  3063. store :
  3064. store.index(index.name);
  3065. var direction = reverse ?
  3066. unique ?
  3067. "prevunique" :
  3068. "prev" :
  3069. unique ?
  3070. "nextunique" :
  3071. "next";
  3072. var req = values || !('openKeyCursor' in source) ?
  3073. source.openCursor(makeIDBKeyRange(range), direction) :
  3074. source.openKeyCursor(makeIDBKeyRange(range), direction);
  3075. req.onerror = eventRejectHandler(reject);
  3076. req.onsuccess = wrap(function (ev) {
  3077. var cursor = req.result;
  3078. if (!cursor) {
  3079. resolve(null);
  3080. return;
  3081. }
  3082. cursor.___id = ++_id_counter;
  3083. cursor.done = false;
  3084. var _cursorContinue = cursor.continue.bind(cursor);
  3085. var _cursorContinuePrimaryKey = cursor.continuePrimaryKey;
  3086. if (_cursorContinuePrimaryKey)
  3087. _cursorContinuePrimaryKey = _cursorContinuePrimaryKey.bind(cursor);
  3088. var _cursorAdvance = cursor.advance.bind(cursor);
  3089. var doThrowCursorIsNotStarted = function () { throw new Error("Cursor not started"); };
  3090. var doThrowCursorIsStopped = function () { throw new Error("Cursor not stopped"); };
  3091. cursor.trans = trans;
  3092. cursor.stop = cursor.continue = cursor.continuePrimaryKey = cursor.advance = doThrowCursorIsNotStarted;
  3093. cursor.fail = wrap(reject);
  3094. cursor.next = function () {
  3095. var _this = this;
  3096. var gotOne = 1;
  3097. return this.start(function () { return gotOne-- ? _this.continue() : _this.stop(); }).then(function () { return _this; });
  3098. };
  3099. cursor.start = function (callback) {
  3100. var iterationPromise = new Promise(function (resolveIteration, rejectIteration) {
  3101. resolveIteration = wrap(resolveIteration);
  3102. req.onerror = eventRejectHandler(rejectIteration);
  3103. cursor.fail = rejectIteration;
  3104. cursor.stop = function (value) {
  3105. cursor.stop = cursor.continue = cursor.continuePrimaryKey = cursor.advance = doThrowCursorIsStopped;
  3106. resolveIteration(value);
  3107. };
  3108. });
  3109. var guardedCallback = function () {
  3110. if (req.result) {
  3111. try {
  3112. callback();
  3113. }
  3114. catch (err) {
  3115. cursor.fail(err);
  3116. }
  3117. }
  3118. else {
  3119. cursor.done = true;
  3120. cursor.start = function () { throw new Error("Cursor behind last entry"); };
  3121. cursor.stop();
  3122. }
  3123. };
  3124. req.onsuccess = wrap(function (ev) {
  3125. req.onsuccess = guardedCallback;
  3126. guardedCallback();
  3127. });
  3128. cursor.continue = _cursorContinue;
  3129. cursor.continuePrimaryKey = _cursorContinuePrimaryKey;
  3130. cursor.advance = _cursorAdvance;
  3131. guardedCallback();
  3132. return iterationPromise;
  3133. };
  3134. resolve(cursor);
  3135. }, reject);
  3136. });
  3137. }
  3138. function query(hasGetAll) {
  3139. return function (request) {
  3140. return new Promise(function (resolve, reject) {
  3141. resolve = wrap(resolve);
  3142. var trans = request.trans, values = request.values, limit = request.limit, query = request.query;
  3143. var nonInfinitLimit = limit === Infinity ? undefined : limit;
  3144. var index = query.index, range = query.range;
  3145. var store = trans.objectStore(tableName);
  3146. var source = index.isPrimaryKey ? store : store.index(index.name);
  3147. var idbKeyRange = makeIDBKeyRange(range);
  3148. if (limit === 0)
  3149. return resolve({ result: [] });
  3150. if (hasGetAll) {
  3151. var req = values ?
  3152. source.getAll(idbKeyRange, nonInfinitLimit) :
  3153. source.getAllKeys(idbKeyRange, nonInfinitLimit);
  3154. req.onsuccess = function (event) { return resolve({ result: event.target.result }); };
  3155. req.onerror = eventRejectHandler(reject);
  3156. }
  3157. else {
  3158. var count_1 = 0;
  3159. var req_1 = values || !('openKeyCursor' in source) ?
  3160. source.openCursor(idbKeyRange) :
  3161. source.openKeyCursor(idbKeyRange);
  3162. var result_1 = [];
  3163. req_1.onsuccess = function (event) {
  3164. var cursor = req_1.result;
  3165. if (!cursor)
  3166. return resolve({ result: result_1 });
  3167. result_1.push(values ? cursor.value : cursor.primaryKey);
  3168. if (++count_1 === limit)
  3169. return resolve({ result: result_1 });
  3170. cursor.continue();
  3171. };
  3172. req_1.onerror = eventRejectHandler(reject);
  3173. }
  3174. });
  3175. };
  3176. }
  3177. return {
  3178. name: tableName,
  3179. schema: tableSchema,
  3180. mutate: mutate,
  3181. getMany: function (_a) {
  3182. var trans = _a.trans, keys = _a.keys;
  3183. return new Promise(function (resolve, reject) {
  3184. resolve = wrap(resolve);
  3185. var store = trans.objectStore(tableName);
  3186. var length = keys.length;
  3187. var result = new Array(length);
  3188. var keyCount = 0;
  3189. var callbackCount = 0;
  3190. var req;
  3191. var successHandler = function (event) {
  3192. var req = event.target;
  3193. if ((result[req._pos] = req.result) != null)
  3194. ;
  3195. if (++callbackCount === keyCount)
  3196. resolve(result);
  3197. };
  3198. var errorHandler = eventRejectHandler(reject);
  3199. for (var i = 0; i < length; ++i) {
  3200. var key = keys[i];
  3201. if (key != null) {
  3202. req = store.get(keys[i]);
  3203. req._pos = i;
  3204. req.onsuccess = successHandler;
  3205. req.onerror = errorHandler;
  3206. ++keyCount;
  3207. }
  3208. }
  3209. if (keyCount === 0)
  3210. resolve(result);
  3211. });
  3212. },
  3213. get: function (_a) {
  3214. var trans = _a.trans, key = _a.key;
  3215. return new Promise(function (resolve, reject) {
  3216. resolve = wrap(resolve);
  3217. var store = trans.objectStore(tableName);
  3218. var req = store.get(key);
  3219. req.onsuccess = function (event) { return resolve(event.target.result); };
  3220. req.onerror = eventRejectHandler(reject);
  3221. });
  3222. },
  3223. query: query(hasGetAll),
  3224. openCursor: openCursor,
  3225. count: function (_a) {
  3226. var query = _a.query, trans = _a.trans;
  3227. var index = query.index, range = query.range;
  3228. return new Promise(function (resolve, reject) {
  3229. var store = trans.objectStore(tableName);
  3230. var source = index.isPrimaryKey ? store : store.index(index.name);
  3231. var idbKeyRange = makeIDBKeyRange(range);
  3232. var req = idbKeyRange ? source.count(idbKeyRange) : source.count();
  3233. req.onsuccess = wrap(function (ev) { return resolve(ev.target.result); });
  3234. req.onerror = eventRejectHandler(reject);
  3235. });
  3236. }
  3237. };
  3238. }
  3239. var _a = extractSchema(db, tmpTrans), schema = _a.schema, hasGetAll = _a.hasGetAll;
  3240. var tables = schema.tables.map(function (tableSchema) { return createDbCoreTable(tableSchema); });
  3241. var tableMap = {};
  3242. tables.forEach(function (table) { return tableMap[table.name] = table; });
  3243. return {
  3244. stack: "dbcore",
  3245. transaction: db.transaction.bind(db),
  3246. table: function (name) {
  3247. var result = tableMap[name];
  3248. if (!result)
  3249. throw new Error("Table '" + name + "' not found");
  3250. return tableMap[name];
  3251. },
  3252. MIN_KEY: -Infinity,
  3253. MAX_KEY: getMaxKey(IdbKeyRange),
  3254. schema: schema
  3255. };
  3256. }
  3257. function createMiddlewareStack(stackImpl, middlewares) {
  3258. return middlewares.reduce(function (down, _a) {
  3259. var create = _a.create;
  3260. return (__assign(__assign({}, down), create(down)));
  3261. }, stackImpl);
  3262. }
  3263. function createMiddlewareStacks(middlewares, idbdb, _a, tmpTrans) {
  3264. var IDBKeyRange = _a.IDBKeyRange; _a.indexedDB;
  3265. var dbcore = createMiddlewareStack(createDBCore(idbdb, IDBKeyRange, tmpTrans), middlewares.dbcore);
  3266. return {
  3267. dbcore: dbcore
  3268. };
  3269. }
  3270. function generateMiddlewareStacks(_a, tmpTrans) {
  3271. var db = _a._novip;
  3272. var idbdb = tmpTrans.db;
  3273. var stacks = createMiddlewareStacks(db._middlewares, idbdb, db._deps, tmpTrans);
  3274. db.core = stacks.dbcore;
  3275. db.tables.forEach(function (table) {
  3276. var tableName = table.name;
  3277. if (db.core.schema.tables.some(function (tbl) { return tbl.name === tableName; })) {
  3278. table.core = db.core.table(tableName);
  3279. if (db[tableName] instanceof db.Table) {
  3280. db[tableName].core = table.core;
  3281. }
  3282. }
  3283. });
  3284. }
  3285. function setApiOnPlace(_a, objs, tableNames, dbschema) {
  3286. var db = _a._novip;
  3287. tableNames.forEach(function (tableName) {
  3288. var schema = dbschema[tableName];
  3289. objs.forEach(function (obj) {
  3290. var propDesc = getPropertyDescriptor(obj, tableName);
  3291. if (!propDesc || ("value" in propDesc && propDesc.value === undefined)) {
  3292. if (obj === db.Transaction.prototype || obj instanceof db.Transaction) {
  3293. setProp(obj, tableName, {
  3294. get: function () { return this.table(tableName); },
  3295. set: function (value) {
  3296. defineProperty(this, tableName, { value: value, writable: true, configurable: true, enumerable: true });
  3297. }
  3298. });
  3299. }
  3300. else {
  3301. obj[tableName] = new db.Table(tableName, schema);
  3302. }
  3303. }
  3304. });
  3305. });
  3306. }
  3307. function removeTablesApi(_a, objs) {
  3308. var db = _a._novip;
  3309. objs.forEach(function (obj) {
  3310. for (var key in obj) {
  3311. if (obj[key] instanceof db.Table)
  3312. delete obj[key];
  3313. }
  3314. });
  3315. }
  3316. function lowerVersionFirst(a, b) {
  3317. return a._cfg.version - b._cfg.version;
  3318. }
  3319. function runUpgraders(db, oldVersion, idbUpgradeTrans, reject) {
  3320. var globalSchema = db._dbSchema;
  3321. var trans = db._createTransaction('readwrite', db._storeNames, globalSchema);
  3322. trans.create(idbUpgradeTrans);
  3323. trans._completion.catch(reject);
  3324. var rejectTransaction = trans._reject.bind(trans);
  3325. var transless = PSD.transless || PSD;
  3326. newScope(function () {
  3327. PSD.trans = trans;
  3328. PSD.transless = transless;
  3329. if (oldVersion === 0) {
  3330. keys(globalSchema).forEach(function (tableName) {
  3331. createTable(idbUpgradeTrans, tableName, globalSchema[tableName].primKey, globalSchema[tableName].indexes);
  3332. });
  3333. generateMiddlewareStacks(db, idbUpgradeTrans);
  3334. DexiePromise.follow(function () { return db.on.populate.fire(trans); }).catch(rejectTransaction);
  3335. }
  3336. else
  3337. updateTablesAndIndexes(db, oldVersion, trans, idbUpgradeTrans).catch(rejectTransaction);
  3338. });
  3339. }
  3340. function updateTablesAndIndexes(_a, oldVersion, trans, idbUpgradeTrans) {
  3341. var db = _a._novip;
  3342. var queue = [];
  3343. var versions = db._versions;
  3344. var globalSchema = db._dbSchema = buildGlobalSchema(db, db.idbdb, idbUpgradeTrans);
  3345. var anyContentUpgraderHasRun = false;
  3346. var versToRun = versions.filter(function (v) { return v._cfg.version >= oldVersion; });
  3347. versToRun.forEach(function (version) {
  3348. queue.push(function () {
  3349. var oldSchema = globalSchema;
  3350. var newSchema = version._cfg.dbschema;
  3351. adjustToExistingIndexNames(db, oldSchema, idbUpgradeTrans);
  3352. adjustToExistingIndexNames(db, newSchema, idbUpgradeTrans);
  3353. globalSchema = db._dbSchema = newSchema;
  3354. var diff = getSchemaDiff(oldSchema, newSchema);
  3355. diff.add.forEach(function (tuple) {
  3356. createTable(idbUpgradeTrans, tuple[0], tuple[1].primKey, tuple[1].indexes);
  3357. });
  3358. diff.change.forEach(function (change) {
  3359. if (change.recreate) {
  3360. throw new exceptions.Upgrade("Not yet support for changing primary key");
  3361. }
  3362. else {
  3363. var store_1 = idbUpgradeTrans.objectStore(change.name);
  3364. change.add.forEach(function (idx) { return addIndex(store_1, idx); });
  3365. change.change.forEach(function (idx) {
  3366. store_1.deleteIndex(idx.name);
  3367. addIndex(store_1, idx);
  3368. });
  3369. change.del.forEach(function (idxName) { return store_1.deleteIndex(idxName); });
  3370. }
  3371. });
  3372. var contentUpgrade = version._cfg.contentUpgrade;
  3373. if (contentUpgrade && version._cfg.version > oldVersion) {
  3374. generateMiddlewareStacks(db, idbUpgradeTrans);
  3375. trans._memoizedTables = {};
  3376. anyContentUpgraderHasRun = true;
  3377. var upgradeSchema_1 = shallowClone(newSchema);
  3378. diff.del.forEach(function (table) {
  3379. upgradeSchema_1[table] = oldSchema[table];
  3380. });
  3381. removeTablesApi(db, [db.Transaction.prototype]);
  3382. setApiOnPlace(db, [db.Transaction.prototype], keys(upgradeSchema_1), upgradeSchema_1);
  3383. trans.schema = upgradeSchema_1;
  3384. var contentUpgradeIsAsync_1 = isAsyncFunction(contentUpgrade);
  3385. if (contentUpgradeIsAsync_1) {
  3386. incrementExpectedAwaits();
  3387. }
  3388. var returnValue_1;
  3389. var promiseFollowed = DexiePromise.follow(function () {
  3390. returnValue_1 = contentUpgrade(trans);
  3391. if (returnValue_1) {
  3392. if (contentUpgradeIsAsync_1) {
  3393. var decrementor = decrementExpectedAwaits.bind(null, null);
  3394. returnValue_1.then(decrementor, decrementor);
  3395. }
  3396. }
  3397. });
  3398. return (returnValue_1 && typeof returnValue_1.then === 'function' ?
  3399. DexiePromise.resolve(returnValue_1) : promiseFollowed.then(function () { return returnValue_1; }));
  3400. }
  3401. });
  3402. queue.push(function (idbtrans) {
  3403. if (!anyContentUpgraderHasRun || !hasIEDeleteObjectStoreBug) {
  3404. var newSchema = version._cfg.dbschema;
  3405. deleteRemovedTables(newSchema, idbtrans);
  3406. }
  3407. removeTablesApi(db, [db.Transaction.prototype]);
  3408. setApiOnPlace(db, [db.Transaction.prototype], db._storeNames, db._dbSchema);
  3409. trans.schema = db._dbSchema;
  3410. });
  3411. });
  3412. function runQueue() {
  3413. return queue.length ? DexiePromise.resolve(queue.shift()(trans.idbtrans)).then(runQueue) :
  3414. DexiePromise.resolve();
  3415. }
  3416. return runQueue().then(function () {
  3417. createMissingTables(globalSchema, idbUpgradeTrans);
  3418. });
  3419. }
  3420. function getSchemaDiff(oldSchema, newSchema) {
  3421. var diff = {
  3422. del: [],
  3423. add: [],
  3424. change: []
  3425. };
  3426. var table;
  3427. for (table in oldSchema) {
  3428. if (!newSchema[table])
  3429. diff.del.push(table);
  3430. }
  3431. for (table in newSchema) {
  3432. var oldDef = oldSchema[table], newDef = newSchema[table];
  3433. if (!oldDef) {
  3434. diff.add.push([table, newDef]);
  3435. }
  3436. else {
  3437. var change = {
  3438. name: table,
  3439. def: newDef,
  3440. recreate: false,
  3441. del: [],
  3442. add: [],
  3443. change: []
  3444. };
  3445. if ((
  3446. '' + (oldDef.primKey.keyPath || '')) !== ('' + (newDef.primKey.keyPath || '')) ||
  3447. (oldDef.primKey.auto !== newDef.primKey.auto && !isIEOrEdge))
  3448. {
  3449. change.recreate = true;
  3450. diff.change.push(change);
  3451. }
  3452. else {
  3453. var oldIndexes = oldDef.idxByName;
  3454. var newIndexes = newDef.idxByName;
  3455. var idxName = void 0;
  3456. for (idxName in oldIndexes) {
  3457. if (!newIndexes[idxName])
  3458. change.del.push(idxName);
  3459. }
  3460. for (idxName in newIndexes) {
  3461. var oldIdx = oldIndexes[idxName], newIdx = newIndexes[idxName];
  3462. if (!oldIdx)
  3463. change.add.push(newIdx);
  3464. else if (oldIdx.src !== newIdx.src)
  3465. change.change.push(newIdx);
  3466. }
  3467. if (change.del.length > 0 || change.add.length > 0 || change.change.length > 0) {
  3468. diff.change.push(change);
  3469. }
  3470. }
  3471. }
  3472. }
  3473. return diff;
  3474. }
  3475. function createTable(idbtrans, tableName, primKey, indexes) {
  3476. var store = idbtrans.db.createObjectStore(tableName, primKey.keyPath ?
  3477. { keyPath: primKey.keyPath, autoIncrement: primKey.auto } :
  3478. { autoIncrement: primKey.auto });
  3479. indexes.forEach(function (idx) { return addIndex(store, idx); });
  3480. return store;
  3481. }
  3482. function createMissingTables(newSchema, idbtrans) {
  3483. keys(newSchema).forEach(function (tableName) {
  3484. if (!idbtrans.db.objectStoreNames.contains(tableName)) {
  3485. createTable(idbtrans, tableName, newSchema[tableName].primKey, newSchema[tableName].indexes);
  3486. }
  3487. });
  3488. }
  3489. function deleteRemovedTables(newSchema, idbtrans) {
  3490. [].slice.call(idbtrans.db.objectStoreNames).forEach(function (storeName) {
  3491. return newSchema[storeName] == null && idbtrans.db.deleteObjectStore(storeName);
  3492. });
  3493. }
  3494. function addIndex(store, idx) {
  3495. store.createIndex(idx.name, idx.keyPath, { unique: idx.unique, multiEntry: idx.multi });
  3496. }
  3497. function buildGlobalSchema(db, idbdb, tmpTrans) {
  3498. var globalSchema = {};
  3499. var dbStoreNames = slice(idbdb.objectStoreNames, 0);
  3500. dbStoreNames.forEach(function (storeName) {
  3501. var store = tmpTrans.objectStore(storeName);
  3502. var keyPath = store.keyPath;
  3503. var primKey = createIndexSpec(nameFromKeyPath(keyPath), keyPath || "", false, false, !!store.autoIncrement, keyPath && typeof keyPath !== "string", true);
  3504. var indexes = [];
  3505. for (var j = 0; j < store.indexNames.length; ++j) {
  3506. var idbindex = store.index(store.indexNames[j]);
  3507. keyPath = idbindex.keyPath;
  3508. var index = createIndexSpec(idbindex.name, keyPath, !!idbindex.unique, !!idbindex.multiEntry, false, keyPath && typeof keyPath !== "string", false);
  3509. indexes.push(index);
  3510. }
  3511. globalSchema[storeName] = createTableSchema(storeName, primKey, indexes);
  3512. });
  3513. return globalSchema;
  3514. }
  3515. function readGlobalSchema(_a, idbdb, tmpTrans) {
  3516. var db = _a._novip;
  3517. db.verno = idbdb.version / 10;
  3518. var globalSchema = db._dbSchema = buildGlobalSchema(db, idbdb, tmpTrans);
  3519. db._storeNames = slice(idbdb.objectStoreNames, 0);
  3520. setApiOnPlace(db, [db._allTables], keys(globalSchema), globalSchema);
  3521. }
  3522. function verifyInstalledSchema(db, tmpTrans) {
  3523. var installedSchema = buildGlobalSchema(db, db.idbdb, tmpTrans);
  3524. var diff = getSchemaDiff(installedSchema, db._dbSchema);
  3525. return !(diff.add.length || diff.change.some(function (ch) { return ch.add.length || ch.change.length; }));
  3526. }
  3527. function adjustToExistingIndexNames(_a, schema, idbtrans) {
  3528. var db = _a._novip;
  3529. var storeNames = idbtrans.db.objectStoreNames;
  3530. for (var i = 0; i < storeNames.length; ++i) {
  3531. var storeName = storeNames[i];
  3532. var store = idbtrans.objectStore(storeName);
  3533. db._hasGetAll = 'getAll' in store;
  3534. for (var j = 0; j < store.indexNames.length; ++j) {
  3535. var indexName = store.indexNames[j];
  3536. var keyPath = store.index(indexName).keyPath;
  3537. var dexieName = typeof keyPath === 'string' ? keyPath : "[" + slice(keyPath).join('+') + "]";
  3538. if (schema[storeName]) {
  3539. var indexSpec = schema[storeName].idxByName[dexieName];
  3540. if (indexSpec) {
  3541. indexSpec.name = indexName;
  3542. delete schema[storeName].idxByName[dexieName];
  3543. schema[storeName].idxByName[indexName] = indexSpec;
  3544. }
  3545. }
  3546. }
  3547. }
  3548. if (typeof navigator !== 'undefined' && /Safari/.test(navigator.userAgent) &&
  3549. !/(Chrome\/|Edge\/)/.test(navigator.userAgent) &&
  3550. _global.WorkerGlobalScope && _global instanceof _global.WorkerGlobalScope &&
  3551. [].concat(navigator.userAgent.match(/Safari\/(\d*)/))[1] < 604) {
  3552. db._hasGetAll = false;
  3553. }
  3554. }
  3555. function parseIndexSyntax(primKeyAndIndexes) {
  3556. return primKeyAndIndexes.split(',').map(function (index, indexNum) {
  3557. index = index.trim();
  3558. var name = index.replace(/([&*]|\+\+)/g, "");
  3559. var keyPath = /^\[/.test(name) ? name.match(/^\[(.*)\]$/)[1].split('+') : name;
  3560. return createIndexSpec(name, keyPath || null, /\&/.test(index), /\*/.test(index), /\+\+/.test(index), isArray(keyPath), indexNum === 0);
  3561. });
  3562. }
  3563. var Version = (function () {
  3564. function Version() {
  3565. }
  3566. Version.prototype._parseStoresSpec = function (stores, outSchema) {
  3567. keys(stores).forEach(function (tableName) {
  3568. if (stores[tableName] !== null) {
  3569. var indexes = parseIndexSyntax(stores[tableName]);
  3570. var primKey = indexes.shift();
  3571. if (primKey.multi)
  3572. throw new exceptions.Schema("Primary key cannot be multi-valued");
  3573. indexes.forEach(function (idx) {
  3574. if (idx.auto)
  3575. throw new exceptions.Schema("Only primary key can be marked as autoIncrement (++)");
  3576. if (!idx.keyPath)
  3577. throw new exceptions.Schema("Index must have a name and cannot be an empty string");
  3578. });
  3579. outSchema[tableName] = createTableSchema(tableName, primKey, indexes);
  3580. }
  3581. });
  3582. };
  3583. Version.prototype.stores = function (stores) {
  3584. var db = this.db;
  3585. this._cfg.storesSource = this._cfg.storesSource ?
  3586. extend(this._cfg.storesSource, stores) :
  3587. stores;
  3588. var versions = db._versions;
  3589. var storesSpec = {};
  3590. var dbschema = {};
  3591. versions.forEach(function (version) {
  3592. extend(storesSpec, version._cfg.storesSource);
  3593. dbschema = (version._cfg.dbschema = {});
  3594. version._parseStoresSpec(storesSpec, dbschema);
  3595. });
  3596. db._dbSchema = dbschema;
  3597. removeTablesApi(db, [db._allTables, db, db.Transaction.prototype]);
  3598. setApiOnPlace(db, [db._allTables, db, db.Transaction.prototype, this._cfg.tables], keys(dbschema), dbschema);
  3599. db._storeNames = keys(dbschema);
  3600. return this;
  3601. };
  3602. Version.prototype.upgrade = function (upgradeFunction) {
  3603. this._cfg.contentUpgrade = promisableChain(this._cfg.contentUpgrade || nop, upgradeFunction);
  3604. return this;
  3605. };
  3606. return Version;
  3607. }());
  3608. function createVersionConstructor(db) {
  3609. return makeClassConstructor(Version.prototype, function Version(versionNumber) {
  3610. this.db = db;
  3611. this._cfg = {
  3612. version: versionNumber,
  3613. storesSource: null,
  3614. dbschema: {},
  3615. tables: {},
  3616. contentUpgrade: null
  3617. };
  3618. });
  3619. }
  3620. function getDbNamesTable(indexedDB, IDBKeyRange) {
  3621. var dbNamesDB = indexedDB["_dbNamesDB"];
  3622. if (!dbNamesDB) {
  3623. dbNamesDB = indexedDB["_dbNamesDB"] = new Dexie$1(DBNAMES_DB, {
  3624. addons: [],
  3625. indexedDB: indexedDB,
  3626. IDBKeyRange: IDBKeyRange,
  3627. });
  3628. dbNamesDB.version(1).stores({ dbnames: "name" });
  3629. }
  3630. return dbNamesDB.table("dbnames");
  3631. }
  3632. function hasDatabasesNative(indexedDB) {
  3633. return indexedDB && typeof indexedDB.databases === "function";
  3634. }
  3635. function getDatabaseNames(_a) {
  3636. var indexedDB = _a.indexedDB, IDBKeyRange = _a.IDBKeyRange;
  3637. return hasDatabasesNative(indexedDB)
  3638. ? Promise.resolve(indexedDB.databases()).then(function (infos) {
  3639. return infos
  3640. .map(function (info) { return info.name; })
  3641. .filter(function (name) { return name !== DBNAMES_DB; });
  3642. })
  3643. : getDbNamesTable(indexedDB, IDBKeyRange).toCollection().primaryKeys();
  3644. }
  3645. function _onDatabaseCreated(_a, name) {
  3646. var indexedDB = _a.indexedDB, IDBKeyRange = _a.IDBKeyRange;
  3647. !hasDatabasesNative(indexedDB) &&
  3648. name !== DBNAMES_DB &&
  3649. getDbNamesTable(indexedDB, IDBKeyRange).put({ name: name }).catch(nop);
  3650. }
  3651. function _onDatabaseDeleted(_a, name) {
  3652. var indexedDB = _a.indexedDB, IDBKeyRange = _a.IDBKeyRange;
  3653. !hasDatabasesNative(indexedDB) &&
  3654. name !== DBNAMES_DB &&
  3655. getDbNamesTable(indexedDB, IDBKeyRange).delete(name).catch(nop);
  3656. }
  3657. function vip(fn) {
  3658. return newScope(function () {
  3659. PSD.letThrough = true;
  3660. return fn();
  3661. });
  3662. }
  3663. function idbReady() {
  3664. var isSafari = !navigator.userAgentData &&
  3665. /Safari\//.test(navigator.userAgent) &&
  3666. !/Chrom(e|ium)\//.test(navigator.userAgent);
  3667. if (!isSafari || !indexedDB.databases)
  3668. return Promise.resolve();
  3669. var intervalId;
  3670. return new Promise(function (resolve) {
  3671. var tryIdb = function () { return indexedDB.databases().finally(resolve); };
  3672. intervalId = setInterval(tryIdb, 100);
  3673. tryIdb();
  3674. }).finally(function () { return clearInterval(intervalId); });
  3675. }
  3676. function dexieOpen(db) {
  3677. var state = db._state;
  3678. var indexedDB = db._deps.indexedDB;
  3679. if (state.isBeingOpened || db.idbdb)
  3680. return state.dbReadyPromise.then(function () { return state.dbOpenError ?
  3681. rejection(state.dbOpenError) :
  3682. db; });
  3683. debug && (state.openCanceller._stackHolder = getErrorWithStack());
  3684. state.isBeingOpened = true;
  3685. state.dbOpenError = null;
  3686. state.openComplete = false;
  3687. var openCanceller = state.openCanceller;
  3688. function throwIfCancelled() {
  3689. if (state.openCanceller !== openCanceller)
  3690. throw new exceptions.DatabaseClosed('db.open() was cancelled');
  3691. }
  3692. var resolveDbReady = state.dbReadyResolve,
  3693. upgradeTransaction = null, wasCreated = false;
  3694. return DexiePromise.race([openCanceller, (typeof navigator === 'undefined' ? DexiePromise.resolve() : idbReady()).then(function () { return new DexiePromise(function (resolve, reject) {
  3695. throwIfCancelled();
  3696. if (!indexedDB)
  3697. throw new exceptions.MissingAPI();
  3698. var dbName = db.name;
  3699. var req = state.autoSchema ?
  3700. indexedDB.open(dbName) :
  3701. indexedDB.open(dbName, Math.round(db.verno * 10));
  3702. if (!req)
  3703. throw new exceptions.MissingAPI();
  3704. req.onerror = eventRejectHandler(reject);
  3705. req.onblocked = wrap(db._fireOnBlocked);
  3706. req.onupgradeneeded = wrap(function (e) {
  3707. upgradeTransaction = req.transaction;
  3708. if (state.autoSchema && !db._options.allowEmptyDB) {
  3709. req.onerror = preventDefault;
  3710. upgradeTransaction.abort();
  3711. req.result.close();
  3712. var delreq = indexedDB.deleteDatabase(dbName);
  3713. delreq.onsuccess = delreq.onerror = wrap(function () {
  3714. reject(new exceptions.NoSuchDatabase("Database " + dbName + " doesnt exist"));
  3715. });
  3716. }
  3717. else {
  3718. upgradeTransaction.onerror = eventRejectHandler(reject);
  3719. var oldVer = e.oldVersion > Math.pow(2, 62) ? 0 : e.oldVersion;
  3720. wasCreated = oldVer < 1;
  3721. db._novip.idbdb = req.result;
  3722. runUpgraders(db, oldVer / 10, upgradeTransaction, reject);
  3723. }
  3724. }, reject);
  3725. req.onsuccess = wrap(function () {
  3726. upgradeTransaction = null;
  3727. var idbdb = db._novip.idbdb = req.result;
  3728. var objectStoreNames = slice(idbdb.objectStoreNames);
  3729. if (objectStoreNames.length > 0)
  3730. try {
  3731. var tmpTrans = idbdb.transaction(safariMultiStoreFix(objectStoreNames), 'readonly');
  3732. if (state.autoSchema)
  3733. readGlobalSchema(db, idbdb, tmpTrans);
  3734. else {
  3735. adjustToExistingIndexNames(db, db._dbSchema, tmpTrans);
  3736. if (!verifyInstalledSchema(db, tmpTrans)) {
  3737. console.warn("Dexie SchemaDiff: Schema was extended without increasing the number passed to db.version(). Some queries may fail.");
  3738. }
  3739. }
  3740. generateMiddlewareStacks(db, tmpTrans);
  3741. }
  3742. catch (e) {
  3743. }
  3744. connections.push(db);
  3745. idbdb.onversionchange = wrap(function (ev) {
  3746. state.vcFired = true;
  3747. db.on("versionchange").fire(ev);
  3748. });
  3749. idbdb.onclose = wrap(function (ev) {
  3750. db.on("close").fire(ev);
  3751. });
  3752. if (wasCreated)
  3753. _onDatabaseCreated(db._deps, dbName);
  3754. resolve();
  3755. }, reject);
  3756. }); })]).then(function () {
  3757. throwIfCancelled();
  3758. state.onReadyBeingFired = [];
  3759. return DexiePromise.resolve(vip(function () { return db.on.ready.fire(db.vip); })).then(function fireRemainders() {
  3760. if (state.onReadyBeingFired.length > 0) {
  3761. var remainders_1 = state.onReadyBeingFired.reduce(promisableChain, nop);
  3762. state.onReadyBeingFired = [];
  3763. return DexiePromise.resolve(vip(function () { return remainders_1(db.vip); })).then(fireRemainders);
  3764. }
  3765. });
  3766. }).finally(function () {
  3767. state.onReadyBeingFired = null;
  3768. state.isBeingOpened = false;
  3769. }).then(function () {
  3770. return db;
  3771. }).catch(function (err) {
  3772. state.dbOpenError = err;
  3773. try {
  3774. upgradeTransaction && upgradeTransaction.abort();
  3775. }
  3776. catch (_a) { }
  3777. if (openCanceller === state.openCanceller) {
  3778. db._close();
  3779. }
  3780. return rejection(err);
  3781. }).finally(function () {
  3782. state.openComplete = true;
  3783. resolveDbReady();
  3784. });
  3785. }
  3786. function awaitIterator(iterator) {
  3787. var callNext = function (result) { return iterator.next(result); }, doThrow = function (error) { return iterator.throw(error); }, onSuccess = step(callNext), onError = step(doThrow);
  3788. function step(getNext) {
  3789. return function (val) {
  3790. var next = getNext(val), value = next.value;
  3791. return next.done ? value :
  3792. (!value || typeof value.then !== 'function' ?
  3793. isArray(value) ? Promise.all(value).then(onSuccess, onError) : onSuccess(value) :
  3794. value.then(onSuccess, onError));
  3795. };
  3796. }
  3797. return step(callNext)();
  3798. }
  3799. function extractTransactionArgs(mode, _tableArgs_, scopeFunc) {
  3800. var i = arguments.length;
  3801. if (i < 2)
  3802. throw new exceptions.InvalidArgument("Too few arguments");
  3803. var args = new Array(i - 1);
  3804. while (--i)
  3805. args[i - 1] = arguments[i];
  3806. scopeFunc = args.pop();
  3807. var tables = flatten(args);
  3808. return [mode, tables, scopeFunc];
  3809. }
  3810. function enterTransactionScope(db, mode, storeNames, parentTransaction, scopeFunc) {
  3811. return DexiePromise.resolve().then(function () {
  3812. var transless = PSD.transless || PSD;
  3813. var trans = db._createTransaction(mode, storeNames, db._dbSchema, parentTransaction);
  3814. var zoneProps = {
  3815. trans: trans,
  3816. transless: transless
  3817. };
  3818. if (parentTransaction) {
  3819. trans.idbtrans = parentTransaction.idbtrans;
  3820. }
  3821. else {
  3822. try {
  3823. trans.create();
  3824. db._state.PR1398_maxLoop = 3;
  3825. }
  3826. catch (ex) {
  3827. if (ex.name === errnames.InvalidState && db.isOpen() && --db._state.PR1398_maxLoop > 0) {
  3828. console.warn('Dexie: Need to reopen db');
  3829. db._close();
  3830. return db.open().then(function () { return enterTransactionScope(db, mode, storeNames, null, scopeFunc); });
  3831. }
  3832. return rejection(ex);
  3833. }
  3834. }
  3835. var scopeFuncIsAsync = isAsyncFunction(scopeFunc);
  3836. if (scopeFuncIsAsync) {
  3837. incrementExpectedAwaits();
  3838. }
  3839. var returnValue;
  3840. var promiseFollowed = DexiePromise.follow(function () {
  3841. returnValue = scopeFunc.call(trans, trans);
  3842. if (returnValue) {
  3843. if (scopeFuncIsAsync) {
  3844. var decrementor = decrementExpectedAwaits.bind(null, null);
  3845. returnValue.then(decrementor, decrementor);
  3846. }
  3847. else if (typeof returnValue.next === 'function' && typeof returnValue.throw === 'function') {
  3848. returnValue = awaitIterator(returnValue);
  3849. }
  3850. }
  3851. }, zoneProps);
  3852. return (returnValue && typeof returnValue.then === 'function' ?
  3853. DexiePromise.resolve(returnValue).then(function (x) { return trans.active ?
  3854. x
  3855. : rejection(new exceptions.PrematureCommit("Transaction committed too early. See http://bit.ly/2kdckMn")); })
  3856. : promiseFollowed.then(function () { return returnValue; })).then(function (x) {
  3857. if (parentTransaction)
  3858. trans._resolve();
  3859. return trans._completion.then(function () { return x; });
  3860. }).catch(function (e) {
  3861. trans._reject(e);
  3862. return rejection(e);
  3863. });
  3864. });
  3865. }
  3866. function pad(a, value, count) {
  3867. var result = isArray(a) ? a.slice() : [a];
  3868. for (var i = 0; i < count; ++i)
  3869. result.push(value);
  3870. return result;
  3871. }
  3872. function createVirtualIndexMiddleware(down) {
  3873. return __assign(__assign({}, down), { table: function (tableName) {
  3874. var table = down.table(tableName);
  3875. var schema = table.schema;
  3876. var indexLookup = {};
  3877. var allVirtualIndexes = [];
  3878. function addVirtualIndexes(keyPath, keyTail, lowLevelIndex) {
  3879. var keyPathAlias = getKeyPathAlias(keyPath);
  3880. var indexList = (indexLookup[keyPathAlias] = indexLookup[keyPathAlias] || []);
  3881. var keyLength = keyPath == null ? 0 : typeof keyPath === 'string' ? 1 : keyPath.length;
  3882. var isVirtual = keyTail > 0;
  3883. var virtualIndex = __assign(__assign({}, lowLevelIndex), { isVirtual: isVirtual, keyTail: keyTail, keyLength: keyLength, extractKey: getKeyExtractor(keyPath), unique: !isVirtual && lowLevelIndex.unique });
  3884. indexList.push(virtualIndex);
  3885. if (!virtualIndex.isPrimaryKey) {
  3886. allVirtualIndexes.push(virtualIndex);
  3887. }
  3888. if (keyLength > 1) {
  3889. var virtualKeyPath = keyLength === 2 ?
  3890. keyPath[0] :
  3891. keyPath.slice(0, keyLength - 1);
  3892. addVirtualIndexes(virtualKeyPath, keyTail + 1, lowLevelIndex);
  3893. }
  3894. indexList.sort(function (a, b) { return a.keyTail - b.keyTail; });
  3895. return virtualIndex;
  3896. }
  3897. var primaryKey = addVirtualIndexes(schema.primaryKey.keyPath, 0, schema.primaryKey);
  3898. indexLookup[":id"] = [primaryKey];
  3899. for (var _i = 0, _a = schema.indexes; _i < _a.length; _i++) {
  3900. var index = _a[_i];
  3901. addVirtualIndexes(index.keyPath, 0, index);
  3902. }
  3903. function findBestIndex(keyPath) {
  3904. var result = indexLookup[getKeyPathAlias(keyPath)];
  3905. return result && result[0];
  3906. }
  3907. function translateRange(range, keyTail) {
  3908. return {
  3909. type: range.type === 1 ?
  3910. 2 :
  3911. range.type,
  3912. lower: pad(range.lower, range.lowerOpen ? down.MAX_KEY : down.MIN_KEY, keyTail),
  3913. lowerOpen: true,
  3914. upper: pad(range.upper, range.upperOpen ? down.MIN_KEY : down.MAX_KEY, keyTail),
  3915. upperOpen: true
  3916. };
  3917. }
  3918. function translateRequest(req) {
  3919. var index = req.query.index;
  3920. return index.isVirtual ? __assign(__assign({}, req), { query: {
  3921. index: index,
  3922. range: translateRange(req.query.range, index.keyTail)
  3923. } }) : req;
  3924. }
  3925. var result = __assign(__assign({}, table), { schema: __assign(__assign({}, schema), { primaryKey: primaryKey, indexes: allVirtualIndexes, getIndexByKeyPath: findBestIndex }), count: function (req) {
  3926. return table.count(translateRequest(req));
  3927. }, query: function (req) {
  3928. return table.query(translateRequest(req));
  3929. }, openCursor: function (req) {
  3930. var _a = req.query.index, keyTail = _a.keyTail, isVirtual = _a.isVirtual, keyLength = _a.keyLength;
  3931. if (!isVirtual)
  3932. return table.openCursor(req);
  3933. function createVirtualCursor(cursor) {
  3934. function _continue(key) {
  3935. key != null ?
  3936. cursor.continue(pad(key, req.reverse ? down.MAX_KEY : down.MIN_KEY, keyTail)) :
  3937. req.unique ?
  3938. cursor.continue(cursor.key.slice(0, keyLength)
  3939. .concat(req.reverse
  3940. ? down.MIN_KEY
  3941. : down.MAX_KEY, keyTail)) :
  3942. cursor.continue();
  3943. }
  3944. var virtualCursor = Object.create(cursor, {
  3945. continue: { value: _continue },
  3946. continuePrimaryKey: {
  3947. value: function (key, primaryKey) {
  3948. cursor.continuePrimaryKey(pad(key, down.MAX_KEY, keyTail), primaryKey);
  3949. }
  3950. },
  3951. primaryKey: {
  3952. get: function () {
  3953. return cursor.primaryKey;
  3954. }
  3955. },
  3956. key: {
  3957. get: function () {
  3958. var key = cursor.key;
  3959. return keyLength === 1 ?
  3960. key[0] :
  3961. key.slice(0, keyLength);
  3962. }
  3963. },
  3964. value: {
  3965. get: function () {
  3966. return cursor.value;
  3967. }
  3968. }
  3969. });
  3970. return virtualCursor;
  3971. }
  3972. return table.openCursor(translateRequest(req))
  3973. .then(function (cursor) { return cursor && createVirtualCursor(cursor); });
  3974. } });
  3975. return result;
  3976. } });
  3977. }
  3978. var virtualIndexMiddleware = {
  3979. stack: "dbcore",
  3980. name: "VirtualIndexMiddleware",
  3981. level: 1,
  3982. create: createVirtualIndexMiddleware
  3983. };
  3984. function getObjectDiff(a, b, rv, prfx) {
  3985. rv = rv || {};
  3986. prfx = prfx || '';
  3987. keys(a).forEach(function (prop) {
  3988. if (!hasOwn(b, prop)) {
  3989. rv[prfx + prop] = undefined;
  3990. }
  3991. else {
  3992. var ap = a[prop], bp = b[prop];
  3993. if (typeof ap === 'object' && typeof bp === 'object' && ap && bp) {
  3994. var apTypeName = toStringTag(ap);
  3995. var bpTypeName = toStringTag(bp);
  3996. if (apTypeName !== bpTypeName) {
  3997. rv[prfx + prop] = b[prop];
  3998. }
  3999. else if (apTypeName === 'Object') {
  4000. getObjectDiff(ap, bp, rv, prfx + prop + '.');
  4001. }
  4002. else if (ap !== bp) {
  4003. rv[prfx + prop] = b[prop];
  4004. }
  4005. }
  4006. else if (ap !== bp)
  4007. rv[prfx + prop] = b[prop];
  4008. }
  4009. });
  4010. keys(b).forEach(function (prop) {
  4011. if (!hasOwn(a, prop)) {
  4012. rv[prfx + prop] = b[prop];
  4013. }
  4014. });
  4015. return rv;
  4016. }
  4017. function getEffectiveKeys(primaryKey, req) {
  4018. if (req.type === 'delete')
  4019. return req.keys;
  4020. return req.keys || req.values.map(primaryKey.extractKey);
  4021. }
  4022. var hooksMiddleware = {
  4023. stack: "dbcore",
  4024. name: "HooksMiddleware",
  4025. level: 2,
  4026. create: function (downCore) { return (__assign(__assign({}, downCore), { table: function (tableName) {
  4027. var downTable = downCore.table(tableName);
  4028. var primaryKey = downTable.schema.primaryKey;
  4029. var tableMiddleware = __assign(__assign({}, downTable), { mutate: function (req) {
  4030. var dxTrans = PSD.trans;
  4031. var _a = dxTrans.table(tableName).hook, deleting = _a.deleting, creating = _a.creating, updating = _a.updating;
  4032. switch (req.type) {
  4033. case 'add':
  4034. if (creating.fire === nop)
  4035. break;
  4036. return dxTrans._promise('readwrite', function () { return addPutOrDelete(req); }, true);
  4037. case 'put':
  4038. if (creating.fire === nop && updating.fire === nop)
  4039. break;
  4040. return dxTrans._promise('readwrite', function () { return addPutOrDelete(req); }, true);
  4041. case 'delete':
  4042. if (deleting.fire === nop)
  4043. break;
  4044. return dxTrans._promise('readwrite', function () { return addPutOrDelete(req); }, true);
  4045. case 'deleteRange':
  4046. if (deleting.fire === nop)
  4047. break;
  4048. return dxTrans._promise('readwrite', function () { return deleteRange(req); }, true);
  4049. }
  4050. return downTable.mutate(req);
  4051. function addPutOrDelete(req) {
  4052. var dxTrans = PSD.trans;
  4053. var keys = req.keys || getEffectiveKeys(primaryKey, req);
  4054. if (!keys)
  4055. throw new Error("Keys missing");
  4056. req = req.type === 'add' || req.type === 'put' ? __assign(__assign({}, req), { keys: keys }) : __assign({}, req);
  4057. if (req.type !== 'delete')
  4058. req.values = __spreadArray([], req.values, true);
  4059. if (req.keys)
  4060. req.keys = __spreadArray([], req.keys, true);
  4061. return getExistingValues(downTable, req, keys).then(function (existingValues) {
  4062. var contexts = keys.map(function (key, i) {
  4063. var existingValue = existingValues[i];
  4064. var ctx = { onerror: null, onsuccess: null };
  4065. if (req.type === 'delete') {
  4066. deleting.fire.call(ctx, key, existingValue, dxTrans);
  4067. }
  4068. else if (req.type === 'add' || existingValue === undefined) {
  4069. var generatedPrimaryKey = creating.fire.call(ctx, key, req.values[i], dxTrans);
  4070. if (key == null && generatedPrimaryKey != null) {
  4071. key = generatedPrimaryKey;
  4072. req.keys[i] = key;
  4073. if (!primaryKey.outbound) {
  4074. setByKeyPath(req.values[i], primaryKey.keyPath, key);
  4075. }
  4076. }
  4077. }
  4078. else {
  4079. var objectDiff = getObjectDiff(existingValue, req.values[i]);
  4080. var additionalChanges_1 = updating.fire.call(ctx, objectDiff, key, existingValue, dxTrans);
  4081. if (additionalChanges_1) {
  4082. var requestedValue_1 = req.values[i];
  4083. Object.keys(additionalChanges_1).forEach(function (keyPath) {
  4084. if (hasOwn(requestedValue_1, keyPath)) {
  4085. requestedValue_1[keyPath] = additionalChanges_1[keyPath];
  4086. }
  4087. else {
  4088. setByKeyPath(requestedValue_1, keyPath, additionalChanges_1[keyPath]);
  4089. }
  4090. });
  4091. }
  4092. }
  4093. return ctx;
  4094. });
  4095. return downTable.mutate(req).then(function (_a) {
  4096. var failures = _a.failures, results = _a.results, numFailures = _a.numFailures, lastResult = _a.lastResult;
  4097. for (var i = 0; i < keys.length; ++i) {
  4098. var primKey = results ? results[i] : keys[i];
  4099. var ctx = contexts[i];
  4100. if (primKey == null) {
  4101. ctx.onerror && ctx.onerror(failures[i]);
  4102. }
  4103. else {
  4104. ctx.onsuccess && ctx.onsuccess(req.type === 'put' && existingValues[i] ?
  4105. req.values[i] :
  4106. primKey
  4107. );
  4108. }
  4109. }
  4110. return { failures: failures, results: results, numFailures: numFailures, lastResult: lastResult };
  4111. }).catch(function (error) {
  4112. contexts.forEach(function (ctx) { return ctx.onerror && ctx.onerror(error); });
  4113. return Promise.reject(error);
  4114. });
  4115. });
  4116. }
  4117. function deleteRange(req) {
  4118. return deleteNextChunk(req.trans, req.range, 10000);
  4119. }
  4120. function deleteNextChunk(trans, range, limit) {
  4121. return downTable.query({ trans: trans, values: false, query: { index: primaryKey, range: range }, limit: limit })
  4122. .then(function (_a) {
  4123. var result = _a.result;
  4124. return addPutOrDelete({ type: 'delete', keys: result, trans: trans }).then(function (res) {
  4125. if (res.numFailures > 0)
  4126. return Promise.reject(res.failures[0]);
  4127. if (result.length < limit) {
  4128. return { failures: [], numFailures: 0, lastResult: undefined };
  4129. }
  4130. else {
  4131. return deleteNextChunk(trans, __assign(__assign({}, range), { lower: result[result.length - 1], lowerOpen: true }), limit);
  4132. }
  4133. });
  4134. });
  4135. }
  4136. } });
  4137. return tableMiddleware;
  4138. } })); }
  4139. };
  4140. function getExistingValues(table, req, effectiveKeys) {
  4141. return req.type === "add"
  4142. ? Promise.resolve([])
  4143. : table.getMany({ trans: req.trans, keys: effectiveKeys, cache: "immutable" });
  4144. }
  4145. function getFromTransactionCache(keys, cache, clone) {
  4146. try {
  4147. if (!cache)
  4148. return null;
  4149. if (cache.keys.length < keys.length)
  4150. return null;
  4151. var result = [];
  4152. for (var i = 0, j = 0; i < cache.keys.length && j < keys.length; ++i) {
  4153. if (cmp(cache.keys[i], keys[j]) !== 0)
  4154. continue;
  4155. result.push(clone ? deepClone(cache.values[i]) : cache.values[i]);
  4156. ++j;
  4157. }
  4158. return result.length === keys.length ? result : null;
  4159. }
  4160. catch (_a) {
  4161. return null;
  4162. }
  4163. }
  4164. var cacheExistingValuesMiddleware = {
  4165. stack: "dbcore",
  4166. level: -1,
  4167. create: function (core) {
  4168. return {
  4169. table: function (tableName) {
  4170. var table = core.table(tableName);
  4171. return __assign(__assign({}, table), { getMany: function (req) {
  4172. if (!req.cache) {
  4173. return table.getMany(req);
  4174. }
  4175. var cachedResult = getFromTransactionCache(req.keys, req.trans["_cache"], req.cache === "clone");
  4176. if (cachedResult) {
  4177. return DexiePromise.resolve(cachedResult);
  4178. }
  4179. return table.getMany(req).then(function (res) {
  4180. req.trans["_cache"] = {
  4181. keys: req.keys,
  4182. values: req.cache === "clone" ? deepClone(res) : res,
  4183. };
  4184. return res;
  4185. });
  4186. }, mutate: function (req) {
  4187. if (req.type !== "add")
  4188. req.trans["_cache"] = null;
  4189. return table.mutate(req);
  4190. } });
  4191. },
  4192. };
  4193. },
  4194. };
  4195. var _a;
  4196. function isEmptyRange(node) {
  4197. return !("from" in node);
  4198. }
  4199. var RangeSet = function (fromOrTree, to) {
  4200. if (this) {
  4201. extend(this, arguments.length ? { d: 1, from: fromOrTree, to: arguments.length > 1 ? to : fromOrTree } : { d: 0 });
  4202. }
  4203. else {
  4204. var rv = new RangeSet();
  4205. if (fromOrTree && ("d" in fromOrTree)) {
  4206. extend(rv, fromOrTree);
  4207. }
  4208. return rv;
  4209. }
  4210. };
  4211. props(RangeSet.prototype, (_a = {
  4212. add: function (rangeSet) {
  4213. mergeRanges(this, rangeSet);
  4214. return this;
  4215. },
  4216. addKey: function (key) {
  4217. addRange(this, key, key);
  4218. return this;
  4219. },
  4220. addKeys: function (keys) {
  4221. var _this = this;
  4222. keys.forEach(function (key) { return addRange(_this, key, key); });
  4223. return this;
  4224. }
  4225. },
  4226. _a[iteratorSymbol] = function () {
  4227. return getRangeSetIterator(this);
  4228. },
  4229. _a));
  4230. function addRange(target, from, to) {
  4231. var diff = cmp(from, to);
  4232. if (isNaN(diff))
  4233. return;
  4234. if (diff > 0)
  4235. throw RangeError();
  4236. if (isEmptyRange(target))
  4237. return extend(target, { from: from, to: to, d: 1 });
  4238. var left = target.l;
  4239. var right = target.r;
  4240. if (cmp(to, target.from) < 0) {
  4241. left
  4242. ? addRange(left, from, to)
  4243. : (target.l = { from: from, to: to, d: 1, l: null, r: null });
  4244. return rebalance(target);
  4245. }
  4246. if (cmp(from, target.to) > 0) {
  4247. right
  4248. ? addRange(right, from, to)
  4249. : (target.r = { from: from, to: to, d: 1, l: null, r: null });
  4250. return rebalance(target);
  4251. }
  4252. if (cmp(from, target.from) < 0) {
  4253. target.from = from;
  4254. target.l = null;
  4255. target.d = right ? right.d + 1 : 1;
  4256. }
  4257. if (cmp(to, target.to) > 0) {
  4258. target.to = to;
  4259. target.r = null;
  4260. target.d = target.l ? target.l.d + 1 : 1;
  4261. }
  4262. var rightWasCutOff = !target.r;
  4263. if (left && !target.l) {
  4264. mergeRanges(target, left);
  4265. }
  4266. if (right && rightWasCutOff) {
  4267. mergeRanges(target, right);
  4268. }
  4269. }
  4270. function mergeRanges(target, newSet) {
  4271. function _addRangeSet(target, _a) {
  4272. var from = _a.from, to = _a.to, l = _a.l, r = _a.r;
  4273. addRange(target, from, to);
  4274. if (l)
  4275. _addRangeSet(target, l);
  4276. if (r)
  4277. _addRangeSet(target, r);
  4278. }
  4279. if (!isEmptyRange(newSet))
  4280. _addRangeSet(target, newSet);
  4281. }
  4282. function rangesOverlap(rangeSet1, rangeSet2) {
  4283. var i1 = getRangeSetIterator(rangeSet2);
  4284. var nextResult1 = i1.next();
  4285. if (nextResult1.done)
  4286. return false;
  4287. var a = nextResult1.value;
  4288. var i2 = getRangeSetIterator(rangeSet1);
  4289. var nextResult2 = i2.next(a.from);
  4290. var b = nextResult2.value;
  4291. while (!nextResult1.done && !nextResult2.done) {
  4292. if (cmp(b.from, a.to) <= 0 && cmp(b.to, a.from) >= 0)
  4293. return true;
  4294. cmp(a.from, b.from) < 0
  4295. ? (a = (nextResult1 = i1.next(b.from)).value)
  4296. : (b = (nextResult2 = i2.next(a.from)).value);
  4297. }
  4298. return false;
  4299. }
  4300. function getRangeSetIterator(node) {
  4301. var state = isEmptyRange(node) ? null : { s: 0, n: node };
  4302. return {
  4303. next: function (key) {
  4304. var keyProvided = arguments.length > 0;
  4305. while (state) {
  4306. switch (state.s) {
  4307. case 0:
  4308. state.s = 1;
  4309. if (keyProvided) {
  4310. while (state.n.l && cmp(key, state.n.from) < 0)
  4311. state = { up: state, n: state.n.l, s: 1 };
  4312. }
  4313. else {
  4314. while (state.n.l)
  4315. state = { up: state, n: state.n.l, s: 1 };
  4316. }
  4317. case 1:
  4318. state.s = 2;
  4319. if (!keyProvided || cmp(key, state.n.to) <= 0)
  4320. return { value: state.n, done: false };
  4321. case 2:
  4322. if (state.n.r) {
  4323. state.s = 3;
  4324. state = { up: state, n: state.n.r, s: 0 };
  4325. continue;
  4326. }
  4327. case 3:
  4328. state = state.up;
  4329. }
  4330. }
  4331. return { done: true };
  4332. },
  4333. };
  4334. }
  4335. function rebalance(target) {
  4336. var _a, _b;
  4337. var diff = (((_a = target.r) === null || _a === void 0 ? void 0 : _a.d) || 0) - (((_b = target.l) === null || _b === void 0 ? void 0 : _b.d) || 0);
  4338. var r = diff > 1 ? "r" : diff < -1 ? "l" : "";
  4339. if (r) {
  4340. var l = r === "r" ? "l" : "r";
  4341. var rootClone = __assign({}, target);
  4342. var oldRootRight = target[r];
  4343. target.from = oldRootRight.from;
  4344. target.to = oldRootRight.to;
  4345. target[r] = oldRootRight[r];
  4346. rootClone[r] = oldRootRight[l];
  4347. target[l] = rootClone;
  4348. rootClone.d = computeDepth(rootClone);
  4349. }
  4350. target.d = computeDepth(target);
  4351. }
  4352. function computeDepth(_a) {
  4353. var r = _a.r, l = _a.l;
  4354. return (r ? (l ? Math.max(r.d, l.d) : r.d) : l ? l.d : 0) + 1;
  4355. }
  4356. var observabilityMiddleware = {
  4357. stack: "dbcore",
  4358. level: 0,
  4359. create: function (core) {
  4360. var dbName = core.schema.name;
  4361. var FULL_RANGE = new RangeSet(core.MIN_KEY, core.MAX_KEY);
  4362. return __assign(__assign({}, core), { table: function (tableName) {
  4363. var table = core.table(tableName);
  4364. var schema = table.schema;
  4365. var primaryKey = schema.primaryKey;
  4366. var extractKey = primaryKey.extractKey, outbound = primaryKey.outbound;
  4367. var tableClone = __assign(__assign({}, table), { mutate: function (req) {
  4368. var trans = req.trans;
  4369. var mutatedParts = trans.mutatedParts || (trans.mutatedParts = {});
  4370. var getRangeSet = function (indexName) {
  4371. var part = "idb://" + dbName + "/" + tableName + "/" + indexName;
  4372. return (mutatedParts[part] ||
  4373. (mutatedParts[part] = new RangeSet()));
  4374. };
  4375. var pkRangeSet = getRangeSet("");
  4376. var delsRangeSet = getRangeSet(":dels");
  4377. var type = req.type;
  4378. var _a = req.type === "deleteRange"
  4379. ? [req.range]
  4380. : req.type === "delete"
  4381. ? [req.keys]
  4382. : req.values.length < 50
  4383. ? [[], req.values]
  4384. : [], keys = _a[0], newObjs = _a[1];
  4385. var oldCache = req.trans["_cache"];
  4386. return table.mutate(req).then(function (res) {
  4387. if (isArray(keys)) {
  4388. if (type !== "delete")
  4389. keys = res.results;
  4390. pkRangeSet.addKeys(keys);
  4391. var oldObjs = getFromTransactionCache(keys, oldCache);
  4392. if (!oldObjs && type !== "add") {
  4393. delsRangeSet.addKeys(keys);
  4394. }
  4395. if (oldObjs || newObjs) {
  4396. trackAffectedIndexes(getRangeSet, schema, oldObjs, newObjs);
  4397. }
  4398. }
  4399. else if (keys) {
  4400. var range = { from: keys.lower, to: keys.upper };
  4401. delsRangeSet.add(range);
  4402. pkRangeSet.add(range);
  4403. }
  4404. else {
  4405. pkRangeSet.add(FULL_RANGE);
  4406. delsRangeSet.add(FULL_RANGE);
  4407. schema.indexes.forEach(function (idx) { return getRangeSet(idx.name).add(FULL_RANGE); });
  4408. }
  4409. return res;
  4410. });
  4411. } });
  4412. var getRange = function (_a) {
  4413. var _b, _c;
  4414. var _d = _a.query, index = _d.index, range = _d.range;
  4415. return [
  4416. index,
  4417. new RangeSet((_b = range.lower) !== null && _b !== void 0 ? _b : core.MIN_KEY, (_c = range.upper) !== null && _c !== void 0 ? _c : core.MAX_KEY),
  4418. ];
  4419. };
  4420. var readSubscribers = {
  4421. get: function (req) { return [primaryKey, new RangeSet(req.key)]; },
  4422. getMany: function (req) { return [primaryKey, new RangeSet().addKeys(req.keys)]; },
  4423. count: getRange,
  4424. query: getRange,
  4425. openCursor: getRange,
  4426. };
  4427. keys(readSubscribers).forEach(function (method) {
  4428. tableClone[method] = function (req) {
  4429. var subscr = PSD.subscr;
  4430. if (subscr) {
  4431. var getRangeSet = function (indexName) {
  4432. var part = "idb://" + dbName + "/" + tableName + "/" + indexName;
  4433. return (subscr[part] ||
  4434. (subscr[part] = new RangeSet()));
  4435. };
  4436. var pkRangeSet_1 = getRangeSet("");
  4437. var delsRangeSet_1 = getRangeSet(":dels");
  4438. var _a = readSubscribers[method](req), queriedIndex = _a[0], queriedRanges = _a[1];
  4439. getRangeSet(queriedIndex.name || "").add(queriedRanges);
  4440. if (!queriedIndex.isPrimaryKey) {
  4441. if (method === "count") {
  4442. delsRangeSet_1.add(FULL_RANGE);
  4443. }
  4444. else {
  4445. var keysPromise_1 = method === "query" &&
  4446. outbound &&
  4447. req.values &&
  4448. table.query(__assign(__assign({}, req), { values: false }));
  4449. return table[method].apply(this, arguments).then(function (res) {
  4450. if (method === "query") {
  4451. if (outbound && req.values) {
  4452. return keysPromise_1.then(function (_a) {
  4453. var resultingKeys = _a.result;
  4454. pkRangeSet_1.addKeys(resultingKeys);
  4455. return res;
  4456. });
  4457. }
  4458. var pKeys = req.values
  4459. ? res.result.map(extractKey)
  4460. : res.result;
  4461. if (req.values) {
  4462. pkRangeSet_1.addKeys(pKeys);
  4463. }
  4464. else {
  4465. delsRangeSet_1.addKeys(pKeys);
  4466. }
  4467. }
  4468. else if (method === "openCursor") {
  4469. var cursor_1 = res;
  4470. var wantValues_1 = req.values;
  4471. return (cursor_1 &&
  4472. Object.create(cursor_1, {
  4473. key: {
  4474. get: function () {
  4475. delsRangeSet_1.addKey(cursor_1.primaryKey);
  4476. return cursor_1.key;
  4477. },
  4478. },
  4479. primaryKey: {
  4480. get: function () {
  4481. var pkey = cursor_1.primaryKey;
  4482. delsRangeSet_1.addKey(pkey);
  4483. return pkey;
  4484. },
  4485. },
  4486. value: {
  4487. get: function () {
  4488. wantValues_1 && pkRangeSet_1.addKey(cursor_1.primaryKey);
  4489. return cursor_1.value;
  4490. },
  4491. },
  4492. }));
  4493. }
  4494. return res;
  4495. });
  4496. }
  4497. }
  4498. }
  4499. return table[method].apply(this, arguments);
  4500. };
  4501. });
  4502. return tableClone;
  4503. } });
  4504. },
  4505. };
  4506. function trackAffectedIndexes(getRangeSet, schema, oldObjs, newObjs) {
  4507. function addAffectedIndex(ix) {
  4508. var rangeSet = getRangeSet(ix.name || "");
  4509. function extractKey(obj) {
  4510. return obj != null ? ix.extractKey(obj) : null;
  4511. }
  4512. var addKeyOrKeys = function (key) { return ix.multiEntry && isArray(key)
  4513. ? key.forEach(function (key) { return rangeSet.addKey(key); })
  4514. : rangeSet.addKey(key); };
  4515. (oldObjs || newObjs).forEach(function (_, i) {
  4516. var oldKey = oldObjs && extractKey(oldObjs[i]);
  4517. var newKey = newObjs && extractKey(newObjs[i]);
  4518. if (cmp(oldKey, newKey) !== 0) {
  4519. if (oldKey != null)
  4520. addKeyOrKeys(oldKey);
  4521. if (newKey != null)
  4522. addKeyOrKeys(newKey);
  4523. }
  4524. });
  4525. }
  4526. schema.indexes.forEach(addAffectedIndex);
  4527. }
  4528. var Dexie$1 = (function () {
  4529. function Dexie(name, options) {
  4530. var _this = this;
  4531. this._middlewares = {};
  4532. this.verno = 0;
  4533. var deps = Dexie.dependencies;
  4534. this._options = options = __assign({
  4535. addons: Dexie.addons, autoOpen: true,
  4536. indexedDB: deps.indexedDB, IDBKeyRange: deps.IDBKeyRange }, options);
  4537. this._deps = {
  4538. indexedDB: options.indexedDB,
  4539. IDBKeyRange: options.IDBKeyRange
  4540. };
  4541. var addons = options.addons;
  4542. this._dbSchema = {};
  4543. this._versions = [];
  4544. this._storeNames = [];
  4545. this._allTables = {};
  4546. this.idbdb = null;
  4547. this._novip = this;
  4548. var state = {
  4549. dbOpenError: null,
  4550. isBeingOpened: false,
  4551. onReadyBeingFired: null,
  4552. openComplete: false,
  4553. dbReadyResolve: nop,
  4554. dbReadyPromise: null,
  4555. cancelOpen: nop,
  4556. openCanceller: null,
  4557. autoSchema: true,
  4558. PR1398_maxLoop: 3
  4559. };
  4560. state.dbReadyPromise = new DexiePromise(function (resolve) {
  4561. state.dbReadyResolve = resolve;
  4562. });
  4563. state.openCanceller = new DexiePromise(function (_, reject) {
  4564. state.cancelOpen = reject;
  4565. });
  4566. this._state = state;
  4567. this.name = name;
  4568. this.on = Events(this, "populate", "blocked", "versionchange", "close", { ready: [promisableChain, nop] });
  4569. this.on.ready.subscribe = override(this.on.ready.subscribe, function (subscribe) {
  4570. return function (subscriber, bSticky) {
  4571. Dexie.vip(function () {
  4572. var state = _this._state;
  4573. if (state.openComplete) {
  4574. if (!state.dbOpenError)
  4575. DexiePromise.resolve().then(subscriber);
  4576. if (bSticky)
  4577. subscribe(subscriber);
  4578. }
  4579. else if (state.onReadyBeingFired) {
  4580. state.onReadyBeingFired.push(subscriber);
  4581. if (bSticky)
  4582. subscribe(subscriber);
  4583. }
  4584. else {
  4585. subscribe(subscriber);
  4586. var db_1 = _this;
  4587. if (!bSticky)
  4588. subscribe(function unsubscribe() {
  4589. db_1.on.ready.unsubscribe(subscriber);
  4590. db_1.on.ready.unsubscribe(unsubscribe);
  4591. });
  4592. }
  4593. });
  4594. };
  4595. });
  4596. this.Collection = createCollectionConstructor(this);
  4597. this.Table = createTableConstructor(this);
  4598. this.Transaction = createTransactionConstructor(this);
  4599. this.Version = createVersionConstructor(this);
  4600. this.WhereClause = createWhereClauseConstructor(this);
  4601. this.on("versionchange", function (ev) {
  4602. if (ev.newVersion > 0)
  4603. console.warn("Another connection wants to upgrade database '" + _this.name + "'. Closing db now to resume the upgrade.");
  4604. else
  4605. console.warn("Another connection wants to delete database '" + _this.name + "'. Closing db now to resume the delete request.");
  4606. _this.close();
  4607. });
  4608. this.on("blocked", function (ev) {
  4609. if (!ev.newVersion || ev.newVersion < ev.oldVersion)
  4610. console.warn("Dexie.delete('" + _this.name + "') was blocked");
  4611. else
  4612. console.warn("Upgrade '" + _this.name + "' blocked by other connection holding version " + ev.oldVersion / 10);
  4613. });
  4614. this._maxKey = getMaxKey(options.IDBKeyRange);
  4615. this._createTransaction = function (mode, storeNames, dbschema, parentTransaction) { return new _this.Transaction(mode, storeNames, dbschema, _this._options.chromeTransactionDurability, parentTransaction); };
  4616. this._fireOnBlocked = function (ev) {
  4617. _this.on("blocked").fire(ev);
  4618. connections
  4619. .filter(function (c) { return c.name === _this.name && c !== _this && !c._state.vcFired; })
  4620. .map(function (c) { return c.on("versionchange").fire(ev); });
  4621. };
  4622. this.use(virtualIndexMiddleware);
  4623. this.use(hooksMiddleware);
  4624. this.use(observabilityMiddleware);
  4625. this.use(cacheExistingValuesMiddleware);
  4626. this.vip = Object.create(this, { _vip: { value: true } });
  4627. addons.forEach(function (addon) { return addon(_this); });
  4628. }
  4629. Dexie.prototype.version = function (versionNumber) {
  4630. if (isNaN(versionNumber) || versionNumber < 0.1)
  4631. throw new exceptions.Type("Given version is not a positive number");
  4632. versionNumber = Math.round(versionNumber * 10) / 10;
  4633. if (this.idbdb || this._state.isBeingOpened)
  4634. throw new exceptions.Schema("Cannot add version when database is open");
  4635. this.verno = Math.max(this.verno, versionNumber);
  4636. var versions = this._versions;
  4637. var versionInstance = versions.filter(function (v) { return v._cfg.version === versionNumber; })[0];
  4638. if (versionInstance)
  4639. return versionInstance;
  4640. versionInstance = new this.Version(versionNumber);
  4641. versions.push(versionInstance);
  4642. versions.sort(lowerVersionFirst);
  4643. versionInstance.stores({});
  4644. this._state.autoSchema = false;
  4645. return versionInstance;
  4646. };
  4647. Dexie.prototype._whenReady = function (fn) {
  4648. var _this = this;
  4649. return (this.idbdb && (this._state.openComplete || PSD.letThrough || this._vip)) ? fn() : new DexiePromise(function (resolve, reject) {
  4650. if (_this._state.openComplete) {
  4651. return reject(new exceptions.DatabaseClosed(_this._state.dbOpenError));
  4652. }
  4653. if (!_this._state.isBeingOpened) {
  4654. if (!_this._options.autoOpen) {
  4655. reject(new exceptions.DatabaseClosed());
  4656. return;
  4657. }
  4658. _this.open().catch(nop);
  4659. }
  4660. _this._state.dbReadyPromise.then(resolve, reject);
  4661. }).then(fn);
  4662. };
  4663. Dexie.prototype.use = function (_a) {
  4664. var stack = _a.stack, create = _a.create, level = _a.level, name = _a.name;
  4665. if (name)
  4666. this.unuse({ stack: stack, name: name });
  4667. var middlewares = this._middlewares[stack] || (this._middlewares[stack] = []);
  4668. middlewares.push({ stack: stack, create: create, level: level == null ? 10 : level, name: name });
  4669. middlewares.sort(function (a, b) { return a.level - b.level; });
  4670. return this;
  4671. };
  4672. Dexie.prototype.unuse = function (_a) {
  4673. var stack = _a.stack, name = _a.name, create = _a.create;
  4674. if (stack && this._middlewares[stack]) {
  4675. this._middlewares[stack] = this._middlewares[stack].filter(function (mw) {
  4676. return create ? mw.create !== create :
  4677. name ? mw.name !== name :
  4678. false;
  4679. });
  4680. }
  4681. return this;
  4682. };
  4683. Dexie.prototype.open = function () {
  4684. return dexieOpen(this);
  4685. };
  4686. Dexie.prototype._close = function () {
  4687. var state = this._state;
  4688. var idx = connections.indexOf(this);
  4689. if (idx >= 0)
  4690. connections.splice(idx, 1);
  4691. if (this.idbdb) {
  4692. try {
  4693. this.idbdb.close();
  4694. }
  4695. catch (e) { }
  4696. this._novip.idbdb = null;
  4697. }
  4698. state.dbReadyPromise = new DexiePromise(function (resolve) {
  4699. state.dbReadyResolve = resolve;
  4700. });
  4701. state.openCanceller = new DexiePromise(function (_, reject) {
  4702. state.cancelOpen = reject;
  4703. });
  4704. };
  4705. Dexie.prototype.close = function () {
  4706. this._close();
  4707. var state = this._state;
  4708. this._options.autoOpen = false;
  4709. state.dbOpenError = new exceptions.DatabaseClosed();
  4710. if (state.isBeingOpened)
  4711. state.cancelOpen(state.dbOpenError);
  4712. };
  4713. Dexie.prototype.delete = function () {
  4714. var _this = this;
  4715. var hasArguments = arguments.length > 0;
  4716. var state = this._state;
  4717. return new DexiePromise(function (resolve, reject) {
  4718. var doDelete = function () {
  4719. _this.close();
  4720. var req = _this._deps.indexedDB.deleteDatabase(_this.name);
  4721. req.onsuccess = wrap(function () {
  4722. _onDatabaseDeleted(_this._deps, _this.name);
  4723. resolve();
  4724. });
  4725. req.onerror = eventRejectHandler(reject);
  4726. req.onblocked = _this._fireOnBlocked;
  4727. };
  4728. if (hasArguments)
  4729. throw new exceptions.InvalidArgument("Arguments not allowed in db.delete()");
  4730. if (state.isBeingOpened) {
  4731. state.dbReadyPromise.then(doDelete);
  4732. }
  4733. else {
  4734. doDelete();
  4735. }
  4736. });
  4737. };
  4738. Dexie.prototype.backendDB = function () {
  4739. return this.idbdb;
  4740. };
  4741. Dexie.prototype.isOpen = function () {
  4742. return this.idbdb !== null;
  4743. };
  4744. Dexie.prototype.hasBeenClosed = function () {
  4745. var dbOpenError = this._state.dbOpenError;
  4746. return dbOpenError && (dbOpenError.name === 'DatabaseClosed');
  4747. };
  4748. Dexie.prototype.hasFailed = function () {
  4749. return this._state.dbOpenError !== null;
  4750. };
  4751. Dexie.prototype.dynamicallyOpened = function () {
  4752. return this._state.autoSchema;
  4753. };
  4754. Object.defineProperty(Dexie.prototype, "tables", {
  4755. get: function () {
  4756. var _this = this;
  4757. return keys(this._allTables).map(function (name) { return _this._allTables[name]; });
  4758. },
  4759. enumerable: false,
  4760. configurable: true
  4761. });
  4762. Dexie.prototype.transaction = function () {
  4763. var args = extractTransactionArgs.apply(this, arguments);
  4764. return this._transaction.apply(this, args);
  4765. };
  4766. Dexie.prototype._transaction = function (mode, tables, scopeFunc) {
  4767. var _this = this;
  4768. var parentTransaction = PSD.trans;
  4769. if (!parentTransaction || parentTransaction.db !== this || mode.indexOf('!') !== -1)
  4770. parentTransaction = null;
  4771. var onlyIfCompatible = mode.indexOf('?') !== -1;
  4772. mode = mode.replace('!', '').replace('?', '');
  4773. var idbMode, storeNames;
  4774. try {
  4775. storeNames = tables.map(function (table) {
  4776. var storeName = table instanceof _this.Table ? table.name : table;
  4777. if (typeof storeName !== 'string')
  4778. throw new TypeError("Invalid table argument to Dexie.transaction(). Only Table or String are allowed");
  4779. return storeName;
  4780. });
  4781. if (mode == "r" || mode === READONLY)
  4782. idbMode = READONLY;
  4783. else if (mode == "rw" || mode == READWRITE)
  4784. idbMode = READWRITE;
  4785. else
  4786. throw new exceptions.InvalidArgument("Invalid transaction mode: " + mode);
  4787. if (parentTransaction) {
  4788. if (parentTransaction.mode === READONLY && idbMode === READWRITE) {
  4789. if (onlyIfCompatible) {
  4790. parentTransaction = null;
  4791. }
  4792. else
  4793. throw new exceptions.SubTransaction("Cannot enter a sub-transaction with READWRITE mode when parent transaction is READONLY");
  4794. }
  4795. if (parentTransaction) {
  4796. storeNames.forEach(function (storeName) {
  4797. if (parentTransaction && parentTransaction.storeNames.indexOf(storeName) === -1) {
  4798. if (onlyIfCompatible) {
  4799. parentTransaction = null;
  4800. }
  4801. else
  4802. throw new exceptions.SubTransaction("Table " + storeName +
  4803. " not included in parent transaction.");
  4804. }
  4805. });
  4806. }
  4807. if (onlyIfCompatible && parentTransaction && !parentTransaction.active) {
  4808. parentTransaction = null;
  4809. }
  4810. }
  4811. }
  4812. catch (e) {
  4813. return parentTransaction ?
  4814. parentTransaction._promise(null, function (_, reject) { reject(e); }) :
  4815. rejection(e);
  4816. }
  4817. var enterTransaction = enterTransactionScope.bind(null, this, idbMode, storeNames, parentTransaction, scopeFunc);
  4818. return (parentTransaction ?
  4819. parentTransaction._promise(idbMode, enterTransaction, "lock") :
  4820. PSD.trans ?
  4821. usePSD(PSD.transless, function () { return _this._whenReady(enterTransaction); }) :
  4822. this._whenReady(enterTransaction));
  4823. };
  4824. Dexie.prototype.table = function (tableName) {
  4825. if (!hasOwn(this._allTables, tableName)) {
  4826. throw new exceptions.InvalidTable("Table " + tableName + " does not exist");
  4827. }
  4828. return this._allTables[tableName];
  4829. };
  4830. return Dexie;
  4831. }());
  4832. var symbolObservable = typeof Symbol !== "undefined" && "observable" in Symbol
  4833. ? Symbol.observable
  4834. : "@@observable";
  4835. var Observable = (function () {
  4836. function Observable(subscribe) {
  4837. this._subscribe = subscribe;
  4838. }
  4839. Observable.prototype.subscribe = function (x, error, complete) {
  4840. return this._subscribe(!x || typeof x === "function" ? { next: x, error: error, complete: complete } : x);
  4841. };
  4842. Observable.prototype[symbolObservable] = function () {
  4843. return this;
  4844. };
  4845. return Observable;
  4846. }());
  4847. function extendObservabilitySet(target, newSet) {
  4848. keys(newSet).forEach(function (part) {
  4849. var rangeSet = target[part] || (target[part] = new RangeSet());
  4850. mergeRanges(rangeSet, newSet[part]);
  4851. });
  4852. return target;
  4853. }
  4854. function liveQuery(querier) {
  4855. return new Observable(function (observer) {
  4856. var scopeFuncIsAsync = isAsyncFunction(querier);
  4857. function execute(subscr) {
  4858. if (scopeFuncIsAsync) {
  4859. incrementExpectedAwaits();
  4860. }
  4861. var exec = function () { return newScope(querier, { subscr: subscr, trans: null }); };
  4862. var rv = PSD.trans
  4863. ?
  4864. usePSD(PSD.transless, exec)
  4865. : exec();
  4866. if (scopeFuncIsAsync) {
  4867. rv.then(decrementExpectedAwaits, decrementExpectedAwaits);
  4868. }
  4869. return rv;
  4870. }
  4871. var closed = false;
  4872. var accumMuts = {};
  4873. var currentObs = {};
  4874. var subscription = {
  4875. get closed() {
  4876. return closed;
  4877. },
  4878. unsubscribe: function () {
  4879. closed = true;
  4880. globalEvents.storagemutated.unsubscribe(mutationListener);
  4881. },
  4882. };
  4883. observer.start && observer.start(subscription);
  4884. var querying = false, startedListening = false;
  4885. function shouldNotify() {
  4886. return keys(currentObs).some(function (key) {
  4887. return accumMuts[key] && rangesOverlap(accumMuts[key], currentObs[key]);
  4888. });
  4889. }
  4890. var mutationListener = function (parts) {
  4891. extendObservabilitySet(accumMuts, parts);
  4892. if (shouldNotify()) {
  4893. doQuery();
  4894. }
  4895. };
  4896. var doQuery = function () {
  4897. if (querying || closed)
  4898. return;
  4899. accumMuts = {};
  4900. var subscr = {};
  4901. var ret = execute(subscr);
  4902. if (!startedListening) {
  4903. globalEvents(DEXIE_STORAGE_MUTATED_EVENT_NAME, mutationListener);
  4904. startedListening = true;
  4905. }
  4906. querying = true;
  4907. Promise.resolve(ret).then(function (result) {
  4908. querying = false;
  4909. if (closed)
  4910. return;
  4911. if (shouldNotify()) {
  4912. doQuery();
  4913. }
  4914. else {
  4915. accumMuts = {};
  4916. currentObs = subscr;
  4917. observer.next && observer.next(result);
  4918. }
  4919. }, function (err) {
  4920. querying = false;
  4921. observer.error && observer.error(err);
  4922. subscription.unsubscribe();
  4923. });
  4924. };
  4925. doQuery();
  4926. return subscription;
  4927. });
  4928. }
  4929. var domDeps;
  4930. try {
  4931. domDeps = {
  4932. indexedDB: _global.indexedDB || _global.mozIndexedDB || _global.webkitIndexedDB || _global.msIndexedDB,
  4933. IDBKeyRange: _global.IDBKeyRange || _global.webkitIDBKeyRange
  4934. };
  4935. }
  4936. catch (e) {
  4937. domDeps = { indexedDB: null, IDBKeyRange: null };
  4938. }
  4939. var Dexie = Dexie$1;
  4940. props(Dexie, __assign(__assign({}, fullNameExceptions), {
  4941. delete: function (databaseName) {
  4942. var db = new Dexie(databaseName, { addons: [] });
  4943. return db.delete();
  4944. },
  4945. exists: function (name) {
  4946. return new Dexie(name, { addons: [] }).open().then(function (db) {
  4947. db.close();
  4948. return true;
  4949. }).catch('NoSuchDatabaseError', function () { return false; });
  4950. },
  4951. getDatabaseNames: function (cb) {
  4952. try {
  4953. return getDatabaseNames(Dexie.dependencies).then(cb);
  4954. }
  4955. catch (_a) {
  4956. return rejection(new exceptions.MissingAPI());
  4957. }
  4958. },
  4959. defineClass: function () {
  4960. function Class(content) {
  4961. extend(this, content);
  4962. }
  4963. return Class;
  4964. }, ignoreTransaction: function (scopeFunc) {
  4965. return PSD.trans ?
  4966. usePSD(PSD.transless, scopeFunc) :
  4967. scopeFunc();
  4968. }, vip: vip, async: function (generatorFn) {
  4969. return function () {
  4970. try {
  4971. var rv = awaitIterator(generatorFn.apply(this, arguments));
  4972. if (!rv || typeof rv.then !== 'function')
  4973. return DexiePromise.resolve(rv);
  4974. return rv;
  4975. }
  4976. catch (e) {
  4977. return rejection(e);
  4978. }
  4979. };
  4980. }, spawn: function (generatorFn, args, thiz) {
  4981. try {
  4982. var rv = awaitIterator(generatorFn.apply(thiz, args || []));
  4983. if (!rv || typeof rv.then !== 'function')
  4984. return DexiePromise.resolve(rv);
  4985. return rv;
  4986. }
  4987. catch (e) {
  4988. return rejection(e);
  4989. }
  4990. },
  4991. currentTransaction: {
  4992. get: function () { return PSD.trans || null; }
  4993. }, waitFor: function (promiseOrFunction, optionalTimeout) {
  4994. var promise = DexiePromise.resolve(typeof promiseOrFunction === 'function' ?
  4995. Dexie.ignoreTransaction(promiseOrFunction) :
  4996. promiseOrFunction)
  4997. .timeout(optionalTimeout || 60000);
  4998. return PSD.trans ?
  4999. PSD.trans.waitFor(promise) :
  5000. promise;
  5001. },
  5002. Promise: DexiePromise,
  5003. debug: {
  5004. get: function () { return debug; },
  5005. set: function (value) {
  5006. setDebug(value, value === 'dexie' ? function () { return true; } : dexieStackFrameFilter);
  5007. }
  5008. },
  5009. derive: derive, extend: extend, props: props, override: override,
  5010. Events: Events, on: globalEvents, liveQuery: liveQuery, extendObservabilitySet: extendObservabilitySet,
  5011. getByKeyPath: getByKeyPath, setByKeyPath: setByKeyPath, delByKeyPath: delByKeyPath, shallowClone: shallowClone, deepClone: deepClone, getObjectDiff: getObjectDiff, cmp: cmp, asap: asap$1,
  5012. minKey: minKey,
  5013. addons: [],
  5014. connections: connections,
  5015. errnames: errnames,
  5016. dependencies: domDeps,
  5017. semVer: DEXIE_VERSION, version: DEXIE_VERSION.split('.')
  5018. .map(function (n) { return parseInt(n); })
  5019. .reduce(function (p, c, i) { return p + (c / Math.pow(10, i * 2)); }) }));
  5020. Dexie.maxKey = getMaxKey(Dexie.dependencies.IDBKeyRange);
  5021. if (typeof dispatchEvent !== 'undefined' && typeof addEventListener !== 'undefined') {
  5022. globalEvents(DEXIE_STORAGE_MUTATED_EVENT_NAME, function (updatedParts) {
  5023. if (!propagatingLocally) {
  5024. var event_1;
  5025. if (isIEOrEdge) {
  5026. event_1 = document.createEvent('CustomEvent');
  5027. event_1.initCustomEvent(STORAGE_MUTATED_DOM_EVENT_NAME, true, true, updatedParts);
  5028. }
  5029. else {
  5030. event_1 = new CustomEvent(STORAGE_MUTATED_DOM_EVENT_NAME, {
  5031. detail: updatedParts
  5032. });
  5033. }
  5034. propagatingLocally = true;
  5035. dispatchEvent(event_1);
  5036. propagatingLocally = false;
  5037. }
  5038. });
  5039. addEventListener(STORAGE_MUTATED_DOM_EVENT_NAME, function (_a) {
  5040. var detail = _a.detail;
  5041. if (!propagatingLocally) {
  5042. propagateLocally(detail);
  5043. }
  5044. });
  5045. }
  5046. function propagateLocally(updateParts) {
  5047. var wasMe = propagatingLocally;
  5048. try {
  5049. propagatingLocally = true;
  5050. globalEvents.storagemutated.fire(updateParts);
  5051. }
  5052. finally {
  5053. propagatingLocally = wasMe;
  5054. }
  5055. }
  5056. var propagatingLocally = false;
  5057. if (typeof BroadcastChannel !== 'undefined') {
  5058. var bc_1 = new BroadcastChannel(STORAGE_MUTATED_DOM_EVENT_NAME);
  5059. if (typeof bc_1.unref === 'function') {
  5060. bc_1.unref();
  5061. }
  5062. globalEvents(DEXIE_STORAGE_MUTATED_EVENT_NAME, function (changedParts) {
  5063. if (!propagatingLocally) {
  5064. bc_1.postMessage(changedParts);
  5065. }
  5066. });
  5067. bc_1.onmessage = function (ev) {
  5068. if (ev.data)
  5069. propagateLocally(ev.data);
  5070. };
  5071. }
  5072. else if (typeof self !== 'undefined' && typeof navigator !== 'undefined') {
  5073. globalEvents(DEXIE_STORAGE_MUTATED_EVENT_NAME, function (changedParts) {
  5074. try {
  5075. if (!propagatingLocally) {
  5076. if (typeof localStorage !== 'undefined') {
  5077. localStorage.setItem(STORAGE_MUTATED_DOM_EVENT_NAME, JSON.stringify({
  5078. trig: Math.random(),
  5079. changedParts: changedParts,
  5080. }));
  5081. }
  5082. if (typeof self['clients'] === 'object') {
  5083. __spreadArray([], self['clients'].matchAll({ includeUncontrolled: true }), true).forEach(function (client) {
  5084. return client.postMessage({
  5085. type: STORAGE_MUTATED_DOM_EVENT_NAME,
  5086. changedParts: changedParts,
  5087. });
  5088. });
  5089. }
  5090. }
  5091. }
  5092. catch (_a) { }
  5093. });
  5094. if (typeof addEventListener !== 'undefined') {
  5095. addEventListener('storage', function (ev) {
  5096. if (ev.key === STORAGE_MUTATED_DOM_EVENT_NAME) {
  5097. var data = JSON.parse(ev.newValue);
  5098. if (data)
  5099. propagateLocally(data.changedParts);
  5100. }
  5101. });
  5102. }
  5103. var swContainer = self.document && navigator.serviceWorker;
  5104. if (swContainer) {
  5105. swContainer.addEventListener('message', propagateMessageLocally);
  5106. }
  5107. }
  5108. function propagateMessageLocally(_a) {
  5109. var data = _a.data;
  5110. if (data && data.type === STORAGE_MUTATED_DOM_EVENT_NAME) {
  5111. propagateLocally(data.changedParts);
  5112. }
  5113. }
  5114. DexiePromise.rejectionMapper = mapError;
  5115. setDebug(debug, dexieStackFrameFilter);
  5116. var namedExports = /*#__PURE__*/Object.freeze({
  5117. __proto__: null,
  5118. Dexie: Dexie$1,
  5119. liveQuery: liveQuery,
  5120. 'default': Dexie$1,
  5121. RangeSet: RangeSet,
  5122. mergeRanges: mergeRanges,
  5123. rangesOverlap: rangesOverlap
  5124. });
  5125. __assign(Dexie$1, namedExports, { default: Dexie$1 });
  5126. return Dexie$1;
  5127. }));
  5128. //# sourceMappingURL=dexie.js.map