mobx.umd.js 193 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645
  1. /** MobX - (c) Michel Weststrate 2015 - 2018 - MIT Licensed */
  2. (function (global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  4. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  5. (global = global || self, factory(global.mobx = {}));
  6. }(this, function (exports) { 'use strict';
  7. /*! *****************************************************************************
  8. Copyright (c) Microsoft Corporation. All rights reserved.
  9. Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  10. this file except in compliance with the License. You may obtain a copy of the
  11. License at http://www.apache.org/licenses/LICENSE-2.0
  12. THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  14. WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  15. MERCHANTABLITY OR NON-INFRINGEMENT.
  16. See the Apache Version 2.0 License for specific language governing permissions
  17. and limitations under the License.
  18. ***************************************************************************** */
  19. /* global Reflect, Promise */
  20. var extendStatics = function(d, b) {
  21. extendStatics = Object.setPrototypeOf ||
  22. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  23. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  24. return extendStatics(d, b);
  25. };
  26. function __extends(d, b) {
  27. extendStatics(d, b);
  28. function __() { this.constructor = d; }
  29. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  30. }
  31. var __assign = function() {
  32. __assign = Object.assign || function __assign(t) {
  33. for (var s, i = 1, n = arguments.length; i < n; i++) {
  34. s = arguments[i];
  35. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
  36. }
  37. return t;
  38. };
  39. return __assign.apply(this, arguments);
  40. };
  41. function __read(o, n) {
  42. var m = typeof Symbol === "function" && o[Symbol.iterator];
  43. if (!m) return o;
  44. var i = m.call(o), r, ar = [], e;
  45. try {
  46. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  47. }
  48. catch (error) { e = { error: error }; }
  49. finally {
  50. try {
  51. if (r && !r.done && (m = i["return"])) m.call(i);
  52. }
  53. finally { if (e) throw e.error; }
  54. }
  55. return ar;
  56. }
  57. function __spread() {
  58. for (var ar = [], i = 0; i < arguments.length; i++)
  59. ar = ar.concat(__read(arguments[i]));
  60. return ar;
  61. }
  62. var OBFUSCATED_ERROR = "An invariant failed, however the error is obfuscated because this is an production build.";
  63. var EMPTY_ARRAY = [];
  64. Object.freeze(EMPTY_ARRAY);
  65. var EMPTY_OBJECT = {};
  66. Object.freeze(EMPTY_OBJECT);
  67. var mockGlobal = {};
  68. function getGlobal() {
  69. if (typeof window !== "undefined") {
  70. return window;
  71. }
  72. if (typeof global !== "undefined") {
  73. return global;
  74. }
  75. if (typeof self !== "undefined") {
  76. return self;
  77. }
  78. return mockGlobal;
  79. }
  80. function getNextId() {
  81. return ++globalState.mobxGuid;
  82. }
  83. function fail(message) {
  84. invariant(false, message);
  85. throw "X"; // unreachable
  86. }
  87. function invariant(check, message) {
  88. if (!check)
  89. throw new Error("[mobx] " + (message || OBFUSCATED_ERROR));
  90. }
  91. /**
  92. * Prints a deprecation message, but only one time.
  93. * Returns false if the deprecated message was already printed before
  94. */
  95. var deprecatedMessages = [];
  96. function deprecated(msg, thing) {
  97. if (process.env.NODE_ENV === "production")
  98. return false;
  99. if (thing) {
  100. return deprecated("'" + msg + "', use '" + thing + "' instead.");
  101. }
  102. if (deprecatedMessages.indexOf(msg) !== -1)
  103. return false;
  104. deprecatedMessages.push(msg);
  105. console.error("[mobx] Deprecated: " + msg);
  106. return true;
  107. }
  108. /**
  109. * Makes sure that the provided function is invoked at most once.
  110. */
  111. function once(func) {
  112. var invoked = false;
  113. return function () {
  114. if (invoked)
  115. return;
  116. invoked = true;
  117. return func.apply(this, arguments);
  118. };
  119. }
  120. var noop = function () { };
  121. function unique(list) {
  122. var res = [];
  123. list.forEach(function (item) {
  124. if (res.indexOf(item) === -1)
  125. res.push(item);
  126. });
  127. return res;
  128. }
  129. function isObject(value) {
  130. return value !== null && typeof value === "object";
  131. }
  132. function isPlainObject(value) {
  133. if (value === null || typeof value !== "object")
  134. return false;
  135. var proto = Object.getPrototypeOf(value);
  136. return proto === Object.prototype || proto === null;
  137. }
  138. function convertToMap(dataStructure) {
  139. if (isES6Map(dataStructure) || isObservableMap(dataStructure)) {
  140. return dataStructure;
  141. }
  142. else if (Array.isArray(dataStructure)) {
  143. return new Map(dataStructure);
  144. }
  145. else if (isPlainObject(dataStructure)) {
  146. return new Map(Object.entries(dataStructure));
  147. }
  148. else {
  149. return fail("Cannot convert to map from '" + dataStructure + "'");
  150. }
  151. }
  152. function makeNonEnumerable(object, propNames) {
  153. for (var i = 0; i < propNames.length; i++) {
  154. addHiddenProp(object, propNames[i], object[propNames[i]]);
  155. }
  156. }
  157. function addHiddenProp(object, propName, value) {
  158. Object.defineProperty(object, propName, {
  159. enumerable: false,
  160. writable: true,
  161. configurable: true,
  162. value: value
  163. });
  164. }
  165. function addHiddenFinalProp(object, propName, value) {
  166. Object.defineProperty(object, propName, {
  167. enumerable: false,
  168. writable: false,
  169. configurable: true,
  170. value: value
  171. });
  172. }
  173. function isPropertyConfigurable(object, prop) {
  174. var descriptor = Object.getOwnPropertyDescriptor(object, prop);
  175. return !descriptor || (descriptor.configurable !== false && descriptor.writable !== false);
  176. }
  177. function assertPropertyConfigurable(object, prop) {
  178. if (process.env.NODE_ENV !== "production" && !isPropertyConfigurable(object, prop))
  179. fail("Cannot make property '" + prop + "' observable, it is not configurable and writable in the target object");
  180. }
  181. function createInstanceofPredicate(name, clazz) {
  182. var propName = "isMobX" + name;
  183. clazz.prototype[propName] = true;
  184. return function (x) {
  185. return isObject(x) && x[propName] === true;
  186. };
  187. }
  188. function areBothNaN(a, b) {
  189. return typeof a === "number" && typeof b === "number" && isNaN(a) && isNaN(b);
  190. }
  191. /**
  192. * Returns whether the argument is an array, disregarding observability.
  193. */
  194. function isArrayLike(x) {
  195. return Array.isArray(x) || isObservableArray(x);
  196. }
  197. function isES6Map(thing) {
  198. if (getGlobal().Map !== undefined && thing instanceof getGlobal().Map)
  199. return true;
  200. return false;
  201. }
  202. function isES6Set(thing) {
  203. return thing instanceof Set;
  204. }
  205. // use Array.from in Mobx 5
  206. function iteratorToArray(it) {
  207. var res = [];
  208. while (true) {
  209. var r = it.next();
  210. if (r.done)
  211. break;
  212. res.push(r.value);
  213. }
  214. return res;
  215. }
  216. function primitiveSymbol() {
  217. // es-disable-next-line
  218. return (typeof Symbol === "function" && Symbol.toPrimitive) || "@@toPrimitive";
  219. }
  220. function toPrimitive(value) {
  221. return value === null ? null : typeof value === "object" ? "" + value : value;
  222. }
  223. function iteratorSymbol() {
  224. return (typeof Symbol === "function" && Symbol.iterator) || "@@iterator";
  225. }
  226. function declareIterator(prototType, iteratorFactory) {
  227. addHiddenFinalProp(prototType, iteratorSymbol(), iteratorFactory);
  228. }
  229. function makeIterable(iterator) {
  230. iterator[iteratorSymbol()] = getSelf;
  231. return iterator;
  232. }
  233. function toStringTagSymbol() {
  234. return (typeof Symbol === "function" && Symbol.toStringTag) || "@@toStringTag";
  235. }
  236. function getSelf() {
  237. return this;
  238. }
  239. /**
  240. * Anything that can be used to _store_ state is an Atom in mobx. Atoms have two important jobs
  241. *
  242. * 1) detect when they are being _used_ and report this (using reportObserved). This allows mobx to make the connection between running functions and the data they used
  243. * 2) they should notify mobx whenever they have _changed_. This way mobx can re-run any functions (derivations) that are using this atom.
  244. */
  245. var Atom = /** @class */ (function () {
  246. /**
  247. * Create a new atom. For debugging purposes it is recommended to give it a name.
  248. * The onBecomeObserved and onBecomeUnobserved callbacks can be used for resource management.
  249. */
  250. function Atom(name) {
  251. if (name === void 0) { name = "Atom@" + getNextId(); }
  252. this.name = name;
  253. this.isPendingUnobservation = false; // for effective unobserving. BaseAtom has true, for extra optimization, so its onBecomeUnobserved never gets called, because it's not needed
  254. this.isBeingObserved = false;
  255. this.observers = [];
  256. this.observersIndexes = {};
  257. this.diffValue = 0;
  258. this.lastAccessedBy = 0;
  259. this.lowestObserverState = exports.IDerivationState.NOT_TRACKING;
  260. }
  261. Atom.prototype.onBecomeUnobserved = function () {
  262. // noop
  263. };
  264. Atom.prototype.onBecomeObserved = function () {
  265. /* noop */
  266. };
  267. /**
  268. * Invoke this method to notify mobx that your atom has been used somehow.
  269. * Returns true if there is currently a reactive context.
  270. */
  271. Atom.prototype.reportObserved = function () {
  272. return reportObserved(this);
  273. };
  274. /**
  275. * Invoke this method _after_ this method has changed to signal mobx that all its observers should invalidate.
  276. */
  277. Atom.prototype.reportChanged = function () {
  278. startBatch();
  279. propagateChanged(this);
  280. endBatch();
  281. };
  282. Atom.prototype.toString = function () {
  283. return this.name;
  284. };
  285. return Atom;
  286. }());
  287. var isAtom = createInstanceofPredicate("Atom", Atom);
  288. function createAtom(name, onBecomeObservedHandler, onBecomeUnobservedHandler) {
  289. if (onBecomeObservedHandler === void 0) { onBecomeObservedHandler = noop; }
  290. if (onBecomeUnobservedHandler === void 0) { onBecomeUnobservedHandler = noop; }
  291. var atom = new Atom(name);
  292. onBecomeObserved(atom, onBecomeObservedHandler);
  293. onBecomeUnobserved(atom, onBecomeUnobservedHandler);
  294. return atom;
  295. }
  296. function identityComparer(a, b) {
  297. return a === b;
  298. }
  299. function structuralComparer(a, b) {
  300. return deepEqual(a, b);
  301. }
  302. function shallowComparer(a, b) {
  303. return deepEqual(a, b, 1);
  304. }
  305. function defaultComparer(a, b) {
  306. return areBothNaN(a, b) || identityComparer(a, b);
  307. }
  308. var comparer = {
  309. identity: identityComparer,
  310. structural: structuralComparer,
  311. default: defaultComparer,
  312. shallow: shallowComparer
  313. };
  314. var enumerableDescriptorCache = {};
  315. var nonEnumerableDescriptorCache = {};
  316. function createPropertyInitializerDescriptor(prop, enumerable) {
  317. var cache = enumerable ? enumerableDescriptorCache : nonEnumerableDescriptorCache;
  318. return (cache[prop] ||
  319. (cache[prop] = {
  320. configurable: true,
  321. enumerable: enumerable,
  322. get: function () {
  323. initializeInstance(this);
  324. return this[prop];
  325. },
  326. set: function (value) {
  327. initializeInstance(this);
  328. this[prop] = value;
  329. }
  330. }));
  331. }
  332. function initializeInstance(target) {
  333. if (target.__mobxDidRunLazyInitializers === true)
  334. return;
  335. var decorators = target.__mobxDecorators;
  336. if (decorators) {
  337. addHiddenProp(target, "__mobxDidRunLazyInitializers", true);
  338. for (var key in decorators) {
  339. var d = decorators[key];
  340. d.propertyCreator(target, d.prop, d.descriptor, d.decoratorTarget, d.decoratorArguments);
  341. }
  342. }
  343. }
  344. function createPropDecorator(propertyInitiallyEnumerable, propertyCreator) {
  345. return function decoratorFactory() {
  346. var decoratorArguments;
  347. var decorator = function decorate(target, prop, descriptor, applyImmediately
  348. // This is a special parameter to signal the direct application of a decorator, allow extendObservable to skip the entire type decoration part,
  349. // as the instance to apply the decorator to equals the target
  350. ) {
  351. if (applyImmediately === true) {
  352. propertyCreator(target, prop, descriptor, target, decoratorArguments);
  353. return null;
  354. }
  355. if (process.env.NODE_ENV !== "production" && !quacksLikeADecorator(arguments))
  356. fail("This function is a decorator, but it wasn't invoked like a decorator");
  357. if (!Object.prototype.hasOwnProperty.call(target, "__mobxDecorators")) {
  358. var inheritedDecorators = target.__mobxDecorators;
  359. addHiddenProp(target, "__mobxDecorators", __assign({}, inheritedDecorators));
  360. }
  361. target.__mobxDecorators[prop] = {
  362. prop: prop,
  363. propertyCreator: propertyCreator,
  364. descriptor: descriptor,
  365. decoratorTarget: target,
  366. decoratorArguments: decoratorArguments
  367. };
  368. return createPropertyInitializerDescriptor(prop, propertyInitiallyEnumerable);
  369. };
  370. if (quacksLikeADecorator(arguments)) {
  371. // @decorator
  372. decoratorArguments = EMPTY_ARRAY;
  373. return decorator.apply(null, arguments);
  374. }
  375. else {
  376. // @decorator(args)
  377. decoratorArguments = Array.prototype.slice.call(arguments);
  378. return decorator;
  379. }
  380. };
  381. }
  382. function quacksLikeADecorator(args) {
  383. return (((args.length === 2 || args.length === 3) && typeof args[1] === "string") ||
  384. (args.length === 4 && args[3] === true));
  385. }
  386. function deepEnhancer(v, _, name) {
  387. // it is an observable already, done
  388. if (isObservable(v))
  389. return v;
  390. // something that can be converted and mutated?
  391. if (Array.isArray(v))
  392. return observable.array(v, { name: name });
  393. if (isPlainObject(v))
  394. return observable.object(v, undefined, { name: name });
  395. if (isES6Map(v))
  396. return observable.map(v, { name: name });
  397. if (isES6Set(v))
  398. return observable.set(v, { name: name });
  399. return v;
  400. }
  401. function shallowEnhancer(v, _, name) {
  402. if (v === undefined || v === null)
  403. return v;
  404. if (isObservableObject(v) || isObservableArray(v) || isObservableMap(v) || isObservableSet(v))
  405. return v;
  406. if (Array.isArray(v))
  407. return observable.array(v, { name: name, deep: false });
  408. if (isPlainObject(v))
  409. return observable.object(v, undefined, { name: name, deep: false });
  410. if (isES6Map(v))
  411. return observable.map(v, { name: name, deep: false });
  412. if (isES6Set(v))
  413. return observable.set(v, { name: name, deep: false });
  414. return fail(process.env.NODE_ENV !== "production" &&
  415. "The shallow modifier / decorator can only used in combination with arrays, objects, maps and sets");
  416. }
  417. function referenceEnhancer(newValue) {
  418. // never turn into an observable
  419. return newValue;
  420. }
  421. function refStructEnhancer(v, oldValue, name) {
  422. if (process.env.NODE_ENV !== "production" && isObservable(v))
  423. throw "observable.struct should not be used with observable values";
  424. if (deepEqual(v, oldValue))
  425. return oldValue;
  426. return v;
  427. }
  428. function createDecoratorForEnhancer(enhancer) {
  429. var decorator = createPropDecorator(true, function (target, propertyName, descriptor, _decoratorTarget, decoratorArgs) {
  430. if (process.env.NODE_ENV !== "production") {
  431. invariant(!descriptor || !descriptor.get, "@observable cannot be used on getter (property \"" + propertyName + "\"), use @computed instead.");
  432. }
  433. var initialValue = descriptor
  434. ? descriptor.initializer
  435. ? descriptor.initializer.call(target)
  436. : descriptor.value
  437. : undefined;
  438. defineObservableProperty(target, propertyName, initialValue, enhancer);
  439. });
  440. var res =
  441. // Extra process checks, as this happens during module initialization
  442. typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production"
  443. ? function observableDecorator() {
  444. // This wrapper function is just to detect illegal decorator invocations, deprecate in a next version
  445. // and simply return the created prop decorator
  446. if (arguments.length < 2)
  447. return fail("Incorrect decorator invocation. @observable decorator doesn't expect any arguments");
  448. return decorator.apply(null, arguments);
  449. }
  450. : decorator;
  451. res.enhancer = enhancer;
  452. return res;
  453. }
  454. // Predefined bags of create observable options, to avoid allocating temporarily option objects
  455. // in the majority of cases
  456. var defaultCreateObservableOptions = {
  457. deep: true,
  458. name: undefined,
  459. defaultDecorator: undefined
  460. };
  461. var shallowCreateObservableOptions = {
  462. deep: false,
  463. name: undefined,
  464. defaultDecorator: undefined
  465. };
  466. Object.freeze(defaultCreateObservableOptions);
  467. Object.freeze(shallowCreateObservableOptions);
  468. function assertValidOption(key) {
  469. if (!/^(deep|name|equals|defaultDecorator)$/.test(key))
  470. fail("invalid option for (extend)observable: " + key);
  471. }
  472. function asCreateObservableOptions(thing) {
  473. if (thing === null || thing === undefined)
  474. return defaultCreateObservableOptions;
  475. if (typeof thing === "string")
  476. return { name: thing, deep: true };
  477. if (process.env.NODE_ENV !== "production") {
  478. if (typeof thing !== "object")
  479. return fail("expected options object");
  480. Object.keys(thing).forEach(assertValidOption);
  481. }
  482. return thing;
  483. }
  484. function getEnhancerFromOptions(options) {
  485. return options.defaultDecorator
  486. ? options.defaultDecorator.enhancer
  487. : options.deep === false
  488. ? referenceEnhancer
  489. : deepEnhancer;
  490. }
  491. var deepDecorator = createDecoratorForEnhancer(deepEnhancer);
  492. var shallowDecorator = createDecoratorForEnhancer(shallowEnhancer);
  493. var refDecorator = createDecoratorForEnhancer(referenceEnhancer);
  494. var refStructDecorator = createDecoratorForEnhancer(refStructEnhancer);
  495. /**
  496. * Turns an object, array or function into a reactive structure.
  497. * @param v the value which should become observable.
  498. */
  499. function createObservable(v, arg2, arg3) {
  500. // @observable someProp;
  501. if (typeof arguments[1] === "string") {
  502. return deepDecorator.apply(null, arguments);
  503. }
  504. // it is an observable already, done
  505. if (isObservable(v))
  506. return v;
  507. // something that can be converted and mutated?
  508. var res = isPlainObject(v)
  509. ? observable.object(v, arg2, arg3)
  510. : Array.isArray(v)
  511. ? observable.array(v, arg2)
  512. : isES6Map(v)
  513. ? observable.map(v, arg2)
  514. : isES6Set(v)
  515. ? observable.set(v, arg2)
  516. : v;
  517. // this value could be converted to a new observable data structure, return it
  518. if (res !== v)
  519. return res;
  520. // otherwise, just box it
  521. fail(process.env.NODE_ENV !== "production" &&
  522. "The provided value could not be converted into an observable. If you want just create an observable reference to the object use 'observable.box(value)'");
  523. }
  524. var observableFactories = {
  525. box: function (value, options) {
  526. if (arguments.length > 2)
  527. incorrectlyUsedAsDecorator("box");
  528. var o = asCreateObservableOptions(options);
  529. return new ObservableValue(value, getEnhancerFromOptions(o), o.name, true, o.equals);
  530. },
  531. shallowBox: function (value, name) {
  532. if (arguments.length > 2)
  533. incorrectlyUsedAsDecorator("shallowBox");
  534. deprecated("observable.shallowBox", "observable.box(value, { deep: false })");
  535. return observable.box(value, { name: name, deep: false });
  536. },
  537. array: function (initialValues, options) {
  538. if (arguments.length > 2)
  539. incorrectlyUsedAsDecorator("array");
  540. var o = asCreateObservableOptions(options);
  541. return new ObservableArray(initialValues, getEnhancerFromOptions(o), o.name);
  542. },
  543. shallowArray: function (initialValues, name) {
  544. if (arguments.length > 2)
  545. incorrectlyUsedAsDecorator("shallowArray");
  546. deprecated("observable.shallowArray", "observable.array(values, { deep: false })");
  547. return observable.array(initialValues, { name: name, deep: false });
  548. },
  549. map: function (initialValues, options) {
  550. if (arguments.length > 2)
  551. incorrectlyUsedAsDecorator("map");
  552. var o = asCreateObservableOptions(options);
  553. return new ObservableMap(initialValues, getEnhancerFromOptions(o), o.name);
  554. },
  555. shallowMap: function (initialValues, name) {
  556. if (arguments.length > 2)
  557. incorrectlyUsedAsDecorator("shallowMap");
  558. deprecated("observable.shallowMap", "observable.map(values, { deep: false })");
  559. return observable.map(initialValues, { name: name, deep: false });
  560. },
  561. set: function (initialValues, options) {
  562. if (arguments.length > 2)
  563. incorrectlyUsedAsDecorator("set");
  564. var o = asCreateObservableOptions(options);
  565. return new ObservableSet(initialValues, getEnhancerFromOptions(o), o.name);
  566. },
  567. object: function (props, decorators, options) {
  568. if (typeof arguments[1] === "string")
  569. incorrectlyUsedAsDecorator("object");
  570. var o = asCreateObservableOptions(options);
  571. return extendObservable({}, props, decorators, o);
  572. },
  573. shallowObject: function (props, name) {
  574. if (typeof arguments[1] === "string")
  575. incorrectlyUsedAsDecorator("shallowObject");
  576. deprecated("observable.shallowObject", "observable.object(values, {}, { deep: false })");
  577. return observable.object(props, {}, { name: name, deep: false });
  578. },
  579. ref: refDecorator,
  580. shallow: shallowDecorator,
  581. deep: deepDecorator,
  582. struct: refStructDecorator
  583. };
  584. var observable = createObservable;
  585. // weird trick to keep our typings nicely with our funcs, and still extend the observable function
  586. Object.keys(observableFactories).forEach(function (name) { return (observable[name] = observableFactories[name]); });
  587. function incorrectlyUsedAsDecorator(methodName) {
  588. fail(
  589. // process.env.NODE_ENV !== "production" &&
  590. "Expected one or two arguments to observable." + methodName + ". Did you accidentally try to use observable." + methodName + " as decorator?");
  591. }
  592. var computedDecorator = createPropDecorator(false, function (instance, propertyName, descriptor, decoratorTarget, decoratorArgs) {
  593. var get = descriptor.get, set = descriptor.set; // initialValue is the descriptor for get / set props
  594. // Optimization: faster on decorator target or instance? Assuming target
  595. // Optimization: find out if declaring on instance isn't just faster. (also makes the property descriptor simpler). But, more memory usage..
  596. // Forcing instance now, fixes hot reloadig issues on React Native:
  597. var options = decoratorArgs[0] || {};
  598. defineComputedProperty(instance, propertyName, __assign({ get: get, set: set }, options));
  599. });
  600. var computedStructDecorator = computedDecorator({ equals: comparer.structural });
  601. /**
  602. * Decorator for class properties: @computed get value() { return expr; }.
  603. * For legacy purposes also invokable as ES5 observable created: `computed(() => expr)`;
  604. */
  605. var computed = function computed(arg1, arg2, arg3) {
  606. if (typeof arg2 === "string") {
  607. // @computed
  608. return computedDecorator.apply(null, arguments);
  609. }
  610. if (arg1 !== null && typeof arg1 === "object" && arguments.length === 1) {
  611. // @computed({ options })
  612. return computedDecorator.apply(null, arguments);
  613. }
  614. // computed(expr, options?)
  615. if (process.env.NODE_ENV !== "production") {
  616. invariant(typeof arg1 === "function", "First argument to `computed` should be an expression.");
  617. invariant(arguments.length < 3, "Computed takes one or two arguments if used as function");
  618. }
  619. var opts = typeof arg2 === "object" ? arg2 : {};
  620. opts.get = arg1;
  621. opts.set = typeof arg2 === "function" ? arg2 : opts.set;
  622. opts.name = opts.name || arg1.name || ""; /* for generated name */
  623. return new ComputedValue(opts);
  624. };
  625. computed.struct = computedStructDecorator;
  626. (function (IDerivationState) {
  627. // before being run or (outside batch and not being observed)
  628. // at this point derivation is not holding any data about dependency tree
  629. IDerivationState[IDerivationState["NOT_TRACKING"] = -1] = "NOT_TRACKING";
  630. // no shallow dependency changed since last computation
  631. // won't recalculate derivation
  632. // this is what makes mobx fast
  633. IDerivationState[IDerivationState["UP_TO_DATE"] = 0] = "UP_TO_DATE";
  634. // some deep dependency changed, but don't know if shallow dependency changed
  635. // will require to check first if UP_TO_DATE or POSSIBLY_STALE
  636. // currently only ComputedValue will propagate POSSIBLY_STALE
  637. //
  638. // having this state is second big optimization:
  639. // don't have to recompute on every dependency change, but only when it's needed
  640. IDerivationState[IDerivationState["POSSIBLY_STALE"] = 1] = "POSSIBLY_STALE";
  641. // A shallow dependency has changed since last computation and the derivation
  642. // will need to recompute when it's needed next.
  643. IDerivationState[IDerivationState["STALE"] = 2] = "STALE";
  644. })(exports.IDerivationState || (exports.IDerivationState = {}));
  645. var TraceMode;
  646. (function (TraceMode) {
  647. TraceMode[TraceMode["NONE"] = 0] = "NONE";
  648. TraceMode[TraceMode["LOG"] = 1] = "LOG";
  649. TraceMode[TraceMode["BREAK"] = 2] = "BREAK";
  650. })(TraceMode || (TraceMode = {}));
  651. var CaughtException = /** @class */ (function () {
  652. function CaughtException(cause) {
  653. this.cause = cause;
  654. // Empty
  655. }
  656. return CaughtException;
  657. }());
  658. function isCaughtException(e) {
  659. return e instanceof CaughtException;
  660. }
  661. /**
  662. * Finds out whether any dependency of the derivation has actually changed.
  663. * If dependenciesState is 1 then it will recalculate dependencies,
  664. * if any dependency changed it will propagate it by changing dependenciesState to 2.
  665. *
  666. * By iterating over the dependencies in the same order that they were reported and
  667. * stopping on the first change, all the recalculations are only called for ComputedValues
  668. * that will be tracked by derivation. That is because we assume that if the first x
  669. * dependencies of the derivation doesn't change then the derivation should run the same way
  670. * up until accessing x-th dependency.
  671. */
  672. function shouldCompute(derivation) {
  673. switch (derivation.dependenciesState) {
  674. case exports.IDerivationState.UP_TO_DATE:
  675. return false;
  676. case exports.IDerivationState.NOT_TRACKING:
  677. case exports.IDerivationState.STALE:
  678. return true;
  679. case exports.IDerivationState.POSSIBLY_STALE: {
  680. var prevUntracked = untrackedStart(); // no need for those computeds to be reported, they will be picked up in trackDerivedFunction.
  681. var obs = derivation.observing, l = obs.length;
  682. for (var i = 0; i < l; i++) {
  683. var obj = obs[i];
  684. if (isComputedValue(obj)) {
  685. if (globalState.disableErrorBoundaries) {
  686. obj.get();
  687. }
  688. else {
  689. try {
  690. obj.get();
  691. }
  692. catch (e) {
  693. // we are not interested in the value *or* exception at this moment, but if there is one, notify all
  694. untrackedEnd(prevUntracked);
  695. return true;
  696. }
  697. }
  698. // if ComputedValue `obj` actually changed it will be computed and propagated to its observers.
  699. // and `derivation` is an observer of `obj`
  700. // invariantShouldCompute(derivation)
  701. if (derivation.dependenciesState === exports.IDerivationState.STALE) {
  702. untrackedEnd(prevUntracked);
  703. return true;
  704. }
  705. }
  706. }
  707. changeDependenciesStateTo0(derivation);
  708. untrackedEnd(prevUntracked);
  709. return false;
  710. }
  711. }
  712. }
  713. // function invariantShouldCompute(derivation: IDerivation) {
  714. // const newDepState = (derivation as any).dependenciesState
  715. // if (
  716. // process.env.NODE_ENV === "production" &&
  717. // (newDepState === IDerivationState.POSSIBLY_STALE ||
  718. // newDepState === IDerivationState.NOT_TRACKING)
  719. // )
  720. // fail("Illegal dependency state")
  721. // }
  722. function isComputingDerivation() {
  723. return globalState.trackingDerivation !== null; // filter out actions inside computations
  724. }
  725. function checkIfStateModificationsAreAllowed(atom) {
  726. var hasObservers = atom.observers.length > 0;
  727. // Should never be possible to change an observed observable from inside computed, see #798
  728. if (globalState.computationDepth > 0 && hasObservers)
  729. fail(process.env.NODE_ENV !== "production" &&
  730. "Computed values are not allowed to cause side effects by changing observables that are already being observed. Tried to modify: " + atom.name);
  731. // Should not be possible to change observed state outside strict mode, except during initialization, see #563
  732. if (!globalState.allowStateChanges && (hasObservers || globalState.enforceActions === "strict"))
  733. fail(process.env.NODE_ENV !== "production" &&
  734. (globalState.enforceActions
  735. ? "Since strict-mode is enabled, changing observed observable values outside actions is not allowed. Please wrap the code in an `action` if this change is intended. Tried to modify: "
  736. : "Side effects like changing state are not allowed at this point. Are you trying to modify state from, for example, the render function of a React component? Tried to modify: ") +
  737. atom.name);
  738. }
  739. function checkIfStateReadsAreAllowed(observable) {
  740. if (process.env.NODE_ENV !== "production" &&
  741. !globalState.allowStateReads &&
  742. globalState.observableRequiresReaction) {
  743. console.warn("[mobx] Observable " + observable.name + " being read outside a reactive context");
  744. }
  745. }
  746. /**
  747. * Executes the provided function `f` and tracks which observables are being accessed.
  748. * The tracking information is stored on the `derivation` object and the derivation is registered
  749. * as observer of any of the accessed observables.
  750. */
  751. function trackDerivedFunction(derivation, f, context) {
  752. var prevAllowStateReads = allowStateReadsStart(true);
  753. // pre allocate array allocation + room for variation in deps
  754. // array will be trimmed by bindDependencies
  755. changeDependenciesStateTo0(derivation);
  756. derivation.newObserving = new Array(derivation.observing.length + 100);
  757. derivation.unboundDepsCount = 0;
  758. derivation.runId = ++globalState.runId;
  759. var prevTracking = globalState.trackingDerivation;
  760. globalState.trackingDerivation = derivation;
  761. var result;
  762. if (globalState.disableErrorBoundaries === true) {
  763. result = f.call(context);
  764. }
  765. else {
  766. try {
  767. result = f.call(context);
  768. }
  769. catch (e) {
  770. result = new CaughtException(e);
  771. }
  772. }
  773. globalState.trackingDerivation = prevTracking;
  774. bindDependencies(derivation);
  775. if (derivation.observing.length === 0) {
  776. warnAboutDerivationWithoutDependencies(derivation);
  777. }
  778. allowStateReadsEnd(prevAllowStateReads);
  779. return result;
  780. }
  781. function warnAboutDerivationWithoutDependencies(derivation) {
  782. if (process.env.NODE_ENV === "production")
  783. return;
  784. if (globalState.reactionRequiresObservable || derivation.requiresObservable) {
  785. console.warn("[mobx] Derivation " + derivation.name + " is created/updated without reading any observable value");
  786. }
  787. }
  788. /**
  789. * diffs newObserving with observing.
  790. * update observing to be newObserving with unique observables
  791. * notify observers that become observed/unobserved
  792. */
  793. function bindDependencies(derivation) {
  794. // invariant(derivation.dependenciesState !== IDerivationState.NOT_TRACKING, "INTERNAL ERROR bindDependencies expects derivation.dependenciesState !== -1");
  795. var prevObserving = derivation.observing;
  796. var observing = (derivation.observing = derivation.newObserving);
  797. var lowestNewObservingDerivationState = exports.IDerivationState.UP_TO_DATE;
  798. // Go through all new observables and check diffValue: (this list can contain duplicates):
  799. // 0: first occurrence, change to 1 and keep it
  800. // 1: extra occurrence, drop it
  801. var i0 = 0, l = derivation.unboundDepsCount;
  802. for (var i = 0; i < l; i++) {
  803. var dep = observing[i];
  804. if (dep.diffValue === 0) {
  805. dep.diffValue = 1;
  806. if (i0 !== i)
  807. observing[i0] = dep;
  808. i0++;
  809. }
  810. // Upcast is 'safe' here, because if dep is IObservable, `dependenciesState` will be undefined,
  811. // not hitting the condition
  812. if (dep.dependenciesState > lowestNewObservingDerivationState) {
  813. lowestNewObservingDerivationState = dep.dependenciesState;
  814. }
  815. }
  816. observing.length = i0;
  817. derivation.newObserving = null; // newObserving shouldn't be needed outside tracking (statement moved down to work around FF bug, see #614)
  818. // Go through all old observables and check diffValue: (it is unique after last bindDependencies)
  819. // 0: it's not in new observables, unobserve it
  820. // 1: it keeps being observed, don't want to notify it. change to 0
  821. l = prevObserving.length;
  822. while (l--) {
  823. var dep = prevObserving[l];
  824. if (dep.diffValue === 0) {
  825. removeObserver(dep, derivation);
  826. }
  827. dep.diffValue = 0;
  828. }
  829. // Go through all new observables and check diffValue: (now it should be unique)
  830. // 0: it was set to 0 in last loop. don't need to do anything.
  831. // 1: it wasn't observed, let's observe it. set back to 0
  832. while (i0--) {
  833. var dep = observing[i0];
  834. if (dep.diffValue === 1) {
  835. dep.diffValue = 0;
  836. addObserver(dep, derivation);
  837. }
  838. }
  839. // Some new observed derivations may become stale during this derivation computation
  840. // so they have had no chance to propagate staleness (#916)
  841. if (lowestNewObservingDerivationState !== exports.IDerivationState.UP_TO_DATE) {
  842. derivation.dependenciesState = lowestNewObservingDerivationState;
  843. derivation.onBecomeStale();
  844. }
  845. }
  846. function clearObserving(derivation) {
  847. // invariant(globalState.inBatch > 0, "INTERNAL ERROR clearObserving should be called only inside batch");
  848. var obs = derivation.observing;
  849. derivation.observing = [];
  850. var i = obs.length;
  851. while (i--)
  852. removeObserver(obs[i], derivation);
  853. derivation.dependenciesState = exports.IDerivationState.NOT_TRACKING;
  854. }
  855. function untracked(action) {
  856. var prev = untrackedStart();
  857. var res = action();
  858. untrackedEnd(prev);
  859. return res;
  860. }
  861. function untrackedStart() {
  862. var prev = globalState.trackingDerivation;
  863. globalState.trackingDerivation = null;
  864. return prev;
  865. }
  866. function untrackedEnd(prev) {
  867. globalState.trackingDerivation = prev;
  868. }
  869. function allowStateReadsStart(allowStateReads) {
  870. var prev = globalState.allowStateReads;
  871. globalState.allowStateReads = allowStateReads;
  872. return prev;
  873. }
  874. function allowStateReadsEnd(prev) {
  875. globalState.allowStateReads = prev;
  876. }
  877. /**
  878. * needed to keep `lowestObserverState` correct. when changing from (2 or 1) to 0
  879. *
  880. */
  881. function changeDependenciesStateTo0(derivation) {
  882. if (derivation.dependenciesState === exports.IDerivationState.UP_TO_DATE)
  883. return;
  884. derivation.dependenciesState = exports.IDerivationState.UP_TO_DATE;
  885. var obs = derivation.observing;
  886. var i = obs.length;
  887. while (i--)
  888. obs[i].lowestObserverState = exports.IDerivationState.UP_TO_DATE;
  889. }
  890. // we don't use globalState for these in order to avoid possible issues with multiple
  891. // mobx versions
  892. var currentActionId = 0;
  893. var nextActionId = 1;
  894. function createAction(actionName, fn) {
  895. if (process.env.NODE_ENV !== "production") {
  896. invariant(typeof fn === "function", "`action` can only be invoked on functions");
  897. if (typeof actionName !== "string" || !actionName)
  898. fail("actions should have valid names, got: '" + actionName + "'");
  899. }
  900. var res = function () {
  901. return executeAction(actionName, fn, this, arguments);
  902. };
  903. res.isMobxAction = true;
  904. return res;
  905. }
  906. function executeAction(actionName, fn, scope, args) {
  907. var runInfo = _startAction(actionName, scope, args);
  908. try {
  909. return fn.apply(scope, args);
  910. }
  911. catch (err) {
  912. runInfo.error = err;
  913. throw err;
  914. }
  915. finally {
  916. _endAction(runInfo);
  917. }
  918. }
  919. function _startAction(actionName, scope, args) {
  920. var notifySpy = isSpyEnabled() && !!actionName;
  921. var startTime = 0;
  922. if (notifySpy) {
  923. startTime = Date.now();
  924. var l = (args && args.length) || 0;
  925. var flattendArgs = new Array(l);
  926. if (l > 0)
  927. for (var i = 0; i < l; i++)
  928. flattendArgs[i] = args[i];
  929. spyReportStart({
  930. type: "action",
  931. name: actionName,
  932. object: scope,
  933. arguments: flattendArgs
  934. });
  935. }
  936. var prevDerivation = untrackedStart();
  937. startBatch();
  938. var prevAllowStateChanges = allowStateChangesStart(true);
  939. var prevAllowStateReads = allowStateReadsStart(true);
  940. var runInfo = {
  941. prevDerivation: prevDerivation,
  942. prevAllowStateChanges: prevAllowStateChanges,
  943. prevAllowStateReads: prevAllowStateReads,
  944. notifySpy: notifySpy,
  945. startTime: startTime,
  946. actionId: nextActionId++,
  947. parentActionId: currentActionId
  948. };
  949. currentActionId = runInfo.actionId;
  950. return runInfo;
  951. }
  952. function _endAction(runInfo) {
  953. if (currentActionId !== runInfo.actionId) {
  954. fail("invalid action stack. did you forget to finish an action?");
  955. }
  956. currentActionId = runInfo.parentActionId;
  957. if (runInfo.error !== undefined) {
  958. globalState.suppressReactionErrors = true;
  959. }
  960. allowStateChangesEnd(runInfo.prevAllowStateChanges);
  961. allowStateReadsEnd(runInfo.prevAllowStateReads);
  962. endBatch();
  963. untrackedEnd(runInfo.prevDerivation);
  964. if (runInfo.notifySpy) {
  965. spyReportEnd({ time: Date.now() - runInfo.startTime });
  966. }
  967. globalState.suppressReactionErrors = false;
  968. }
  969. function allowStateChanges(allowStateChanges, func) {
  970. var prev = allowStateChangesStart(allowStateChanges);
  971. var res;
  972. try {
  973. res = func();
  974. }
  975. finally {
  976. allowStateChangesEnd(prev);
  977. }
  978. return res;
  979. }
  980. function allowStateChangesStart(allowStateChanges) {
  981. var prev = globalState.allowStateChanges;
  982. globalState.allowStateChanges = allowStateChanges;
  983. return prev;
  984. }
  985. function allowStateChangesEnd(prev) {
  986. globalState.allowStateChanges = prev;
  987. }
  988. function allowStateChangesInsideComputed(func) {
  989. var prev = globalState.computationDepth;
  990. globalState.computationDepth = 0;
  991. var res;
  992. try {
  993. res = func();
  994. }
  995. finally {
  996. globalState.computationDepth = prev;
  997. }
  998. return res;
  999. }
  1000. var ObservableValue = /** @class */ (function (_super) {
  1001. __extends(ObservableValue, _super);
  1002. function ObservableValue(value, enhancer, name, notifySpy, equals) {
  1003. if (name === void 0) { name = "ObservableValue@" + getNextId(); }
  1004. if (notifySpy === void 0) { notifySpy = true; }
  1005. if (equals === void 0) { equals = comparer.default; }
  1006. var _this = _super.call(this, name) || this;
  1007. _this.enhancer = enhancer;
  1008. _this.name = name;
  1009. _this.equals = equals;
  1010. _this.hasUnreportedChange = false;
  1011. _this.value = enhancer(value, undefined, name);
  1012. if (notifySpy && isSpyEnabled()) {
  1013. // only notify spy if this is a stand-alone observable
  1014. spyReport({ type: "create", name: _this.name, newValue: "" + _this.value });
  1015. }
  1016. return _this;
  1017. }
  1018. ObservableValue.prototype.dehanceValue = function (value) {
  1019. if (this.dehancer !== undefined)
  1020. return this.dehancer(value);
  1021. return value;
  1022. };
  1023. ObservableValue.prototype.set = function (newValue) {
  1024. var oldValue = this.value;
  1025. newValue = this.prepareNewValue(newValue);
  1026. if (newValue !== globalState.UNCHANGED) {
  1027. var notifySpy = isSpyEnabled();
  1028. if (notifySpy) {
  1029. spyReportStart({
  1030. type: "update",
  1031. name: this.name,
  1032. newValue: newValue,
  1033. oldValue: oldValue
  1034. });
  1035. }
  1036. this.setNewValue(newValue);
  1037. if (notifySpy)
  1038. spyReportEnd();
  1039. }
  1040. };
  1041. ObservableValue.prototype.prepareNewValue = function (newValue) {
  1042. checkIfStateModificationsAreAllowed(this);
  1043. if (hasInterceptors(this)) {
  1044. var change = interceptChange(this, {
  1045. object: this,
  1046. type: "update",
  1047. newValue: newValue
  1048. });
  1049. if (!change)
  1050. return globalState.UNCHANGED;
  1051. newValue = change.newValue;
  1052. }
  1053. // apply modifier
  1054. newValue = this.enhancer(newValue, this.value, this.name);
  1055. return this.equals(this.value, newValue) ? globalState.UNCHANGED : newValue;
  1056. };
  1057. ObservableValue.prototype.setNewValue = function (newValue) {
  1058. var oldValue = this.value;
  1059. this.value = newValue;
  1060. this.reportChanged();
  1061. if (hasListeners(this)) {
  1062. notifyListeners(this, {
  1063. type: "update",
  1064. object: this,
  1065. newValue: newValue,
  1066. oldValue: oldValue
  1067. });
  1068. }
  1069. };
  1070. ObservableValue.prototype.get = function () {
  1071. this.reportObserved();
  1072. return this.dehanceValue(this.value);
  1073. };
  1074. ObservableValue.prototype.intercept = function (handler) {
  1075. return registerInterceptor(this, handler);
  1076. };
  1077. ObservableValue.prototype.observe = function (listener, fireImmediately) {
  1078. if (fireImmediately)
  1079. listener({
  1080. object: this,
  1081. type: "update",
  1082. newValue: this.value,
  1083. oldValue: undefined
  1084. });
  1085. return registerListener(this, listener);
  1086. };
  1087. ObservableValue.prototype.toJSON = function () {
  1088. return this.get();
  1089. };
  1090. ObservableValue.prototype.toString = function () {
  1091. return this.name + "[" + this.value + "]";
  1092. };
  1093. ObservableValue.prototype.valueOf = function () {
  1094. return toPrimitive(this.get());
  1095. };
  1096. return ObservableValue;
  1097. }(Atom));
  1098. ObservableValue.prototype[primitiveSymbol()] = ObservableValue.prototype.valueOf;
  1099. var isObservableValue = createInstanceofPredicate("ObservableValue", ObservableValue);
  1100. /**
  1101. * A node in the state dependency root that observes other nodes, and can be observed itself.
  1102. *
  1103. * ComputedValue will remember the result of the computation for the duration of the batch, or
  1104. * while being observed.
  1105. *
  1106. * During this time it will recompute only when one of its direct dependencies changed,
  1107. * but only when it is being accessed with `ComputedValue.get()`.
  1108. *
  1109. * Implementation description:
  1110. * 1. First time it's being accessed it will compute and remember result
  1111. * give back remembered result until 2. happens
  1112. * 2. First time any deep dependency change, propagate POSSIBLY_STALE to all observers, wait for 3.
  1113. * 3. When it's being accessed, recompute if any shallow dependency changed.
  1114. * if result changed: propagate STALE to all observers, that were POSSIBLY_STALE from the last step.
  1115. * go to step 2. either way
  1116. *
  1117. * If at any point it's outside batch and it isn't observed: reset everything and go to 1.
  1118. */
  1119. var ComputedValue = /** @class */ (function () {
  1120. /**
  1121. * Create a new computed value based on a function expression.
  1122. *
  1123. * The `name` property is for debug purposes only.
  1124. *
  1125. * The `equals` property specifies the comparer function to use to determine if a newly produced
  1126. * value differs from the previous value. Two comparers are provided in the library; `defaultComparer`
  1127. * compares based on identity comparison (===), and `structualComparer` deeply compares the structure.
  1128. * Structural comparison can be convenient if you always produce a new aggregated object and
  1129. * don't want to notify observers if it is structurally the same.
  1130. * This is useful for working with vectors, mouse coordinates etc.
  1131. */
  1132. function ComputedValue(options) {
  1133. this.dependenciesState = exports.IDerivationState.NOT_TRACKING;
  1134. this.observing = []; // nodes we are looking at. Our value depends on these nodes
  1135. this.newObserving = null; // during tracking it's an array with new observed observers
  1136. this.isBeingObserved = false;
  1137. this.isPendingUnobservation = false;
  1138. this.observers = [];
  1139. this.observersIndexes = {};
  1140. this.diffValue = 0;
  1141. this.runId = 0;
  1142. this.lastAccessedBy = 0;
  1143. this.lowestObserverState = exports.IDerivationState.UP_TO_DATE;
  1144. this.unboundDepsCount = 0;
  1145. this.__mapid = "#" + getNextId();
  1146. this.value = new CaughtException(null);
  1147. this.isComputing = false; // to check for cycles
  1148. this.isRunningSetter = false;
  1149. this.isTracing = TraceMode.NONE;
  1150. if (process.env.NODE_ENV !== "production" && !options.get)
  1151. return fail("missing option for computed: get");
  1152. this.derivation = options.get;
  1153. this.name = options.name || "ComputedValue@" + getNextId();
  1154. if (options.set)
  1155. this.setter = createAction(this.name + "-setter", options.set);
  1156. this.equals =
  1157. options.equals ||
  1158. (options.compareStructural || options.struct
  1159. ? comparer.structural
  1160. : comparer.default);
  1161. this.scope = options.context;
  1162. this.requiresReaction = !!options.requiresReaction;
  1163. this.keepAlive = !!options.keepAlive;
  1164. }
  1165. ComputedValue.prototype.onBecomeStale = function () {
  1166. propagateMaybeChanged(this);
  1167. };
  1168. ComputedValue.prototype.onBecomeUnobserved = function () { };
  1169. ComputedValue.prototype.onBecomeObserved = function () { };
  1170. /**
  1171. * Returns the current value of this computed value.
  1172. * Will evaluate its computation first if needed.
  1173. */
  1174. ComputedValue.prototype.get = function () {
  1175. if (this.isComputing)
  1176. fail("Cycle detected in computation " + this.name + ": " + this.derivation);
  1177. if (globalState.inBatch === 0 && this.observers.length === 0 && !this.keepAlive) {
  1178. if (shouldCompute(this)) {
  1179. this.warnAboutUntrackedRead();
  1180. startBatch(); // See perf test 'computed memoization'
  1181. this.value = this.computeValue(false);
  1182. endBatch();
  1183. }
  1184. }
  1185. else {
  1186. reportObserved(this);
  1187. if (shouldCompute(this))
  1188. if (this.trackAndCompute())
  1189. propagateChangeConfirmed(this);
  1190. }
  1191. var result = this.value;
  1192. if (isCaughtException(result))
  1193. throw result.cause;
  1194. return result;
  1195. };
  1196. ComputedValue.prototype.peek = function () {
  1197. var res = this.computeValue(false);
  1198. if (isCaughtException(res))
  1199. throw res.cause;
  1200. return res;
  1201. };
  1202. ComputedValue.prototype.set = function (value) {
  1203. if (this.setter) {
  1204. invariant(!this.isRunningSetter, "The setter of computed value '" + this.name + "' is trying to update itself. Did you intend to update an _observable_ value, instead of the computed property?");
  1205. this.isRunningSetter = true;
  1206. try {
  1207. this.setter.call(this.scope, value);
  1208. }
  1209. finally {
  1210. this.isRunningSetter = false;
  1211. }
  1212. }
  1213. else
  1214. invariant(false, process.env.NODE_ENV !== "production" &&
  1215. "[ComputedValue '" + this.name + "'] It is not possible to assign a new value to a computed value.");
  1216. };
  1217. ComputedValue.prototype.trackAndCompute = function () {
  1218. if (isSpyEnabled()) {
  1219. spyReport({
  1220. object: this.scope,
  1221. type: "compute",
  1222. name: this.name
  1223. });
  1224. }
  1225. var oldValue = this.value;
  1226. var wasSuspended =
  1227. /* see #1208 */ this.dependenciesState === exports.IDerivationState.NOT_TRACKING;
  1228. var newValue = this.computeValue(true);
  1229. var changed = wasSuspended ||
  1230. isCaughtException(oldValue) ||
  1231. isCaughtException(newValue) ||
  1232. !this.equals(oldValue, newValue);
  1233. if (changed) {
  1234. this.value = newValue;
  1235. }
  1236. return changed;
  1237. };
  1238. ComputedValue.prototype.computeValue = function (track) {
  1239. this.isComputing = true;
  1240. globalState.computationDepth++;
  1241. var res;
  1242. if (track) {
  1243. res = trackDerivedFunction(this, this.derivation, this.scope);
  1244. }
  1245. else {
  1246. if (globalState.disableErrorBoundaries === true) {
  1247. res = this.derivation.call(this.scope);
  1248. }
  1249. else {
  1250. try {
  1251. res = this.derivation.call(this.scope);
  1252. }
  1253. catch (e) {
  1254. res = new CaughtException(e);
  1255. }
  1256. }
  1257. }
  1258. globalState.computationDepth--;
  1259. this.isComputing = false;
  1260. return res;
  1261. };
  1262. ComputedValue.prototype.suspend = function () {
  1263. if (!this.keepAlive) {
  1264. clearObserving(this);
  1265. this.value = undefined; // don't hold on to computed value!
  1266. }
  1267. };
  1268. ComputedValue.prototype.observe = function (listener, fireImmediately) {
  1269. var _this = this;
  1270. var firstTime = true;
  1271. var prevValue = undefined;
  1272. return autorun(function () {
  1273. var newValue = _this.get();
  1274. if (!firstTime || fireImmediately) {
  1275. var prevU = untrackedStart();
  1276. listener({
  1277. type: "update",
  1278. object: _this,
  1279. newValue: newValue,
  1280. oldValue: prevValue
  1281. });
  1282. untrackedEnd(prevU);
  1283. }
  1284. firstTime = false;
  1285. prevValue = newValue;
  1286. });
  1287. };
  1288. ComputedValue.prototype.warnAboutUntrackedRead = function () {
  1289. if (process.env.NODE_ENV === "production")
  1290. return;
  1291. if (this.requiresReaction === true) {
  1292. fail("[mobx] Computed value " + this.name + " is read outside a reactive context");
  1293. }
  1294. if (this.isTracing !== TraceMode.NONE) {
  1295. console.log("[mobx.trace] '" + this.name + "' is being read outside a reactive context. Doing a full recompute");
  1296. }
  1297. if (globalState.computedRequiresReaction) {
  1298. console.warn("[mobx] Computed value " + this.name + " is being read outside a reactive context. Doing a full recompute");
  1299. }
  1300. };
  1301. ComputedValue.prototype.toJSON = function () {
  1302. return this.get();
  1303. };
  1304. ComputedValue.prototype.toString = function () {
  1305. return this.name + "[" + this.derivation.toString() + "]";
  1306. };
  1307. ComputedValue.prototype.valueOf = function () {
  1308. return toPrimitive(this.get());
  1309. };
  1310. return ComputedValue;
  1311. }());
  1312. ComputedValue.prototype[primitiveSymbol()] = ComputedValue.prototype.valueOf;
  1313. var isComputedValue = createInstanceofPredicate("ComputedValue", ComputedValue);
  1314. /**
  1315. * These values will persist if global state is reset
  1316. */
  1317. var persistentKeys = [
  1318. "mobxGuid",
  1319. "spyListeners",
  1320. "enforceActions",
  1321. "computedRequiresReaction",
  1322. "reactionRequiresObservable",
  1323. "observableRequiresReaction",
  1324. "allowStateReads",
  1325. "disableErrorBoundaries",
  1326. "runId",
  1327. "UNCHANGED"
  1328. ];
  1329. var MobXGlobals = /** @class */ (function () {
  1330. function MobXGlobals() {
  1331. /**
  1332. * MobXGlobals version.
  1333. * MobX compatiblity with other versions loaded in memory as long as this version matches.
  1334. * It indicates that the global state still stores similar information
  1335. *
  1336. * N.B: this version is unrelated to the package version of MobX, and is only the version of the
  1337. * internal state storage of MobX, and can be the same across many different package versions
  1338. */
  1339. this.version = 5;
  1340. /**
  1341. * globally unique token to signal unchanged
  1342. */
  1343. this.UNCHANGED = {};
  1344. /**
  1345. * Currently running derivation
  1346. */
  1347. this.trackingDerivation = null;
  1348. /**
  1349. * Are we running a computation currently? (not a reaction)
  1350. */
  1351. this.computationDepth = 0;
  1352. /**
  1353. * Each time a derivation is tracked, it is assigned a unique run-id
  1354. */
  1355. this.runId = 0;
  1356. /**
  1357. * 'guid' for general purpose. Will be persisted amongst resets.
  1358. */
  1359. this.mobxGuid = 0;
  1360. /**
  1361. * Are we in a batch block? (and how many of them)
  1362. */
  1363. this.inBatch = 0;
  1364. /**
  1365. * Observables that don't have observers anymore, and are about to be
  1366. * suspended, unless somebody else accesses it in the same batch
  1367. *
  1368. * @type {IObservable[]}
  1369. */
  1370. this.pendingUnobservations = [];
  1371. /**
  1372. * List of scheduled, not yet executed, reactions.
  1373. */
  1374. this.pendingReactions = [];
  1375. /**
  1376. * Are we currently processing reactions?
  1377. */
  1378. this.isRunningReactions = false;
  1379. /**
  1380. * Is it allowed to change observables at this point?
  1381. * In general, MobX doesn't allow that when running computations and React.render.
  1382. * To ensure that those functions stay pure.
  1383. */
  1384. this.allowStateChanges = true;
  1385. /**
  1386. * Is it allowed to read observables at this point?
  1387. * Used to hold the state needed for `observableRequiresReaction`
  1388. */
  1389. this.allowStateReads = true;
  1390. /**
  1391. * If strict mode is enabled, state changes are by default not allowed
  1392. */
  1393. this.enforceActions = false;
  1394. /**
  1395. * Spy callbacks
  1396. */
  1397. this.spyListeners = [];
  1398. /**
  1399. * Globally attached error handlers that react specifically to errors in reactions
  1400. */
  1401. this.globalReactionErrorHandlers = [];
  1402. /**
  1403. * Warn if computed values are accessed outside a reactive context
  1404. */
  1405. this.computedRequiresReaction = false;
  1406. /**
  1407. * (Experimental)
  1408. * Warn if you try to create to derivation / reactive context without accessing any observable.
  1409. */
  1410. this.reactionRequiresObservable = false;
  1411. /**
  1412. * (Experimental)
  1413. * Warn if observables are accessed outside a reactive context
  1414. */
  1415. this.observableRequiresReaction = false;
  1416. /**
  1417. * Allows overwriting of computed properties, useful in tests but not prod as it can cause
  1418. * memory leaks. See https://github.com/mobxjs/mobx/issues/1867
  1419. */
  1420. this.computedConfigurable = false;
  1421. /*
  1422. * Don't catch and rethrow exceptions. This is useful for inspecting the state of
  1423. * the stack when an exception occurs while debugging.
  1424. */
  1425. this.disableErrorBoundaries = false;
  1426. /*
  1427. * If true, we are already handling an exception in an action. Any errors in reactions should be supressed, as
  1428. * they are not the cause, see: https://github.com/mobxjs/mobx/issues/1836
  1429. */
  1430. this.suppressReactionErrors = false;
  1431. }
  1432. return MobXGlobals;
  1433. }());
  1434. var canMergeGlobalState = true;
  1435. var isolateCalled = false;
  1436. var globalState = (function () {
  1437. var global = getGlobal();
  1438. if (global.__mobxInstanceCount > 0 && !global.__mobxGlobals)
  1439. canMergeGlobalState = false;
  1440. if (global.__mobxGlobals && global.__mobxGlobals.version !== new MobXGlobals().version)
  1441. canMergeGlobalState = false;
  1442. if (!canMergeGlobalState) {
  1443. setTimeout(function () {
  1444. if (!isolateCalled) {
  1445. fail("There are multiple, different versions of MobX active. Make sure MobX is loaded only once or use `configure({ isolateGlobalState: true })`");
  1446. }
  1447. }, 1);
  1448. return new MobXGlobals();
  1449. }
  1450. else if (global.__mobxGlobals) {
  1451. global.__mobxInstanceCount += 1;
  1452. if (!global.__mobxGlobals.UNCHANGED)
  1453. global.__mobxGlobals.UNCHANGED = {}; // make merge backward compatible
  1454. return global.__mobxGlobals;
  1455. }
  1456. else {
  1457. global.__mobxInstanceCount = 1;
  1458. return (global.__mobxGlobals = new MobXGlobals());
  1459. }
  1460. })();
  1461. function isolateGlobalState() {
  1462. if (globalState.pendingReactions.length ||
  1463. globalState.inBatch ||
  1464. globalState.isRunningReactions)
  1465. fail("isolateGlobalState should be called before MobX is running any reactions");
  1466. isolateCalled = true;
  1467. if (canMergeGlobalState) {
  1468. if (--getGlobal().__mobxInstanceCount === 0)
  1469. getGlobal().__mobxGlobals = undefined;
  1470. globalState = new MobXGlobals();
  1471. }
  1472. }
  1473. function getGlobalState() {
  1474. return globalState;
  1475. }
  1476. /**
  1477. * For testing purposes only; this will break the internal state of existing observables,
  1478. * but can be used to get back at a stable state after throwing errors
  1479. */
  1480. function resetGlobalState() {
  1481. var defaultGlobals = new MobXGlobals();
  1482. for (var key in defaultGlobals)
  1483. if (persistentKeys.indexOf(key) === -1)
  1484. globalState[key] = defaultGlobals[key];
  1485. globalState.allowStateChanges = !globalState.enforceActions;
  1486. }
  1487. function hasObservers(observable) {
  1488. return observable.observers && observable.observers.length > 0;
  1489. }
  1490. function getObservers(observable) {
  1491. return observable.observers;
  1492. }
  1493. // function invariantObservers(observable: IObservable) {
  1494. // const list = observable.observers
  1495. // const map = observable.observersIndexes
  1496. // const l = list.length
  1497. // for (let i = 0; i < l; i++) {
  1498. // const id = list[i].__mapid
  1499. // if (i) {
  1500. // invariant(map[id] === i, "INTERNAL ERROR maps derivation.__mapid to index in list") // for performance
  1501. // } else {
  1502. // invariant(!(id in map), "INTERNAL ERROR observer on index 0 shouldn't be held in map.") // for performance
  1503. // }
  1504. // }
  1505. // invariant(
  1506. // list.length === 0 || Object.keys(map).length === list.length - 1,
  1507. // "INTERNAL ERROR there is no junk in map"
  1508. // )
  1509. // }
  1510. function addObserver(observable, node) {
  1511. // invariant(node.dependenciesState !== -1, "INTERNAL ERROR, can add only dependenciesState !== -1");
  1512. // invariant(observable._observers.indexOf(node) === -1, "INTERNAL ERROR add already added node");
  1513. // invariantObservers(observable);
  1514. var l = observable.observers.length;
  1515. if (l) {
  1516. // because object assignment is relatively expensive, let's not store data about index 0.
  1517. observable.observersIndexes[node.__mapid] = l;
  1518. }
  1519. observable.observers[l] = node;
  1520. if (observable.lowestObserverState > node.dependenciesState)
  1521. observable.lowestObserverState = node.dependenciesState;
  1522. // invariantObservers(observable);
  1523. // invariant(observable._observers.indexOf(node) !== -1, "INTERNAL ERROR didn't add node");
  1524. }
  1525. function removeObserver(observable, node) {
  1526. // invariant(globalState.inBatch > 0, "INTERNAL ERROR, remove should be called only inside batch");
  1527. // invariant(observable._observers.indexOf(node) !== -1, "INTERNAL ERROR remove already removed node");
  1528. // invariantObservers(observable);
  1529. if (observable.observers.length === 1) {
  1530. // deleting last observer
  1531. observable.observers.length = 0;
  1532. queueForUnobservation(observable);
  1533. }
  1534. else {
  1535. // deleting from _observersIndexes is straight forward, to delete from _observers, let's swap `node` with last element
  1536. var list = observable.observers;
  1537. var map = observable.observersIndexes;
  1538. var filler = list.pop(); // get last element, which should fill the place of `node`, so the array doesn't have holes
  1539. if (filler !== node) {
  1540. // otherwise node was the last element, which already got removed from array
  1541. var index = map[node.__mapid] || 0; // getting index of `node`. this is the only place we actually use map.
  1542. if (index) {
  1543. // map store all indexes but 0, see comment in `addObserver`
  1544. map[filler.__mapid] = index;
  1545. }
  1546. else {
  1547. delete map[filler.__mapid];
  1548. }
  1549. list[index] = filler;
  1550. }
  1551. delete map[node.__mapid];
  1552. }
  1553. // invariantObservers(observable);
  1554. // invariant(observable._observers.indexOf(node) === -1, "INTERNAL ERROR remove already removed node2");
  1555. }
  1556. function queueForUnobservation(observable) {
  1557. if (observable.isPendingUnobservation === false) {
  1558. // invariant(observable._observers.length === 0, "INTERNAL ERROR, should only queue for unobservation unobserved observables");
  1559. observable.isPendingUnobservation = true;
  1560. globalState.pendingUnobservations.push(observable);
  1561. }
  1562. }
  1563. /**
  1564. * Batch starts a transaction, at least for purposes of memoizing ComputedValues when nothing else does.
  1565. * During a batch `onBecomeUnobserved` will be called at most once per observable.
  1566. * Avoids unnecessary recalculations.
  1567. */
  1568. function startBatch() {
  1569. globalState.inBatch++;
  1570. }
  1571. function endBatch() {
  1572. if (--globalState.inBatch === 0) {
  1573. runReactions();
  1574. // the batch is actually about to finish, all unobserving should happen here.
  1575. var list = globalState.pendingUnobservations;
  1576. for (var i = 0; i < list.length; i++) {
  1577. var observable = list[i];
  1578. observable.isPendingUnobservation = false;
  1579. if (observable.observers.length === 0) {
  1580. if (observable.isBeingObserved) {
  1581. // if this observable had reactive observers, trigger the hooks
  1582. observable.isBeingObserved = false;
  1583. observable.onBecomeUnobserved();
  1584. }
  1585. if (observable instanceof ComputedValue) {
  1586. // computed values are automatically teared down when the last observer leaves
  1587. // this process happens recursively, this computed might be the last observabe of another, etc..
  1588. observable.suspend();
  1589. }
  1590. }
  1591. }
  1592. globalState.pendingUnobservations = [];
  1593. }
  1594. }
  1595. function reportObserved(observable) {
  1596. checkIfStateReadsAreAllowed(observable);
  1597. var derivation = globalState.trackingDerivation;
  1598. if (derivation !== null) {
  1599. /**
  1600. * Simple optimization, give each derivation run an unique id (runId)
  1601. * Check if last time this observable was accessed the same runId is used
  1602. * if this is the case, the relation is already known
  1603. */
  1604. if (derivation.runId !== observable.lastAccessedBy) {
  1605. observable.lastAccessedBy = derivation.runId;
  1606. derivation.newObserving[derivation.unboundDepsCount++] = observable;
  1607. if (!observable.isBeingObserved) {
  1608. observable.isBeingObserved = true;
  1609. observable.onBecomeObserved();
  1610. }
  1611. }
  1612. return true;
  1613. }
  1614. else if (observable.observers.length === 0 && globalState.inBatch > 0) {
  1615. queueForUnobservation(observable);
  1616. }
  1617. return false;
  1618. }
  1619. // function invariantLOS(observable: IObservable, msg: string) {
  1620. // // it's expensive so better not run it in produciton. but temporarily helpful for testing
  1621. // const min = getObservers(observable).reduce((a, b) => Math.min(a, b.dependenciesState), 2)
  1622. // if (min >= observable.lowestObserverState) return // <- the only assumption about `lowestObserverState`
  1623. // throw new Error(
  1624. // "lowestObserverState is wrong for " +
  1625. // msg +
  1626. // " because " +
  1627. // min +
  1628. // " < " +
  1629. // observable.lowestObserverState
  1630. // )
  1631. // }
  1632. /**
  1633. * NOTE: current propagation mechanism will in case of self reruning autoruns behave unexpectedly
  1634. * It will propagate changes to observers from previous run
  1635. * It's hard or maybe impossible (with reasonable perf) to get it right with current approach
  1636. * Hopefully self reruning autoruns aren't a feature people should depend on
  1637. * Also most basic use cases should be ok
  1638. */
  1639. // Called by Atom when its value changes
  1640. function propagateChanged(observable) {
  1641. // invariantLOS(observable, "changed start");
  1642. if (observable.lowestObserverState === exports.IDerivationState.STALE)
  1643. return;
  1644. observable.lowestObserverState = exports.IDerivationState.STALE;
  1645. var observers = observable.observers;
  1646. var i = observers.length;
  1647. while (i--) {
  1648. var d = observers[i];
  1649. if (d.dependenciesState === exports.IDerivationState.UP_TO_DATE) {
  1650. if (d.isTracing !== TraceMode.NONE) {
  1651. logTraceInfo(d, observable);
  1652. }
  1653. d.onBecomeStale();
  1654. }
  1655. d.dependenciesState = exports.IDerivationState.STALE;
  1656. }
  1657. // invariantLOS(observable, "changed end");
  1658. }
  1659. // Called by ComputedValue when it recalculate and its value changed
  1660. function propagateChangeConfirmed(observable) {
  1661. // invariantLOS(observable, "confirmed start");
  1662. if (observable.lowestObserverState === exports.IDerivationState.STALE)
  1663. return;
  1664. observable.lowestObserverState = exports.IDerivationState.STALE;
  1665. var observers = observable.observers;
  1666. var i = observers.length;
  1667. while (i--) {
  1668. var d = observers[i];
  1669. if (d.dependenciesState === exports.IDerivationState.POSSIBLY_STALE)
  1670. d.dependenciesState = exports.IDerivationState.STALE;
  1671. else if (d.dependenciesState === exports.IDerivationState.UP_TO_DATE // this happens during computing of `d`, just keep lowestObserverState up to date.
  1672. )
  1673. observable.lowestObserverState = exports.IDerivationState.UP_TO_DATE;
  1674. }
  1675. // invariantLOS(observable, "confirmed end");
  1676. }
  1677. // Used by computed when its dependency changed, but we don't wan't to immediately recompute.
  1678. function propagateMaybeChanged(observable) {
  1679. // invariantLOS(observable, "maybe start");
  1680. if (observable.lowestObserverState !== exports.IDerivationState.UP_TO_DATE)
  1681. return;
  1682. observable.lowestObserverState = exports.IDerivationState.POSSIBLY_STALE;
  1683. var observers = observable.observers;
  1684. var i = observers.length;
  1685. while (i--) {
  1686. var d = observers[i];
  1687. if (d.dependenciesState === exports.IDerivationState.UP_TO_DATE) {
  1688. d.dependenciesState = exports.IDerivationState.POSSIBLY_STALE;
  1689. if (d.isTracing !== TraceMode.NONE) {
  1690. logTraceInfo(d, observable);
  1691. }
  1692. d.onBecomeStale();
  1693. }
  1694. }
  1695. // invariantLOS(observable, "maybe end");
  1696. }
  1697. function logTraceInfo(derivation, observable) {
  1698. console.log("[mobx.trace] '" + derivation.name + "' is invalidated due to a change in: '" + observable.name + "'");
  1699. if (derivation.isTracing === TraceMode.BREAK) {
  1700. var lines = [];
  1701. printDepTree(getDependencyTree(derivation), lines, 1);
  1702. // prettier-ignore
  1703. new Function("debugger;\n/*\nTracing '" + derivation.name + "'\n\nYou are entering this break point because derivation '" + derivation.name + "' is being traced and '" + observable.name + "' is now forcing it to update.\nJust follow the stacktrace you should now see in the devtools to see precisely what piece of your code is causing this update\nThe stackframe you are looking for is at least ~6-8 stack-frames up.\n\n" + (derivation instanceof ComputedValue ? derivation.derivation.toString().replace(/[*]\//g, "/") : "") + "\n\nThe dependencies for this derivation are:\n\n" + lines.join("\n") + "\n*/\n ")();
  1704. }
  1705. }
  1706. function printDepTree(tree, lines, depth) {
  1707. if (lines.length >= 1000) {
  1708. lines.push("(and many more)");
  1709. return;
  1710. }
  1711. lines.push("" + new Array(depth).join("\t") + tree.name); // MWE: not the fastest, but the easiest way :)
  1712. if (tree.dependencies)
  1713. tree.dependencies.forEach(function (child) { return printDepTree(child, lines, depth + 1); });
  1714. }
  1715. var Reaction = /** @class */ (function () {
  1716. function Reaction(name, onInvalidate, errorHandler, requiresObservable) {
  1717. if (name === void 0) { name = "Reaction@" + getNextId(); }
  1718. if (requiresObservable === void 0) { requiresObservable = false; }
  1719. this.name = name;
  1720. this.onInvalidate = onInvalidate;
  1721. this.errorHandler = errorHandler;
  1722. this.requiresObservable = requiresObservable;
  1723. this.observing = []; // nodes we are looking at. Our value depends on these nodes
  1724. this.newObserving = [];
  1725. this.dependenciesState = exports.IDerivationState.NOT_TRACKING;
  1726. this.diffValue = 0;
  1727. this.runId = 0;
  1728. this.unboundDepsCount = 0;
  1729. this.__mapid = "#" + getNextId();
  1730. this.isDisposed = false;
  1731. this._isScheduled = false;
  1732. this._isTrackPending = false;
  1733. this._isRunning = false;
  1734. this.isTracing = TraceMode.NONE;
  1735. }
  1736. Reaction.prototype.onBecomeStale = function () {
  1737. this.schedule();
  1738. };
  1739. Reaction.prototype.schedule = function () {
  1740. if (!this._isScheduled) {
  1741. this._isScheduled = true;
  1742. globalState.pendingReactions.push(this);
  1743. runReactions();
  1744. }
  1745. };
  1746. Reaction.prototype.isScheduled = function () {
  1747. return this._isScheduled;
  1748. };
  1749. /**
  1750. * internal, use schedule() if you intend to kick off a reaction
  1751. */
  1752. Reaction.prototype.runReaction = function () {
  1753. if (!this.isDisposed) {
  1754. startBatch();
  1755. this._isScheduled = false;
  1756. if (shouldCompute(this)) {
  1757. this._isTrackPending = true;
  1758. try {
  1759. this.onInvalidate();
  1760. if (this._isTrackPending && isSpyEnabled()) {
  1761. // onInvalidate didn't trigger track right away..
  1762. spyReport({
  1763. name: this.name,
  1764. type: "scheduled-reaction"
  1765. });
  1766. }
  1767. }
  1768. catch (e) {
  1769. this.reportExceptionInDerivation(e);
  1770. }
  1771. }
  1772. endBatch();
  1773. }
  1774. };
  1775. Reaction.prototype.track = function (fn) {
  1776. startBatch();
  1777. var notify = isSpyEnabled();
  1778. var startTime;
  1779. if (notify) {
  1780. startTime = Date.now();
  1781. spyReportStart({
  1782. name: this.name,
  1783. type: "reaction"
  1784. });
  1785. }
  1786. this._isRunning = true;
  1787. var result = trackDerivedFunction(this, fn, undefined);
  1788. this._isRunning = false;
  1789. this._isTrackPending = false;
  1790. if (this.isDisposed) {
  1791. // disposed during last run. Clean up everything that was bound after the dispose call.
  1792. clearObserving(this);
  1793. }
  1794. if (isCaughtException(result))
  1795. this.reportExceptionInDerivation(result.cause);
  1796. if (notify) {
  1797. spyReportEnd({
  1798. time: Date.now() - startTime
  1799. });
  1800. }
  1801. endBatch();
  1802. };
  1803. Reaction.prototype.reportExceptionInDerivation = function (error) {
  1804. var _this = this;
  1805. if (this.errorHandler) {
  1806. this.errorHandler(error, this);
  1807. return;
  1808. }
  1809. if (globalState.disableErrorBoundaries)
  1810. throw error;
  1811. var message = "[mobx] Encountered an uncaught exception that was thrown by a reaction or observer component, in: '" + this + "'";
  1812. if (globalState.suppressReactionErrors) {
  1813. console.warn("[mobx] (error in reaction '" + this.name + "' suppressed, fix error of causing action below)"); // prettier-ignore
  1814. }
  1815. else {
  1816. console.error(message, error);
  1817. /** If debugging brought you here, please, read the above message :-). Tnx! */
  1818. }
  1819. if (isSpyEnabled()) {
  1820. spyReport({
  1821. type: "error",
  1822. name: this.name,
  1823. message: message,
  1824. error: "" + error
  1825. });
  1826. }
  1827. globalState.globalReactionErrorHandlers.forEach(function (f) { return f(error, _this); });
  1828. };
  1829. Reaction.prototype.dispose = function () {
  1830. if (!this.isDisposed) {
  1831. this.isDisposed = true;
  1832. if (!this._isRunning) {
  1833. // if disposed while running, clean up later. Maybe not optimal, but rare case
  1834. startBatch();
  1835. clearObserving(this);
  1836. endBatch();
  1837. }
  1838. }
  1839. };
  1840. Reaction.prototype.getDisposer = function () {
  1841. var r = this.dispose.bind(this);
  1842. r.$mobx = this;
  1843. return r;
  1844. };
  1845. Reaction.prototype.toString = function () {
  1846. return "Reaction[" + this.name + "]";
  1847. };
  1848. Reaction.prototype.trace = function (enterBreakPoint) {
  1849. if (enterBreakPoint === void 0) { enterBreakPoint = false; }
  1850. trace(this, enterBreakPoint);
  1851. };
  1852. return Reaction;
  1853. }());
  1854. function onReactionError(handler) {
  1855. globalState.globalReactionErrorHandlers.push(handler);
  1856. return function () {
  1857. var idx = globalState.globalReactionErrorHandlers.indexOf(handler);
  1858. if (idx >= 0)
  1859. globalState.globalReactionErrorHandlers.splice(idx, 1);
  1860. };
  1861. }
  1862. /**
  1863. * Magic number alert!
  1864. * Defines within how many times a reaction is allowed to re-trigger itself
  1865. * until it is assumed that this is gonna be a never ending loop...
  1866. */
  1867. var MAX_REACTION_ITERATIONS = 100;
  1868. var reactionScheduler = function (f) { return f(); };
  1869. function runReactions() {
  1870. // Trampolining, if runReactions are already running, new reactions will be picked up
  1871. if (globalState.inBatch > 0 || globalState.isRunningReactions)
  1872. return;
  1873. reactionScheduler(runReactionsHelper);
  1874. }
  1875. function runReactionsHelper() {
  1876. globalState.isRunningReactions = true;
  1877. var allReactions = globalState.pendingReactions;
  1878. var iterations = 0;
  1879. // While running reactions, new reactions might be triggered.
  1880. // Hence we work with two variables and check whether
  1881. // we converge to no remaining reactions after a while.
  1882. while (allReactions.length > 0) {
  1883. if (++iterations === MAX_REACTION_ITERATIONS) {
  1884. console.error("Reaction doesn't converge to a stable state after " + MAX_REACTION_ITERATIONS + " iterations." +
  1885. (" Probably there is a cycle in the reactive function: " + allReactions[0]));
  1886. allReactions.splice(0); // clear reactions
  1887. }
  1888. var remainingReactions = allReactions.splice(0);
  1889. for (var i = 0, l = remainingReactions.length; i < l; i++)
  1890. remainingReactions[i].runReaction();
  1891. }
  1892. globalState.isRunningReactions = false;
  1893. }
  1894. var isReaction = createInstanceofPredicate("Reaction", Reaction);
  1895. function setReactionScheduler(fn) {
  1896. var baseScheduler = reactionScheduler;
  1897. reactionScheduler = function (f) { return fn(function () { return baseScheduler(f); }); };
  1898. }
  1899. function isSpyEnabled() {
  1900. return !!globalState.spyListeners.length;
  1901. }
  1902. function spyReport(event) {
  1903. if (!globalState.spyListeners.length)
  1904. return;
  1905. var listeners = globalState.spyListeners;
  1906. for (var i = 0, l = listeners.length; i < l; i++)
  1907. listeners[i](event);
  1908. }
  1909. function spyReportStart(event) {
  1910. var change = __assign(__assign({}, event), { spyReportStart: true });
  1911. spyReport(change);
  1912. }
  1913. var END_EVENT = { spyReportEnd: true };
  1914. function spyReportEnd(change) {
  1915. if (change)
  1916. spyReport(__assign(__assign({}, change), { spyReportEnd: true }));
  1917. else
  1918. spyReport(END_EVENT);
  1919. }
  1920. function spy(listener) {
  1921. globalState.spyListeners.push(listener);
  1922. return once(function () {
  1923. globalState.spyListeners = globalState.spyListeners.filter(function (l) { return l !== listener; });
  1924. });
  1925. }
  1926. function dontReassignFields() {
  1927. fail(process.env.NODE_ENV !== "production" && "@action fields are not reassignable");
  1928. }
  1929. function namedActionDecorator(name) {
  1930. return function (target, prop, descriptor) {
  1931. if (descriptor) {
  1932. if (process.env.NODE_ENV !== "production" && descriptor.get !== undefined) {
  1933. return fail("@action cannot be used with getters");
  1934. }
  1935. // babel / typescript
  1936. // @action method() { }
  1937. if (descriptor.value) {
  1938. // typescript
  1939. return {
  1940. value: createAction(name, descriptor.value),
  1941. enumerable: false,
  1942. configurable: true,
  1943. writable: true // for typescript, this must be writable, otherwise it cannot inherit :/ (see inheritable actions test)
  1944. };
  1945. }
  1946. // babel only: @action method = () => {}
  1947. var initializer_1 = descriptor.initializer;
  1948. return {
  1949. enumerable: false,
  1950. configurable: true,
  1951. writable: true,
  1952. initializer: function () {
  1953. // N.B: we can't immediately invoke initializer; this would be wrong
  1954. return createAction(name, initializer_1.call(this));
  1955. }
  1956. };
  1957. }
  1958. // bound instance methods
  1959. return actionFieldDecorator(name).apply(this, arguments);
  1960. };
  1961. }
  1962. function actionFieldDecorator(name) {
  1963. // Simple property that writes on first invocation to the current instance
  1964. return function (target, prop, descriptor) {
  1965. Object.defineProperty(target, prop, {
  1966. configurable: true,
  1967. enumerable: false,
  1968. get: function () {
  1969. return undefined;
  1970. },
  1971. set: function (value) {
  1972. addHiddenProp(this, prop, action(name, value));
  1973. }
  1974. });
  1975. };
  1976. }
  1977. function boundActionDecorator(target, propertyName, descriptor, applyToInstance) {
  1978. if (applyToInstance === true) {
  1979. defineBoundAction(target, propertyName, descriptor.value);
  1980. return null;
  1981. }
  1982. if (descriptor) {
  1983. // if (descriptor.value)
  1984. // Typescript / Babel: @action.bound method() { }
  1985. // also: babel @action.bound method = () => {}
  1986. return {
  1987. configurable: true,
  1988. enumerable: false,
  1989. get: function () {
  1990. defineBoundAction(this, propertyName, descriptor.value || descriptor.initializer.call(this));
  1991. return this[propertyName];
  1992. },
  1993. set: dontReassignFields
  1994. };
  1995. }
  1996. // field decorator Typescript @action.bound method = () => {}
  1997. return {
  1998. enumerable: false,
  1999. configurable: true,
  2000. set: function (v) {
  2001. defineBoundAction(this, propertyName, v);
  2002. },
  2003. get: function () {
  2004. return undefined;
  2005. }
  2006. };
  2007. }
  2008. var action = function action(arg1, arg2, arg3, arg4) {
  2009. // action(fn() {})
  2010. if (arguments.length === 1 && typeof arg1 === "function")
  2011. return createAction(arg1.name || "<unnamed action>", arg1);
  2012. // action("name", fn() {})
  2013. if (arguments.length === 2 && typeof arg2 === "function")
  2014. return createAction(arg1, arg2);
  2015. // @action("name") fn() {}
  2016. if (arguments.length === 1 && typeof arg1 === "string")
  2017. return namedActionDecorator(arg1);
  2018. // @action fn() {}
  2019. if (arg4 === true) {
  2020. // apply to instance immediately
  2021. arg1[arg2] = createAction(arg1.name || arg2, arg3.value);
  2022. }
  2023. else {
  2024. return namedActionDecorator(arg2).apply(null, arguments);
  2025. }
  2026. };
  2027. action.bound = boundActionDecorator;
  2028. function runInAction(arg1, arg2) {
  2029. // TODO: deprecate?
  2030. var actionName = typeof arg1 === "string" ? arg1 : arg1.name || "<unnamed action>";
  2031. var fn = typeof arg1 === "function" ? arg1 : arg2;
  2032. if (process.env.NODE_ENV !== "production") {
  2033. invariant(typeof fn === "function" && fn.length === 0, "`runInAction` expects a function without arguments");
  2034. if (typeof actionName !== "string" || !actionName)
  2035. fail("actions should have valid names, got: '" + actionName + "'");
  2036. }
  2037. return executeAction(actionName, fn, this, undefined);
  2038. }
  2039. function isAction(thing) {
  2040. return typeof thing === "function" && thing.isMobxAction === true;
  2041. }
  2042. function defineBoundAction(target, propertyName, fn) {
  2043. addHiddenProp(target, propertyName, createAction(propertyName, fn.bind(target)));
  2044. }
  2045. /**
  2046. * Creates a named reactive view and keeps it alive, so that the view is always
  2047. * updated if one of the dependencies changes, even when the view is not further used by something else.
  2048. * @param view The reactive view
  2049. * @returns disposer function, which can be used to stop the view from being updated in the future.
  2050. */
  2051. function autorun(view, opts) {
  2052. if (opts === void 0) { opts = EMPTY_OBJECT; }
  2053. if (process.env.NODE_ENV !== "production") {
  2054. invariant(typeof view === "function", "Autorun expects a function as first argument");
  2055. invariant(isAction(view) === false, "Autorun does not accept actions since actions are untrackable");
  2056. }
  2057. var name = (opts && opts.name) || view.name || "Autorun@" + getNextId();
  2058. var runSync = !opts.scheduler && !opts.delay;
  2059. var reaction;
  2060. if (runSync) {
  2061. // normal autorun
  2062. reaction = new Reaction(name, function () {
  2063. this.track(reactionRunner);
  2064. }, opts.onError, opts.requiresObservable);
  2065. }
  2066. else {
  2067. var scheduler_1 = createSchedulerFromOptions(opts);
  2068. // debounced autorun
  2069. var isScheduled_1 = false;
  2070. reaction = new Reaction(name, function () {
  2071. if (!isScheduled_1) {
  2072. isScheduled_1 = true;
  2073. scheduler_1(function () {
  2074. isScheduled_1 = false;
  2075. if (!reaction.isDisposed)
  2076. reaction.track(reactionRunner);
  2077. });
  2078. }
  2079. }, opts.onError, opts.requiresObservable);
  2080. }
  2081. function reactionRunner() {
  2082. view(reaction);
  2083. }
  2084. reaction.schedule();
  2085. return reaction.getDisposer();
  2086. }
  2087. var run = function (f) { return f(); };
  2088. function createSchedulerFromOptions(opts) {
  2089. return opts.scheduler
  2090. ? opts.scheduler
  2091. : opts.delay
  2092. ? function (f) { return setTimeout(f, opts.delay); }
  2093. : run;
  2094. }
  2095. function reaction(expression, effect, opts) {
  2096. if (opts === void 0) { opts = EMPTY_OBJECT; }
  2097. if (typeof opts === "boolean") {
  2098. opts = { fireImmediately: opts };
  2099. deprecated("Using fireImmediately as argument is deprecated. Use '{ fireImmediately: true }' instead");
  2100. }
  2101. if (process.env.NODE_ENV !== "production") {
  2102. invariant(typeof expression === "function", "First argument to reaction should be a function");
  2103. invariant(typeof opts === "object", "Third argument of reactions should be an object");
  2104. }
  2105. var name = opts.name || "Reaction@" + getNextId();
  2106. var effectAction = action(name, opts.onError ? wrapErrorHandler(opts.onError, effect) : effect);
  2107. var runSync = !opts.scheduler && !opts.delay;
  2108. var scheduler = createSchedulerFromOptions(opts);
  2109. var firstTime = true;
  2110. var isScheduled = false;
  2111. var value;
  2112. var equals = opts.compareStructural
  2113. ? comparer.structural
  2114. : opts.equals || comparer.default;
  2115. var r = new Reaction(name, function () {
  2116. if (firstTime || runSync) {
  2117. reactionRunner();
  2118. }
  2119. else if (!isScheduled) {
  2120. isScheduled = true;
  2121. scheduler(reactionRunner);
  2122. }
  2123. }, opts.onError, opts.requiresObservable);
  2124. function reactionRunner() {
  2125. isScheduled = false; // Q: move into reaction runner?
  2126. if (r.isDisposed)
  2127. return;
  2128. var changed = false;
  2129. r.track(function () {
  2130. var nextValue = expression(r);
  2131. changed = firstTime || !equals(value, nextValue);
  2132. value = nextValue;
  2133. });
  2134. if (firstTime && opts.fireImmediately)
  2135. effectAction(value, r);
  2136. if (!firstTime && changed === true)
  2137. effectAction(value, r);
  2138. if (firstTime)
  2139. firstTime = false;
  2140. }
  2141. r.schedule();
  2142. return r.getDisposer();
  2143. }
  2144. function wrapErrorHandler(errorHandler, baseFn) {
  2145. return function () {
  2146. try {
  2147. return baseFn.apply(this, arguments);
  2148. }
  2149. catch (e) {
  2150. errorHandler.call(this, e);
  2151. }
  2152. };
  2153. }
  2154. function onBecomeObserved(thing, arg2, arg3) {
  2155. return interceptHook("onBecomeObserved", thing, arg2, arg3);
  2156. }
  2157. function onBecomeUnobserved(thing, arg2, arg3) {
  2158. return interceptHook("onBecomeUnobserved", thing, arg2, arg3);
  2159. }
  2160. function interceptHook(hook, thing, arg2, arg3) {
  2161. var atom = typeof arg3 === "function" ? getAtom(thing, arg2) : getAtom(thing);
  2162. var cb = typeof arg3 === "function" ? arg3 : arg2;
  2163. var orig = atom[hook];
  2164. if (typeof orig !== "function")
  2165. return fail(process.env.NODE_ENV !== "production" && "Not an atom that can be (un)observed");
  2166. atom[hook] = function () {
  2167. orig.call(this);
  2168. cb.call(this);
  2169. };
  2170. return function () {
  2171. atom[hook] = orig;
  2172. };
  2173. }
  2174. function configure(options) {
  2175. var enforceActions = options.enforceActions, computedRequiresReaction = options.computedRequiresReaction, computedConfigurable = options.computedConfigurable, disableErrorBoundaries = options.disableErrorBoundaries, arrayBuffer = options.arrayBuffer, reactionScheduler = options.reactionScheduler, reactionRequiresObservable = options.reactionRequiresObservable, observableRequiresReaction = options.observableRequiresReaction;
  2176. if (options.isolateGlobalState === true) {
  2177. isolateGlobalState();
  2178. }
  2179. if (enforceActions !== undefined) {
  2180. if (typeof enforceActions === "boolean" || enforceActions === "strict")
  2181. deprecated("Deprecated value for 'enforceActions', use 'false' => '\"never\"', 'true' => '\"observed\"', '\"strict\"' => \"'always'\" instead");
  2182. var ea = void 0;
  2183. switch (enforceActions) {
  2184. case true:
  2185. case "observed":
  2186. ea = true;
  2187. break;
  2188. case false:
  2189. case "never":
  2190. ea = false;
  2191. break;
  2192. case "strict":
  2193. case "always":
  2194. ea = "strict";
  2195. break;
  2196. default:
  2197. fail("Invalid value for 'enforceActions': '" + enforceActions + "', expected 'never', 'always' or 'observed'");
  2198. }
  2199. globalState.enforceActions = ea;
  2200. globalState.allowStateChanges = ea === true || ea === "strict" ? false : true;
  2201. }
  2202. if (computedRequiresReaction !== undefined) {
  2203. globalState.computedRequiresReaction = !!computedRequiresReaction;
  2204. }
  2205. if (reactionRequiresObservable !== undefined) {
  2206. globalState.reactionRequiresObservable = !!reactionRequiresObservable;
  2207. }
  2208. if (observableRequiresReaction !== undefined) {
  2209. globalState.observableRequiresReaction = !!observableRequiresReaction;
  2210. globalState.allowStateReads = !globalState.observableRequiresReaction;
  2211. }
  2212. if (computedConfigurable !== undefined) {
  2213. globalState.computedConfigurable = !!computedConfigurable;
  2214. }
  2215. if (disableErrorBoundaries !== undefined) {
  2216. if (disableErrorBoundaries === true)
  2217. console.warn("WARNING: Debug feature only. MobX will NOT recover from errors if this is on.");
  2218. globalState.disableErrorBoundaries = !!disableErrorBoundaries;
  2219. }
  2220. if (typeof arrayBuffer === "number") {
  2221. reserveArrayBuffer(arrayBuffer);
  2222. }
  2223. if (reactionScheduler) {
  2224. setReactionScheduler(reactionScheduler);
  2225. }
  2226. }
  2227. function decorate(thing, decorators) {
  2228. if (process.env.NODE_ENV !== "production" && !isPlainObject(decorators))
  2229. fail("Decorators should be a key value map");
  2230. var target = typeof thing === "function" ? thing.prototype : thing;
  2231. var _loop_1 = function (prop) {
  2232. var propertyDecorators = decorators[prop];
  2233. if (!Array.isArray(propertyDecorators)) {
  2234. propertyDecorators = [propertyDecorators];
  2235. }
  2236. // prettier-ignore
  2237. if (process.env.NODE_ENV !== "production" && !propertyDecorators.every(function (decorator) { return typeof decorator === "function"; }))
  2238. fail("Decorate: expected a decorator function or array of decorator functions for '" + prop + "'");
  2239. var descriptor = Object.getOwnPropertyDescriptor(target, prop);
  2240. var newDescriptor = propertyDecorators.reduce(function (accDescriptor, decorator) { return decorator(target, prop, accDescriptor); }, descriptor);
  2241. if (newDescriptor)
  2242. Object.defineProperty(target, prop, newDescriptor);
  2243. };
  2244. for (var prop in decorators) {
  2245. _loop_1(prop);
  2246. }
  2247. return thing;
  2248. }
  2249. function extendShallowObservable(target, properties, decorators) {
  2250. deprecated("'extendShallowObservable' is deprecated, use 'extendObservable(target, props, { deep: false })' instead");
  2251. return extendObservable(target, properties, decorators, shallowCreateObservableOptions);
  2252. }
  2253. function extendObservable(target, properties, decorators, options) {
  2254. if (process.env.NODE_ENV !== "production") {
  2255. invariant(arguments.length >= 2 && arguments.length <= 4, "'extendObservable' expected 2-4 arguments");
  2256. invariant(typeof target === "object", "'extendObservable' expects an object as first argument");
  2257. invariant(!isObservableMap(target), "'extendObservable' should not be used on maps, use map.merge instead");
  2258. invariant(!isObservable(properties), "Extending an object with another observable (object) is not supported. Please construct an explicit propertymap, using `toJS` if need. See issue #540");
  2259. if (decorators)
  2260. for (var key in decorators)
  2261. if (!(key in properties))
  2262. fail("Trying to declare a decorator for unspecified property '" + key + "'");
  2263. }
  2264. options = asCreateObservableOptions(options);
  2265. var defaultDecorator = options.defaultDecorator || (options.deep === false ? refDecorator : deepDecorator);
  2266. initializeInstance(target);
  2267. asObservableObject(target, options.name, defaultDecorator.enhancer); // make sure object is observable, even without initial props
  2268. startBatch();
  2269. try {
  2270. for (var key in properties) {
  2271. var descriptor = Object.getOwnPropertyDescriptor(properties, key);
  2272. if (process.env.NODE_ENV !== "production") {
  2273. if (Object.getOwnPropertyDescriptor(target, key))
  2274. fail("'extendObservable' can only be used to introduce new properties. Use 'set' or 'decorate' instead. The property '" + key + "' already exists on '" + target + "'");
  2275. if (isComputed(descriptor.value))
  2276. fail("Passing a 'computed' as initial property value is no longer supported by extendObservable. Use a getter or decorator instead");
  2277. }
  2278. var decorator = decorators && key in decorators
  2279. ? decorators[key]
  2280. : descriptor.get
  2281. ? computedDecorator
  2282. : defaultDecorator;
  2283. if (process.env.NODE_ENV !== "production" && typeof decorator !== "function")
  2284. return fail("Not a valid decorator for '" + key + "', got: " + decorator);
  2285. var resultDescriptor = decorator(target, key, descriptor, true);
  2286. if (resultDescriptor // otherwise, assume already applied, due to `applyToInstance`
  2287. )
  2288. Object.defineProperty(target, key, resultDescriptor);
  2289. }
  2290. }
  2291. finally {
  2292. endBatch();
  2293. }
  2294. return target;
  2295. }
  2296. function getDependencyTree(thing, property) {
  2297. return nodeToDependencyTree(getAtom(thing, property));
  2298. }
  2299. function nodeToDependencyTree(node) {
  2300. var result = {
  2301. name: node.name
  2302. };
  2303. if (node.observing && node.observing.length > 0)
  2304. result.dependencies = unique(node.observing).map(nodeToDependencyTree);
  2305. return result;
  2306. }
  2307. function getObserverTree(thing, property) {
  2308. return nodeToObserverTree(getAtom(thing, property));
  2309. }
  2310. function nodeToObserverTree(node) {
  2311. var result = {
  2312. name: node.name
  2313. };
  2314. if (hasObservers(node))
  2315. result.observers = getObservers(node).map(nodeToObserverTree);
  2316. return result;
  2317. }
  2318. var generatorId = 0;
  2319. function FlowCancellationError() {
  2320. this.message = "FLOW_CANCELLED";
  2321. }
  2322. FlowCancellationError.prototype = Object.create(Error.prototype);
  2323. function isFlowCancellationError(error) {
  2324. return error instanceof FlowCancellationError;
  2325. }
  2326. function flow(generator) {
  2327. if (arguments.length !== 1)
  2328. fail(!!process.env.NODE_ENV && "Flow expects one 1 argument and cannot be used as decorator");
  2329. var name = generator.name || "<unnamed flow>";
  2330. // Implementation based on https://github.com/tj/co/blob/master/index.js
  2331. return function () {
  2332. var ctx = this;
  2333. var args = arguments;
  2334. var runId = ++generatorId;
  2335. var gen = action(name + " - runid: " + runId + " - init", generator).apply(ctx, args);
  2336. var rejector;
  2337. var pendingPromise = undefined;
  2338. var res = new Promise(function (resolve, reject) {
  2339. var stepId = 0;
  2340. rejector = reject;
  2341. function onFulfilled(res) {
  2342. pendingPromise = undefined;
  2343. var ret;
  2344. try {
  2345. ret = action(name + " - runid: " + runId + " - yield " + stepId++, gen.next).call(gen, res);
  2346. }
  2347. catch (e) {
  2348. return reject(e);
  2349. }
  2350. next(ret);
  2351. }
  2352. function onRejected(err) {
  2353. pendingPromise = undefined;
  2354. var ret;
  2355. try {
  2356. ret = action(name + " - runid: " + runId + " - yield " + stepId++, gen.throw).call(gen, err);
  2357. }
  2358. catch (e) {
  2359. return reject(e);
  2360. }
  2361. next(ret);
  2362. }
  2363. function next(ret) {
  2364. if (ret && typeof ret.then === "function") {
  2365. // an async iterator
  2366. ret.then(next, reject);
  2367. return;
  2368. }
  2369. if (ret.done)
  2370. return resolve(ret.value);
  2371. pendingPromise = Promise.resolve(ret.value);
  2372. return pendingPromise.then(onFulfilled, onRejected);
  2373. }
  2374. onFulfilled(undefined); // kick off the process
  2375. });
  2376. res.cancel = action(name + " - runid: " + runId + " - cancel", function () {
  2377. try {
  2378. if (pendingPromise)
  2379. cancelPromise(pendingPromise);
  2380. // Finally block can return (or yield) stuff..
  2381. var res_1 = gen.return();
  2382. // eat anything that promise would do, it's cancelled!
  2383. var yieldedPromise = Promise.resolve(res_1.value);
  2384. yieldedPromise.then(noop, noop);
  2385. cancelPromise(yieldedPromise); // maybe it can be cancelled :)
  2386. // reject our original promise
  2387. rejector(new FlowCancellationError());
  2388. }
  2389. catch (e) {
  2390. rejector(e); // there could be a throwing finally block
  2391. }
  2392. });
  2393. return res;
  2394. };
  2395. }
  2396. function cancelPromise(promise) {
  2397. if (typeof promise.cancel === "function")
  2398. promise.cancel();
  2399. }
  2400. function interceptReads(thing, propOrHandler, handler) {
  2401. var target;
  2402. if (isObservableMap(thing) || isObservableArray(thing) || isObservableValue(thing)) {
  2403. target = getAdministration(thing);
  2404. }
  2405. else if (isObservableObject(thing)) {
  2406. if (typeof propOrHandler !== "string")
  2407. return fail(process.env.NODE_ENV !== "production" &&
  2408. "InterceptReads can only be used with a specific property, not with an object in general");
  2409. target = getAdministration(thing, propOrHandler);
  2410. }
  2411. else {
  2412. return fail(process.env.NODE_ENV !== "production" &&
  2413. "Expected observable map, object or array as first array");
  2414. }
  2415. if (target.dehancer !== undefined)
  2416. return fail(process.env.NODE_ENV !== "production" && "An intercept reader was already established");
  2417. target.dehancer = typeof propOrHandler === "function" ? propOrHandler : handler;
  2418. return function () {
  2419. target.dehancer = undefined;
  2420. };
  2421. }
  2422. function intercept(thing, propOrHandler, handler) {
  2423. if (typeof handler === "function")
  2424. return interceptProperty(thing, propOrHandler, handler);
  2425. else
  2426. return interceptInterceptable(thing, propOrHandler);
  2427. }
  2428. function interceptInterceptable(thing, handler) {
  2429. return getAdministration(thing).intercept(handler);
  2430. }
  2431. function interceptProperty(thing, property, handler) {
  2432. return getAdministration(thing, property).intercept(handler);
  2433. }
  2434. function _isComputed(value, property) {
  2435. if (value === null || value === undefined)
  2436. return false;
  2437. if (property !== undefined) {
  2438. if (isObservableObject(value) === false)
  2439. return false;
  2440. if (!value.$mobx.values[property])
  2441. return false;
  2442. var atom = getAtom(value, property);
  2443. return isComputedValue(atom);
  2444. }
  2445. return isComputedValue(value);
  2446. }
  2447. function isComputed(value) {
  2448. if (arguments.length > 1)
  2449. return fail(process.env.NODE_ENV !== "production" &&
  2450. "isComputed expects only 1 argument. Use isObservableProp to inspect the observability of a property");
  2451. return _isComputed(value);
  2452. }
  2453. function isComputedProp(value, propName) {
  2454. if (typeof propName !== "string")
  2455. return fail(process.env.NODE_ENV !== "production" &&
  2456. "isComputed expected a property name as second argument");
  2457. return _isComputed(value, propName);
  2458. }
  2459. function _isObservable(value, property) {
  2460. if (value === null || value === undefined)
  2461. return false;
  2462. if (property !== undefined) {
  2463. if (process.env.NODE_ENV !== "production" &&
  2464. (isObservableMap(value) || isObservableArray(value)))
  2465. return fail("isObservable(object, propertyName) is not supported for arrays and maps. Use map.has or array.length instead.");
  2466. if (isObservableObject(value)) {
  2467. var o = value.$mobx;
  2468. return o.values && !!o.values[property];
  2469. }
  2470. return false;
  2471. }
  2472. // For first check, see #701
  2473. return (isObservableObject(value) ||
  2474. !!value.$mobx ||
  2475. isAtom(value) ||
  2476. isReaction(value) ||
  2477. isComputedValue(value));
  2478. }
  2479. function isObservable(value) {
  2480. if (arguments.length !== 1)
  2481. fail(process.env.NODE_ENV !== "production" &&
  2482. "isObservable expects only 1 argument. Use isObservableProp to inspect the observability of a property");
  2483. return _isObservable(value);
  2484. }
  2485. function isObservableProp(value, propName) {
  2486. if (typeof propName !== "string")
  2487. return fail(process.env.NODE_ENV !== "production" && "expected a property name as second argument");
  2488. return _isObservable(value, propName);
  2489. }
  2490. function keys(obj) {
  2491. if (isObservableObject(obj)) {
  2492. return obj.$mobx.getKeys();
  2493. }
  2494. if (isObservableMap(obj)) {
  2495. return obj._keys.slice();
  2496. }
  2497. if (isObservableSet(obj)) {
  2498. return iteratorToArray(obj.keys());
  2499. }
  2500. if (isObservableArray(obj)) {
  2501. return obj.map(function (_, index) { return index; });
  2502. }
  2503. return fail(process.env.NODE_ENV !== "production" &&
  2504. "'keys()' can only be used on observable objects, arrays, sets and maps");
  2505. }
  2506. function values(obj) {
  2507. if (isObservableObject(obj)) {
  2508. return keys(obj).map(function (key) { return obj[key]; });
  2509. }
  2510. if (isObservableMap(obj)) {
  2511. return keys(obj).map(function (key) { return obj.get(key); });
  2512. }
  2513. if (isObservableSet(obj)) {
  2514. return iteratorToArray(obj.values());
  2515. }
  2516. if (isObservableArray(obj)) {
  2517. return obj.slice();
  2518. }
  2519. return fail(process.env.NODE_ENV !== "production" &&
  2520. "'values()' can only be used on observable objects, arrays, sets and maps");
  2521. }
  2522. function entries(obj) {
  2523. if (isObservableObject(obj)) {
  2524. return keys(obj).map(function (key) { return [key, obj[key]]; });
  2525. }
  2526. if (isObservableMap(obj)) {
  2527. return keys(obj).map(function (key) { return [key, obj.get(key)]; });
  2528. }
  2529. if (isObservableSet(obj)) {
  2530. return iteratorToArray(obj.entries());
  2531. }
  2532. if (isObservableArray(obj)) {
  2533. return obj.map(function (key, index) { return [index, key]; });
  2534. }
  2535. return fail(process.env.NODE_ENV !== "production" &&
  2536. "'entries()' can only be used on observable objects, arrays and maps");
  2537. }
  2538. function set(obj, key, value) {
  2539. if (arguments.length === 2 && !isObservableSet(obj)) {
  2540. startBatch();
  2541. var values_1 = key;
  2542. try {
  2543. for (var key_1 in values_1)
  2544. set(obj, key_1, values_1[key_1]);
  2545. }
  2546. finally {
  2547. endBatch();
  2548. }
  2549. return;
  2550. }
  2551. if (isObservableObject(obj)) {
  2552. var adm = obj.$mobx;
  2553. var existingObservable = adm.values[key];
  2554. if (existingObservable) {
  2555. adm.write(obj, key, value);
  2556. }
  2557. else {
  2558. defineObservableProperty(obj, key, value, adm.defaultEnhancer);
  2559. }
  2560. }
  2561. else if (isObservableMap(obj)) {
  2562. obj.set(key, value);
  2563. }
  2564. else if (isObservableSet(obj)) {
  2565. obj.add(key);
  2566. }
  2567. else if (isObservableArray(obj)) {
  2568. if (typeof key !== "number")
  2569. key = parseInt(key, 10);
  2570. invariant(key >= 0, "Not a valid index: '" + key + "'");
  2571. startBatch();
  2572. if (key >= obj.length)
  2573. obj.length = key + 1;
  2574. obj[key] = value;
  2575. endBatch();
  2576. }
  2577. else {
  2578. return fail(process.env.NODE_ENV !== "production" &&
  2579. "'set()' can only be used on observable objects, arrays and maps");
  2580. }
  2581. }
  2582. function remove(obj, key) {
  2583. if (isObservableObject(obj)) {
  2584. obj.$mobx.remove(key);
  2585. }
  2586. else if (isObservableMap(obj)) {
  2587. obj.delete(key);
  2588. }
  2589. else if (isObservableSet(obj)) {
  2590. obj.delete(key);
  2591. }
  2592. else if (isObservableArray(obj)) {
  2593. if (typeof key !== "number")
  2594. key = parseInt(key, 10);
  2595. invariant(key >= 0, "Not a valid index: '" + key + "'");
  2596. obj.splice(key, 1);
  2597. }
  2598. else {
  2599. return fail(process.env.NODE_ENV !== "production" &&
  2600. "'remove()' can only be used on observable objects, arrays and maps");
  2601. }
  2602. }
  2603. function has(obj, key) {
  2604. if (isObservableObject(obj)) {
  2605. // return keys(obj).indexOf(key) >= 0
  2606. var adm = getAdministration(obj);
  2607. adm.getKeys(); // make sure we get notified of key changes, but for performance, use the values map to look up existence
  2608. return !!adm.values[key];
  2609. }
  2610. else if (isObservableMap(obj)) {
  2611. return obj.has(key);
  2612. }
  2613. else if (isObservableSet(obj)) {
  2614. return obj.has(key);
  2615. }
  2616. else if (isObservableArray(obj)) {
  2617. return key >= 0 && key < obj.length;
  2618. }
  2619. else {
  2620. return fail(process.env.NODE_ENV !== "production" &&
  2621. "'has()' can only be used on observable objects, arrays and maps");
  2622. }
  2623. }
  2624. function get(obj, key) {
  2625. if (!has(obj, key))
  2626. return undefined;
  2627. if (isObservableObject(obj)) {
  2628. return obj[key];
  2629. }
  2630. else if (isObservableMap(obj)) {
  2631. return obj.get(key);
  2632. }
  2633. else if (isObservableArray(obj)) {
  2634. return obj[key];
  2635. }
  2636. else {
  2637. return fail(process.env.NODE_ENV !== "production" &&
  2638. "'get()' can only be used on observable objects, arrays and maps");
  2639. }
  2640. }
  2641. function observe(thing, propOrCb, cbOrFire, fireImmediately) {
  2642. if (typeof cbOrFire === "function")
  2643. return observeObservableProperty(thing, propOrCb, cbOrFire, fireImmediately);
  2644. else
  2645. return observeObservable(thing, propOrCb, cbOrFire);
  2646. }
  2647. function observeObservable(thing, listener, fireImmediately) {
  2648. return getAdministration(thing).observe(listener, fireImmediately);
  2649. }
  2650. function observeObservableProperty(thing, property, listener, fireImmediately) {
  2651. return getAdministration(thing, property).observe(listener, fireImmediately);
  2652. }
  2653. var defaultOptions = {
  2654. detectCycles: true,
  2655. exportMapsAsObjects: true,
  2656. recurseEverything: false
  2657. };
  2658. function cache(map, key, value, options) {
  2659. if (options.detectCycles)
  2660. map.set(key, value);
  2661. return value;
  2662. }
  2663. function toJSHelper(source, options, __alreadySeen) {
  2664. if (!options.recurseEverything && !isObservable(source))
  2665. return source;
  2666. if (typeof source !== "object")
  2667. return source;
  2668. // Directly return null if source is null
  2669. if (source === null)
  2670. return null;
  2671. // Directly return the Date object itself if contained in the observable
  2672. if (source instanceof Date)
  2673. return source;
  2674. if (isObservableValue(source))
  2675. return toJSHelper(source.get(), options, __alreadySeen);
  2676. // make sure we track the keys of the object
  2677. if (isObservable(source))
  2678. keys(source);
  2679. var detectCycles = options.detectCycles === true;
  2680. if (detectCycles && source !== null && __alreadySeen.has(source)) {
  2681. return __alreadySeen.get(source);
  2682. }
  2683. if (isObservableArray(source) || Array.isArray(source)) {
  2684. var res_1 = cache(__alreadySeen, source, [], options);
  2685. var toAdd = source.map(function (value) { return toJSHelper(value, options, __alreadySeen); });
  2686. res_1.length = toAdd.length;
  2687. for (var i = 0, l = toAdd.length; i < l; i++)
  2688. res_1[i] = toAdd[i];
  2689. return res_1;
  2690. }
  2691. if (isObservableSet(source) || Object.getPrototypeOf(source) === Set.prototype) {
  2692. if (options.exportMapsAsObjects === false) {
  2693. var res_2 = cache(__alreadySeen, source, new Set(), options);
  2694. source.forEach(function (value) {
  2695. res_2.add(toJSHelper(value, options, __alreadySeen));
  2696. });
  2697. return res_2;
  2698. }
  2699. else {
  2700. var res_3 = cache(__alreadySeen, source, [], options);
  2701. source.forEach(function (value) {
  2702. res_3.push(toJSHelper(value, options, __alreadySeen));
  2703. });
  2704. return res_3;
  2705. }
  2706. }
  2707. if (isObservableMap(source) || Object.getPrototypeOf(source) === Map.prototype) {
  2708. if (options.exportMapsAsObjects === false) {
  2709. var res_4 = cache(__alreadySeen, source, new Map(), options);
  2710. source.forEach(function (value, key) {
  2711. res_4.set(key, toJSHelper(value, options, __alreadySeen));
  2712. });
  2713. return res_4;
  2714. }
  2715. else {
  2716. var res_5 = cache(__alreadySeen, source, {}, options);
  2717. source.forEach(function (value, key) {
  2718. res_5[key] = toJSHelper(value, options, __alreadySeen);
  2719. });
  2720. return res_5;
  2721. }
  2722. }
  2723. // Fallback to the situation that source is an ObservableObject or a plain object
  2724. var res = cache(__alreadySeen, source, {}, options);
  2725. for (var key in source) {
  2726. res[key] = toJSHelper(source[key], options, __alreadySeen);
  2727. }
  2728. return res;
  2729. }
  2730. function toJS(source, options) {
  2731. // backward compatibility
  2732. if (typeof options === "boolean")
  2733. options = { detectCycles: options };
  2734. if (!options)
  2735. options = defaultOptions;
  2736. options.detectCycles =
  2737. options.detectCycles === undefined
  2738. ? options.recurseEverything === true
  2739. : options.detectCycles === true;
  2740. var __alreadySeen;
  2741. if (options.detectCycles)
  2742. __alreadySeen = new Map();
  2743. return toJSHelper(source, options, __alreadySeen);
  2744. }
  2745. function trace() {
  2746. var args = [];
  2747. for (var _i = 0; _i < arguments.length; _i++) {
  2748. args[_i] = arguments[_i];
  2749. }
  2750. var enterBreakPoint = false;
  2751. if (typeof args[args.length - 1] === "boolean")
  2752. enterBreakPoint = args.pop();
  2753. var derivation = getAtomFromArgs(args);
  2754. if (!derivation) {
  2755. return fail(process.env.NODE_ENV !== "production" &&
  2756. "'trace(break?)' can only be used inside a tracked computed value or a Reaction. Consider passing in the computed value or reaction explicitly");
  2757. }
  2758. if (derivation.isTracing === TraceMode.NONE) {
  2759. console.log("[mobx.trace] '" + derivation.name + "' tracing enabled");
  2760. }
  2761. derivation.isTracing = enterBreakPoint ? TraceMode.BREAK : TraceMode.LOG;
  2762. }
  2763. function getAtomFromArgs(args) {
  2764. switch (args.length) {
  2765. case 0:
  2766. return globalState.trackingDerivation;
  2767. case 1:
  2768. return getAtom(args[0]);
  2769. case 2:
  2770. return getAtom(args[0], args[1]);
  2771. }
  2772. }
  2773. /**
  2774. * During a transaction no views are updated until the end of the transaction.
  2775. * The transaction will be run synchronously nonetheless.
  2776. *
  2777. * @param action a function that updates some reactive state
  2778. * @returns any value that was returned by the 'action' parameter.
  2779. */
  2780. function transaction(action, thisArg) {
  2781. if (thisArg === void 0) { thisArg = undefined; }
  2782. startBatch();
  2783. try {
  2784. return action.apply(thisArg);
  2785. }
  2786. finally {
  2787. endBatch();
  2788. }
  2789. }
  2790. function when(predicate, arg1, arg2) {
  2791. if (arguments.length === 1 || (arg1 && typeof arg1 === "object"))
  2792. return whenPromise(predicate, arg1);
  2793. return _when(predicate, arg1, arg2 || {});
  2794. }
  2795. function _when(predicate, effect, opts) {
  2796. var timeoutHandle;
  2797. if (typeof opts.timeout === "number") {
  2798. timeoutHandle = setTimeout(function () {
  2799. if (!disposer.$mobx.isDisposed) {
  2800. disposer();
  2801. var error = new Error("WHEN_TIMEOUT");
  2802. if (opts.onError)
  2803. opts.onError(error);
  2804. else
  2805. throw error;
  2806. }
  2807. }, opts.timeout);
  2808. }
  2809. opts.name = opts.name || "When@" + getNextId();
  2810. var effectAction = createAction(opts.name + "-effect", effect);
  2811. var disposer = autorun(function (r) {
  2812. if (predicate()) {
  2813. r.dispose();
  2814. if (timeoutHandle)
  2815. clearTimeout(timeoutHandle);
  2816. effectAction();
  2817. }
  2818. }, opts);
  2819. return disposer;
  2820. }
  2821. function whenPromise(predicate, opts) {
  2822. if (process.env.NODE_ENV !== "production" && opts && opts.onError)
  2823. return fail("the options 'onError' and 'promise' cannot be combined");
  2824. var cancel;
  2825. var res = new Promise(function (resolve, reject) {
  2826. var disposer = _when(predicate, resolve, __assign(__assign({}, opts), { onError: reject }));
  2827. cancel = function () {
  2828. disposer();
  2829. reject("WHEN_CANCELLED");
  2830. };
  2831. });
  2832. res.cancel = cancel;
  2833. return res;
  2834. }
  2835. function hasInterceptors(interceptable) {
  2836. return interceptable.interceptors !== undefined && interceptable.interceptors.length > 0;
  2837. }
  2838. function registerInterceptor(interceptable, handler) {
  2839. var interceptors = interceptable.interceptors || (interceptable.interceptors = []);
  2840. interceptors.push(handler);
  2841. return once(function () {
  2842. var idx = interceptors.indexOf(handler);
  2843. if (idx !== -1)
  2844. interceptors.splice(idx, 1);
  2845. });
  2846. }
  2847. function interceptChange(interceptable, change) {
  2848. var prevU = untrackedStart();
  2849. try {
  2850. var interceptors = interceptable.interceptors;
  2851. if (interceptors)
  2852. for (var i = 0, l = interceptors.length; i < l; i++) {
  2853. change = interceptors[i](change);
  2854. invariant(!change || change.type, "Intercept handlers should return nothing or a change object");
  2855. if (!change)
  2856. break;
  2857. }
  2858. return change;
  2859. }
  2860. finally {
  2861. untrackedEnd(prevU);
  2862. }
  2863. }
  2864. function hasListeners(listenable) {
  2865. return listenable.changeListeners !== undefined && listenable.changeListeners.length > 0;
  2866. }
  2867. function registerListener(listenable, handler) {
  2868. var listeners = listenable.changeListeners || (listenable.changeListeners = []);
  2869. listeners.push(handler);
  2870. return once(function () {
  2871. var idx = listeners.indexOf(handler);
  2872. if (idx !== -1)
  2873. listeners.splice(idx, 1);
  2874. });
  2875. }
  2876. function notifyListeners(listenable, change) {
  2877. var prevU = untrackedStart();
  2878. var listeners = listenable.changeListeners;
  2879. if (!listeners)
  2880. return;
  2881. listeners = listeners.slice();
  2882. for (var i = 0, l = listeners.length; i < l; i++) {
  2883. listeners[i](change);
  2884. }
  2885. untrackedEnd(prevU);
  2886. }
  2887. var MAX_SPLICE_SIZE = 10000; // See e.g. https://github.com/mobxjs/mobx/issues/859
  2888. // Detects bug in safari 9.1.1 (or iOS 9 safari mobile). See #364
  2889. var safariPrototypeSetterInheritanceBug = (function () {
  2890. var v = false;
  2891. var p = {};
  2892. Object.defineProperty(p, "0", {
  2893. set: function () {
  2894. v = true;
  2895. }
  2896. });
  2897. Object.create(p)["0"] = 1;
  2898. return v === false;
  2899. })();
  2900. /**
  2901. * This array buffer contains two lists of properties, so that all arrays
  2902. * can recycle their property definitions, which significantly improves performance of creating
  2903. * properties on the fly.
  2904. */
  2905. var OBSERVABLE_ARRAY_BUFFER_SIZE = 0;
  2906. // Typescript workaround to make sure ObservableArray extends Array
  2907. var StubArray = /** @class */ (function () {
  2908. function StubArray() {
  2909. }
  2910. return StubArray;
  2911. }());
  2912. function inherit(ctor, proto) {
  2913. if (typeof Object["setPrototypeOf"] !== "undefined") {
  2914. Object["setPrototypeOf"](ctor.prototype, proto);
  2915. }
  2916. else if (typeof ctor.prototype.__proto__ !== "undefined") {
  2917. ctor.prototype.__proto__ = proto;
  2918. }
  2919. else {
  2920. ctor["prototype"] = proto;
  2921. }
  2922. }
  2923. inherit(StubArray, Array.prototype);
  2924. // Weex freeze Array.prototype
  2925. // Make them writeable and configurable in prototype chain
  2926. // https://github.com/alibaba/weex/pull/1529
  2927. if (Object.isFrozen(Array)) {
  2928. [
  2929. "constructor",
  2930. "push",
  2931. "shift",
  2932. "concat",
  2933. "pop",
  2934. "unshift",
  2935. "replace",
  2936. "find",
  2937. "findIndex",
  2938. "splice",
  2939. "reverse",
  2940. "sort"
  2941. ].forEach(function (key) {
  2942. Object.defineProperty(StubArray.prototype, key, {
  2943. configurable: true,
  2944. writable: true,
  2945. value: Array.prototype[key]
  2946. });
  2947. });
  2948. }
  2949. var ObservableArrayAdministration = /** @class */ (function () {
  2950. function ObservableArrayAdministration(name, enhancer, array, owned) {
  2951. this.array = array;
  2952. this.owned = owned;
  2953. this.values = [];
  2954. this.lastKnownLength = 0;
  2955. this.atom = new Atom(name || "ObservableArray@" + getNextId());
  2956. this.enhancer = function (newV, oldV) { return enhancer(newV, oldV, name + "[..]"); };
  2957. }
  2958. ObservableArrayAdministration.prototype.dehanceValue = function (value) {
  2959. if (this.dehancer !== undefined)
  2960. return this.dehancer(value);
  2961. return value;
  2962. };
  2963. ObservableArrayAdministration.prototype.dehanceValues = function (values) {
  2964. if (this.dehancer !== undefined && values.length > 0)
  2965. return values.map(this.dehancer);
  2966. return values;
  2967. };
  2968. ObservableArrayAdministration.prototype.intercept = function (handler) {
  2969. return registerInterceptor(this, handler);
  2970. };
  2971. ObservableArrayAdministration.prototype.observe = function (listener, fireImmediately) {
  2972. if (fireImmediately === void 0) { fireImmediately = false; }
  2973. if (fireImmediately) {
  2974. listener({
  2975. object: this.array,
  2976. type: "splice",
  2977. index: 0,
  2978. added: this.values.slice(),
  2979. addedCount: this.values.length,
  2980. removed: [],
  2981. removedCount: 0
  2982. });
  2983. }
  2984. return registerListener(this, listener);
  2985. };
  2986. ObservableArrayAdministration.prototype.getArrayLength = function () {
  2987. this.atom.reportObserved();
  2988. return this.values.length;
  2989. };
  2990. ObservableArrayAdministration.prototype.setArrayLength = function (newLength) {
  2991. if (typeof newLength !== "number" || newLength < 0)
  2992. throw new Error("[mobx.array] Out of range: " + newLength);
  2993. var currentLength = this.values.length;
  2994. if (newLength === currentLength)
  2995. return;
  2996. else if (newLength > currentLength) {
  2997. var newItems = new Array(newLength - currentLength);
  2998. for (var i = 0; i < newLength - currentLength; i++)
  2999. newItems[i] = undefined; // No Array.fill everywhere...
  3000. this.spliceWithArray(currentLength, 0, newItems);
  3001. }
  3002. else
  3003. this.spliceWithArray(newLength, currentLength - newLength);
  3004. };
  3005. // adds / removes the necessary numeric properties to this object
  3006. ObservableArrayAdministration.prototype.updateArrayLength = function (oldLength, delta) {
  3007. if (oldLength !== this.lastKnownLength)
  3008. throw new Error("[mobx] Modification exception: the internal structure of an observable array was changed. Did you use peek() to change it?");
  3009. this.lastKnownLength += delta;
  3010. if (delta > 0 && oldLength + delta + 1 > OBSERVABLE_ARRAY_BUFFER_SIZE)
  3011. reserveArrayBuffer(oldLength + delta + 1);
  3012. };
  3013. ObservableArrayAdministration.prototype.spliceWithArray = function (index, deleteCount, newItems) {
  3014. var _this = this;
  3015. checkIfStateModificationsAreAllowed(this.atom);
  3016. var length = this.values.length;
  3017. if (index === undefined)
  3018. index = 0;
  3019. else if (index > length)
  3020. index = length;
  3021. else if (index < 0)
  3022. index = Math.max(0, length + index);
  3023. if (arguments.length === 1)
  3024. deleteCount = length - index;
  3025. else if (deleteCount === undefined || deleteCount === null)
  3026. deleteCount = 0;
  3027. else
  3028. deleteCount = Math.max(0, Math.min(deleteCount, length - index));
  3029. if (newItems === undefined)
  3030. newItems = EMPTY_ARRAY;
  3031. if (hasInterceptors(this)) {
  3032. var change = interceptChange(this, {
  3033. object: this.array,
  3034. type: "splice",
  3035. index: index,
  3036. removedCount: deleteCount,
  3037. added: newItems
  3038. });
  3039. if (!change)
  3040. return EMPTY_ARRAY;
  3041. deleteCount = change.removedCount;
  3042. newItems = change.added;
  3043. }
  3044. newItems =
  3045. newItems.length === 0 ? newItems : newItems.map(function (v) { return _this.enhancer(v, undefined); });
  3046. var lengthDelta = newItems.length - deleteCount;
  3047. this.updateArrayLength(length, lengthDelta); // create or remove new entries
  3048. var res = this.spliceItemsIntoValues(index, deleteCount, newItems);
  3049. if (deleteCount !== 0 || newItems.length !== 0)
  3050. this.notifyArraySplice(index, newItems, res);
  3051. return this.dehanceValues(res);
  3052. };
  3053. ObservableArrayAdministration.prototype.spliceItemsIntoValues = function (index, deleteCount, newItems) {
  3054. var _a;
  3055. if (newItems.length < MAX_SPLICE_SIZE) {
  3056. return (_a = this.values).splice.apply(_a, __spread([index, deleteCount], newItems));
  3057. }
  3058. else {
  3059. var res = this.values.slice(index, index + deleteCount);
  3060. this.values = this.values
  3061. .slice(0, index)
  3062. .concat(newItems, this.values.slice(index + deleteCount));
  3063. return res;
  3064. }
  3065. };
  3066. ObservableArrayAdministration.prototype.notifyArrayChildUpdate = function (index, newValue, oldValue) {
  3067. var notifySpy = !this.owned && isSpyEnabled();
  3068. var notify = hasListeners(this);
  3069. var change = notify || notifySpy
  3070. ? {
  3071. object: this.array,
  3072. type: "update",
  3073. index: index,
  3074. newValue: newValue,
  3075. oldValue: oldValue
  3076. }
  3077. : null;
  3078. if (notifySpy)
  3079. spyReportStart(__assign(__assign({}, change), { name: this.atom.name }));
  3080. this.atom.reportChanged();
  3081. if (notify)
  3082. notifyListeners(this, change);
  3083. if (notifySpy)
  3084. spyReportEnd();
  3085. };
  3086. ObservableArrayAdministration.prototype.notifyArraySplice = function (index, added, removed) {
  3087. var notifySpy = !this.owned && isSpyEnabled();
  3088. var notify = hasListeners(this);
  3089. var change = notify || notifySpy
  3090. ? {
  3091. object: this.array,
  3092. type: "splice",
  3093. index: index,
  3094. removed: removed,
  3095. added: added,
  3096. removedCount: removed.length,
  3097. addedCount: added.length
  3098. }
  3099. : null;
  3100. if (notifySpy)
  3101. spyReportStart(__assign(__assign({}, change), { name: this.atom.name }));
  3102. this.atom.reportChanged();
  3103. // conform: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/observe
  3104. if (notify)
  3105. notifyListeners(this, change);
  3106. if (notifySpy)
  3107. spyReportEnd();
  3108. };
  3109. return ObservableArrayAdministration;
  3110. }());
  3111. var ObservableArray = /** @class */ (function (_super) {
  3112. __extends(ObservableArray, _super);
  3113. function ObservableArray(initialValues, enhancer, name, owned) {
  3114. if (name === void 0) { name = "ObservableArray@" + getNextId(); }
  3115. if (owned === void 0) { owned = false; }
  3116. var _this = _super.call(this) || this;
  3117. var adm = new ObservableArrayAdministration(name, enhancer, _this, owned);
  3118. addHiddenFinalProp(_this, "$mobx", adm);
  3119. if (initialValues && initialValues.length) {
  3120. var prev = allowStateChangesStart(true);
  3121. _this.spliceWithArray(0, 0, initialValues);
  3122. allowStateChangesEnd(prev);
  3123. }
  3124. if (safariPrototypeSetterInheritanceBug) {
  3125. // Seems that Safari won't use numeric prototype setter untill any * numeric property is
  3126. // defined on the instance. After that it works fine, even if this property is deleted.
  3127. Object.defineProperty(adm.array, "0", ENTRY_0);
  3128. }
  3129. return _this;
  3130. }
  3131. ObservableArray.prototype.intercept = function (handler) {
  3132. return this.$mobx.intercept(handler);
  3133. };
  3134. ObservableArray.prototype.observe = function (listener, fireImmediately) {
  3135. if (fireImmediately === void 0) { fireImmediately = false; }
  3136. return this.$mobx.observe(listener, fireImmediately);
  3137. };
  3138. ObservableArray.prototype.clear = function () {
  3139. return this.splice(0);
  3140. };
  3141. ObservableArray.prototype.concat = function () {
  3142. var arrays = [];
  3143. for (var _i = 0; _i < arguments.length; _i++) {
  3144. arrays[_i] = arguments[_i];
  3145. }
  3146. this.$mobx.atom.reportObserved();
  3147. return Array.prototype.concat.apply(this.peek(), arrays.map(function (a) { return (isObservableArray(a) ? a.peek() : a); }));
  3148. };
  3149. ObservableArray.prototype.replace = function (newItems) {
  3150. return this.$mobx.spliceWithArray(0, this.$mobx.values.length, newItems);
  3151. };
  3152. /**
  3153. * Converts this array back to a (shallow) javascript structure.
  3154. * For a deep clone use mobx.toJS
  3155. */
  3156. ObservableArray.prototype.toJS = function () {
  3157. return this.slice();
  3158. };
  3159. ObservableArray.prototype.toJSON = function () {
  3160. // Used by JSON.stringify
  3161. return this.toJS();
  3162. };
  3163. ObservableArray.prototype.peek = function () {
  3164. this.$mobx.atom.reportObserved();
  3165. return this.$mobx.dehanceValues(this.$mobx.values);
  3166. };
  3167. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
  3168. ObservableArray.prototype.find = function (predicate, thisArg, fromIndex) {
  3169. if (fromIndex === void 0) { fromIndex = 0; }
  3170. if (arguments.length === 3)
  3171. deprecated("The array.find fromIndex argument to find will not be supported anymore in the next major");
  3172. var idx = this.findIndex.apply(this, arguments);
  3173. return idx === -1 ? undefined : this.get(idx);
  3174. };
  3175. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
  3176. ObservableArray.prototype.findIndex = function (predicate, thisArg, fromIndex) {
  3177. if (fromIndex === void 0) { fromIndex = 0; }
  3178. if (arguments.length === 3)
  3179. deprecated("The array.findIndex fromIndex argument to find will not be supported anymore in the next major");
  3180. var items = this.peek(), l = items.length;
  3181. for (var i = fromIndex; i < l; i++)
  3182. if (predicate.call(thisArg, items[i], i, this))
  3183. return i;
  3184. return -1;
  3185. };
  3186. /*
  3187. * functions that do alter the internal structure of the array, (based on lib.es6.d.ts)
  3188. * since these functions alter the inner structure of the array, the have side effects.
  3189. * Because the have side effects, they should not be used in computed function,
  3190. * and for that reason the do not call dependencyState.notifyObserved
  3191. */
  3192. ObservableArray.prototype.splice = function (index, deleteCount) {
  3193. var newItems = [];
  3194. for (var _i = 2; _i < arguments.length; _i++) {
  3195. newItems[_i - 2] = arguments[_i];
  3196. }
  3197. switch (arguments.length) {
  3198. case 0:
  3199. return [];
  3200. case 1:
  3201. return this.$mobx.spliceWithArray(index);
  3202. case 2:
  3203. return this.$mobx.spliceWithArray(index, deleteCount);
  3204. }
  3205. return this.$mobx.spliceWithArray(index, deleteCount, newItems);
  3206. };
  3207. ObservableArray.prototype.spliceWithArray = function (index, deleteCount, newItems) {
  3208. return this.$mobx.spliceWithArray(index, deleteCount, newItems);
  3209. };
  3210. ObservableArray.prototype.push = function () {
  3211. var items = [];
  3212. for (var _i = 0; _i < arguments.length; _i++) {
  3213. items[_i] = arguments[_i];
  3214. }
  3215. var adm = this.$mobx;
  3216. adm.spliceWithArray(adm.values.length, 0, items);
  3217. return adm.values.length;
  3218. };
  3219. ObservableArray.prototype.pop = function () {
  3220. return this.splice(Math.max(this.$mobx.values.length - 1, 0), 1)[0];
  3221. };
  3222. ObservableArray.prototype.shift = function () {
  3223. return this.splice(0, 1)[0];
  3224. };
  3225. ObservableArray.prototype.unshift = function () {
  3226. var items = [];
  3227. for (var _i = 0; _i < arguments.length; _i++) {
  3228. items[_i] = arguments[_i];
  3229. }
  3230. var adm = this.$mobx;
  3231. adm.spliceWithArray(0, 0, items);
  3232. return adm.values.length;
  3233. };
  3234. ObservableArray.prototype.reverse = function () {
  3235. // reverse by default mutates in place before returning the result
  3236. // which makes it both a 'derivation' and a 'mutation'.
  3237. // so we deviate from the default and just make it an dervitation
  3238. var clone = this.slice();
  3239. return clone.reverse.apply(clone, arguments);
  3240. };
  3241. ObservableArray.prototype.sort = function (compareFn) {
  3242. // sort by default mutates in place before returning the result
  3243. // which goes against all good practices. Let's not change the array in place!
  3244. var clone = this.slice();
  3245. return clone.sort.apply(clone, arguments);
  3246. };
  3247. ObservableArray.prototype.remove = function (value) {
  3248. var idx = this.$mobx.dehanceValues(this.$mobx.values).indexOf(value);
  3249. if (idx > -1) {
  3250. this.splice(idx, 1);
  3251. return true;
  3252. }
  3253. return false;
  3254. };
  3255. ObservableArray.prototype.move = function (fromIndex, toIndex) {
  3256. deprecated("observableArray.move is deprecated, use .slice() & .replace() instead");
  3257. function checkIndex(index) {
  3258. if (index < 0) {
  3259. throw new Error("[mobx.array] Index out of bounds: " + index + " is negative");
  3260. }
  3261. var length = this.$mobx.values.length;
  3262. if (index >= length) {
  3263. throw new Error("[mobx.array] Index out of bounds: " + index + " is not smaller than " + length);
  3264. }
  3265. }
  3266. checkIndex.call(this, fromIndex);
  3267. checkIndex.call(this, toIndex);
  3268. if (fromIndex === toIndex) {
  3269. return;
  3270. }
  3271. var oldItems = this.$mobx.values;
  3272. var newItems;
  3273. if (fromIndex < toIndex) {
  3274. newItems = __spread(oldItems.slice(0, fromIndex), oldItems.slice(fromIndex + 1, toIndex + 1), [
  3275. oldItems[fromIndex]
  3276. ], oldItems.slice(toIndex + 1));
  3277. }
  3278. else {
  3279. // toIndex < fromIndex
  3280. newItems = __spread(oldItems.slice(0, toIndex), [
  3281. oldItems[fromIndex]
  3282. ], oldItems.slice(toIndex, fromIndex), oldItems.slice(fromIndex + 1));
  3283. }
  3284. this.replace(newItems);
  3285. };
  3286. // See #734, in case property accessors are unreliable...
  3287. ObservableArray.prototype.get = function (index) {
  3288. var impl = this.$mobx;
  3289. if (impl) {
  3290. if (index < impl.values.length) {
  3291. impl.atom.reportObserved();
  3292. return impl.dehanceValue(impl.values[index]);
  3293. }
  3294. console.warn("[mobx.array] Attempt to read an array index (" + index + ") that is out of bounds (" + impl.values.length + "). Please check length first. Out of bound indices will not be tracked by MobX");
  3295. }
  3296. return undefined;
  3297. };
  3298. // See #734, in case property accessors are unreliable...
  3299. ObservableArray.prototype.set = function (index, newValue) {
  3300. var adm = this.$mobx;
  3301. var values = adm.values;
  3302. if (index < values.length) {
  3303. // update at index in range
  3304. checkIfStateModificationsAreAllowed(adm.atom);
  3305. var oldValue = values[index];
  3306. if (hasInterceptors(adm)) {
  3307. var change = interceptChange(adm, {
  3308. type: "update",
  3309. object: this,
  3310. index: index,
  3311. newValue: newValue
  3312. });
  3313. if (!change)
  3314. return;
  3315. newValue = change.newValue;
  3316. }
  3317. newValue = adm.enhancer(newValue, oldValue);
  3318. var changed = newValue !== oldValue;
  3319. if (changed) {
  3320. values[index] = newValue;
  3321. adm.notifyArrayChildUpdate(index, newValue, oldValue);
  3322. }
  3323. }
  3324. else if (index === values.length) {
  3325. // add a new item
  3326. adm.spliceWithArray(index, 0, [newValue]);
  3327. }
  3328. else {
  3329. // out of bounds
  3330. throw new Error("[mobx.array] Index out of bounds, " + index + " is larger than " + values.length);
  3331. }
  3332. };
  3333. return ObservableArray;
  3334. }(StubArray));
  3335. declareIterator(ObservableArray.prototype, function () {
  3336. this.$mobx.atom.reportObserved();
  3337. var self = this;
  3338. var nextIndex = 0;
  3339. return makeIterable({
  3340. next: function () {
  3341. return nextIndex < self.length
  3342. ? { value: self[nextIndex++], done: false }
  3343. : { done: true, value: undefined };
  3344. }
  3345. });
  3346. });
  3347. Object.defineProperty(ObservableArray.prototype, "length", {
  3348. enumerable: false,
  3349. configurable: true,
  3350. get: function () {
  3351. return this.$mobx.getArrayLength();
  3352. },
  3353. set: function (newLength) {
  3354. this.$mobx.setArrayLength(newLength);
  3355. }
  3356. });
  3357. addHiddenProp(ObservableArray.prototype, toStringTagSymbol(), "Array");
  3358. [
  3359. "every",
  3360. "filter",
  3361. "forEach",
  3362. "indexOf",
  3363. "join",
  3364. "lastIndexOf",
  3365. "map",
  3366. "reduce",
  3367. "reduceRight",
  3368. "slice",
  3369. "some",
  3370. "toString",
  3371. "toLocaleString"
  3372. ].forEach(function (funcName) {
  3373. var baseFunc = Array.prototype[funcName];
  3374. invariant(typeof baseFunc === "function", "Base function not defined on Array prototype: '" + funcName + "'");
  3375. addHiddenProp(ObservableArray.prototype, funcName, function () {
  3376. return baseFunc.apply(this.peek(), arguments);
  3377. });
  3378. });
  3379. /**
  3380. * We don't want those to show up in `for (const key in ar)` ...
  3381. */
  3382. makeNonEnumerable(ObservableArray.prototype, [
  3383. "constructor",
  3384. "intercept",
  3385. "observe",
  3386. "clear",
  3387. "concat",
  3388. "get",
  3389. "replace",
  3390. "toJS",
  3391. "toJSON",
  3392. "peek",
  3393. "find",
  3394. "findIndex",
  3395. "splice",
  3396. "spliceWithArray",
  3397. "push",
  3398. "pop",
  3399. "set",
  3400. "shift",
  3401. "unshift",
  3402. "reverse",
  3403. "sort",
  3404. "remove",
  3405. "move",
  3406. "toString",
  3407. "toLocaleString"
  3408. ]);
  3409. // See #364
  3410. var ENTRY_0 = createArrayEntryDescriptor(0);
  3411. function createArrayEntryDescriptor(index) {
  3412. return {
  3413. enumerable: false,
  3414. configurable: false,
  3415. get: function () {
  3416. return this.get(index);
  3417. },
  3418. set: function (value) {
  3419. this.set(index, value);
  3420. }
  3421. };
  3422. }
  3423. function createArrayBufferItem(index) {
  3424. Object.defineProperty(ObservableArray.prototype, "" + index, createArrayEntryDescriptor(index));
  3425. }
  3426. function reserveArrayBuffer(max) {
  3427. for (var index = OBSERVABLE_ARRAY_BUFFER_SIZE; index < max; index++)
  3428. createArrayBufferItem(index);
  3429. OBSERVABLE_ARRAY_BUFFER_SIZE = max;
  3430. }
  3431. reserveArrayBuffer(1000);
  3432. var isObservableArrayAdministration = createInstanceofPredicate("ObservableArrayAdministration", ObservableArrayAdministration);
  3433. function isObservableArray(thing) {
  3434. return isObject(thing) && isObservableArrayAdministration(thing.$mobx);
  3435. }
  3436. var ObservableMapMarker = {};
  3437. var ObservableMap = /** @class */ (function () {
  3438. function ObservableMap(initialData, enhancer, name) {
  3439. if (enhancer === void 0) { enhancer = deepEnhancer; }
  3440. if (name === void 0) { name = "ObservableMap@" + getNextId(); }
  3441. this.enhancer = enhancer;
  3442. this.name = name;
  3443. this.$mobx = ObservableMapMarker;
  3444. this._keys = (new ObservableArray(undefined, referenceEnhancer, this.name + ".keys()", true));
  3445. if (typeof Map !== "function") {
  3446. throw new Error("mobx.map requires Map polyfill for the current browser. Check babel-polyfill or core-js/es6/map.js");
  3447. }
  3448. this._data = new Map();
  3449. this._hasMap = new Map();
  3450. this.merge(initialData);
  3451. }
  3452. ObservableMap.prototype._has = function (key) {
  3453. return this._data.has(key);
  3454. };
  3455. ObservableMap.prototype.has = function (key) {
  3456. var _this = this;
  3457. if (!globalState.trackingDerivation)
  3458. return this._has(key);
  3459. var entry = this._hasMap.get(key);
  3460. if (!entry) {
  3461. // todo: replace with atom (breaking change)
  3462. var newEntry = (entry = new ObservableValue(this._has(key), referenceEnhancer, this.name + "." + stringifyKey(key) + "?", false));
  3463. this._hasMap.set(key, newEntry);
  3464. onBecomeUnobserved(newEntry, function () { return _this._hasMap.delete(key); });
  3465. }
  3466. return entry.get();
  3467. };
  3468. ObservableMap.prototype.set = function (key, value) {
  3469. var hasKey = this._has(key);
  3470. if (hasInterceptors(this)) {
  3471. var change = interceptChange(this, {
  3472. type: hasKey ? "update" : "add",
  3473. object: this,
  3474. newValue: value,
  3475. name: key
  3476. });
  3477. if (!change)
  3478. return this;
  3479. value = change.newValue;
  3480. }
  3481. if (hasKey) {
  3482. this._updateValue(key, value);
  3483. }
  3484. else {
  3485. this._addValue(key, value);
  3486. }
  3487. return this;
  3488. };
  3489. ObservableMap.prototype.delete = function (key) {
  3490. var _this = this;
  3491. if (hasInterceptors(this)) {
  3492. var change = interceptChange(this, {
  3493. type: "delete",
  3494. object: this,
  3495. name: key
  3496. });
  3497. if (!change)
  3498. return false;
  3499. }
  3500. if (this._has(key)) {
  3501. var notifySpy = isSpyEnabled();
  3502. var notify = hasListeners(this);
  3503. var change = notify || notifySpy
  3504. ? {
  3505. type: "delete",
  3506. object: this,
  3507. oldValue: this._data.get(key).value,
  3508. name: key
  3509. }
  3510. : null;
  3511. if (notifySpy)
  3512. spyReportStart(__assign(__assign({}, change), { name: this.name, key: key }));
  3513. transaction(function () {
  3514. _this._keys.remove(key);
  3515. _this._updateHasMapEntry(key, false);
  3516. var observable = _this._data.get(key);
  3517. observable.setNewValue(undefined);
  3518. _this._data.delete(key);
  3519. });
  3520. if (notify)
  3521. notifyListeners(this, change);
  3522. if (notifySpy)
  3523. spyReportEnd();
  3524. return true;
  3525. }
  3526. return false;
  3527. };
  3528. ObservableMap.prototype._updateHasMapEntry = function (key, value) {
  3529. var entry = this._hasMap.get(key);
  3530. if (entry) {
  3531. entry.setNewValue(value);
  3532. }
  3533. };
  3534. ObservableMap.prototype._updateValue = function (key, newValue) {
  3535. var observable = this._data.get(key);
  3536. newValue = observable.prepareNewValue(newValue);
  3537. if (newValue !== globalState.UNCHANGED) {
  3538. var notifySpy = isSpyEnabled();
  3539. var notify = hasListeners(this);
  3540. var change = notify || notifySpy
  3541. ? {
  3542. type: "update",
  3543. object: this,
  3544. oldValue: observable.value,
  3545. name: key,
  3546. newValue: newValue
  3547. }
  3548. : null;
  3549. if (notifySpy)
  3550. spyReportStart(__assign(__assign({}, change), { name: this.name, key: key }));
  3551. observable.setNewValue(newValue);
  3552. if (notify)
  3553. notifyListeners(this, change);
  3554. if (notifySpy)
  3555. spyReportEnd();
  3556. }
  3557. };
  3558. ObservableMap.prototype._addValue = function (key, newValue) {
  3559. var _this = this;
  3560. transaction(function () {
  3561. var observable = new ObservableValue(newValue, _this.enhancer, _this.name + "." + stringifyKey(key), false);
  3562. _this._data.set(key, observable);
  3563. newValue = observable.value; // value might have been changed
  3564. _this._updateHasMapEntry(key, true);
  3565. _this._keys.push(key);
  3566. });
  3567. var notifySpy = isSpyEnabled();
  3568. var notify = hasListeners(this);
  3569. var change = notify || notifySpy
  3570. ? {
  3571. type: "add",
  3572. object: this,
  3573. name: key,
  3574. newValue: newValue
  3575. }
  3576. : null;
  3577. if (notifySpy)
  3578. spyReportStart(__assign(__assign({}, change), { name: this.name, key: key }));
  3579. if (notify)
  3580. notifyListeners(this, change);
  3581. if (notifySpy)
  3582. spyReportEnd();
  3583. };
  3584. ObservableMap.prototype.get = function (key) {
  3585. if (this.has(key))
  3586. return this.dehanceValue(this._data.get(key).get());
  3587. return this.dehanceValue(undefined);
  3588. };
  3589. ObservableMap.prototype.dehanceValue = function (value) {
  3590. if (this.dehancer !== undefined) {
  3591. return this.dehancer(value);
  3592. }
  3593. return value;
  3594. };
  3595. ObservableMap.prototype.keys = function () {
  3596. return this._keys[iteratorSymbol()]();
  3597. };
  3598. ObservableMap.prototype.values = function () {
  3599. var self = this;
  3600. var nextIndex = 0;
  3601. return makeIterable({
  3602. next: function () {
  3603. return nextIndex < self._keys.length
  3604. ? { value: self.get(self._keys[nextIndex++]), done: false }
  3605. : { value: undefined, done: true };
  3606. }
  3607. });
  3608. };
  3609. ObservableMap.prototype.entries = function () {
  3610. var self = this;
  3611. var nextIndex = 0;
  3612. return makeIterable({
  3613. next: function () {
  3614. if (nextIndex < self._keys.length) {
  3615. var key = self._keys[nextIndex++];
  3616. return {
  3617. value: [key, self.get(key)],
  3618. done: false
  3619. };
  3620. }
  3621. return { done: true };
  3622. }
  3623. });
  3624. };
  3625. ObservableMap.prototype.forEach = function (callback, thisArg) {
  3626. var _this = this;
  3627. this._keys.forEach(function (key) { return callback.call(thisArg, _this.get(key), key, _this); });
  3628. };
  3629. /** Merge another object into this object, returns this. */
  3630. ObservableMap.prototype.merge = function (other) {
  3631. var _this = this;
  3632. if (isObservableMap(other)) {
  3633. other = other.toJS();
  3634. }
  3635. transaction(function () {
  3636. if (isPlainObject(other))
  3637. Object.keys(other).forEach(function (key) { return _this.set(key, other[key]); });
  3638. else if (Array.isArray(other))
  3639. other.forEach(function (_a) {
  3640. var _b = __read(_a, 2), key = _b[0], value = _b[1];
  3641. return _this.set(key, value);
  3642. });
  3643. else if (isES6Map(other)) {
  3644. if (other.constructor !== Map)
  3645. fail("Cannot initialize from classes that inherit from Map: " + other.constructor.name); // prettier-ignore
  3646. else
  3647. other.forEach(function (value, key) { return _this.set(key, value); });
  3648. }
  3649. else if (other !== null && other !== undefined)
  3650. fail("Cannot initialize map from " + other);
  3651. });
  3652. return this;
  3653. };
  3654. ObservableMap.prototype.clear = function () {
  3655. var _this = this;
  3656. transaction(function () {
  3657. untracked(function () {
  3658. _this._keys.slice().forEach(function (key) { return _this.delete(key); });
  3659. });
  3660. });
  3661. };
  3662. ObservableMap.prototype.replace = function (values) {
  3663. var _this = this;
  3664. transaction(function () {
  3665. var replacementMap = convertToMap(values);
  3666. var oldKeys = _this._keys;
  3667. var newKeys = Array.from(replacementMap.keys());
  3668. var keysChanged = false;
  3669. for (var i = 0; i < oldKeys.length; i++) {
  3670. var oldKey = oldKeys[i];
  3671. // key order change
  3672. if (oldKeys.length === newKeys.length && oldKey !== newKeys[i]) {
  3673. keysChanged = true;
  3674. }
  3675. // deleted key
  3676. if (!replacementMap.has(oldKey)) {
  3677. keysChanged = true;
  3678. _this.delete(oldKey);
  3679. }
  3680. }
  3681. replacementMap.forEach(function (value, key) {
  3682. // new key
  3683. if (!_this._data.has(key)) {
  3684. keysChanged = true;
  3685. }
  3686. _this.set(key, value);
  3687. });
  3688. if (keysChanged) {
  3689. _this._keys.replace(newKeys);
  3690. }
  3691. });
  3692. return this;
  3693. };
  3694. Object.defineProperty(ObservableMap.prototype, "size", {
  3695. get: function () {
  3696. return this._keys.length;
  3697. },
  3698. enumerable: true,
  3699. configurable: true
  3700. });
  3701. /**
  3702. * Returns a plain object that represents this map.
  3703. * Note that all the keys being stringified.
  3704. * If there are duplicating keys after converting them to strings, behaviour is undetermined.
  3705. */
  3706. ObservableMap.prototype.toPOJO = function () {
  3707. var _this = this;
  3708. var res = {};
  3709. this._keys.forEach(function (key) { return (res[typeof key === "symbol" ? key : stringifyKey(key)] = _this.get(key)); });
  3710. return res;
  3711. };
  3712. /**
  3713. * Returns a shallow non observable object clone of this map.
  3714. * Note that the values migth still be observable. For a deep clone use mobx.toJS.
  3715. */
  3716. ObservableMap.prototype.toJS = function () {
  3717. var _this = this;
  3718. var res = new Map();
  3719. this._keys.forEach(function (key) { return res.set(key, _this.get(key)); });
  3720. return res;
  3721. };
  3722. ObservableMap.prototype.toJSON = function () {
  3723. // Used by JSON.stringify
  3724. return this.toPOJO();
  3725. };
  3726. ObservableMap.prototype.toString = function () {
  3727. var _this = this;
  3728. return (this.name +
  3729. "[{ " +
  3730. this._keys.map(function (key) { return stringifyKey(key) + ": " + ("" + _this.get(key)); }).join(", ") +
  3731. " }]");
  3732. };
  3733. /**
  3734. * Observes this object. Triggers for the events 'add', 'update' and 'delete'.
  3735. * See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/observe
  3736. * for callback details
  3737. */
  3738. ObservableMap.prototype.observe = function (listener, fireImmediately) {
  3739. process.env.NODE_ENV !== "production" &&
  3740. invariant(fireImmediately !== true, "`observe` doesn't support fireImmediately=true in combination with maps.");
  3741. return registerListener(this, listener);
  3742. };
  3743. ObservableMap.prototype.intercept = function (handler) {
  3744. return registerInterceptor(this, handler);
  3745. };
  3746. return ObservableMap;
  3747. }());
  3748. function stringifyKey(key) {
  3749. if (key && key.toString)
  3750. return key.toString();
  3751. else
  3752. return new String(key).toString();
  3753. }
  3754. declareIterator(ObservableMap.prototype, function () {
  3755. return this.entries();
  3756. });
  3757. addHiddenFinalProp(ObservableMap.prototype, toStringTagSymbol(), "Map");
  3758. /* 'var' fixes small-build issue */
  3759. var isObservableMap = createInstanceofPredicate("ObservableMap", ObservableMap);
  3760. var ObservableSetMarker = {};
  3761. var ObservableSet = /** @class */ (function () {
  3762. function ObservableSet(initialData, enhancer, name) {
  3763. if (enhancer === void 0) { enhancer = deepEnhancer; }
  3764. if (name === void 0) { name = "ObservableSet@" + getNextId(); }
  3765. this.name = name;
  3766. this.$mobx = ObservableSetMarker;
  3767. this._data = new Set();
  3768. this._atom = createAtom(this.name);
  3769. if (typeof Set !== "function") {
  3770. throw new Error("mobx.set requires Set polyfill for the current browser. Check babel-polyfill or core-js/es6/set.js");
  3771. }
  3772. this.enhancer = function (newV, oldV) { return enhancer(newV, oldV, name); };
  3773. if (initialData) {
  3774. this.replace(initialData);
  3775. }
  3776. }
  3777. ObservableSet.prototype.dehanceValue = function (value) {
  3778. if (this.dehancer !== undefined) {
  3779. return this.dehancer(value);
  3780. }
  3781. return value;
  3782. };
  3783. ObservableSet.prototype.clear = function () {
  3784. var _this = this;
  3785. transaction(function () {
  3786. untracked(function () {
  3787. _this._data.forEach(function (value) {
  3788. _this.delete(value);
  3789. });
  3790. });
  3791. });
  3792. };
  3793. ObservableSet.prototype.forEach = function (callbackFn, thisArg) {
  3794. var _this = this;
  3795. this._data.forEach(function (value) {
  3796. callbackFn.call(thisArg, value, value, _this);
  3797. });
  3798. };
  3799. Object.defineProperty(ObservableSet.prototype, "size", {
  3800. get: function () {
  3801. this._atom.reportObserved();
  3802. return this._data.size;
  3803. },
  3804. enumerable: true,
  3805. configurable: true
  3806. });
  3807. ObservableSet.prototype.add = function (value) {
  3808. var _this = this;
  3809. checkIfStateModificationsAreAllowed(this._atom);
  3810. if (hasInterceptors(this)) {
  3811. var change = interceptChange(this, {
  3812. type: "add",
  3813. object: this,
  3814. newValue: value
  3815. });
  3816. if (!change)
  3817. return this;
  3818. // TODO: ideally, value = change.value would be done here, so that values can be
  3819. // changed by interceptor. Same applies for other Set and Map api's.
  3820. }
  3821. if (!this.has(value)) {
  3822. transaction(function () {
  3823. _this._data.add(_this.enhancer(value, undefined));
  3824. _this._atom.reportChanged();
  3825. });
  3826. var notifySpy = isSpyEnabled();
  3827. var notify = hasListeners(this);
  3828. var change = notify || notifySpy
  3829. ? {
  3830. type: "add",
  3831. object: this,
  3832. newValue: value
  3833. }
  3834. : null;
  3835. if (notifySpy && process.env.NODE_ENV !== "production")
  3836. spyReportStart(change);
  3837. if (notify)
  3838. notifyListeners(this, change);
  3839. if (notifySpy && process.env.NODE_ENV !== "production")
  3840. spyReportEnd();
  3841. }
  3842. return this;
  3843. };
  3844. ObservableSet.prototype.delete = function (value) {
  3845. var _this = this;
  3846. if (hasInterceptors(this)) {
  3847. var change = interceptChange(this, {
  3848. type: "delete",
  3849. object: this,
  3850. oldValue: value
  3851. });
  3852. if (!change)
  3853. return false;
  3854. }
  3855. if (this.has(value)) {
  3856. var notifySpy = isSpyEnabled();
  3857. var notify = hasListeners(this);
  3858. var change = notify || notifySpy
  3859. ? {
  3860. type: "delete",
  3861. object: this,
  3862. oldValue: value
  3863. }
  3864. : null;
  3865. if (notifySpy && process.env.NODE_ENV !== "production")
  3866. spyReportStart(__assign(__assign({}, change), { name: this.name }));
  3867. transaction(function () {
  3868. _this._atom.reportChanged();
  3869. _this._data.delete(value);
  3870. });
  3871. if (notify)
  3872. notifyListeners(this, change);
  3873. if (notifySpy && process.env.NODE_ENV !== "production")
  3874. spyReportEnd();
  3875. return true;
  3876. }
  3877. return false;
  3878. };
  3879. ObservableSet.prototype.has = function (value) {
  3880. this._atom.reportObserved();
  3881. return this._data.has(this.dehanceValue(value));
  3882. };
  3883. ObservableSet.prototype.entries = function () {
  3884. var nextIndex = 0;
  3885. var keys = iteratorToArray(this.keys());
  3886. var values = iteratorToArray(this.values());
  3887. return makeIterable({
  3888. next: function () {
  3889. var index = nextIndex;
  3890. nextIndex += 1;
  3891. return index < values.length
  3892. ? { value: [keys[index], values[index]], done: false }
  3893. : { done: true };
  3894. }
  3895. });
  3896. };
  3897. ObservableSet.prototype.keys = function () {
  3898. return this.values();
  3899. };
  3900. ObservableSet.prototype.values = function () {
  3901. this._atom.reportObserved();
  3902. var self = this;
  3903. var nextIndex = 0;
  3904. var observableValues;
  3905. if (this._data.values !== undefined) {
  3906. observableValues = iteratorToArray(this._data.values());
  3907. }
  3908. else {
  3909. // There is no values function in IE11
  3910. observableValues = [];
  3911. this._data.forEach(function (e) { return observableValues.push(e); });
  3912. }
  3913. return makeIterable({
  3914. next: function () {
  3915. return nextIndex < observableValues.length
  3916. ? { value: self.dehanceValue(observableValues[nextIndex++]), done: false }
  3917. : { done: true };
  3918. }
  3919. });
  3920. };
  3921. ObservableSet.prototype.replace = function (other) {
  3922. var _this = this;
  3923. if (isObservableSet(other)) {
  3924. other = other.toJS();
  3925. }
  3926. transaction(function () {
  3927. if (Array.isArray(other)) {
  3928. _this.clear();
  3929. other.forEach(function (value) { return _this.add(value); });
  3930. }
  3931. else if (isES6Set(other)) {
  3932. _this.clear();
  3933. other.forEach(function (value) { return _this.add(value); });
  3934. }
  3935. else if (other !== null && other !== undefined) {
  3936. fail("Cannot initialize set from " + other);
  3937. }
  3938. });
  3939. return this;
  3940. };
  3941. ObservableSet.prototype.observe = function (listener, fireImmediately) {
  3942. // TODO 'fireImmediately' can be true?
  3943. process.env.NODE_ENV !== "production" &&
  3944. invariant(fireImmediately !== true, "`observe` doesn't support fireImmediately=true in combination with sets.");
  3945. return registerListener(this, listener);
  3946. };
  3947. ObservableSet.prototype.intercept = function (handler) {
  3948. return registerInterceptor(this, handler);
  3949. };
  3950. ObservableSet.prototype.toJS = function () {
  3951. return new Set(this);
  3952. };
  3953. ObservableSet.prototype.toString = function () {
  3954. return this.name + "[ " + iteratorToArray(this.keys()).join(", ") + " ]";
  3955. };
  3956. return ObservableSet;
  3957. }());
  3958. declareIterator(ObservableSet.prototype, function () {
  3959. return this.values();
  3960. });
  3961. addHiddenFinalProp(ObservableSet.prototype, toStringTagSymbol(), "Set");
  3962. var isObservableSet = createInstanceofPredicate("ObservableSet", ObservableSet);
  3963. var ObservableObjectAdministration = /** @class */ (function () {
  3964. function ObservableObjectAdministration(target, name, defaultEnhancer) {
  3965. this.target = target;
  3966. this.name = name;
  3967. this.defaultEnhancer = defaultEnhancer;
  3968. this.values = {};
  3969. }
  3970. ObservableObjectAdministration.prototype.read = function (owner, key) {
  3971. if (process.env.NODE_ENV === "production" && this.target !== owner) {
  3972. this.illegalAccess(owner, key);
  3973. if (!this.values[key])
  3974. return undefined;
  3975. }
  3976. return this.values[key].get();
  3977. };
  3978. ObservableObjectAdministration.prototype.write = function (owner, key, newValue) {
  3979. var instance = this.target;
  3980. if (process.env.NODE_ENV === "production" && instance !== owner) {
  3981. this.illegalAccess(owner, key);
  3982. }
  3983. var observable = this.values[key];
  3984. if (observable instanceof ComputedValue) {
  3985. observable.set(newValue);
  3986. return;
  3987. }
  3988. // intercept
  3989. if (hasInterceptors(this)) {
  3990. var change = interceptChange(this, {
  3991. type: "update",
  3992. object: instance,
  3993. name: key,
  3994. newValue: newValue
  3995. });
  3996. if (!change)
  3997. return;
  3998. newValue = change.newValue;
  3999. }
  4000. newValue = observable.prepareNewValue(newValue);
  4001. // notify spy & observers
  4002. if (newValue !== globalState.UNCHANGED) {
  4003. var notify = hasListeners(this);
  4004. var notifySpy = isSpyEnabled();
  4005. var change = notify || notifySpy
  4006. ? {
  4007. type: "update",
  4008. object: instance,
  4009. oldValue: observable.value,
  4010. name: key,
  4011. newValue: newValue
  4012. }
  4013. : null;
  4014. if (notifySpy)
  4015. spyReportStart(__assign(__assign({}, change), { name: this.name, key: key }));
  4016. observable.setNewValue(newValue);
  4017. if (notify)
  4018. notifyListeners(this, change);
  4019. if (notifySpy)
  4020. spyReportEnd();
  4021. }
  4022. };
  4023. ObservableObjectAdministration.prototype.remove = function (key) {
  4024. if (!this.values[key])
  4025. return;
  4026. var target = this.target;
  4027. if (hasInterceptors(this)) {
  4028. var change = interceptChange(this, {
  4029. object: target,
  4030. name: key,
  4031. type: "remove"
  4032. });
  4033. if (!change)
  4034. return;
  4035. }
  4036. try {
  4037. startBatch();
  4038. var notify = hasListeners(this);
  4039. var notifySpy = isSpyEnabled();
  4040. var oldValue = this.values[key].get();
  4041. if (this.keys)
  4042. this.keys.remove(key);
  4043. delete this.values[key];
  4044. delete this.target[key];
  4045. var change = notify || notifySpy
  4046. ? {
  4047. type: "remove",
  4048. object: target,
  4049. oldValue: oldValue,
  4050. name: key
  4051. }
  4052. : null;
  4053. if (notifySpy)
  4054. spyReportStart(__assign(__assign({}, change), { name: this.name, key: key }));
  4055. if (notify)
  4056. notifyListeners(this, change);
  4057. if (notifySpy)
  4058. spyReportEnd();
  4059. }
  4060. finally {
  4061. endBatch();
  4062. }
  4063. };
  4064. ObservableObjectAdministration.prototype.illegalAccess = function (owner, propName) {
  4065. /**
  4066. * This happens if a property is accessed through the prototype chain, but the property was
  4067. * declared directly as own property on the prototype.
  4068. *
  4069. * E.g.:
  4070. * class A {
  4071. * }
  4072. * extendObservable(A.prototype, { x: 1 })
  4073. *
  4074. * classB extens A {
  4075. * }
  4076. * console.log(new B().x)
  4077. *
  4078. * It is unclear whether the property should be considered 'static' or inherited.
  4079. * Either use `console.log(A.x)`
  4080. * or: decorate(A, { x: observable })
  4081. *
  4082. * When using decorate, the property will always be redeclared as own property on the actual instance
  4083. */
  4084. console.warn("Property '" + propName + "' of '" + owner + "' was accessed through the prototype chain. Use 'decorate' instead to declare the prop or access it statically through it's owner");
  4085. };
  4086. /**
  4087. * Observes this object. Triggers for the events 'add', 'update' and 'delete'.
  4088. * See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/observe
  4089. * for callback details
  4090. */
  4091. ObservableObjectAdministration.prototype.observe = function (callback, fireImmediately) {
  4092. process.env.NODE_ENV !== "production" &&
  4093. invariant(fireImmediately !== true, "`observe` doesn't support the fire immediately property for observable objects.");
  4094. return registerListener(this, callback);
  4095. };
  4096. ObservableObjectAdministration.prototype.intercept = function (handler) {
  4097. return registerInterceptor(this, handler);
  4098. };
  4099. ObservableObjectAdministration.prototype.getKeys = function () {
  4100. var _this = this;
  4101. if (this.keys === undefined) {
  4102. this.keys = (new ObservableArray(Object.keys(this.values).filter(function (key) { return _this.values[key] instanceof ObservableValue; }), referenceEnhancer, "keys(" + this.name + ")", true));
  4103. }
  4104. return this.keys.slice();
  4105. };
  4106. return ObservableObjectAdministration;
  4107. }());
  4108. function asObservableObject(target, name, defaultEnhancer) {
  4109. if (name === void 0) { name = ""; }
  4110. if (defaultEnhancer === void 0) { defaultEnhancer = deepEnhancer; }
  4111. var adm = target.$mobx;
  4112. if (adm)
  4113. return adm;
  4114. process.env.NODE_ENV !== "production" &&
  4115. invariant(Object.isExtensible(target), "Cannot make the designated object observable; it is not extensible");
  4116. if (!isPlainObject(target))
  4117. name = (target.constructor.name || "ObservableObject") + "@" + getNextId();
  4118. if (!name)
  4119. name = "ObservableObject@" + getNextId();
  4120. adm = new ObservableObjectAdministration(target, name, defaultEnhancer);
  4121. addHiddenFinalProp(target, "$mobx", adm);
  4122. return adm;
  4123. }
  4124. function defineObservableProperty(target, propName, newValue, enhancer) {
  4125. var adm = asObservableObject(target);
  4126. assertPropertyConfigurable(target, propName);
  4127. if (hasInterceptors(adm)) {
  4128. var change = interceptChange(adm, {
  4129. object: target,
  4130. name: propName,
  4131. type: "add",
  4132. newValue: newValue
  4133. });
  4134. if (!change)
  4135. return;
  4136. newValue = change.newValue;
  4137. }
  4138. var observable = (adm.values[propName] = new ObservableValue(newValue, enhancer, adm.name + "." + propName, false));
  4139. newValue = observable.value; // observableValue might have changed it
  4140. Object.defineProperty(target, propName, generateObservablePropConfig(propName));
  4141. if (adm.keys)
  4142. adm.keys.push(propName);
  4143. notifyPropertyAddition(adm, target, propName, newValue);
  4144. }
  4145. function defineComputedProperty(target, // which objects holds the observable and provides `this` context?
  4146. propName, options) {
  4147. var adm = asObservableObject(target);
  4148. options.name = adm.name + "." + propName;
  4149. options.context = target;
  4150. adm.values[propName] = new ComputedValue(options);
  4151. Object.defineProperty(target, propName, generateComputedPropConfig(propName));
  4152. }
  4153. var observablePropertyConfigs = Object.create(null);
  4154. var computedPropertyConfigs = Object.create(null);
  4155. function generateObservablePropConfig(propName) {
  4156. return (observablePropertyConfigs[propName] ||
  4157. (observablePropertyConfigs[propName] = {
  4158. configurable: true,
  4159. enumerable: true,
  4160. get: function () {
  4161. return this.$mobx.read(this, propName);
  4162. },
  4163. set: function (v) {
  4164. this.$mobx.write(this, propName, v);
  4165. }
  4166. }));
  4167. }
  4168. function getAdministrationForComputedPropOwner(owner) {
  4169. var adm = owner.$mobx;
  4170. if (!adm) {
  4171. // because computed props are declared on proty,
  4172. // the current instance might not have been initialized yet
  4173. initializeInstance(owner);
  4174. return owner.$mobx;
  4175. }
  4176. return adm;
  4177. }
  4178. function generateComputedPropConfig(propName) {
  4179. return (computedPropertyConfigs[propName] ||
  4180. (computedPropertyConfigs[propName] = {
  4181. configurable: globalState.computedConfigurable,
  4182. enumerable: false,
  4183. get: function () {
  4184. return getAdministrationForComputedPropOwner(this).read(this, propName);
  4185. },
  4186. set: function (v) {
  4187. getAdministrationForComputedPropOwner(this).write(this, propName, v);
  4188. }
  4189. }));
  4190. }
  4191. function notifyPropertyAddition(adm, object, key, newValue) {
  4192. var notify = hasListeners(adm);
  4193. var notifySpy = isSpyEnabled();
  4194. var change = notify || notifySpy
  4195. ? {
  4196. type: "add",
  4197. object: object,
  4198. name: key,
  4199. newValue: newValue
  4200. }
  4201. : null;
  4202. if (notifySpy)
  4203. spyReportStart(__assign(__assign({}, change), { name: adm.name, key: key }));
  4204. if (notify)
  4205. notifyListeners(adm, change);
  4206. if (notifySpy)
  4207. spyReportEnd();
  4208. }
  4209. var isObservableObjectAdministration = createInstanceofPredicate("ObservableObjectAdministration", ObservableObjectAdministration);
  4210. function isObservableObject(thing) {
  4211. if (isObject(thing)) {
  4212. // Initializers run lazily when transpiling to babel, so make sure they are run...
  4213. initializeInstance(thing);
  4214. return isObservableObjectAdministration(thing.$mobx);
  4215. }
  4216. return false;
  4217. }
  4218. function getAtom(thing, property) {
  4219. if (typeof thing === "object" && thing !== null) {
  4220. if (isObservableArray(thing)) {
  4221. if (property !== undefined)
  4222. fail(process.env.NODE_ENV !== "production" &&
  4223. "It is not possible to get index atoms from arrays");
  4224. return thing.$mobx.atom;
  4225. }
  4226. if (isObservableSet(thing)) {
  4227. return thing.$mobx;
  4228. }
  4229. if (isObservableMap(thing)) {
  4230. var anyThing = thing;
  4231. if (property === undefined)
  4232. return getAtom(anyThing._keys);
  4233. var observable = anyThing._data.get(property) || anyThing._hasMap.get(property);
  4234. if (!observable)
  4235. fail(process.env.NODE_ENV !== "production" &&
  4236. "the entry '" + property + "' does not exist in the observable map '" + getDebugName(thing) + "'");
  4237. return observable;
  4238. }
  4239. // Initializers run lazily when transpiling to babel, so make sure they are run...
  4240. initializeInstance(thing);
  4241. if (property && !thing.$mobx)
  4242. thing[property]; // See #1072
  4243. if (isObservableObject(thing)) {
  4244. if (!property)
  4245. return fail(process.env.NODE_ENV !== "production" && "please specify a property");
  4246. var observable = thing.$mobx.values[property];
  4247. if (!observable)
  4248. fail(process.env.NODE_ENV !== "production" &&
  4249. "no observable property '" + property + "' found on the observable object '" + getDebugName(thing) + "'");
  4250. return observable;
  4251. }
  4252. if (isAtom(thing) || isComputedValue(thing) || isReaction(thing)) {
  4253. return thing;
  4254. }
  4255. }
  4256. else if (typeof thing === "function") {
  4257. if (isReaction(thing.$mobx)) {
  4258. // disposer function
  4259. return thing.$mobx;
  4260. }
  4261. }
  4262. return fail(process.env.NODE_ENV !== "production" && "Cannot obtain atom from " + thing);
  4263. }
  4264. function getAdministration(thing, property) {
  4265. if (!thing)
  4266. fail("Expecting some object");
  4267. if (property !== undefined)
  4268. return getAdministration(getAtom(thing, property));
  4269. if (isAtom(thing) || isComputedValue(thing) || isReaction(thing))
  4270. return thing;
  4271. if (isObservableMap(thing) || isObservableSet(thing))
  4272. return thing;
  4273. // Initializers run lazily when transpiling to babel, so make sure they are run...
  4274. initializeInstance(thing);
  4275. if (thing.$mobx)
  4276. return thing.$mobx;
  4277. fail(process.env.NODE_ENV !== "production" && "Cannot obtain administration from " + thing);
  4278. }
  4279. function getDebugName(thing, property) {
  4280. var named;
  4281. if (property !== undefined)
  4282. named = getAtom(thing, property);
  4283. else if (isObservableObject(thing) || isObservableMap(thing) || isObservableSet(thing))
  4284. named = getAdministration(thing);
  4285. else
  4286. named = getAtom(thing); // valid for arrays as well
  4287. return named.name;
  4288. }
  4289. var toString = Object.prototype.toString;
  4290. function deepEqual(a, b, depth) {
  4291. if (depth === void 0) { depth = -1; }
  4292. return eq(a, b, depth);
  4293. }
  4294. // Copied from https://github.com/jashkenas/underscore/blob/5c237a7c682fb68fd5378203f0bf22dce1624854/underscore.js#L1186-L1289
  4295. // Internal recursive comparison function for `isEqual`.
  4296. function eq(a, b, depth, aStack, bStack) {
  4297. // Identical objects are equal. `0 === -0`, but they aren't identical.
  4298. // See the [Harmony `egal` proposal](http://wiki.ecmascript.org/doku.php?id=harmony:egal).
  4299. if (a === b)
  4300. return a !== 0 || 1 / a === 1 / b;
  4301. // `null` or `undefined` only equal to itself (strict comparison).
  4302. if (a == null || b == null)
  4303. return false;
  4304. // `NaN`s are equivalent, but non-reflexive.
  4305. if (a !== a)
  4306. return b !== b;
  4307. // Exhaust primitive checks
  4308. var type = typeof a;
  4309. if (type !== "function" && type !== "object" && typeof b != "object")
  4310. return false;
  4311. // Unwrap any wrapped objects.
  4312. a = unwrap(a);
  4313. b = unwrap(b);
  4314. // Compare `[[Class]]` names.
  4315. var className = toString.call(a);
  4316. if (className !== toString.call(b))
  4317. return false;
  4318. switch (className) {
  4319. // Strings, numbers, regular expressions, dates, and booleans are compared by value.
  4320. case "[object RegExp]":
  4321. // RegExps are coerced to strings for comparison (Note: '' + /a/i === '/a/i')
  4322. case "[object String]":
  4323. // Primitives and their corresponding object wrappers are equivalent; thus, `"5"` is
  4324. // equivalent to `new String("5")`.
  4325. return "" + a === "" + b;
  4326. case "[object Number]":
  4327. // `NaN`s are equivalent, but non-reflexive.
  4328. // Object(NaN) is equivalent to NaN.
  4329. if (+a !== +a)
  4330. return +b !== +b;
  4331. // An `egal` comparison is performed for other numeric values.
  4332. return +a === 0 ? 1 / +a === 1 / b : +a === +b;
  4333. case "[object Date]":
  4334. case "[object Boolean]":
  4335. // Coerce dates and booleans to numeric primitive values. Dates are compared by their
  4336. // millisecond representations. Note that invalid dates with millisecond representations
  4337. // of `NaN` are not equivalent.
  4338. return +a === +b;
  4339. case "[object Symbol]":
  4340. return (
  4341. // eslint-disable-next-line
  4342. typeof Symbol !== "undefined" && Symbol.valueOf.call(a) === Symbol.valueOf.call(b));
  4343. }
  4344. var areArrays = className === "[object Array]";
  4345. if (!areArrays) {
  4346. if (typeof a != "object" || typeof b != "object")
  4347. return false;
  4348. // Objects with different constructors are not equivalent, but `Object`s or `Array`s
  4349. // from different frames are.
  4350. var aCtor = a.constructor, bCtor = b.constructor;
  4351. if (aCtor !== bCtor &&
  4352. !(typeof aCtor === "function" &&
  4353. aCtor instanceof aCtor &&
  4354. typeof bCtor === "function" &&
  4355. bCtor instanceof bCtor) &&
  4356. ("constructor" in a && "constructor" in b)) {
  4357. return false;
  4358. }
  4359. }
  4360. if (depth === 0) {
  4361. return false;
  4362. }
  4363. else if (depth < 0) {
  4364. depth = -1;
  4365. }
  4366. // Assume equality for cyclic structures. The algorithm for detecting cyclic
  4367. // structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`.
  4368. // Initializing stack of traversed objects.
  4369. // It's done here since we only need them for objects and arrays comparison.
  4370. aStack = aStack || [];
  4371. bStack = bStack || [];
  4372. var length = aStack.length;
  4373. while (length--) {
  4374. // Linear search. Performance is inversely proportional to the number of
  4375. // unique nested structures.
  4376. if (aStack[length] === a)
  4377. return bStack[length] === b;
  4378. }
  4379. // Add the first object to the stack of traversed objects.
  4380. aStack.push(a);
  4381. bStack.push(b);
  4382. // Recursively compare objects and arrays.
  4383. if (areArrays) {
  4384. // Compare array lengths to determine if a deep comparison is necessary.
  4385. length = a.length;
  4386. if (length !== b.length)
  4387. return false;
  4388. // Deep compare the contents, ignoring non-numeric properties.
  4389. while (length--) {
  4390. if (!eq(a[length], b[length], depth - 1, aStack, bStack))
  4391. return false;
  4392. }
  4393. }
  4394. else {
  4395. // Deep compare objects.
  4396. var keys = Object.keys(a);
  4397. var key = void 0;
  4398. length = keys.length;
  4399. // Ensure that both objects contain the same number of properties before comparing deep equality.
  4400. if (Object.keys(b).length !== length)
  4401. return false;
  4402. while (length--) {
  4403. // Deep compare each member
  4404. key = keys[length];
  4405. if (!(has$1(b, key) && eq(a[key], b[key], depth - 1, aStack, bStack)))
  4406. return false;
  4407. }
  4408. }
  4409. // Remove the first object from the stack of traversed objects.
  4410. aStack.pop();
  4411. bStack.pop();
  4412. return true;
  4413. }
  4414. function unwrap(a) {
  4415. if (isObservableArray(a))
  4416. return a.peek();
  4417. if (isES6Map(a) || isObservableMap(a))
  4418. return iteratorToArray(a.entries());
  4419. if (isES6Set(a) || isObservableSet(a))
  4420. return iteratorToArray(a.entries());
  4421. return a;
  4422. }
  4423. function has$1(a, key) {
  4424. return Object.prototype.hasOwnProperty.call(a, key);
  4425. }
  4426. /*
  4427. The only reason for this file to exist is pure horror:
  4428. Without it rollup can make the bundling fail at any point in time; when it rolls up the files in the wrong order
  4429. it will cause undefined errors (for example because super classes or local variables not being hosted).
  4430. With this file that will still happen,
  4431. but at least in this file we can magically reorder the imports with trial and error until the build succeeds again.
  4432. */
  4433. /**
  4434. * (c) Michel Weststrate 2015 - 2019
  4435. * MIT Licensed
  4436. *
  4437. * Welcome to the mobx sources! To get an global overview of how MobX internally works,
  4438. * this is a good place to start:
  4439. * https://medium.com/@mweststrate/becoming-fully-reactive-an-in-depth-explanation-of-mobservable-55995262a254#.xvbh6qd74
  4440. *
  4441. * Source folders:
  4442. * ===============
  4443. *
  4444. * - api/ Most of the public static methods exposed by the module can be found here.
  4445. * - core/ Implementation of the MobX algorithm; atoms, derivations, reactions, dependency trees, optimizations. Cool stuff can be found here.
  4446. * - types/ All the magic that is need to have observable objects, arrays and values is in this folder. Including the modifiers like `asFlat`.
  4447. * - utils/ Utility stuff.
  4448. *
  4449. */
  4450. try {
  4451. // define process.env if needed
  4452. // if this is not a production build in the first place
  4453. // (in which case the expression below would be substituted with 'production')
  4454. // tslint:disable-next-line
  4455. process.env.NODE_ENV;
  4456. }
  4457. catch (e) {
  4458. var g = getGlobal();
  4459. if (typeof process === "undefined")
  4460. g.process = {};
  4461. g.process.env = {};
  4462. }
  4463. (function () {
  4464. function testCodeMinification() { }
  4465. if (testCodeMinification.name !== "testCodeMinification" &&
  4466. process.env.NODE_ENV !== "production" &&
  4467. process.env.IGNORE_MOBX_MINIFY_WARNING !== "true") {
  4468. // trick so it doesn't get replaced
  4469. var varName = ["process", "env", "NODE_ENV"].join(".");
  4470. console.warn("[mobx] you are running a minified build, but '" + varName + "' was not set to 'production' in your bundler. This results in an unnecessarily large and slow bundle");
  4471. }
  4472. })();
  4473. // forward compatibility with mobx, so that packages can easily support mobx 4 & 5
  4474. var $mobx = "$mobx";
  4475. if (typeof __MOBX_DEVTOOLS_GLOBAL_HOOK__ === "object") {
  4476. // See: https://github.com/andykog/mobx-devtools/
  4477. __MOBX_DEVTOOLS_GLOBAL_HOOK__.injectMobx({
  4478. spy: spy,
  4479. extras: {
  4480. getDebugName: getDebugName
  4481. },
  4482. $mobx: $mobx
  4483. });
  4484. }
  4485. // TODO: remove in some future build
  4486. if (process.env.NODE_ENV !== "production" &&
  4487. typeof module !== "undefined" &&
  4488. typeof module.exports !== "undefined") {
  4489. var warnedAboutDefaultExport_1 = false;
  4490. Object.defineProperty(module.exports, "default", {
  4491. enumerable: false,
  4492. get: function () {
  4493. if (!warnedAboutDefaultExport_1) {
  4494. warnedAboutDefaultExport_1 = true;
  4495. console.warn("The MobX package does not have a default export. Use 'import { thing } from \"mobx\"' (recommended) or 'import * as mobx from \"mobx\"' instead.\"");
  4496. }
  4497. return undefined;
  4498. }
  4499. });
  4500. [
  4501. "extras",
  4502. "Atom",
  4503. "BaseAtom",
  4504. "asFlat",
  4505. "asMap",
  4506. "asReference",
  4507. "asStructure",
  4508. "autorunAsync",
  4509. "createTranformer",
  4510. "expr",
  4511. "isModifierDescriptor",
  4512. "isStrictModeEnabled",
  4513. "map",
  4514. "useStrict",
  4515. "whyRun"
  4516. ].forEach(function (prop) {
  4517. Object.defineProperty(module.exports, prop, {
  4518. enumerable: false,
  4519. get: function () {
  4520. fail("'" + prop + "' is no longer part of the public MobX api. Please consult the changelog to find out where this functionality went");
  4521. },
  4522. set: function () { }
  4523. });
  4524. });
  4525. }
  4526. exports.$mobx = $mobx;
  4527. exports.FlowCancellationError = FlowCancellationError;
  4528. exports.ObservableMap = ObservableMap;
  4529. exports.ObservableSet = ObservableSet;
  4530. exports.Reaction = Reaction;
  4531. exports._allowStateChanges = allowStateChanges;
  4532. exports._allowStateChangesInsideComputed = allowStateChangesInsideComputed;
  4533. exports._endAction = _endAction;
  4534. exports._getAdministration = getAdministration;
  4535. exports._getGlobalState = getGlobalState;
  4536. exports._interceptReads = interceptReads;
  4537. exports._isComputingDerivation = isComputingDerivation;
  4538. exports._resetGlobalState = resetGlobalState;
  4539. exports._startAction = _startAction;
  4540. exports.action = action;
  4541. exports.autorun = autorun;
  4542. exports.comparer = comparer;
  4543. exports.computed = computed;
  4544. exports.configure = configure;
  4545. exports.createAtom = createAtom;
  4546. exports.decorate = decorate;
  4547. exports.entries = entries;
  4548. exports.extendObservable = extendObservable;
  4549. exports.extendShallowObservable = extendShallowObservable;
  4550. exports.flow = flow;
  4551. exports.get = get;
  4552. exports.getAtom = getAtom;
  4553. exports.getDebugName = getDebugName;
  4554. exports.getDependencyTree = getDependencyTree;
  4555. exports.getObserverTree = getObserverTree;
  4556. exports.has = has;
  4557. exports.intercept = intercept;
  4558. exports.isAction = isAction;
  4559. exports.isArrayLike = isArrayLike;
  4560. exports.isBoxedObservable = isObservableValue;
  4561. exports.isComputed = isComputed;
  4562. exports.isComputedProp = isComputedProp;
  4563. exports.isFlowCancellationError = isFlowCancellationError;
  4564. exports.isObservable = isObservable;
  4565. exports.isObservableArray = isObservableArray;
  4566. exports.isObservableMap = isObservableMap;
  4567. exports.isObservableObject = isObservableObject;
  4568. exports.isObservableProp = isObservableProp;
  4569. exports.isObservableSet = isObservableSet;
  4570. exports.keys = keys;
  4571. exports.observable = observable;
  4572. exports.observe = observe;
  4573. exports.onBecomeObserved = onBecomeObserved;
  4574. exports.onBecomeUnobserved = onBecomeUnobserved;
  4575. exports.onReactionError = onReactionError;
  4576. exports.reaction = reaction;
  4577. exports.remove = remove;
  4578. exports.runInAction = runInAction;
  4579. exports.set = set;
  4580. exports.spy = spy;
  4581. exports.toJS = toJS;
  4582. exports.trace = trace;
  4583. exports.transaction = transaction;
  4584. exports.untracked = untracked;
  4585. exports.values = values;
  4586. exports.when = when;
  4587. Object.defineProperty(exports, '__esModule', { value: true });
  4588. }));