mobx-state-tree.umd.js 254 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('mobx')) :
  3. typeof define === 'function' && define.amd ? define(['exports', 'mobx'], factory) :
  4. (global = global || self, factory(global.mobxStateTree = {}, global.mobx));
  5. }(this, function (exports, mobx) { 'use strict';
  6. var livelinessChecking = "warn";
  7. /**
  8. * Defines what MST should do when running into reads / writes to objects that have died.
  9. * By default it will print a warning.
  10. * Use the `"error"` option to easy debugging to see where the error was thrown and when the offending read / write took place
  11. *
  12. * @param mode `"warn"`, `"error"` or `"ignore"`
  13. */
  14. function setLivelinessChecking(mode) {
  15. livelinessChecking = mode;
  16. }
  17. /**
  18. * Returns the current liveliness checking mode.
  19. *
  20. * @returns `"warn"`, `"error"` or `"ignore"`
  21. */
  22. function getLivelinessChecking() {
  23. return livelinessChecking;
  24. }
  25. /**
  26. * @deprecated use setLivelinessChecking instead
  27. * @hidden
  28. *
  29. * Defines what MST should do when running into reads / writes to objects that have died.
  30. * By default it will print a warning.
  31. * Use the `"error"` option to easy debugging to see where the error was thrown and when the offending read / write took place
  32. *
  33. * @param mode `"warn"`, `"error"` or `"ignore"`
  34. */
  35. function setLivelynessChecking(mode) {
  36. setLivelinessChecking(mode);
  37. }
  38. /**
  39. * @hidden
  40. */
  41. var Hook;
  42. (function (Hook) {
  43. Hook["afterCreate"] = "afterCreate";
  44. Hook["afterAttach"] = "afterAttach";
  45. Hook["afterCreationFinalization"] = "afterCreationFinalization";
  46. Hook["beforeDetach"] = "beforeDetach";
  47. Hook["beforeDestroy"] = "beforeDestroy";
  48. })(Hook || (Hook = {}));
  49. /*! *****************************************************************************
  50. Copyright (c) Microsoft Corporation. All rights reserved.
  51. Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  52. this file except in compliance with the License. You may obtain a copy of the
  53. License at http://www.apache.org/licenses/LICENSE-2.0
  54. THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  55. KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  56. WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  57. MERCHANTABLITY OR NON-INFRINGEMENT.
  58. See the Apache Version 2.0 License for specific language governing permissions
  59. and limitations under the License.
  60. ***************************************************************************** */
  61. /* global Reflect, Promise */
  62. var extendStatics = function(d, b) {
  63. extendStatics = Object.setPrototypeOf ||
  64. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  65. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  66. return extendStatics(d, b);
  67. };
  68. function __extends(d, b) {
  69. extendStatics(d, b);
  70. function __() { this.constructor = d; }
  71. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  72. }
  73. var __assign = function() {
  74. __assign = Object.assign || function __assign(t) {
  75. for (var s, i = 1, n = arguments.length; i < n; i++) {
  76. s = arguments[i];
  77. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
  78. }
  79. return t;
  80. };
  81. return __assign.apply(this, arguments);
  82. };
  83. function __rest(s, e) {
  84. var t = {};
  85. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
  86. t[p] = s[p];
  87. if (s != null && typeof Object.getOwnPropertySymbols === "function")
  88. for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  89. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
  90. t[p[i]] = s[p[i]];
  91. }
  92. return t;
  93. }
  94. function __decorate(decorators, target, key, desc) {
  95. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  96. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  97. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  98. return c > 3 && r && Object.defineProperty(target, key, r), r;
  99. }
  100. function __values(o) {
  101. var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
  102. if (m) return m.call(o);
  103. return {
  104. next: function () {
  105. if (o && i >= o.length) o = void 0;
  106. return { value: o && o[i++], done: !o };
  107. }
  108. };
  109. }
  110. function __read(o, n) {
  111. var m = typeof Symbol === "function" && o[Symbol.iterator];
  112. if (!m) return o;
  113. var i = m.call(o), r, ar = [], e;
  114. try {
  115. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  116. }
  117. catch (error) { e = { error: error }; }
  118. finally {
  119. try {
  120. if (r && !r.done && (m = i["return"])) m.call(i);
  121. }
  122. finally { if (e) throw e.error; }
  123. }
  124. return ar;
  125. }
  126. function __spread() {
  127. for (var ar = [], i = 0; i < arguments.length; i++)
  128. ar = ar.concat(__read(arguments[i]));
  129. return ar;
  130. }
  131. /**
  132. * Returns the _actual_ type of the given tree node. (Or throws)
  133. *
  134. * @param object
  135. * @returns
  136. */
  137. function getType(object) {
  138. assertIsStateTreeNode(object, 1);
  139. return getStateTreeNode(object).type;
  140. }
  141. /**
  142. * Returns the _declared_ type of the given sub property of an object, array or map.
  143. * In the case of arrays and maps the property name is optional and will be ignored.
  144. *
  145. * Example:
  146. * ```ts
  147. * const Box = types.model({ x: 0, y: 0 })
  148. * const box = Box.create()
  149. *
  150. * console.log(getChildType(box, "x").name) // 'number'
  151. * ```
  152. *
  153. * @param object
  154. * @param propertyName
  155. * @returns
  156. */
  157. function getChildType(object, propertyName) {
  158. assertIsStateTreeNode(object, 1);
  159. return getStateTreeNode(object).getChildType(propertyName);
  160. }
  161. /**
  162. * Registers a function that will be invoked for each mutation that is applied to the provided model instance, or to any of its children.
  163. * See [patches](https://github.com/mobxjs/mobx-state-tree#patches) for more details. onPatch events are emitted immediately and will not await the end of a transaction.
  164. * Patches can be used to deep observe a model tree.
  165. *
  166. * @param target the model instance from which to receive patches
  167. * @param callback the callback that is invoked for each patch. The reversePatch is a patch that would actually undo the emitted patch
  168. * @returns function to remove the listener
  169. */
  170. function onPatch(target, callback) {
  171. // check all arguments
  172. assertIsStateTreeNode(target, 1);
  173. assertIsFunction(callback, 2);
  174. return getStateTreeNode(target).onPatch(callback);
  175. }
  176. /**
  177. * Registers a function that is invoked whenever a new snapshot for the given model instance is available.
  178. * The listener will only be fire at the end of the current MobX (trans)action.
  179. * See [snapshots](https://github.com/mobxjs/mobx-state-tree#snapshots) for more details.
  180. *
  181. * @param target
  182. * @param callback
  183. * @returns
  184. */
  185. function onSnapshot(target, callback) {
  186. // check all arguments
  187. assertIsStateTreeNode(target, 1);
  188. assertIsFunction(callback, 2);
  189. return getStateTreeNode(target).onSnapshot(callback);
  190. }
  191. /**
  192. * Applies a JSON-patch to the given model instance or bails out if the patch couldn't be applied
  193. * See [patches](https://github.com/mobxjs/mobx-state-tree#patches) for more details.
  194. *
  195. * Can apply a single past, or an array of patches.
  196. *
  197. * @param target
  198. * @param patch
  199. * @returns
  200. */
  201. function applyPatch(target, patch) {
  202. // check all arguments
  203. assertIsStateTreeNode(target, 1);
  204. assertArg(patch, function (p) { return typeof p === "object"; }, "object or array", 2);
  205. getStateTreeNode(target).applyPatches(asArray(patch));
  206. }
  207. /**
  208. * Small abstraction around `onPatch` and `applyPatch`, attaches a patch listener to a tree and records all the patches.
  209. * Returns an recorder object with the following signature:
  210. *
  211. * Example:
  212. * ```ts
  213. * export interface IPatchRecorder {
  214. * // the recorded patches
  215. * patches: IJsonPatch[]
  216. * // the inverse of the recorded patches
  217. * inversePatches: IJsonPatch[]
  218. * // true if currently recording
  219. * recording: boolean
  220. * // stop recording patches
  221. * stop(): void
  222. * // resume recording patches
  223. * resume(): void
  224. * // apply all the recorded patches on the given target (the original subject if omitted)
  225. * replay(target?: IAnyStateTreeNode): void
  226. * // reverse apply the recorded patches on the given target (the original subject if omitted)
  227. * // stops the recorder if not already stopped
  228. * undo(): void
  229. * }
  230. * ```
  231. *
  232. * The optional filter function allows to skip recording certain patches.
  233. *
  234. * @param subject
  235. * @param filter
  236. * @returns
  237. */
  238. function recordPatches(subject, filter) {
  239. // check all arguments
  240. assertIsStateTreeNode(subject, 1);
  241. var data = {
  242. patches: [],
  243. reversedInversePatches: []
  244. };
  245. // we will generate the immutable copy of patches on demand for public consumption
  246. var publicData = {};
  247. var disposer;
  248. var recorder = {
  249. get recording() {
  250. return !!disposer;
  251. },
  252. get patches() {
  253. if (!publicData.patches) {
  254. publicData.patches = data.patches.slice();
  255. }
  256. return publicData.patches;
  257. },
  258. get reversedInversePatches() {
  259. if (!publicData.reversedInversePatches) {
  260. publicData.reversedInversePatches = data.reversedInversePatches.slice();
  261. }
  262. return publicData.reversedInversePatches;
  263. },
  264. get inversePatches() {
  265. if (!publicData.inversePatches) {
  266. publicData.inversePatches = data.reversedInversePatches.slice().reverse();
  267. }
  268. return publicData.inversePatches;
  269. },
  270. stop: function () {
  271. if (disposer) {
  272. disposer();
  273. disposer = undefined;
  274. }
  275. },
  276. resume: function () {
  277. if (disposer)
  278. return;
  279. disposer = onPatch(subject, function (patch, inversePatch) {
  280. // skip patches that are asked to be filtered if there's a filter in place
  281. if (filter && !filter(patch, inversePatch, getRunningActionContext())) {
  282. return;
  283. }
  284. data.patches.push(patch);
  285. data.reversedInversePatches.unshift(inversePatch);
  286. // mark immutable public patches as dirty
  287. publicData.patches = undefined;
  288. publicData.inversePatches = undefined;
  289. publicData.reversedInversePatches = undefined;
  290. });
  291. },
  292. replay: function (target) {
  293. applyPatch(target || subject, data.patches);
  294. },
  295. undo: function (target) {
  296. applyPatch(target || subject, data.reversedInversePatches);
  297. }
  298. };
  299. recorder.resume();
  300. return recorder;
  301. }
  302. /**
  303. * The inverse of `unprotect`.
  304. *
  305. * @param target
  306. */
  307. function protect(target) {
  308. // check all arguments
  309. assertIsStateTreeNode(target, 1);
  310. var node = getStateTreeNode(target);
  311. if (!node.isRoot)
  312. throw fail$1("`protect` can only be invoked on root nodes");
  313. node.isProtectionEnabled = true;
  314. }
  315. /**
  316. * By default it is not allowed to directly modify a model. Models can only be modified through actions.
  317. * However, in some cases you don't care about the advantages (like replayability, traceability, etc) this yields.
  318. * For example because you are building a PoC or don't have any middleware attached to your tree.
  319. *
  320. * In that case you can disable this protection by calling `unprotect` on the root of your tree.
  321. *
  322. * Example:
  323. * ```ts
  324. * const Todo = types.model({
  325. * done: false
  326. * }).actions(self => ({
  327. * toggle() {
  328. * self.done = !self.done
  329. * }
  330. * }))
  331. *
  332. * const todo = Todo.create()
  333. * todo.done = true // throws!
  334. * todo.toggle() // OK
  335. * unprotect(todo)
  336. * todo.done = false // OK
  337. * ```
  338. */
  339. function unprotect(target) {
  340. // check all arguments
  341. assertIsStateTreeNode(target, 1);
  342. var node = getStateTreeNode(target);
  343. if (!node.isRoot)
  344. throw fail$1("`unprotect` can only be invoked on root nodes");
  345. node.isProtectionEnabled = false;
  346. }
  347. /**
  348. * Returns true if the object is in protected mode, @see protect
  349. */
  350. function isProtected(target) {
  351. return getStateTreeNode(target).isProtected;
  352. }
  353. /**
  354. * Applies a snapshot to a given model instances. Patch and snapshot listeners will be invoked as usual.
  355. *
  356. * @param target
  357. * @param snapshot
  358. * @returns
  359. */
  360. function applySnapshot(target, snapshot) {
  361. // check all arguments
  362. assertIsStateTreeNode(target, 1);
  363. return getStateTreeNode(target).applySnapshot(snapshot);
  364. }
  365. /**
  366. * Calculates a snapshot from the given model instance. The snapshot will always reflect the latest state but use
  367. * structural sharing where possible. Doesn't require MobX transactions to be completed.
  368. *
  369. * @param target
  370. * @param applyPostProcess If true (the default) then postProcessSnapshot gets applied.
  371. * @returns
  372. */
  373. function getSnapshot(target, applyPostProcess) {
  374. if (applyPostProcess === void 0) { applyPostProcess = true; }
  375. // check all arguments
  376. assertIsStateTreeNode(target, 1);
  377. var node = getStateTreeNode(target);
  378. if (applyPostProcess)
  379. return node.snapshot;
  380. return freeze(node.type.getSnapshot(node, false));
  381. }
  382. /**
  383. * Given a model instance, returns `true` if the object has a parent, that is, is part of another object, map or array.
  384. *
  385. * @param target
  386. * @param depth How far should we look upward? 1 by default.
  387. * @returns
  388. */
  389. function hasParent(target, depth) {
  390. if (depth === void 0) { depth = 1; }
  391. // check all arguments
  392. assertIsStateTreeNode(target, 1);
  393. assertIsNumber(depth, 2, 0);
  394. var parent = getStateTreeNode(target).parent;
  395. while (parent) {
  396. if (--depth === 0)
  397. return true;
  398. parent = parent.parent;
  399. }
  400. return false;
  401. }
  402. /**
  403. * Returns the immediate parent of this object, or throws.
  404. *
  405. * Note that the immediate parent can be either an object, map or array, and
  406. * doesn't necessarily refer to the parent model.
  407. *
  408. * Please note that in child nodes access to the root is only possible
  409. * once the `afterAttach` hook has fired.
  410. *
  411. * @param target
  412. * @param depth How far should we look upward? 1 by default.
  413. * @returns
  414. */
  415. function getParent(target, depth) {
  416. if (depth === void 0) { depth = 1; }
  417. // check all arguments
  418. assertIsStateTreeNode(target, 1);
  419. assertIsNumber(depth, 2, 0);
  420. var d = depth;
  421. var parent = getStateTreeNode(target).parent;
  422. while (parent) {
  423. if (--d === 0)
  424. return parent.storedValue;
  425. parent = parent.parent;
  426. }
  427. throw fail$1("Failed to find the parent of " + getStateTreeNode(target) + " at depth " + depth);
  428. }
  429. /**
  430. * Given a model instance, returns `true` if the object has a parent of given type, that is, is part of another object, map or array
  431. *
  432. * @param target
  433. * @param type
  434. * @returns
  435. */
  436. function hasParentOfType(target, type) {
  437. // check all arguments
  438. assertIsStateTreeNode(target, 1);
  439. assertIsType(type, 2);
  440. var parent = getStateTreeNode(target).parent;
  441. while (parent) {
  442. if (type.is(parent.storedValue))
  443. return true;
  444. parent = parent.parent;
  445. }
  446. return false;
  447. }
  448. /**
  449. * Returns the target's parent of a given type, or throws.
  450. *
  451. * @param target
  452. * @param type
  453. * @returns
  454. */
  455. function getParentOfType(target, type) {
  456. // check all arguments
  457. assertIsStateTreeNode(target, 1);
  458. assertIsType(type, 2);
  459. var parent = getStateTreeNode(target).parent;
  460. while (parent) {
  461. if (type.is(parent.storedValue))
  462. return parent.storedValue;
  463. parent = parent.parent;
  464. }
  465. throw fail$1("Failed to find the parent of " + getStateTreeNode(target) + " of a given type");
  466. }
  467. /**
  468. * Given an object in a model tree, returns the root object of that tree.
  469. *
  470. * Please note that in child nodes access to the root is only possible
  471. * once the `afterAttach` hook has fired.
  472. *
  473. * @param target
  474. * @returns
  475. */
  476. function getRoot(target) {
  477. // check all arguments
  478. assertIsStateTreeNode(target, 1);
  479. return getStateTreeNode(target).root.storedValue;
  480. }
  481. /**
  482. * Returns the path of the given object in the model tree
  483. *
  484. * @param target
  485. * @returns
  486. */
  487. function getPath(target) {
  488. // check all arguments
  489. assertIsStateTreeNode(target, 1);
  490. return getStateTreeNode(target).path;
  491. }
  492. /**
  493. * Returns the path of the given object as unescaped string array.
  494. *
  495. * @param target
  496. * @returns
  497. */
  498. function getPathParts(target) {
  499. // check all arguments
  500. assertIsStateTreeNode(target, 1);
  501. return splitJsonPath(getStateTreeNode(target).path);
  502. }
  503. /**
  504. * Returns true if the given object is the root of a model tree.
  505. *
  506. * @param target
  507. * @returns
  508. */
  509. function isRoot(target) {
  510. // check all arguments
  511. assertIsStateTreeNode(target, 1);
  512. return getStateTreeNode(target).isRoot;
  513. }
  514. /**
  515. * Resolves a path relatively to a given object.
  516. * Returns undefined if no value can be found.
  517. *
  518. * @param target
  519. * @param path escaped json path
  520. * @returns
  521. */
  522. function resolvePath(target, path) {
  523. // check all arguments
  524. assertIsStateTreeNode(target, 1);
  525. assertIsString(path, 2);
  526. var node = resolveNodeByPath(getStateTreeNode(target), path);
  527. return node ? node.value : undefined;
  528. }
  529. /**
  530. * Resolves a model instance given a root target, the type and the identifier you are searching for.
  531. * Returns undefined if no value can be found.
  532. *
  533. * @param type
  534. * @param target
  535. * @param identifier
  536. * @returns
  537. */
  538. function resolveIdentifier(type, target, identifier) {
  539. // check all arguments
  540. assertIsType(type, 1);
  541. assertIsStateTreeNode(target, 2);
  542. assertIsValidIdentifier(identifier, 3);
  543. var node = getStateTreeNode(target).root.identifierCache.resolve(type, normalizeIdentifier(identifier));
  544. return node ? node.value : undefined;
  545. }
  546. /**
  547. * Returns the identifier of the target node.
  548. * This is the *string normalized* identifier, which might not match the type of the identifier attribute
  549. *
  550. * @param target
  551. * @returns
  552. */
  553. function getIdentifier(target) {
  554. // check all arguments
  555. assertIsStateTreeNode(target, 1);
  556. return getStateTreeNode(target).identifier;
  557. }
  558. /**
  559. * Tests if a reference is valid (pointing to an existing node and optionally if alive) and returns such reference if it the check passes,
  560. * else it returns undefined.
  561. *
  562. * @param getter Function to access the reference.
  563. * @param checkIfAlive true to also make sure the referenced node is alive (default), false to skip this check.
  564. * @returns
  565. */
  566. function tryReference(getter, checkIfAlive) {
  567. if (checkIfAlive === void 0) { checkIfAlive = true; }
  568. try {
  569. var node = getter();
  570. if (node === undefined || node === null) {
  571. return undefined;
  572. }
  573. else if (isStateTreeNode(node)) {
  574. if (!checkIfAlive) {
  575. return node;
  576. }
  577. else {
  578. return isAlive(node) ? node : undefined;
  579. }
  580. }
  581. else {
  582. throw fail$1("The reference to be checked is not one of node, null or undefined");
  583. }
  584. }
  585. catch (e) {
  586. if (e instanceof InvalidReferenceError) {
  587. return undefined;
  588. }
  589. throw e;
  590. }
  591. }
  592. /**
  593. * Tests if a reference is valid (pointing to an existing node and optionally if alive) and returns if the check passes or not.
  594. *
  595. * @param getter Function to access the reference.
  596. * @param checkIfAlive true to also make sure the referenced node is alive (default), false to skip this check.
  597. * @returns
  598. */
  599. function isValidReference(getter, checkIfAlive) {
  600. if (checkIfAlive === void 0) { checkIfAlive = true; }
  601. try {
  602. var node = getter();
  603. if (node === undefined || node === null) {
  604. return false;
  605. }
  606. else if (isStateTreeNode(node)) {
  607. return checkIfAlive ? isAlive(node) : true;
  608. }
  609. else {
  610. throw fail$1("The reference to be checked is not one of node, null or undefined");
  611. }
  612. }
  613. catch (e) {
  614. if (e instanceof InvalidReferenceError) {
  615. return false;
  616. }
  617. throw e;
  618. }
  619. }
  620. /**
  621. * Try to resolve a given path relative to a given node.
  622. *
  623. * @param target
  624. * @param path
  625. * @returns
  626. */
  627. function tryResolve(target, path) {
  628. // check all arguments
  629. assertIsStateTreeNode(target, 1);
  630. assertIsString(path, 2);
  631. var node = resolveNodeByPath(getStateTreeNode(target), path, false);
  632. if (node === undefined)
  633. return undefined;
  634. try {
  635. return node.value;
  636. }
  637. catch (e) {
  638. // For what ever reason not resolvable (e.g. totally not existing path, or value that cannot be fetched)
  639. // see test / issue: 'try resolve doesn't work #686'
  640. return undefined;
  641. }
  642. }
  643. /**
  644. * Given two state tree nodes that are part of the same tree,
  645. * returns the shortest jsonpath needed to navigate from the one to the other
  646. *
  647. * @param base
  648. * @param target
  649. * @returns
  650. */
  651. function getRelativePath(base, target) {
  652. // check all arguments
  653. assertIsStateTreeNode(base, 1);
  654. assertIsStateTreeNode(target, 2);
  655. return getRelativePathBetweenNodes(getStateTreeNode(base), getStateTreeNode(target));
  656. }
  657. /**
  658. * Returns a deep copy of the given state tree node as new tree.
  659. * Short hand for `snapshot(x) = getType(x).create(getSnapshot(x))`
  660. *
  661. * _Tip: clone will create a literal copy, including the same identifiers. To modify identifiers etc during cloning, don't use clone but take a snapshot of the tree, modify it, and create new instance_
  662. *
  663. * @param source
  664. * @param keepEnvironment indicates whether the clone should inherit the same environment (`true`, the default), or not have an environment (`false`). If an object is passed in as second argument, that will act as the environment for the cloned tree.
  665. * @returns
  666. */
  667. function clone(source, keepEnvironment) {
  668. if (keepEnvironment === void 0) { keepEnvironment = true; }
  669. // check all arguments
  670. assertIsStateTreeNode(source, 1);
  671. var node = getStateTreeNode(source);
  672. return node.type.create(node.snapshot, keepEnvironment === true
  673. ? node.root.environment
  674. : keepEnvironment === false
  675. ? undefined
  676. : keepEnvironment); // it's an object or something else
  677. }
  678. /**
  679. * Removes a model element from the state tree, and let it live on as a new state tree
  680. */
  681. function detach(target) {
  682. // check all arguments
  683. assertIsStateTreeNode(target, 1);
  684. getStateTreeNode(target).detach();
  685. return target;
  686. }
  687. /**
  688. * Removes a model element from the state tree, and mark it as end-of-life; the element should not be used anymore
  689. */
  690. function destroy(target) {
  691. // check all arguments
  692. assertIsStateTreeNode(target, 1);
  693. var node = getStateTreeNode(target);
  694. if (node.isRoot)
  695. node.die();
  696. else
  697. node.parent.removeChild(node.subpath);
  698. }
  699. /**
  700. * Returns true if the given state tree node is not killed yet.
  701. * This means that the node is still a part of a tree, and that `destroy`
  702. * has not been called. If a node is not alive anymore, the only thing one can do with it
  703. * is requesting it's last path and snapshot
  704. *
  705. * @param target
  706. * @returns
  707. */
  708. function isAlive(target) {
  709. // check all arguments
  710. assertIsStateTreeNode(target, 1);
  711. return getStateTreeNode(target).observableIsAlive;
  712. }
  713. /**
  714. * Use this utility to register a function that should be called whenever the
  715. * targeted state tree node is destroyed. This is a useful alternative to managing
  716. * cleanup methods yourself using the `beforeDestroy` hook.
  717. *
  718. * This methods returns the same disposer that was passed as argument.
  719. *
  720. * Example:
  721. * ```ts
  722. * const Todo = types.model({
  723. * title: types.string
  724. * }).actions(self => ({
  725. * afterCreate() {
  726. * const autoSaveDisposer = reaction(
  727. * () => getSnapshot(self),
  728. * snapshot => sendSnapshotToServerSomehow(snapshot)
  729. * )
  730. * // stop sending updates to server if this
  731. * // instance is destroyed
  732. * addDisposer(self, autoSaveDisposer)
  733. * }
  734. * }))
  735. * ```
  736. *
  737. * @param target
  738. * @param disposer
  739. * @returns The same disposer that was passed as argument
  740. */
  741. function addDisposer(target, disposer) {
  742. // check all arguments
  743. assertIsStateTreeNode(target, 1);
  744. assertIsFunction(disposer, 2);
  745. var node = getStateTreeNode(target);
  746. node.addDisposer(disposer);
  747. return disposer;
  748. }
  749. /**
  750. * Returns the environment of the current state tree. For more info on environments,
  751. * see [Dependency injection](https://github.com/mobxjs/mobx-state-tree#dependency-injection)
  752. *
  753. * Please note that in child nodes access to the root is only possible
  754. * once the `afterAttach` hook has fired
  755. *
  756. * Returns an empty environment if the tree wasn't initialized with an environment
  757. *
  758. * @param target
  759. * @returns
  760. */
  761. function getEnv(target) {
  762. // check all arguments
  763. assertIsStateTreeNode(target, 1);
  764. var node = getStateTreeNode(target);
  765. var env = node.root.environment;
  766. if (!env)
  767. return EMPTY_OBJECT;
  768. return env;
  769. }
  770. /**
  771. * Performs a depth first walk through a tree.
  772. */
  773. function walk(target, processor) {
  774. // check all arguments
  775. assertIsStateTreeNode(target, 1);
  776. assertIsFunction(processor, 2);
  777. var node = getStateTreeNode(target);
  778. // tslint:disable-next-line:no_unused-variable
  779. node.getChildren().forEach(function (child) {
  780. if (isStateTreeNode(child.storedValue))
  781. walk(child.storedValue, processor);
  782. });
  783. processor(node.storedValue);
  784. }
  785. /**
  786. * Returns a reflection of the model type properties and name for either a model type or model node.
  787. *
  788. * @param typeOrNode
  789. * @returns
  790. */
  791. function getPropertyMembers(typeOrNode) {
  792. var type;
  793. if (isStateTreeNode(typeOrNode)) {
  794. type = getType(typeOrNode);
  795. }
  796. else {
  797. type = typeOrNode;
  798. }
  799. assertArg(type, function (t) { return isModelType(t); }, "model type or model instance", 1);
  800. return {
  801. name: type.name,
  802. properties: __assign({}, type.properties)
  803. };
  804. }
  805. /**
  806. * Returns a reflection of the model node, including name, properties, views, volatile and actions.
  807. *
  808. * @param target
  809. * @returns
  810. */
  811. function getMembers(target) {
  812. var type = getStateTreeNode(target).type;
  813. var reflected = __assign(__assign({}, getPropertyMembers(type)), { actions: [], volatile: [], views: [] });
  814. var props = Object.getOwnPropertyNames(target);
  815. props.forEach(function (key) {
  816. if (key in reflected.properties)
  817. return;
  818. var descriptor = Object.getOwnPropertyDescriptor(target, key);
  819. if (descriptor.get) {
  820. if (mobx.isComputedProp(target, key))
  821. reflected.views.push(key);
  822. else
  823. reflected.volatile.push(key);
  824. return;
  825. }
  826. if (descriptor.value._isMSTAction === true)
  827. reflected.actions.push(key);
  828. else if (mobx.isObservableProp(target, key))
  829. reflected.volatile.push(key);
  830. else
  831. reflected.views.push(key);
  832. });
  833. return reflected;
  834. }
  835. /**
  836. * Casts a node snapshot or instance type to an instance type so it can be assigned to a type instance.
  837. * Note that this is just a cast for the type system, this is, it won't actually convert a snapshot to an instance,
  838. * but just fool typescript into thinking so.
  839. * Either way, casting when outside an assignation operation won't compile.
  840. *
  841. * Example:
  842. * ```ts
  843. * const ModelA = types.model({
  844. * n: types.number
  845. * }).actions(self => ({
  846. * setN(aNumber: number) {
  847. * self.n = aNumber
  848. * }
  849. * }))
  850. *
  851. * const ModelB = types.model({
  852. * innerModel: ModelA
  853. * }).actions(self => ({
  854. * someAction() {
  855. * // this will allow the compiler to assign a snapshot to the property
  856. * self.innerModel = cast({ a: 5 })
  857. * }
  858. * }))
  859. * ```
  860. *
  861. * @param snapshotOrInstance Snapshot or instance
  862. * @returns The same object casted as an instance
  863. */
  864. function cast(snapshotOrInstance) {
  865. return snapshotOrInstance;
  866. }
  867. /**
  868. * Casts a node instance type to an snapshot type so it can be assigned to a type snapshot (e.g. to be used inside a create call).
  869. * Note that this is just a cast for the type system, this is, it won't actually convert an instance to a snapshot,
  870. * but just fool typescript into thinking so.
  871. *
  872. * Example:
  873. * ```ts
  874. * const ModelA = types.model({
  875. * n: types.number
  876. * }).actions(self => ({
  877. * setN(aNumber: number) {
  878. * self.n = aNumber
  879. * }
  880. * }))
  881. *
  882. * const ModelB = types.model({
  883. * innerModel: ModelA
  884. * })
  885. *
  886. * const a = ModelA.create({ n: 5 });
  887. * // this will allow the compiler to use a model as if it were a snapshot
  888. * const b = ModelB.create({ innerModel: castToSnapshot(a)})
  889. * ```
  890. *
  891. * @param snapshotOrInstance Snapshot or instance
  892. * @returns The same object casted as an input (creation) snapshot
  893. */
  894. function castToSnapshot(snapshotOrInstance) {
  895. return snapshotOrInstance;
  896. }
  897. /**
  898. * Casts a node instance type to a reference snapshot type so it can be assigned to a refernence snapshot (e.g. to be used inside a create call).
  899. * Note that this is just a cast for the type system, this is, it won't actually convert an instance to a refererence snapshot,
  900. * but just fool typescript into thinking so.
  901. *
  902. * Example:
  903. * ```ts
  904. * const ModelA = types.model({
  905. * id: types.identifier,
  906. * n: types.number
  907. * }).actions(self => ({
  908. * setN(aNumber: number) {
  909. * self.n = aNumber
  910. * }
  911. * }))
  912. *
  913. * const ModelB = types.model({
  914. * refA: types.reference(ModelA)
  915. * })
  916. *
  917. * const a = ModelA.create({ id: 'someId', n: 5 });
  918. * // this will allow the compiler to use a model as if it were a reference snapshot
  919. * const b = ModelB.create({ refA: castToReference(a)})
  920. * ```
  921. *
  922. * @param instance Instance
  923. * @returns The same object casted as an reference snapshot (string or number)
  924. */
  925. function castToReferenceSnapshot(instance) {
  926. return instance;
  927. }
  928. /**
  929. * Returns the unique node id (not to be confused with the instance identifier) for a
  930. * given instance.
  931. * This id is a number that is unique for each instance.
  932. *
  933. * @export
  934. * @param target
  935. * @returns
  936. */
  937. function getNodeId(target) {
  938. assertIsStateTreeNode(target, 1);
  939. return getStateTreeNode(target).nodeId;
  940. }
  941. /**
  942. * @internal
  943. * @hidden
  944. */
  945. var BaseNode = /** @class */ (function () {
  946. function BaseNode(type, parent, subpath, environment) {
  947. this.type = type;
  948. this.environment = environment;
  949. this._state = NodeLifeCycle.INITIALIZING;
  950. this.environment = environment;
  951. this.baseSetParent(parent, subpath);
  952. }
  953. Object.defineProperty(BaseNode.prototype, "subpath", {
  954. get: function () {
  955. return this._subpath;
  956. },
  957. enumerable: true,
  958. configurable: true
  959. });
  960. Object.defineProperty(BaseNode.prototype, "subpathUponDeath", {
  961. get: function () {
  962. return this._subpathUponDeath;
  963. },
  964. enumerable: true,
  965. configurable: true
  966. });
  967. Object.defineProperty(BaseNode.prototype, "pathUponDeath", {
  968. get: function () {
  969. return this._pathUponDeath;
  970. },
  971. enumerable: true,
  972. configurable: true
  973. });
  974. Object.defineProperty(BaseNode.prototype, "value", {
  975. get: function () {
  976. return this.type.getValue(this);
  977. },
  978. enumerable: true,
  979. configurable: true
  980. });
  981. Object.defineProperty(BaseNode.prototype, "state", {
  982. get: function () {
  983. return this._state;
  984. },
  985. set: function (val) {
  986. var wasAlive = this.isAlive;
  987. this._state = val;
  988. var isAlive = this.isAlive;
  989. if (this.aliveAtom && wasAlive !== isAlive) {
  990. this.aliveAtom.reportChanged();
  991. }
  992. },
  993. enumerable: true,
  994. configurable: true
  995. });
  996. BaseNode.prototype.fireInternalHook = function (name) {
  997. if (this._hookSubscribers) {
  998. this._hookSubscribers.emit(name, this, name);
  999. }
  1000. };
  1001. BaseNode.prototype.registerHook = function (hook, hookHandler) {
  1002. if (!this._hookSubscribers) {
  1003. this._hookSubscribers = new EventHandlers();
  1004. }
  1005. return this._hookSubscribers.register(hook, hookHandler);
  1006. };
  1007. Object.defineProperty(BaseNode.prototype, "parent", {
  1008. get: function () {
  1009. return this._parent;
  1010. },
  1011. enumerable: true,
  1012. configurable: true
  1013. });
  1014. BaseNode.prototype.baseSetParent = function (parent, subpath) {
  1015. this._parent = parent;
  1016. this._subpath = subpath;
  1017. this._escapedSubpath = undefined; // regenerate when needed
  1018. if (this.pathAtom) {
  1019. this.pathAtom.reportChanged();
  1020. }
  1021. };
  1022. Object.defineProperty(BaseNode.prototype, "path", {
  1023. /*
  1024. * Returns (escaped) path representation as string
  1025. */
  1026. get: function () {
  1027. return this.getEscapedPath(true);
  1028. },
  1029. enumerable: true,
  1030. configurable: true
  1031. });
  1032. BaseNode.prototype.getEscapedPath = function (reportObserved) {
  1033. if (reportObserved) {
  1034. if (!this.pathAtom) {
  1035. this.pathAtom = mobx.createAtom("path");
  1036. }
  1037. this.pathAtom.reportObserved();
  1038. }
  1039. if (!this.parent)
  1040. return "";
  1041. // regenerate escaped subpath if needed
  1042. if (this._escapedSubpath === undefined) {
  1043. this._escapedSubpath = !this._subpath ? "" : escapeJsonPath(this._subpath);
  1044. }
  1045. return this.parent.getEscapedPath(reportObserved) + "/" + this._escapedSubpath;
  1046. };
  1047. Object.defineProperty(BaseNode.prototype, "isRoot", {
  1048. get: function () {
  1049. return this.parent === null;
  1050. },
  1051. enumerable: true,
  1052. configurable: true
  1053. });
  1054. Object.defineProperty(BaseNode.prototype, "isAlive", {
  1055. get: function () {
  1056. return this.state !== NodeLifeCycle.DEAD;
  1057. },
  1058. enumerable: true,
  1059. configurable: true
  1060. });
  1061. Object.defineProperty(BaseNode.prototype, "isDetaching", {
  1062. get: function () {
  1063. return this.state === NodeLifeCycle.DETACHING;
  1064. },
  1065. enumerable: true,
  1066. configurable: true
  1067. });
  1068. Object.defineProperty(BaseNode.prototype, "observableIsAlive", {
  1069. get: function () {
  1070. if (!this.aliveAtom) {
  1071. this.aliveAtom = mobx.createAtom("alive");
  1072. }
  1073. this.aliveAtom.reportObserved();
  1074. return this.isAlive;
  1075. },
  1076. enumerable: true,
  1077. configurable: true
  1078. });
  1079. BaseNode.prototype.baseFinalizeCreation = function (whenFinalized) {
  1080. {
  1081. if (!this.isAlive) {
  1082. // istanbul ignore next
  1083. throw fail("assertion failed: cannot finalize the creation of a node that is already dead");
  1084. }
  1085. }
  1086. // goal: afterCreate hooks runs depth-first. After attach runs parent first, so on afterAttach the parent has completed already
  1087. if (this.state === NodeLifeCycle.CREATED) {
  1088. if (this.parent) {
  1089. if (this.parent.state !== NodeLifeCycle.FINALIZED) {
  1090. // parent not ready yet, postpone
  1091. return;
  1092. }
  1093. this.fireHook(Hook.afterAttach);
  1094. }
  1095. this.state = NodeLifeCycle.FINALIZED;
  1096. if (whenFinalized) {
  1097. whenFinalized();
  1098. }
  1099. }
  1100. };
  1101. BaseNode.prototype.baseFinalizeDeath = function () {
  1102. if (this._hookSubscribers) {
  1103. this._hookSubscribers.clearAll();
  1104. }
  1105. this._subpathUponDeath = this._subpath;
  1106. this._pathUponDeath = this.getEscapedPath(false);
  1107. this.baseSetParent(null, "");
  1108. this.state = NodeLifeCycle.DEAD;
  1109. };
  1110. BaseNode.prototype.baseAboutToDie = function () {
  1111. this.fireHook(Hook.beforeDestroy);
  1112. };
  1113. return BaseNode;
  1114. }());
  1115. /**
  1116. * @internal
  1117. * @hidden
  1118. */
  1119. var ScalarNode = /** @class */ (function (_super) {
  1120. __extends(ScalarNode, _super);
  1121. function ScalarNode(simpleType, parent, subpath, environment, initialSnapshot) {
  1122. var _this = _super.call(this, simpleType, parent, subpath, environment) || this;
  1123. try {
  1124. _this.storedValue = simpleType.createNewInstance(initialSnapshot);
  1125. }
  1126. catch (e) {
  1127. // short-cut to die the instance, to avoid the snapshot computed starting to throw...
  1128. _this.state = NodeLifeCycle.DEAD;
  1129. throw e;
  1130. }
  1131. _this.state = NodeLifeCycle.CREATED;
  1132. // for scalar nodes there's no point in firing this event since it would fire on the constructor, before
  1133. // anybody can actually register for/listen to it
  1134. // this.fireHook(Hook.AfterCreate)
  1135. _this.finalizeCreation();
  1136. return _this;
  1137. }
  1138. Object.defineProperty(ScalarNode.prototype, "root", {
  1139. get: function () {
  1140. // future optimization: store root ref in the node and maintain it
  1141. if (!this.parent)
  1142. throw fail$1("This scalar node is not part of a tree");
  1143. return this.parent.root;
  1144. },
  1145. enumerable: true,
  1146. configurable: true
  1147. });
  1148. ScalarNode.prototype.setParent = function (newParent, subpath) {
  1149. var parentChanged = this.parent !== newParent;
  1150. var subpathChanged = this.subpath !== subpath;
  1151. if (!parentChanged && !subpathChanged) {
  1152. return;
  1153. }
  1154. {
  1155. if (!subpath) {
  1156. // istanbul ignore next
  1157. throw fail$1("assertion failed: subpath expected");
  1158. }
  1159. if (!newParent) {
  1160. // istanbul ignore next
  1161. throw fail$1("assertion failed: parent expected");
  1162. }
  1163. if (parentChanged) {
  1164. // istanbul ignore next
  1165. throw fail$1("assertion failed: scalar nodes cannot change their parent");
  1166. }
  1167. }
  1168. this.environment = undefined; // use parent's
  1169. this.baseSetParent(this.parent, subpath);
  1170. };
  1171. Object.defineProperty(ScalarNode.prototype, "snapshot", {
  1172. get: function () {
  1173. return freeze(this.getSnapshot());
  1174. },
  1175. enumerable: true,
  1176. configurable: true
  1177. });
  1178. ScalarNode.prototype.getSnapshot = function () {
  1179. return this.type.getSnapshot(this);
  1180. };
  1181. ScalarNode.prototype.toString = function () {
  1182. var path = (this.isAlive ? this.path : this.pathUponDeath) || "<root>";
  1183. return this.type.name + "@" + path + (this.isAlive ? "" : " [dead]");
  1184. };
  1185. ScalarNode.prototype.die = function () {
  1186. if (!this.isAlive || this.state === NodeLifeCycle.DETACHING)
  1187. return;
  1188. this.aboutToDie();
  1189. this.finalizeDeath();
  1190. };
  1191. ScalarNode.prototype.finalizeCreation = function () {
  1192. this.baseFinalizeCreation();
  1193. };
  1194. ScalarNode.prototype.aboutToDie = function () {
  1195. this.baseAboutToDie();
  1196. };
  1197. ScalarNode.prototype.finalizeDeath = function () {
  1198. this.baseFinalizeDeath();
  1199. };
  1200. ScalarNode.prototype.fireHook = function (name) {
  1201. this.fireInternalHook(name);
  1202. };
  1203. __decorate([
  1204. mobx.action
  1205. ], ScalarNode.prototype, "die", null);
  1206. return ScalarNode;
  1207. }(BaseNode));
  1208. var nextNodeId = 1;
  1209. var snapshotReactionOptions = {
  1210. onError: function (e) {
  1211. throw e;
  1212. }
  1213. };
  1214. /**
  1215. * @internal
  1216. * @hidden
  1217. */
  1218. var ObjectNode = /** @class */ (function (_super) {
  1219. __extends(ObjectNode, _super);
  1220. function ObjectNode(complexType, parent, subpath, environment, initialValue) {
  1221. var _this = _super.call(this, complexType, parent, subpath, environment) || this;
  1222. _this.nodeId = ++nextNodeId;
  1223. _this.isProtectionEnabled = true;
  1224. _this._autoUnbox = true; // unboxing is disabled when reading child nodes
  1225. _this._isRunningAction = false; // only relevant for root
  1226. _this._hasSnapshotReaction = false;
  1227. _this._observableInstanceState = 0 /* UNINITIALIZED */;
  1228. _this._cachedInitialSnapshotCreated = false;
  1229. _this.unbox = _this.unbox.bind(_this);
  1230. _this._initialSnapshot = freeze(initialValue);
  1231. _this.identifierAttribute = complexType.identifierAttribute;
  1232. if (!parent) {
  1233. _this.identifierCache = new IdentifierCache();
  1234. }
  1235. _this._childNodes = complexType.initializeChildNodes(_this, _this._initialSnapshot);
  1236. // identifier can not be changed during lifecycle of a node
  1237. // so we safely can read it from initial snapshot
  1238. _this.identifier = null;
  1239. _this.unnormalizedIdentifier = null;
  1240. if (_this.identifierAttribute && _this._initialSnapshot) {
  1241. var id = _this._initialSnapshot[_this.identifierAttribute];
  1242. if (id === undefined) {
  1243. // try with the actual node if not (for optional identifiers)
  1244. var childNode = _this._childNodes[_this.identifierAttribute];
  1245. if (childNode) {
  1246. id = childNode.value;
  1247. }
  1248. }
  1249. if (typeof id !== "string" && typeof id !== "number") {
  1250. throw fail$1("Instance identifier '" + _this.identifierAttribute + "' for type '" + _this.type.name + "' must be a string or a number");
  1251. }
  1252. // normalize internal identifier to string
  1253. _this.identifier = normalizeIdentifier(id);
  1254. _this.unnormalizedIdentifier = id;
  1255. }
  1256. if (!parent) {
  1257. _this.identifierCache.addNodeToCache(_this);
  1258. }
  1259. else {
  1260. parent.root.identifierCache.addNodeToCache(_this);
  1261. }
  1262. return _this;
  1263. }
  1264. ObjectNode.prototype.applyPatches = function (patches) {
  1265. this.createObservableInstanceIfNeeded();
  1266. this._applyPatches(patches);
  1267. };
  1268. ObjectNode.prototype.applySnapshot = function (snapshot) {
  1269. this.createObservableInstanceIfNeeded();
  1270. this._applySnapshot(snapshot);
  1271. };
  1272. ObjectNode.prototype.createObservableInstanceIfNeeded = function () {
  1273. var e_1, _a;
  1274. if (this._observableInstanceState !== 0 /* UNINITIALIZED */) {
  1275. return;
  1276. }
  1277. {
  1278. if (this.state !== NodeLifeCycle.INITIALIZING) {
  1279. // istanbul ignore next
  1280. throw fail$1("assertion failed: the creation of the observable instance must be done on the initializing phase");
  1281. }
  1282. }
  1283. this._observableInstanceState = 1 /* CREATING */;
  1284. // make sure the parent chain is created as well
  1285. // array with parent chain from parent to child
  1286. var parentChain = [];
  1287. var parent = this.parent;
  1288. // for performance reasons we never go back further than the most direct
  1289. // uninitialized parent
  1290. // this is done to avoid traversing the whole tree to the root when using
  1291. // the same reference again
  1292. while (parent &&
  1293. parent._observableInstanceState === 0 /* UNINITIALIZED */) {
  1294. parentChain.unshift(parent);
  1295. parent = parent.parent;
  1296. }
  1297. try {
  1298. // initialize the uninitialized parent chain from parent to child
  1299. for (var parentChain_1 = __values(parentChain), parentChain_1_1 = parentChain_1.next(); !parentChain_1_1.done; parentChain_1_1 = parentChain_1.next()) {
  1300. var p = parentChain_1_1.value;
  1301. p.createObservableInstanceIfNeeded();
  1302. }
  1303. }
  1304. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  1305. finally {
  1306. try {
  1307. if (parentChain_1_1 && !parentChain_1_1.done && (_a = parentChain_1.return)) _a.call(parentChain_1);
  1308. }
  1309. finally { if (e_1) throw e_1.error; }
  1310. }
  1311. var type = this.type;
  1312. try {
  1313. this.storedValue = type.createNewInstance(this._childNodes);
  1314. this.preboot();
  1315. this._isRunningAction = true;
  1316. type.finalizeNewInstance(this, this.storedValue);
  1317. }
  1318. catch (e) {
  1319. // short-cut to die the instance, to avoid the snapshot computed starting to throw...
  1320. this.state = NodeLifeCycle.DEAD;
  1321. throw e;
  1322. }
  1323. finally {
  1324. this._isRunningAction = false;
  1325. }
  1326. this._observableInstanceState = 2 /* CREATED */;
  1327. // NOTE: we need to touch snapshot, because non-observable
  1328. // "_observableInstanceState" field was touched
  1329. invalidateComputed(this, "snapshot");
  1330. if (this.isRoot)
  1331. this._addSnapshotReaction();
  1332. this._childNodes = EMPTY_OBJECT;
  1333. this.state = NodeLifeCycle.CREATED;
  1334. this.fireHook(Hook.afterCreate);
  1335. this.finalizeCreation();
  1336. };
  1337. Object.defineProperty(ObjectNode.prototype, "root", {
  1338. get: function () {
  1339. var parent = this.parent;
  1340. return parent ? parent.root : this;
  1341. },
  1342. enumerable: true,
  1343. configurable: true
  1344. });
  1345. ObjectNode.prototype.clearParent = function () {
  1346. if (!this.parent)
  1347. return;
  1348. // detach if attached
  1349. this.fireHook(Hook.beforeDetach);
  1350. var previousState = this.state;
  1351. this.state = NodeLifeCycle.DETACHING;
  1352. var root = this.root;
  1353. var newEnv = root.environment;
  1354. var newIdCache = root.identifierCache.splitCache(this);
  1355. try {
  1356. this.parent.removeChild(this.subpath);
  1357. this.baseSetParent(null, "");
  1358. this.environment = newEnv;
  1359. this.identifierCache = newIdCache;
  1360. }
  1361. finally {
  1362. this.state = previousState;
  1363. }
  1364. };
  1365. ObjectNode.prototype.setParent = function (newParent, subpath) {
  1366. var parentChanged = newParent !== this.parent;
  1367. var subpathChanged = subpath !== this.subpath;
  1368. if (!parentChanged && !subpathChanged) {
  1369. return;
  1370. }
  1371. {
  1372. if (!subpath) {
  1373. // istanbul ignore next
  1374. throw fail$1("assertion failed: subpath expected");
  1375. }
  1376. if (!newParent) {
  1377. // istanbul ignore next
  1378. throw fail$1("assertion failed: new parent expected");
  1379. }
  1380. if (this.parent && parentChanged) {
  1381. throw fail$1("A node cannot exists twice in the state tree. Failed to add " + this + " to path '" + newParent.path + "/" + subpath + "'.");
  1382. }
  1383. if (!this.parent && newParent.root === this) {
  1384. throw fail$1("A state tree is not allowed to contain itself. Cannot assign " + this + " to path '" + newParent.path + "/" + subpath + "'");
  1385. }
  1386. if (!this.parent &&
  1387. !!this.environment &&
  1388. this.environment !== newParent.root.environment) {
  1389. throw fail$1("A state tree cannot be made part of another state tree as long as their environments are different.");
  1390. }
  1391. }
  1392. if (parentChanged) {
  1393. // attach to new parent
  1394. this.environment = undefined; // will use root's
  1395. newParent.root.identifierCache.mergeCache(this);
  1396. this.baseSetParent(newParent, subpath);
  1397. this.fireHook(Hook.afterAttach);
  1398. }
  1399. else if (subpathChanged) {
  1400. // moving to a new subpath on the same parent
  1401. this.baseSetParent(this.parent, subpath);
  1402. }
  1403. };
  1404. ObjectNode.prototype.fireHook = function (name) {
  1405. var _this = this;
  1406. this.fireInternalHook(name);
  1407. var fn = this.storedValue &&
  1408. typeof this.storedValue === "object" &&
  1409. this.storedValue[name];
  1410. if (typeof fn === "function") {
  1411. // we check for it to allow old mobx peer dependencies that don't have the method to work (even when still bugged)
  1412. if (mobx._allowStateChangesInsideComputed) {
  1413. mobx._allowStateChangesInsideComputed(function () {
  1414. fn.apply(_this.storedValue);
  1415. });
  1416. }
  1417. else {
  1418. fn.apply(this.storedValue);
  1419. }
  1420. }
  1421. };
  1422. Object.defineProperty(ObjectNode.prototype, "snapshot", {
  1423. // advantage of using computed for a snapshot is that nicely respects transactions etc.
  1424. get: function () {
  1425. return freeze(this.getSnapshot());
  1426. },
  1427. enumerable: true,
  1428. configurable: true
  1429. });
  1430. // NOTE: we use this method to get snapshot without creating @computed overhead
  1431. ObjectNode.prototype.getSnapshot = function () {
  1432. if (!this.isAlive)
  1433. return this._snapshotUponDeath;
  1434. return this._observableInstanceState === 2 /* CREATED */
  1435. ? this._getActualSnapshot()
  1436. : this._getCachedInitialSnapshot();
  1437. };
  1438. ObjectNode.prototype._getActualSnapshot = function () {
  1439. return this.type.getSnapshot(this);
  1440. };
  1441. ObjectNode.prototype._getCachedInitialSnapshot = function () {
  1442. if (!this._cachedInitialSnapshotCreated) {
  1443. var type = this.type;
  1444. var childNodes = this._childNodes;
  1445. var snapshot = this._initialSnapshot;
  1446. this._cachedInitialSnapshot = type.processInitialSnapshot(childNodes, snapshot);
  1447. this._cachedInitialSnapshotCreated = true;
  1448. }
  1449. return this._cachedInitialSnapshot;
  1450. };
  1451. ObjectNode.prototype.isRunningAction = function () {
  1452. if (this._isRunningAction)
  1453. return true;
  1454. if (this.isRoot)
  1455. return false;
  1456. return this.parent.isRunningAction();
  1457. };
  1458. ObjectNode.prototype.assertAlive = function (context) {
  1459. var livelinessChecking = getLivelinessChecking();
  1460. if (!this.isAlive && livelinessChecking !== "ignore") {
  1461. var error = this._getAssertAliveError(context);
  1462. switch (livelinessChecking) {
  1463. case "error":
  1464. throw fail$1(error);
  1465. case "warn":
  1466. warnError(error);
  1467. }
  1468. }
  1469. };
  1470. ObjectNode.prototype._getAssertAliveError = function (context) {
  1471. var escapedPath = this.getEscapedPath(false) || this.pathUponDeath || "";
  1472. var subpath = (context.subpath && escapeJsonPath(context.subpath)) || "";
  1473. var actionContext = context.actionContext || getCurrentActionContext();
  1474. // try to use a real action context if possible since it includes the action name
  1475. if (actionContext && actionContext.type !== "action" && actionContext.parentActionEvent) {
  1476. actionContext = actionContext.parentActionEvent;
  1477. }
  1478. var actionFullPath = "";
  1479. if (actionContext && actionContext.name != null) {
  1480. // try to use the context, and if it not available use the node one
  1481. var actionPath = (actionContext && actionContext.context && getPath(actionContext.context)) ||
  1482. escapedPath;
  1483. actionFullPath = actionPath + "." + actionContext.name + "()";
  1484. }
  1485. return "You are trying to read or write to an object that is no longer part of a state tree. (Object type: '" + this.type.name + "', Path upon death: '" + escapedPath + "', Subpath: '" + subpath + "', Action: '" + actionFullPath + "'). Either detach nodes first, or don't use objects after removing / replacing them in the tree.";
  1486. };
  1487. ObjectNode.prototype.getChildNode = function (subpath) {
  1488. this.assertAlive({
  1489. subpath: subpath
  1490. });
  1491. this._autoUnbox = false;
  1492. try {
  1493. return this._observableInstanceState === 2 /* CREATED */
  1494. ? this.type.getChildNode(this, subpath)
  1495. : this._childNodes[subpath];
  1496. }
  1497. finally {
  1498. this._autoUnbox = true;
  1499. }
  1500. };
  1501. ObjectNode.prototype.getChildren = function () {
  1502. this.assertAlive(EMPTY_OBJECT);
  1503. this._autoUnbox = false;
  1504. try {
  1505. return this._observableInstanceState === 2 /* CREATED */
  1506. ? this.type.getChildren(this)
  1507. : convertChildNodesToArray(this._childNodes);
  1508. }
  1509. finally {
  1510. this._autoUnbox = true;
  1511. }
  1512. };
  1513. ObjectNode.prototype.getChildType = function (propertyName) {
  1514. return this.type.getChildType(propertyName);
  1515. };
  1516. Object.defineProperty(ObjectNode.prototype, "isProtected", {
  1517. get: function () {
  1518. return this.root.isProtectionEnabled;
  1519. },
  1520. enumerable: true,
  1521. configurable: true
  1522. });
  1523. ObjectNode.prototype.assertWritable = function (context) {
  1524. this.assertAlive(context);
  1525. if (!this.isRunningAction() && this.isProtected) {
  1526. throw fail$1("Cannot modify '" + this + "', the object is protected and can only be modified by using an action.");
  1527. }
  1528. };
  1529. ObjectNode.prototype.removeChild = function (subpath) {
  1530. this.type.removeChild(this, subpath);
  1531. };
  1532. // bound on the constructor
  1533. ObjectNode.prototype.unbox = function (childNode) {
  1534. if (!childNode)
  1535. return childNode;
  1536. this.assertAlive({
  1537. subpath: childNode.subpath || childNode.subpathUponDeath
  1538. });
  1539. return this._autoUnbox ? childNode.value : childNode;
  1540. };
  1541. ObjectNode.prototype.toString = function () {
  1542. var path = (this.isAlive ? this.path : this.pathUponDeath) || "<root>";
  1543. var identifier = this.identifier ? "(id: " + this.identifier + ")" : "";
  1544. return this.type.name + "@" + path + identifier + (this.isAlive ? "" : " [dead]");
  1545. };
  1546. ObjectNode.prototype.finalizeCreation = function () {
  1547. var _this = this;
  1548. this.baseFinalizeCreation(function () {
  1549. var e_2, _a;
  1550. try {
  1551. for (var _b = __values(_this.getChildren()), _c = _b.next(); !_c.done; _c = _b.next()) {
  1552. var child = _c.value;
  1553. child.finalizeCreation();
  1554. }
  1555. }
  1556. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  1557. finally {
  1558. try {
  1559. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  1560. }
  1561. finally { if (e_2) throw e_2.error; }
  1562. }
  1563. _this.fireInternalHook(Hook.afterCreationFinalization);
  1564. });
  1565. };
  1566. ObjectNode.prototype.detach = function () {
  1567. if (!this.isAlive)
  1568. throw fail$1("Error while detaching, node is not alive.");
  1569. this.clearParent();
  1570. };
  1571. ObjectNode.prototype.preboot = function () {
  1572. var self = this;
  1573. this._applyPatches = createActionInvoker(this.storedValue, "@APPLY_PATCHES", function (patches) {
  1574. patches.forEach(function (patch) {
  1575. var parts = splitJsonPath(patch.path);
  1576. var node = resolveNodeByPathParts(self, parts.slice(0, -1));
  1577. node.applyPatchLocally(parts[parts.length - 1], patch);
  1578. });
  1579. });
  1580. this._applySnapshot = createActionInvoker(this.storedValue, "@APPLY_SNAPSHOT", function (snapshot) {
  1581. // if the snapshot is the same as the current one, avoid performing a reconcile
  1582. if (snapshot === self.snapshot)
  1583. return;
  1584. // else, apply it by calling the type logic
  1585. return self.type.applySnapshot(self, snapshot);
  1586. });
  1587. addHiddenFinalProp(this.storedValue, "$treenode", this);
  1588. addHiddenFinalProp(this.storedValue, "toJSON", toJSON);
  1589. };
  1590. ObjectNode.prototype.die = function () {
  1591. if (!this.isAlive || this.state === NodeLifeCycle.DETACHING)
  1592. return;
  1593. this.aboutToDie();
  1594. this.finalizeDeath();
  1595. };
  1596. ObjectNode.prototype.aboutToDie = function () {
  1597. if (this._observableInstanceState === 0 /* UNINITIALIZED */) {
  1598. return;
  1599. }
  1600. this.getChildren().forEach(function (node) {
  1601. node.aboutToDie();
  1602. });
  1603. // beforeDestroy should run before the disposers since else we could end up in a situation where
  1604. // a disposer added with addDisposer at this stage (beforeDestroy) is actually never released
  1605. this.baseAboutToDie();
  1606. this._internalEventsEmit("dispose" /* Dispose */);
  1607. this._internalEventsClear("dispose" /* Dispose */);
  1608. };
  1609. ObjectNode.prototype.finalizeDeath = function () {
  1610. // invariant: not called directly but from "die"
  1611. this.getChildren().forEach(function (node) {
  1612. node.finalizeDeath();
  1613. });
  1614. this.root.identifierCache.notifyDied(this);
  1615. // "kill" the computed prop and just store the last snapshot
  1616. var snapshot = this.snapshot;
  1617. this._snapshotUponDeath = snapshot;
  1618. this._internalEventsClearAll();
  1619. this.baseFinalizeDeath();
  1620. };
  1621. ObjectNode.prototype.onSnapshot = function (onChange) {
  1622. this._addSnapshotReaction();
  1623. return this._internalEventsRegister("snapshot" /* Snapshot */, onChange);
  1624. };
  1625. ObjectNode.prototype.emitSnapshot = function (snapshot) {
  1626. this._internalEventsEmit("snapshot" /* Snapshot */, snapshot);
  1627. };
  1628. ObjectNode.prototype.onPatch = function (handler) {
  1629. return this._internalEventsRegister("patch" /* Patch */, handler);
  1630. };
  1631. ObjectNode.prototype.emitPatch = function (basePatch, source) {
  1632. if (this._internalEventsHasSubscribers("patch" /* Patch */)) {
  1633. var localizedPatch = extend({}, basePatch, {
  1634. path: source.path.substr(this.path.length) + "/" + basePatch.path // calculate the relative path of the patch
  1635. });
  1636. var _a = __read(splitPatch(localizedPatch), 2), patch = _a[0], reversePatch = _a[1];
  1637. this._internalEventsEmit("patch" /* Patch */, patch, reversePatch);
  1638. }
  1639. if (this.parent)
  1640. this.parent.emitPatch(basePatch, source);
  1641. };
  1642. ObjectNode.prototype.hasDisposer = function (disposer) {
  1643. return this._internalEventsHas("dispose" /* Dispose */, disposer);
  1644. };
  1645. ObjectNode.prototype.addDisposer = function (disposer) {
  1646. if (!this.hasDisposer(disposer)) {
  1647. this._internalEventsRegister("dispose" /* Dispose */, disposer, true);
  1648. return;
  1649. }
  1650. throw fail$1("cannot add a disposer when it is already registered for execution");
  1651. };
  1652. ObjectNode.prototype.removeDisposer = function (disposer) {
  1653. if (!this._internalEventsHas("dispose" /* Dispose */, disposer)) {
  1654. throw fail$1("cannot remove a disposer which was never registered for execution");
  1655. }
  1656. this._internalEventsUnregister("dispose" /* Dispose */, disposer);
  1657. };
  1658. ObjectNode.prototype.removeMiddleware = function (middleware) {
  1659. if (this.middlewares) {
  1660. var index = this.middlewares.indexOf(middleware);
  1661. if (index >= 0) {
  1662. this.middlewares.splice(index, 1);
  1663. }
  1664. }
  1665. };
  1666. ObjectNode.prototype.addMiddleWare = function (handler, includeHooks) {
  1667. var _this = this;
  1668. if (includeHooks === void 0) { includeHooks = true; }
  1669. var middleware = { handler: handler, includeHooks: includeHooks };
  1670. if (!this.middlewares)
  1671. this.middlewares = [middleware];
  1672. else
  1673. this.middlewares.push(middleware);
  1674. return function () {
  1675. _this.removeMiddleware(middleware);
  1676. };
  1677. };
  1678. ObjectNode.prototype.applyPatchLocally = function (subpath, patch) {
  1679. this.assertWritable({
  1680. subpath: subpath
  1681. });
  1682. this.createObservableInstanceIfNeeded();
  1683. this.type.applyPatchLocally(this, subpath, patch);
  1684. };
  1685. ObjectNode.prototype._addSnapshotReaction = function () {
  1686. var _this = this;
  1687. if (!this._hasSnapshotReaction) {
  1688. var snapshotDisposer = mobx.reaction(function () { return _this.snapshot; }, function (snapshot) { return _this.emitSnapshot(snapshot); }, snapshotReactionOptions);
  1689. this.addDisposer(snapshotDisposer);
  1690. this._hasSnapshotReaction = true;
  1691. }
  1692. };
  1693. // we proxy the methods to avoid creating an EventHandlers instance when it is not needed
  1694. ObjectNode.prototype._internalEventsHasSubscribers = function (event) {
  1695. return !!this._internalEvents && this._internalEvents.hasSubscribers(event);
  1696. };
  1697. ObjectNode.prototype._internalEventsRegister = function (event, eventHandler, atTheBeginning) {
  1698. if (atTheBeginning === void 0) { atTheBeginning = false; }
  1699. if (!this._internalEvents) {
  1700. this._internalEvents = new EventHandlers();
  1701. }
  1702. return this._internalEvents.register(event, eventHandler, atTheBeginning);
  1703. };
  1704. ObjectNode.prototype._internalEventsHas = function (event, eventHandler) {
  1705. return !!this._internalEvents && this._internalEvents.has(event, eventHandler);
  1706. };
  1707. ObjectNode.prototype._internalEventsUnregister = function (event, eventHandler) {
  1708. if (this._internalEvents) {
  1709. this._internalEvents.unregister(event, eventHandler);
  1710. }
  1711. };
  1712. ObjectNode.prototype._internalEventsEmit = function (event) {
  1713. var _a;
  1714. var args = [];
  1715. for (var _i = 1; _i < arguments.length; _i++) {
  1716. args[_i - 1] = arguments[_i];
  1717. }
  1718. if (this._internalEvents) {
  1719. (_a = this._internalEvents).emit.apply(_a, __spread([event], args));
  1720. }
  1721. };
  1722. ObjectNode.prototype._internalEventsClear = function (event) {
  1723. if (this._internalEvents) {
  1724. this._internalEvents.clear(event);
  1725. }
  1726. };
  1727. ObjectNode.prototype._internalEventsClearAll = function () {
  1728. if (this._internalEvents) {
  1729. this._internalEvents.clearAll();
  1730. }
  1731. };
  1732. __decorate([
  1733. mobx.action
  1734. ], ObjectNode.prototype, "createObservableInstanceIfNeeded", null);
  1735. __decorate([
  1736. mobx.computed
  1737. ], ObjectNode.prototype, "snapshot", null);
  1738. __decorate([
  1739. mobx.action
  1740. ], ObjectNode.prototype, "detach", null);
  1741. __decorate([
  1742. mobx.action
  1743. ], ObjectNode.prototype, "die", null);
  1744. return ObjectNode;
  1745. }(BaseNode));
  1746. /**
  1747. * @internal
  1748. * @hidden
  1749. */
  1750. var TypeFlags;
  1751. (function (TypeFlags) {
  1752. TypeFlags[TypeFlags["String"] = 1] = "String";
  1753. TypeFlags[TypeFlags["Number"] = 2] = "Number";
  1754. TypeFlags[TypeFlags["Boolean"] = 4] = "Boolean";
  1755. TypeFlags[TypeFlags["Date"] = 8] = "Date";
  1756. TypeFlags[TypeFlags["Literal"] = 16] = "Literal";
  1757. TypeFlags[TypeFlags["Array"] = 32] = "Array";
  1758. TypeFlags[TypeFlags["Map"] = 64] = "Map";
  1759. TypeFlags[TypeFlags["Object"] = 128] = "Object";
  1760. TypeFlags[TypeFlags["Frozen"] = 256] = "Frozen";
  1761. TypeFlags[TypeFlags["Optional"] = 512] = "Optional";
  1762. TypeFlags[TypeFlags["Reference"] = 1024] = "Reference";
  1763. TypeFlags[TypeFlags["Identifier"] = 2048] = "Identifier";
  1764. TypeFlags[TypeFlags["Late"] = 4096] = "Late";
  1765. TypeFlags[TypeFlags["Refinement"] = 8192] = "Refinement";
  1766. TypeFlags[TypeFlags["Union"] = 16384] = "Union";
  1767. TypeFlags[TypeFlags["Null"] = 32768] = "Null";
  1768. TypeFlags[TypeFlags["Undefined"] = 65536] = "Undefined";
  1769. TypeFlags[TypeFlags["Integer"] = 131072] = "Integer";
  1770. TypeFlags[TypeFlags["Custom"] = 262144] = "Custom";
  1771. TypeFlags[TypeFlags["SnapshotProcessor"] = 524288] = "SnapshotProcessor";
  1772. })(TypeFlags || (TypeFlags = {}));
  1773. /**
  1774. * @internal
  1775. * @hidden
  1776. */
  1777. var cannotDetermineSubtype = "cannotDetermine";
  1778. /**
  1779. * A base type produces a MST node (Node in the state tree)
  1780. *
  1781. * @internal
  1782. * @hidden
  1783. */
  1784. var BaseType = /** @class */ (function () {
  1785. function BaseType(name) {
  1786. this.isType = true;
  1787. this.name = name;
  1788. }
  1789. BaseType.prototype.create = function (snapshot, environment) {
  1790. typecheckInternal(this, snapshot);
  1791. return this.instantiate(null, "", environment, snapshot).value;
  1792. };
  1793. BaseType.prototype.getSnapshot = function (node, applyPostProcess) {
  1794. // istanbul ignore next
  1795. throw fail$1("unimplemented method");
  1796. };
  1797. BaseType.prototype.isAssignableFrom = function (type) {
  1798. return type === this;
  1799. };
  1800. BaseType.prototype.validate = function (value, context) {
  1801. var node = getStateTreeNodeSafe(value);
  1802. if (node) {
  1803. var valueType = getType(value);
  1804. return this.isAssignableFrom(valueType)
  1805. ? typeCheckSuccess()
  1806. : typeCheckFailure(context, value);
  1807. // it is tempting to compare snapshots, but in that case we should always clone on assignments...
  1808. }
  1809. return this.isValidSnapshot(value, context);
  1810. };
  1811. BaseType.prototype.is = function (thing) {
  1812. return this.validate(thing, [{ path: "", type: this }]).length === 0;
  1813. };
  1814. Object.defineProperty(BaseType.prototype, "Type", {
  1815. get: function () {
  1816. // istanbul ignore next
  1817. throw fail$1("Factory.Type should not be actually called. It is just a Type signature that can be used at compile time with Typescript, by using `typeof type.Type`");
  1818. },
  1819. enumerable: true,
  1820. configurable: true
  1821. });
  1822. Object.defineProperty(BaseType.prototype, "TypeWithoutSTN", {
  1823. get: function () {
  1824. // istanbul ignore next
  1825. throw fail$1("Factory.TypeWithoutSTN should not be actually called. It is just a Type signature that can be used at compile time with Typescript, by using `typeof type.TypeWithoutSTN`");
  1826. },
  1827. enumerable: true,
  1828. configurable: true
  1829. });
  1830. Object.defineProperty(BaseType.prototype, "SnapshotType", {
  1831. get: function () {
  1832. // istanbul ignore next
  1833. throw fail$1("Factory.SnapshotType should not be actually called. It is just a Type signature that can be used at compile time with Typescript, by using `typeof type.SnapshotType`");
  1834. },
  1835. enumerable: true,
  1836. configurable: true
  1837. });
  1838. Object.defineProperty(BaseType.prototype, "CreationType", {
  1839. get: function () {
  1840. // istanbul ignore next
  1841. throw fail$1("Factory.CreationType should not be actually called. It is just a Type signature that can be used at compile time with Typescript, by using `typeof type.CreationType`");
  1842. },
  1843. enumerable: true,
  1844. configurable: true
  1845. });
  1846. __decorate([
  1847. mobx.action
  1848. ], BaseType.prototype, "create", null);
  1849. return BaseType;
  1850. }());
  1851. /**
  1852. * A complex type produces a MST node (Node in the state tree)
  1853. *
  1854. * @internal
  1855. * @hidden
  1856. */
  1857. var ComplexType = /** @class */ (function (_super) {
  1858. __extends(ComplexType, _super);
  1859. function ComplexType(name) {
  1860. return _super.call(this, name) || this;
  1861. }
  1862. ComplexType.prototype.create = function (snapshot, environment) {
  1863. if (snapshot === void 0) { snapshot = this.getDefaultSnapshot(); }
  1864. return _super.prototype.create.call(this, snapshot, environment);
  1865. };
  1866. ComplexType.prototype.getValue = function (node) {
  1867. node.createObservableInstanceIfNeeded();
  1868. return node.storedValue;
  1869. };
  1870. ComplexType.prototype.tryToReconcileNode = function (current, newValue) {
  1871. if (current.isDetaching)
  1872. return false;
  1873. if (current.snapshot === newValue) {
  1874. // newValue is the current snapshot of the node, noop
  1875. return true;
  1876. }
  1877. if (isStateTreeNode(newValue) && getStateTreeNode(newValue) === current) {
  1878. // the current node is the same as the new one
  1879. return true;
  1880. }
  1881. if (current.type === this &&
  1882. isMutable(newValue) &&
  1883. !isStateTreeNode(newValue) &&
  1884. (!current.identifierAttribute ||
  1885. current.identifier ===
  1886. normalizeIdentifier(newValue[current.identifierAttribute]))) {
  1887. // the newValue has no node, so can be treated like a snapshot
  1888. // we can reconcile
  1889. current.applySnapshot(newValue);
  1890. return true;
  1891. }
  1892. return false;
  1893. };
  1894. ComplexType.prototype.reconcile = function (current, newValue, parent, subpath) {
  1895. var nodeReconciled = this.tryToReconcileNode(current, newValue);
  1896. if (nodeReconciled) {
  1897. current.setParent(parent, subpath);
  1898. return current;
  1899. }
  1900. // current node cannot be recycled in any way
  1901. current.die(); // noop if detaching
  1902. // attempt to reuse the new one
  1903. if (isStateTreeNode(newValue) && this.isAssignableFrom(getType(newValue))) {
  1904. // newValue is a Node as well, move it here..
  1905. var newNode = getStateTreeNode(newValue);
  1906. newNode.setParent(parent, subpath);
  1907. return newNode;
  1908. }
  1909. // nothing to do, we have to create a new node
  1910. return this.instantiate(parent, subpath, undefined, newValue);
  1911. };
  1912. ComplexType.prototype.getSubTypes = function () {
  1913. return null;
  1914. };
  1915. __decorate([
  1916. mobx.action
  1917. ], ComplexType.prototype, "create", null);
  1918. return ComplexType;
  1919. }(BaseType));
  1920. /**
  1921. * @internal
  1922. * @hidden
  1923. */
  1924. var SimpleType = /** @class */ (function (_super) {
  1925. __extends(SimpleType, _super);
  1926. function SimpleType() {
  1927. return _super !== null && _super.apply(this, arguments) || this;
  1928. }
  1929. SimpleType.prototype.createNewInstance = function (snapshot) {
  1930. return snapshot;
  1931. };
  1932. SimpleType.prototype.getValue = function (node) {
  1933. // if we ever find a case where scalar nodes can be accessed without iterating through its parent
  1934. // uncomment this to make sure the parent chain is created when this is accessed
  1935. // if (node.parent) {
  1936. // node.parent.createObservableInstanceIfNeeded()
  1937. // }
  1938. return node.storedValue;
  1939. };
  1940. SimpleType.prototype.getSnapshot = function (node) {
  1941. return node.storedValue;
  1942. };
  1943. SimpleType.prototype.reconcile = function (current, newValue, parent, subpath) {
  1944. // reconcile only if type and value are still the same, and only if the node is not detaching
  1945. if (!current.isDetaching && current.type === this && current.storedValue === newValue) {
  1946. return current;
  1947. }
  1948. var res = this.instantiate(parent, subpath, undefined, newValue);
  1949. current.die(); // noop if detaching
  1950. return res;
  1951. };
  1952. SimpleType.prototype.getSubTypes = function () {
  1953. return null;
  1954. };
  1955. return SimpleType;
  1956. }(BaseType));
  1957. /**
  1958. * Returns if a given value represents a type.
  1959. *
  1960. * @param value Value to check.
  1961. * @returns `true` if the value is a type.
  1962. */
  1963. function isType(value) {
  1964. return typeof value === "object" && value && value.isType === true;
  1965. }
  1966. /**
  1967. * @internal
  1968. * @hidden
  1969. */
  1970. function assertIsType(type, argNumber) {
  1971. assertArg(type, isType, "mobx-state-tree type", argNumber);
  1972. }
  1973. var runningActions = new Map();
  1974. /**
  1975. * Note: Consider migrating to `createActionTrackingMiddleware2`, it is easier to use.
  1976. *
  1977. * Convenience utility to create action based middleware that supports async processes more easily.
  1978. * All hooks are called for both synchronous and asynchronous actions. Except that either `onSuccess` or `onFail` is called
  1979. *
  1980. * The create middleware tracks the process of an action (assuming it passes the `filter`).
  1981. * `onResume` can return any value, which will be passed as second argument to any other hook. This makes it possible to keep state during a process.
  1982. *
  1983. * See the `atomic` middleware for an example
  1984. *
  1985. * @param hooks
  1986. * @returns
  1987. */
  1988. function createActionTrackingMiddleware(hooks) {
  1989. return function actionTrackingMiddleware(call, next, abort) {
  1990. switch (call.type) {
  1991. case "action": {
  1992. if (!hooks.filter || hooks.filter(call) === true) {
  1993. var context = hooks.onStart(call);
  1994. hooks.onResume(call, context);
  1995. runningActions.set(call.id, {
  1996. call: call,
  1997. context: context,
  1998. async: false
  1999. });
  2000. try {
  2001. var res = next(call);
  2002. hooks.onSuspend(call, context);
  2003. if (runningActions.get(call.id).async === false) {
  2004. runningActions.delete(call.id);
  2005. hooks.onSuccess(call, context, res);
  2006. }
  2007. return res;
  2008. }
  2009. catch (e) {
  2010. runningActions.delete(call.id);
  2011. hooks.onFail(call, context, e);
  2012. throw e;
  2013. }
  2014. }
  2015. else {
  2016. return next(call);
  2017. }
  2018. }
  2019. case "flow_spawn": {
  2020. var root = runningActions.get(call.rootId);
  2021. root.async = true;
  2022. return next(call);
  2023. }
  2024. case "flow_resume":
  2025. case "flow_resume_error": {
  2026. var root = runningActions.get(call.rootId);
  2027. hooks.onResume(call, root.context);
  2028. try {
  2029. return next(call);
  2030. }
  2031. finally {
  2032. hooks.onSuspend(call, root.context);
  2033. }
  2034. }
  2035. case "flow_throw": {
  2036. var root = runningActions.get(call.rootId);
  2037. runningActions.delete(call.rootId);
  2038. hooks.onFail(call, root.context, call.args[0]);
  2039. return next(call);
  2040. }
  2041. case "flow_return": {
  2042. var root = runningActions.get(call.rootId);
  2043. runningActions.delete(call.rootId);
  2044. hooks.onSuccess(call, root.context, call.args[0]);
  2045. return next(call);
  2046. }
  2047. }
  2048. };
  2049. }
  2050. var RunningAction = /** @class */ (function () {
  2051. function RunningAction(hooks, call) {
  2052. this.hooks = hooks;
  2053. this.call = call;
  2054. this.flowsPending = 0;
  2055. this.running = true;
  2056. if (hooks) {
  2057. hooks.onStart(call);
  2058. }
  2059. }
  2060. RunningAction.prototype.finish = function (error) {
  2061. if (this.running) {
  2062. this.running = false;
  2063. if (this.hooks) {
  2064. this.hooks.onFinish(this.call, error);
  2065. }
  2066. }
  2067. };
  2068. RunningAction.prototype.incFlowsPending = function () {
  2069. this.flowsPending++;
  2070. };
  2071. RunningAction.prototype.decFlowsPending = function () {
  2072. this.flowsPending--;
  2073. };
  2074. Object.defineProperty(RunningAction.prototype, "hasFlowsPending", {
  2075. get: function () {
  2076. return this.flowsPending > 0;
  2077. },
  2078. enumerable: true,
  2079. configurable: true
  2080. });
  2081. return RunningAction;
  2082. }());
  2083. /**
  2084. * Convenience utility to create action based middleware that supports async processes more easily.
  2085. * The flow is like this:
  2086. * - for each action: if filter passes -> `onStart` -> (inner actions recursively) -> `onFinish`
  2087. *
  2088. * Example: if we had an action `a` that called inside an action `b1`, then `b2` the flow would be:
  2089. * - `filter(a)`
  2090. * - `onStart(a)`
  2091. * - `filter(b1)`
  2092. * - `onStart(b1)`
  2093. * - `onFinish(b1)`
  2094. * - `filter(b2)`
  2095. * - `onStart(b2)`
  2096. * - `onFinish(b2)`
  2097. * - `onFinish(a)`
  2098. *
  2099. * The flow is the same no matter if the actions are sync or async.
  2100. *
  2101. * See the `atomic` middleware for an example
  2102. *
  2103. * @param hooks
  2104. * @returns
  2105. */
  2106. function createActionTrackingMiddleware2(middlewareHooks) {
  2107. var runningActions = new WeakMap();
  2108. return function actionTrackingMiddleware(call, next) {
  2109. // find parentRunningAction
  2110. var parentRunningAction = call.parentActionEvent
  2111. ? runningActions.get(call.parentActionEvent)
  2112. : undefined;
  2113. if (call.type === "action") {
  2114. var newCall = __assign(__assign({}, call), {
  2115. // make a shallow copy of the parent action env
  2116. env: parentRunningAction && parentRunningAction.call.env, parentCall: parentRunningAction && parentRunningAction.call });
  2117. var passesFilter = !middlewareHooks.filter || middlewareHooks.filter(newCall);
  2118. var hooks = passesFilter ? middlewareHooks : undefined;
  2119. var runningAction = new RunningAction(hooks, newCall);
  2120. runningActions.set(call, runningAction);
  2121. var res = void 0;
  2122. try {
  2123. res = next(call);
  2124. }
  2125. catch (e) {
  2126. runningAction.finish(e);
  2127. throw e;
  2128. }
  2129. if (!runningAction.hasFlowsPending) {
  2130. // sync action finished
  2131. runningAction.finish();
  2132. }
  2133. return res;
  2134. }
  2135. else {
  2136. if (!parentRunningAction) {
  2137. return next(call);
  2138. }
  2139. switch (call.type) {
  2140. case "flow_spawn": {
  2141. parentRunningAction.incFlowsPending();
  2142. return next(call);
  2143. }
  2144. case "flow_resume":
  2145. case "flow_resume_error": {
  2146. return next(call);
  2147. }
  2148. case "flow_throw": {
  2149. var error = call.args[0];
  2150. try {
  2151. return next(call);
  2152. }
  2153. finally {
  2154. parentRunningAction.decFlowsPending();
  2155. if (!parentRunningAction.hasFlowsPending) {
  2156. parentRunningAction.finish(error);
  2157. }
  2158. }
  2159. }
  2160. case "flow_return": {
  2161. try {
  2162. return next(call);
  2163. }
  2164. finally {
  2165. parentRunningAction.decFlowsPending();
  2166. if (!parentRunningAction.hasFlowsPending) {
  2167. parentRunningAction.finish();
  2168. }
  2169. }
  2170. }
  2171. }
  2172. }
  2173. };
  2174. }
  2175. function serializeArgument(node, actionName, index, arg) {
  2176. if (arg instanceof Date)
  2177. return { $MST_DATE: arg.getTime() };
  2178. if (isPrimitive(arg))
  2179. return arg;
  2180. // We should not serialize MST nodes, even if we can, because we don't know if the receiving party can handle a raw snapshot instead of an
  2181. // MST type instance. So if one wants to serialize a MST node that was pass in, either explitly pass: 1: an id, 2: a (relative) path, 3: a snapshot
  2182. if (isStateTreeNode(arg))
  2183. return serializeTheUnserializable("[MSTNode: " + getType(arg).name + "]");
  2184. if (typeof arg === "function")
  2185. return serializeTheUnserializable("[function]");
  2186. if (typeof arg === "object" && !isPlainObject(arg) && !isArray(arg))
  2187. return serializeTheUnserializable("[object " + ((arg && arg.constructor && arg.constructor.name) ||
  2188. "Complex Object") + "]");
  2189. try {
  2190. // Check if serializable, cycle free etc...
  2191. // MWE: there must be a better way....
  2192. JSON.stringify(arg); // or throws
  2193. return arg;
  2194. }
  2195. catch (e) {
  2196. return serializeTheUnserializable("" + e);
  2197. }
  2198. }
  2199. function deserializeArgument(adm, value) {
  2200. if (value && typeof value === "object" && "$MST_DATE" in value)
  2201. return new Date(value["$MST_DATE"]);
  2202. return value;
  2203. }
  2204. function serializeTheUnserializable(baseType) {
  2205. return {
  2206. $MST_UNSERIALIZABLE: true,
  2207. type: baseType
  2208. };
  2209. }
  2210. /**
  2211. * Applies an action or a series of actions in a single MobX transaction.
  2212. * Does not return any value
  2213. * Takes an action description as produced by the `onAction` middleware.
  2214. *
  2215. * @param target
  2216. * @param actions
  2217. */
  2218. function applyAction(target, actions) {
  2219. // check all arguments
  2220. assertIsStateTreeNode(target, 1);
  2221. assertArg(actions, function (a) { return typeof a === "object"; }, "object or array", 2);
  2222. mobx.runInAction(function () {
  2223. asArray(actions).forEach(function (action) { return baseApplyAction(target, action); });
  2224. });
  2225. }
  2226. function baseApplyAction(target, action) {
  2227. var resolvedTarget = tryResolve(target, action.path || "");
  2228. if (!resolvedTarget)
  2229. throw fail$1("Invalid action path: " + (action.path || ""));
  2230. var node = getStateTreeNode(resolvedTarget);
  2231. // Reserved functions
  2232. if (action.name === "@APPLY_PATCHES") {
  2233. return applyPatch.call(null, resolvedTarget, action.args[0]);
  2234. }
  2235. if (action.name === "@APPLY_SNAPSHOT") {
  2236. return applySnapshot.call(null, resolvedTarget, action.args[0]);
  2237. }
  2238. if (!(typeof resolvedTarget[action.name] === "function"))
  2239. throw fail$1("Action '" + action.name + "' does not exist in '" + node.path + "'");
  2240. return resolvedTarget[action.name].apply(resolvedTarget, action.args ? action.args.map(function (v) { return deserializeArgument(node, v); }) : []);
  2241. }
  2242. /**
  2243. * Small abstraction around `onAction` and `applyAction`, attaches an action listener to a tree and records all the actions emitted.
  2244. * Returns an recorder object with the following signature:
  2245. *
  2246. * Example:
  2247. * ```ts
  2248. * export interface IActionRecorder {
  2249. * // the recorded actions
  2250. * actions: ISerializedActionCall[]
  2251. * // true if currently recording
  2252. * recording: boolean
  2253. * // stop recording actions
  2254. * stop(): void
  2255. * // resume recording actions
  2256. * resume(): void
  2257. * // apply all the recorded actions on the given object
  2258. * replay(target: IAnyStateTreeNode): void
  2259. * }
  2260. * ```
  2261. *
  2262. * The optional filter function allows to skip recording certain actions.
  2263. *
  2264. * @param subject
  2265. * @returns
  2266. */
  2267. function recordActions(subject, filter) {
  2268. // check all arguments
  2269. assertIsStateTreeNode(subject, 1);
  2270. var actions = [];
  2271. var listener = function (call) {
  2272. var recordThis = filter ? filter(call, getRunningActionContext()) : true;
  2273. if (recordThis) {
  2274. actions.push(call);
  2275. }
  2276. };
  2277. var disposer;
  2278. var recorder = {
  2279. actions: actions,
  2280. get recording() {
  2281. return !!disposer;
  2282. },
  2283. stop: function () {
  2284. if (disposer) {
  2285. disposer();
  2286. disposer = undefined;
  2287. }
  2288. },
  2289. resume: function () {
  2290. if (disposer)
  2291. return;
  2292. disposer = onAction(subject, listener);
  2293. },
  2294. replay: function (target) {
  2295. applyAction(target, actions);
  2296. }
  2297. };
  2298. recorder.resume();
  2299. return recorder;
  2300. }
  2301. /**
  2302. * Registers a function that will be invoked for each action that is called on the provided model instance, or to any of its children.
  2303. * See [actions](https://github.com/mobxjs/mobx-state-tree#actions) for more details. onAction events are emitted only for the outermost called action in the stack.
  2304. * Action can also be intercepted by middleware using addMiddleware to change the function call before it will be run.
  2305. *
  2306. * Not all action arguments might be serializable. For unserializable arguments, a struct like `{ $MST_UNSERIALIZABLE: true, type: "someType" }` will be generated.
  2307. * MST Nodes are considered non-serializable as well (they could be serialized as there snapshot, but it is uncertain whether an replaying party will be able to handle such a non-instantiated snapshot).
  2308. * Rather, when using `onAction` middleware, one should consider in passing arguments which are 1: an id, 2: a (relative) path, or 3: a snapshot. Instead of a real MST node.
  2309. *
  2310. * Example:
  2311. * ```ts
  2312. * const Todo = types.model({
  2313. * task: types.string
  2314. * })
  2315. *
  2316. * const TodoStore = types.model({
  2317. * todos: types.array(Todo)
  2318. * }).actions(self => ({
  2319. * add(todo) {
  2320. * self.todos.push(todo);
  2321. * }
  2322. * }))
  2323. *
  2324. * const s = TodoStore.create({ todos: [] })
  2325. *
  2326. * let disposer = onAction(s, (call) => {
  2327. * console.log(call);
  2328. * })
  2329. *
  2330. * s.add({ task: "Grab a coffee" })
  2331. * // Logs: { name: "add", path: "", args: [{ task: "Grab a coffee" }] }
  2332. * ```
  2333. *
  2334. * @param target
  2335. * @param listener
  2336. * @param attachAfter (default false) fires the listener *after* the action has executed instead of before.
  2337. * @returns
  2338. */
  2339. function onAction(target, listener, attachAfter) {
  2340. if (attachAfter === void 0) { attachAfter = false; }
  2341. // check all arguments
  2342. assertIsStateTreeNode(target, 1);
  2343. {
  2344. if (!isRoot(target))
  2345. warnError("Warning: Attaching onAction listeners to non root nodes is dangerous: No events will be emitted for actions initiated higher up in the tree.");
  2346. if (!isProtected(target))
  2347. warnError("Warning: Attaching onAction listeners to non protected nodes is dangerous: No events will be emitted for direct modifications without action.");
  2348. }
  2349. return addMiddleware(target, function handler(rawCall, next) {
  2350. if (rawCall.type === "action" && rawCall.id === rawCall.rootId) {
  2351. var sourceNode_1 = getStateTreeNode(rawCall.context);
  2352. var info = {
  2353. name: rawCall.name,
  2354. path: getRelativePathBetweenNodes(getStateTreeNode(target), sourceNode_1),
  2355. args: rawCall.args.map(function (arg, index) {
  2356. return serializeArgument(sourceNode_1, rawCall.name, index, arg);
  2357. })
  2358. };
  2359. if (attachAfter) {
  2360. var res = next(rawCall);
  2361. listener(info);
  2362. return res;
  2363. }
  2364. else {
  2365. listener(info);
  2366. return next(rawCall);
  2367. }
  2368. }
  2369. else {
  2370. return next(rawCall);
  2371. }
  2372. });
  2373. }
  2374. var nextActionId = 1;
  2375. var currentActionContext;
  2376. /**
  2377. * @internal
  2378. * @hidden
  2379. */
  2380. function getCurrentActionContext() {
  2381. return currentActionContext;
  2382. }
  2383. /**
  2384. * @internal
  2385. * @hidden
  2386. */
  2387. function getNextActionId() {
  2388. return nextActionId++;
  2389. }
  2390. // TODO: optimize away entire action context if there is no middleware in tree?
  2391. /**
  2392. * @internal
  2393. * @hidden
  2394. */
  2395. function runWithActionContext(context, fn) {
  2396. var node = getStateTreeNode(context.context);
  2397. if (context.type === "action") {
  2398. node.assertAlive({
  2399. actionContext: context
  2400. });
  2401. }
  2402. var baseIsRunningAction = node._isRunningAction;
  2403. node._isRunningAction = true;
  2404. var previousContext = currentActionContext;
  2405. currentActionContext = context;
  2406. try {
  2407. return runMiddleWares(node, context, fn);
  2408. }
  2409. finally {
  2410. currentActionContext = previousContext;
  2411. node._isRunningAction = baseIsRunningAction;
  2412. }
  2413. }
  2414. /**
  2415. * @internal
  2416. * @hidden
  2417. */
  2418. function getParentActionContext(parentContext) {
  2419. if (!parentContext)
  2420. return undefined;
  2421. if (parentContext.type === "action")
  2422. return parentContext;
  2423. return parentContext.parentActionEvent;
  2424. }
  2425. /**
  2426. * @internal
  2427. * @hidden
  2428. */
  2429. function createActionInvoker(target, name, fn) {
  2430. var res = function () {
  2431. var id = getNextActionId();
  2432. var parentContext = currentActionContext;
  2433. var parentActionContext = getParentActionContext(parentContext);
  2434. return runWithActionContext({
  2435. type: "action",
  2436. name: name,
  2437. id: id,
  2438. args: argsToArray(arguments),
  2439. context: target,
  2440. tree: getRoot(target),
  2441. rootId: parentContext ? parentContext.rootId : id,
  2442. parentId: parentContext ? parentContext.id : 0,
  2443. allParentIds: parentContext
  2444. ? __spread(parentContext.allParentIds, [parentContext.id]) : [],
  2445. parentEvent: parentContext,
  2446. parentActionEvent: parentActionContext
  2447. }, fn);
  2448. };
  2449. res._isMSTAction = true;
  2450. return res;
  2451. }
  2452. /**
  2453. * Middleware can be used to intercept any action is invoked on the subtree where it is attached.
  2454. * If a tree is protected (by default), this means that any mutation of the tree will pass through your middleware.
  2455. *
  2456. * For more details, see the [middleware docs](../middleware.md)
  2457. *
  2458. * @param target Node to apply the middleware to.
  2459. * @param middleware Middleware to apply.
  2460. * @returns A callable function to dispose the middleware.
  2461. */
  2462. function addMiddleware(target, handler, includeHooks) {
  2463. if (includeHooks === void 0) { includeHooks = true; }
  2464. var node = getStateTreeNode(target);
  2465. {
  2466. if (!node.isProtectionEnabled) {
  2467. warnError("It is recommended to protect the state tree before attaching action middleware, as otherwise it cannot be guaranteed that all changes are passed through middleware. See `protect`");
  2468. }
  2469. }
  2470. return node.addMiddleWare(handler, includeHooks);
  2471. }
  2472. /**
  2473. * Binds middleware to a specific action.
  2474. *
  2475. * Example:
  2476. * ```ts
  2477. * type.actions(self => {
  2478. * function takeA____() {
  2479. * self.toilet.donate()
  2480. * self.wipe()
  2481. * self.wipe()
  2482. * self.toilet.flush()
  2483. * }
  2484. * return {
  2485. * takeA____: decorate(atomic, takeA____)
  2486. * }
  2487. * })
  2488. * ```
  2489. *
  2490. * @param handler
  2491. * @param fn
  2492. * @param includeHooks
  2493. * @returns The original function
  2494. */
  2495. function decorate(handler, fn, includeHooks) {
  2496. if (includeHooks === void 0) { includeHooks = true; }
  2497. var middleware = { handler: handler, includeHooks: includeHooks };
  2498. fn.$mst_middleware = fn.$mst_middleware || [];
  2499. fn.$mst_middleware.push(middleware);
  2500. return fn;
  2501. }
  2502. var CollectedMiddlewares = /** @class */ (function () {
  2503. function CollectedMiddlewares(node, fn) {
  2504. this.arrayIndex = 0;
  2505. this.inArrayIndex = 0;
  2506. this.middlewares = [];
  2507. // we just push middleware arrays into an array of arrays to avoid making copies
  2508. if (fn.$mst_middleware) {
  2509. this.middlewares.push(fn.$mst_middleware);
  2510. }
  2511. var n = node;
  2512. // Find all middlewares. Optimization: cache this?
  2513. while (n) {
  2514. if (n.middlewares)
  2515. this.middlewares.push(n.middlewares);
  2516. n = n.parent;
  2517. }
  2518. }
  2519. Object.defineProperty(CollectedMiddlewares.prototype, "isEmpty", {
  2520. get: function () {
  2521. return this.middlewares.length <= 0;
  2522. },
  2523. enumerable: true,
  2524. configurable: true
  2525. });
  2526. CollectedMiddlewares.prototype.getNextMiddleware = function () {
  2527. var array = this.middlewares[this.arrayIndex];
  2528. if (!array)
  2529. return undefined;
  2530. var item = array[this.inArrayIndex++];
  2531. if (!item) {
  2532. this.arrayIndex++;
  2533. this.inArrayIndex = 0;
  2534. return this.getNextMiddleware();
  2535. }
  2536. return item;
  2537. };
  2538. return CollectedMiddlewares;
  2539. }());
  2540. function runMiddleWares(node, baseCall, originalFn) {
  2541. var middlewares = new CollectedMiddlewares(node, originalFn);
  2542. // Short circuit
  2543. if (middlewares.isEmpty)
  2544. return mobx.action(originalFn).apply(null, baseCall.args);
  2545. var result = null;
  2546. function runNextMiddleware(call) {
  2547. var middleware = middlewares.getNextMiddleware();
  2548. var handler = middleware && middleware.handler;
  2549. if (!handler) {
  2550. return mobx.action(originalFn).apply(null, call.args);
  2551. }
  2552. // skip hooks if asked to
  2553. if (!middleware.includeHooks && Hook[call.name]) {
  2554. return runNextMiddleware(call);
  2555. }
  2556. var nextInvoked = false;
  2557. function next(call2, callback) {
  2558. nextInvoked = true;
  2559. // the result can contain
  2560. // - the non manipulated return value from an action
  2561. // - the non manipulated abort value
  2562. // - one of the above but manipulated through the callback function
  2563. result = runNextMiddleware(call2);
  2564. if (callback) {
  2565. result = callback(result);
  2566. }
  2567. }
  2568. var abortInvoked = false;
  2569. function abort(value) {
  2570. abortInvoked = true;
  2571. // overwrite the result
  2572. // can be manipulated through middlewares earlier in the queue using the callback fn
  2573. result = value;
  2574. }
  2575. handler(call, next, abort);
  2576. {
  2577. if (!nextInvoked && !abortInvoked) {
  2578. var node2 = getStateTreeNode(call.tree);
  2579. throw fail$1("Neither the next() nor the abort() callback within the middleware " + handler.name + " for the action: \"" + call.name + "\" on the node: " + node2.type.name + " was invoked.");
  2580. }
  2581. else if (nextInvoked && abortInvoked) {
  2582. var node2 = getStateTreeNode(call.tree);
  2583. throw fail$1("The next() and abort() callback within the middleware " + handler.name + " for the action: \"" + call.name + "\" on the node: " + node2.type.name + " were invoked.");
  2584. }
  2585. }
  2586. return result;
  2587. }
  2588. return runNextMiddleware(baseCall);
  2589. }
  2590. /**
  2591. * Returns the currently executing MST action context, or undefined if none.
  2592. */
  2593. function getRunningActionContext() {
  2594. var current = getCurrentActionContext();
  2595. while (current && current.type !== "action") {
  2596. current = current.parentActionEvent;
  2597. }
  2598. return current;
  2599. }
  2600. function _isActionContextThisOrChildOf(actionContext, sameOrParent, includeSame) {
  2601. var parentId = typeof sameOrParent === "number" ? sameOrParent : sameOrParent.id;
  2602. var current = includeSame
  2603. ? actionContext
  2604. : actionContext.parentActionEvent;
  2605. while (current) {
  2606. if (current.id === parentId) {
  2607. return true;
  2608. }
  2609. current = current.parentActionEvent;
  2610. }
  2611. return false;
  2612. }
  2613. /**
  2614. * Returns if the given action context is a parent of this action context.
  2615. */
  2616. function isActionContextChildOf(actionContext, parent) {
  2617. return _isActionContextThisOrChildOf(actionContext, parent, false);
  2618. }
  2619. /**
  2620. * Returns if the given action context is this or a parent of this action context.
  2621. */
  2622. function isActionContextThisOrChildOf(actionContext, parentOrThis) {
  2623. return _isActionContextThisOrChildOf(actionContext, parentOrThis, true);
  2624. }
  2625. function safeStringify(value) {
  2626. try {
  2627. return JSON.stringify(value);
  2628. }
  2629. catch (e) {
  2630. // istanbul ignore next
  2631. return "<Unserializable: " + e + ">";
  2632. }
  2633. }
  2634. /**
  2635. * @internal
  2636. * @hidden
  2637. */
  2638. function prettyPrintValue(value) {
  2639. return typeof value === "function"
  2640. ? "<function" + (value.name ? " " + value.name : "") + ">"
  2641. : isStateTreeNode(value)
  2642. ? "<" + value + ">"
  2643. : "`" + safeStringify(value) + "`";
  2644. }
  2645. function shortenPrintValue(valueInString) {
  2646. return valueInString.length < 280
  2647. ? valueInString
  2648. : valueInString.substring(0, 272) + "......" + valueInString.substring(valueInString.length - 8);
  2649. }
  2650. function toErrorString(error) {
  2651. var value = error.value;
  2652. var type = error.context[error.context.length - 1].type;
  2653. var fullPath = error.context
  2654. .map(function (_a) {
  2655. var path = _a.path;
  2656. return path;
  2657. })
  2658. .filter(function (path) { return path.length > 0; })
  2659. .join("/");
  2660. var pathPrefix = fullPath.length > 0 ? "at path \"/" + fullPath + "\" " : "";
  2661. var currentTypename = isStateTreeNode(value)
  2662. ? "value of type " + getStateTreeNode(value).type.name + ":"
  2663. : isPrimitive(value)
  2664. ? "value"
  2665. : "snapshot";
  2666. var isSnapshotCompatible = type && isStateTreeNode(value) && type.is(getStateTreeNode(value).snapshot);
  2667. return ("" + pathPrefix + currentTypename + " " + prettyPrintValue(value) + " is not assignable " + (type ? "to type: `" + type.name + "`" : "") +
  2668. (error.message ? " (" + error.message + ")" : "") +
  2669. (type
  2670. ? isPrimitiveType(type) || isPrimitive(value)
  2671. ? "."
  2672. : ", expected an instance of `" + type.name + "` or a snapshot like `" + type.describe() + "` instead." +
  2673. (isSnapshotCompatible
  2674. ? " (Note that a snapshot of the provided value is compatible with the targeted type)"
  2675. : "")
  2676. : "."));
  2677. }
  2678. /**
  2679. * @internal
  2680. * @hidden
  2681. */
  2682. function getContextForPath(context, path, type) {
  2683. return context.concat([{ path: path, type: type }]);
  2684. }
  2685. /**
  2686. * @internal
  2687. * @hidden
  2688. */
  2689. function typeCheckSuccess() {
  2690. return EMPTY_ARRAY;
  2691. }
  2692. /**
  2693. * @internal
  2694. * @hidden
  2695. */
  2696. function typeCheckFailure(context, value, message) {
  2697. return [{ context: context, value: value, message: message }];
  2698. }
  2699. /**
  2700. * @internal
  2701. * @hidden
  2702. */
  2703. function flattenTypeErrors(errors) {
  2704. return errors.reduce(function (a, i) { return a.concat(i); }, []);
  2705. }
  2706. // TODO; doublecheck: typecheck should only needed to be invoked from: type.create and array / map / value.property will change
  2707. /**
  2708. * @internal
  2709. * @hidden
  2710. */
  2711. function typecheckInternal(type, value) {
  2712. // runs typeChecking if it is in dev-mode or through a process.env.ENABLE_TYPE_CHECK flag
  2713. {
  2714. typecheck(type, value);
  2715. }
  2716. }
  2717. /**
  2718. * Run's the typechecker for the given type on the given value, which can be a snapshot or an instance.
  2719. * Throws if the given value is not according the provided type specification.
  2720. * Use this if you need typechecks even in a production build (by default all automatic runtime type checks will be skipped in production builds)
  2721. *
  2722. * @param type Type to check against.
  2723. * @param value Value to be checked, either a snapshot or an instance.
  2724. */
  2725. function typecheck(type, value) {
  2726. var errors = type.validate(value, [{ path: "", type: type }]);
  2727. if (errors.length > 0) {
  2728. throw fail$1(validationErrorsToString(type, value, errors));
  2729. }
  2730. }
  2731. function validationErrorsToString(type, value, errors) {
  2732. if (errors.length === 0) {
  2733. return undefined;
  2734. }
  2735. return ("Error while converting " + shortenPrintValue(prettyPrintValue(value)) + " to `" + type.name + "`:\n\n " + errors.map(toErrorString).join("\n "));
  2736. }
  2737. var identifierCacheId = 0;
  2738. /**
  2739. * @internal
  2740. * @hidden
  2741. */
  2742. var IdentifierCache = /** @class */ (function () {
  2743. function IdentifierCache() {
  2744. this.cacheId = identifierCacheId++;
  2745. // n.b. in cache all identifiers are normalized to strings
  2746. this.cache = mobx.observable.map();
  2747. // last time the cache (array) for a given time changed
  2748. // n.b. it is not really the time, but just an integer that gets increased after each modification to the array
  2749. this.lastCacheModificationPerId = mobx.observable.map();
  2750. }
  2751. IdentifierCache.prototype.updateLastCacheModificationPerId = function (identifier) {
  2752. var lcm = this.lastCacheModificationPerId.get(identifier);
  2753. // we start at 1 since 0 means no update since cache creation
  2754. this.lastCacheModificationPerId.set(identifier, lcm === undefined ? 1 : lcm + 1);
  2755. };
  2756. IdentifierCache.prototype.getLastCacheModificationPerId = function (identifier) {
  2757. var modificationId = this.lastCacheModificationPerId.get(identifier) || 0;
  2758. return this.cacheId + "-" + modificationId;
  2759. };
  2760. IdentifierCache.prototype.addNodeToCache = function (node, lastCacheUpdate) {
  2761. if (lastCacheUpdate === void 0) { lastCacheUpdate = true; }
  2762. if (node.identifierAttribute) {
  2763. var identifier = node.identifier;
  2764. if (!this.cache.has(identifier)) {
  2765. this.cache.set(identifier, mobx.observable.array([], mobxShallow));
  2766. }
  2767. var set = this.cache.get(identifier);
  2768. if (set.indexOf(node) !== -1)
  2769. throw fail$1("Already registered");
  2770. set.push(node);
  2771. if (lastCacheUpdate) {
  2772. this.updateLastCacheModificationPerId(identifier);
  2773. }
  2774. }
  2775. };
  2776. IdentifierCache.prototype.mergeCache = function (node) {
  2777. var _this = this;
  2778. mobx.values(node.identifierCache.cache).forEach(function (nodes) {
  2779. return nodes.forEach(function (child) {
  2780. _this.addNodeToCache(child);
  2781. });
  2782. });
  2783. };
  2784. IdentifierCache.prototype.notifyDied = function (node) {
  2785. if (node.identifierAttribute) {
  2786. var id = node.identifier;
  2787. var set = this.cache.get(id);
  2788. if (set) {
  2789. set.remove(node);
  2790. // remove empty sets from cache
  2791. if (!set.length) {
  2792. this.cache.delete(id);
  2793. }
  2794. this.updateLastCacheModificationPerId(node.identifier);
  2795. }
  2796. }
  2797. };
  2798. IdentifierCache.prototype.splitCache = function (node) {
  2799. var _this = this;
  2800. var res = new IdentifierCache();
  2801. var basePath = node.path;
  2802. mobx.entries(this.cache).forEach(function (_a) {
  2803. var _b = __read(_a, 2), id = _b[0], nodes = _b[1];
  2804. var modified = false;
  2805. for (var i = nodes.length - 1; i >= 0; i--) {
  2806. if (nodes[i].path.indexOf(basePath) === 0) {
  2807. res.addNodeToCache(nodes[i], false); // no need to update lastUpdated since it is a whole new cache
  2808. nodes.splice(i, 1);
  2809. modified = true;
  2810. }
  2811. }
  2812. if (modified) {
  2813. _this.updateLastCacheModificationPerId(id);
  2814. }
  2815. });
  2816. return res;
  2817. };
  2818. IdentifierCache.prototype.has = function (type, identifier) {
  2819. var set = this.cache.get(identifier);
  2820. if (!set)
  2821. return false;
  2822. return set.some(function (candidate) { return type.isAssignableFrom(candidate.type); });
  2823. };
  2824. IdentifierCache.prototype.resolve = function (type, identifier) {
  2825. var set = this.cache.get(identifier);
  2826. if (!set)
  2827. return null;
  2828. var matches = set.filter(function (candidate) { return type.isAssignableFrom(candidate.type); });
  2829. switch (matches.length) {
  2830. case 0:
  2831. return null;
  2832. case 1:
  2833. return matches[0];
  2834. default:
  2835. throw fail$1("Cannot resolve a reference to type '" + type.name + "' with id: '" + identifier + "' unambigously, there are multiple candidates: " + matches
  2836. .map(function (n) { return n.path; })
  2837. .join(", "));
  2838. }
  2839. };
  2840. return IdentifierCache;
  2841. }());
  2842. /**
  2843. * @internal
  2844. * @hidden
  2845. */
  2846. function createObjectNode(type, parent, subpath, environment, initialValue) {
  2847. var existingNode = getStateTreeNodeSafe(initialValue);
  2848. if (existingNode) {
  2849. if (existingNode.parent) {
  2850. // istanbul ignore next
  2851. throw fail$1("Cannot add an object to a state tree if it is already part of the same or another state tree. Tried to assign an object to '" + (parent ? parent.path : "") + "/" + subpath + "', but it lives already at '" + existingNode.path + "'");
  2852. }
  2853. if (parent) {
  2854. existingNode.setParent(parent, subpath);
  2855. }
  2856. // else it already has no parent since it is a pre-requisite
  2857. return existingNode;
  2858. }
  2859. // not a node, a snapshot
  2860. return new ObjectNode(type, parent, subpath, environment, initialValue);
  2861. }
  2862. /**
  2863. * @internal
  2864. * @hidden
  2865. */
  2866. function createScalarNode(type, parent, subpath, environment, initialValue) {
  2867. return new ScalarNode(type, parent, subpath, environment, initialValue);
  2868. }
  2869. /**
  2870. * @internal
  2871. * @hidden
  2872. */
  2873. function isNode(value) {
  2874. return value instanceof ScalarNode || value instanceof ObjectNode;
  2875. }
  2876. /**
  2877. * @internal
  2878. * @hidden
  2879. */
  2880. var NodeLifeCycle;
  2881. (function (NodeLifeCycle) {
  2882. NodeLifeCycle[NodeLifeCycle["INITIALIZING"] = 0] = "INITIALIZING";
  2883. NodeLifeCycle[NodeLifeCycle["CREATED"] = 1] = "CREATED";
  2884. NodeLifeCycle[NodeLifeCycle["FINALIZED"] = 2] = "FINALIZED";
  2885. NodeLifeCycle[NodeLifeCycle["DETACHING"] = 3] = "DETACHING";
  2886. NodeLifeCycle[NodeLifeCycle["DEAD"] = 4] = "DEAD"; // no coming back from this one
  2887. })(NodeLifeCycle || (NodeLifeCycle = {}));
  2888. /**
  2889. * Returns true if the given value is a node in a state tree.
  2890. * More precisely, that is, if the value is an instance of a
  2891. * `types.model`, `types.array` or `types.map`.
  2892. *
  2893. * @param value
  2894. * @returns true if the value is a state tree node.
  2895. */
  2896. function isStateTreeNode(value) {
  2897. return !!(value && value.$treenode);
  2898. }
  2899. /**
  2900. * @internal
  2901. * @hidden
  2902. */
  2903. function assertIsStateTreeNode(value, argNumber) {
  2904. assertArg(value, isStateTreeNode, "mobx-state-tree node", argNumber);
  2905. }
  2906. /**
  2907. * @internal
  2908. * @hidden
  2909. */
  2910. function getStateTreeNode(value) {
  2911. if (!isStateTreeNode(value)) {
  2912. // istanbul ignore next
  2913. throw fail$1("Value " + value + " is no MST Node");
  2914. }
  2915. return value.$treenode;
  2916. }
  2917. /**
  2918. * @internal
  2919. * @hidden
  2920. */
  2921. function getStateTreeNodeSafe(value) {
  2922. return (value && value.$treenode) || null;
  2923. }
  2924. /**
  2925. * @internal
  2926. * @hidden
  2927. */
  2928. function toJSON() {
  2929. return getStateTreeNode(this).snapshot;
  2930. }
  2931. var doubleDot = function (_) { return ".."; };
  2932. /**
  2933. * @internal
  2934. * @hidden
  2935. */
  2936. function getRelativePathBetweenNodes(base, target) {
  2937. // PRE condition target is (a child of) base!
  2938. if (base.root !== target.root) {
  2939. throw fail$1("Cannot calculate relative path: objects '" + base + "' and '" + target + "' are not part of the same object tree");
  2940. }
  2941. var baseParts = splitJsonPath(base.path);
  2942. var targetParts = splitJsonPath(target.path);
  2943. var common = 0;
  2944. for (; common < baseParts.length; common++) {
  2945. if (baseParts[common] !== targetParts[common])
  2946. break;
  2947. }
  2948. // TODO: assert that no targetParts paths are "..", "." or ""!
  2949. return (baseParts
  2950. .slice(common)
  2951. .map(doubleDot)
  2952. .join("/") + joinJsonPath(targetParts.slice(common)));
  2953. }
  2954. /**
  2955. * @internal
  2956. * @hidden
  2957. */
  2958. function resolveNodeByPath(base, path, failIfResolveFails) {
  2959. if (failIfResolveFails === void 0) { failIfResolveFails = true; }
  2960. return resolveNodeByPathParts(base, splitJsonPath(path), failIfResolveFails);
  2961. }
  2962. /**
  2963. * @internal
  2964. * @hidden
  2965. */
  2966. function resolveNodeByPathParts(base, pathParts, failIfResolveFails) {
  2967. if (failIfResolveFails === void 0) { failIfResolveFails = true; }
  2968. var current = base;
  2969. for (var i = 0; i < pathParts.length; i++) {
  2970. var part = pathParts[i];
  2971. if (part === "..") {
  2972. current = current.parent;
  2973. if (current)
  2974. continue; // not everything has a parent
  2975. }
  2976. else if (part === ".") {
  2977. continue;
  2978. }
  2979. else if (current) {
  2980. if (current instanceof ScalarNode) {
  2981. // check if the value of a scalar resolves to a state tree node (e.g. references)
  2982. // then we can continue resolving...
  2983. try {
  2984. var value = current.value;
  2985. if (isStateTreeNode(value)) {
  2986. current = getStateTreeNode(value);
  2987. // fall through
  2988. }
  2989. }
  2990. catch (e) {
  2991. if (!failIfResolveFails) {
  2992. return undefined;
  2993. }
  2994. throw e;
  2995. }
  2996. }
  2997. if (current instanceof ObjectNode) {
  2998. var subType = current.getChildType(part);
  2999. if (subType) {
  3000. current = current.getChildNode(part);
  3001. if (current)
  3002. continue;
  3003. }
  3004. }
  3005. }
  3006. if (failIfResolveFails)
  3007. throw fail$1("Could not resolve '" + part + "' in path '" + (joinJsonPath(pathParts.slice(0, i)) ||
  3008. "/") + "' while resolving '" + joinJsonPath(pathParts) + "'");
  3009. else
  3010. return undefined;
  3011. }
  3012. return current;
  3013. }
  3014. /**
  3015. * @internal
  3016. * @hidden
  3017. */
  3018. function convertChildNodesToArray(childNodes) {
  3019. if (!childNodes)
  3020. return EMPTY_ARRAY;
  3021. var keys = Object.keys(childNodes);
  3022. if (!keys.length)
  3023. return EMPTY_ARRAY;
  3024. var result = new Array(keys.length);
  3025. keys.forEach(function (key, index) {
  3026. result[index] = childNodes[key];
  3027. });
  3028. return result;
  3029. }
  3030. // based on: https://github.com/mobxjs/mobx-utils/blob/master/src/async-action.ts
  3031. /*
  3032. All contents of this file are deprecated.
  3033. The term `process` has been replaced with `flow` to avoid conflicts with the
  3034. global `process` object.
  3035. Refer to `flow.ts` for any further changes to this implementation.
  3036. */
  3037. var DEPRECATION_MESSAGE = "See https://github.com/mobxjs/mobx-state-tree/issues/399 for more information. " +
  3038. "Note that the middleware event types starting with `process` now start with `flow`.";
  3039. /**
  3040. * @hidden
  3041. *
  3042. * @deprecated has been renamed to `flow()`.
  3043. * See https://github.com/mobxjs/mobx-state-tree/issues/399 for more information.
  3044. * Note that the middleware event types starting with `process` now start with `flow`.
  3045. *
  3046. * @returns {Promise}
  3047. */
  3048. function process(asyncAction) {
  3049. deprecated("process", "`process()` has been renamed to `flow()`. " + DEPRECATION_MESSAGE);
  3050. return flow(asyncAction);
  3051. }
  3052. /**
  3053. * @internal
  3054. * @hidden
  3055. */
  3056. var EMPTY_ARRAY = Object.freeze([]);
  3057. /**
  3058. * @internal
  3059. * @hidden
  3060. */
  3061. var EMPTY_OBJECT = Object.freeze({});
  3062. /**
  3063. * @internal
  3064. * @hidden
  3065. */
  3066. var mobxShallow = typeof mobx.$mobx === "string" ? { deep: false } : { deep: false, proxy: false };
  3067. Object.freeze(mobxShallow);
  3068. /**
  3069. * @internal
  3070. * @hidden
  3071. */
  3072. function fail$1(message) {
  3073. if (message === void 0) { message = "Illegal state"; }
  3074. return new Error("[mobx-state-tree] " + message);
  3075. }
  3076. /**
  3077. * @internal
  3078. * @hidden
  3079. */
  3080. function identity(_) {
  3081. return _;
  3082. }
  3083. /**
  3084. * pollyfill (for IE) suggested in MDN:
  3085. * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger
  3086. * @internal
  3087. * @hidden
  3088. */
  3089. var isInteger = Number.isInteger ||
  3090. function (value) {
  3091. return typeof value === "number" && isFinite(value) && Math.floor(value) === value;
  3092. };
  3093. /**
  3094. * @internal
  3095. * @hidden
  3096. */
  3097. function isArray(val) {
  3098. return Array.isArray(val) || mobx.isObservableArray(val);
  3099. }
  3100. /**
  3101. * @internal
  3102. * @hidden
  3103. */
  3104. function asArray(val) {
  3105. if (!val)
  3106. return EMPTY_ARRAY;
  3107. if (isArray(val))
  3108. return val;
  3109. return [val];
  3110. }
  3111. /**
  3112. * @internal
  3113. * @hidden
  3114. */
  3115. function extend(a) {
  3116. var b = [];
  3117. for (var _i = 1; _i < arguments.length; _i++) {
  3118. b[_i - 1] = arguments[_i];
  3119. }
  3120. for (var i = 0; i < b.length; i++) {
  3121. var current = b[i];
  3122. for (var key in current)
  3123. a[key] = current[key];
  3124. }
  3125. return a;
  3126. }
  3127. /**
  3128. * @internal
  3129. * @hidden
  3130. */
  3131. function isPlainObject(value) {
  3132. if (value === null || typeof value !== "object")
  3133. return false;
  3134. var proto = Object.getPrototypeOf(value);
  3135. return proto === Object.prototype || proto === null;
  3136. }
  3137. /**
  3138. * @internal
  3139. * @hidden
  3140. */
  3141. function isMutable(value) {
  3142. return (value !== null &&
  3143. typeof value === "object" &&
  3144. !(value instanceof Date) &&
  3145. !(value instanceof RegExp));
  3146. }
  3147. /**
  3148. * @internal
  3149. * @hidden
  3150. */
  3151. function isPrimitive(value, includeDate) {
  3152. if (includeDate === void 0) { includeDate = true; }
  3153. if (value === null || value === undefined)
  3154. return true;
  3155. if (typeof value === "string" ||
  3156. typeof value === "number" ||
  3157. typeof value === "boolean" ||
  3158. (includeDate && value instanceof Date))
  3159. return true;
  3160. return false;
  3161. }
  3162. /**
  3163. * @internal
  3164. * @hidden
  3165. * Freeze a value and return it (if not in production)
  3166. */
  3167. function freeze(value) {
  3168. return isPrimitive(value) || mobx.isObservableArray(value) ? value : Object.freeze(value);
  3169. }
  3170. /**
  3171. * @internal
  3172. * @hidden
  3173. * Recursively freeze a value (if not in production)
  3174. */
  3175. function deepFreeze(value) {
  3176. freeze(value);
  3177. if (isPlainObject(value)) {
  3178. Object.keys(value).forEach(function (propKey) {
  3179. if (!isPrimitive(value[propKey]) &&
  3180. !Object.isFrozen(value[propKey])) {
  3181. deepFreeze(value[propKey]);
  3182. }
  3183. });
  3184. }
  3185. return value;
  3186. }
  3187. /**
  3188. * @internal
  3189. * @hidden
  3190. */
  3191. function isSerializable(value) {
  3192. return typeof value !== "function";
  3193. }
  3194. /**
  3195. * @internal
  3196. * @hidden
  3197. */
  3198. function addHiddenFinalProp(object, propName, value) {
  3199. Object.defineProperty(object, propName, {
  3200. enumerable: false,
  3201. writable: false,
  3202. configurable: true,
  3203. value: value
  3204. });
  3205. }
  3206. /**
  3207. * @internal
  3208. * @hidden
  3209. */
  3210. function addHiddenWritableProp(object, propName, value) {
  3211. Object.defineProperty(object, propName, {
  3212. enumerable: false,
  3213. writable: true,
  3214. configurable: true,
  3215. value: value
  3216. });
  3217. }
  3218. /**
  3219. * @internal
  3220. * @hidden
  3221. */
  3222. var EventHandler = /** @class */ (function () {
  3223. function EventHandler() {
  3224. this.handlers = [];
  3225. }
  3226. Object.defineProperty(EventHandler.prototype, "hasSubscribers", {
  3227. get: function () {
  3228. return this.handlers.length > 0;
  3229. },
  3230. enumerable: true,
  3231. configurable: true
  3232. });
  3233. EventHandler.prototype.register = function (fn, atTheBeginning) {
  3234. var _this = this;
  3235. if (atTheBeginning === void 0) { atTheBeginning = false; }
  3236. if (atTheBeginning) {
  3237. this.handlers.unshift(fn);
  3238. }
  3239. else {
  3240. this.handlers.push(fn);
  3241. }
  3242. return function () {
  3243. _this.unregister(fn);
  3244. };
  3245. };
  3246. EventHandler.prototype.has = function (fn) {
  3247. return this.handlers.indexOf(fn) >= 0;
  3248. };
  3249. EventHandler.prototype.unregister = function (fn) {
  3250. var index = this.handlers.indexOf(fn);
  3251. if (index >= 0) {
  3252. this.handlers.splice(index, 1);
  3253. }
  3254. };
  3255. EventHandler.prototype.clear = function () {
  3256. this.handlers.length = 0;
  3257. };
  3258. EventHandler.prototype.emit = function () {
  3259. var args = [];
  3260. for (var _i = 0; _i < arguments.length; _i++) {
  3261. args[_i] = arguments[_i];
  3262. }
  3263. // make a copy just in case it changes
  3264. var handlers = this.handlers.slice();
  3265. handlers.forEach(function (f) { return f.apply(void 0, __spread(args)); });
  3266. };
  3267. return EventHandler;
  3268. }());
  3269. /**
  3270. * @internal
  3271. * @hidden
  3272. */
  3273. var EventHandlers = /** @class */ (function () {
  3274. function EventHandlers() {
  3275. }
  3276. EventHandlers.prototype.hasSubscribers = function (event) {
  3277. var handler = this.eventHandlers && this.eventHandlers[event];
  3278. return !!handler && handler.hasSubscribers;
  3279. };
  3280. EventHandlers.prototype.register = function (event, fn, atTheBeginning) {
  3281. if (atTheBeginning === void 0) { atTheBeginning = false; }
  3282. if (!this.eventHandlers) {
  3283. this.eventHandlers = {};
  3284. }
  3285. var handler = this.eventHandlers[event];
  3286. if (!handler) {
  3287. handler = this.eventHandlers[event] = new EventHandler();
  3288. }
  3289. return handler.register(fn, atTheBeginning);
  3290. };
  3291. EventHandlers.prototype.has = function (event, fn) {
  3292. var handler = this.eventHandlers && this.eventHandlers[event];
  3293. return !!handler && handler.has(fn);
  3294. };
  3295. EventHandlers.prototype.unregister = function (event, fn) {
  3296. var handler = this.eventHandlers && this.eventHandlers[event];
  3297. if (handler) {
  3298. handler.unregister(fn);
  3299. }
  3300. };
  3301. EventHandlers.prototype.clear = function (event) {
  3302. if (this.eventHandlers) {
  3303. delete this.eventHandlers[event];
  3304. }
  3305. };
  3306. EventHandlers.prototype.clearAll = function () {
  3307. this.eventHandlers = undefined;
  3308. };
  3309. EventHandlers.prototype.emit = function (event) {
  3310. var _a;
  3311. var args = [];
  3312. for (var _i = 1; _i < arguments.length; _i++) {
  3313. args[_i - 1] = arguments[_i];
  3314. }
  3315. var handler = this.eventHandlers && this.eventHandlers[event];
  3316. if (handler) {
  3317. (_a = handler).emit.apply(_a, __spread(args));
  3318. }
  3319. };
  3320. return EventHandlers;
  3321. }());
  3322. /**
  3323. * @internal
  3324. * @hidden
  3325. */
  3326. function argsToArray(args) {
  3327. var res = new Array(args.length);
  3328. for (var i = 0; i < args.length; i++)
  3329. res[i] = args[i];
  3330. return res;
  3331. }
  3332. /**
  3333. * @internal
  3334. * @hidden
  3335. */
  3336. function invalidateComputed(target, propName) {
  3337. var atom = mobx.getAtom(target, propName);
  3338. atom.trackAndCompute();
  3339. }
  3340. /**
  3341. * @internal
  3342. * @hidden
  3343. */
  3344. function stringStartsWith(str, beginning) {
  3345. return str.indexOf(beginning) === 0;
  3346. }
  3347. /**
  3348. * @internal
  3349. * @hidden
  3350. */
  3351. var deprecated = function (id, message) {
  3352. // warn if hasn't been warned before
  3353. if (deprecated.ids && !deprecated.ids.hasOwnProperty(id)) {
  3354. warnError("Deprecation warning: " + message);
  3355. }
  3356. // mark as warned to avoid duplicate warn message
  3357. if (deprecated.ids)
  3358. deprecated.ids[id] = true;
  3359. };
  3360. deprecated.ids = {};
  3361. /**
  3362. * @internal
  3363. * @hidden
  3364. */
  3365. function warnError(msg) {
  3366. console.warn(new Error("[mobx-state-tree] " + msg));
  3367. }
  3368. /**
  3369. * @internal
  3370. * @hidden
  3371. */
  3372. function assertArg(value, fn, typeName, argNumber) {
  3373. {
  3374. if (!fn(value)) {
  3375. // istanbul ignore next
  3376. throw fail$1("expected " + typeName + " as argument " + asArray(argNumber).join(" or ") + ", got " + value + " instead");
  3377. }
  3378. }
  3379. }
  3380. /**
  3381. * @internal
  3382. * @hidden
  3383. */
  3384. function assertIsFunction(value, argNumber) {
  3385. assertArg(value, function (fn) { return typeof fn === "function"; }, "function", argNumber);
  3386. }
  3387. /**
  3388. * @internal
  3389. * @hidden
  3390. */
  3391. function assertIsNumber(value, argNumber, min, max) {
  3392. assertArg(value, function (n) { return typeof n === "number"; }, "number", argNumber);
  3393. if (min !== undefined) {
  3394. assertArg(value, function (n) { return n >= min; }, "number greater than " + min, argNumber);
  3395. }
  3396. if (max !== undefined) {
  3397. assertArg(value, function (n) { return n <= max; }, "number lesser than " + max, argNumber);
  3398. }
  3399. }
  3400. /**
  3401. * @internal
  3402. * @hidden
  3403. */
  3404. function assertIsString(value, argNumber, canBeEmpty) {
  3405. if (canBeEmpty === void 0) { canBeEmpty = true; }
  3406. assertArg(value, function (s) { return typeof s === "string"; }, "string", argNumber);
  3407. if (!canBeEmpty) {
  3408. assertArg(value, function (s) { return s !== ""; }, "not empty string", argNumber);
  3409. }
  3410. }
  3411. /**
  3412. * See [asynchronous actions](https://github.com/mobxjs/mobx-state-tree/blob/master/docs/async-actions.md).
  3413. *
  3414. * @returns The flow as a promise.
  3415. */
  3416. function flow(generator) {
  3417. return createFlowSpawner(generator.name, generator);
  3418. }
  3419. /**
  3420. * @deprecated Not needed since TS3.6.
  3421. * Used for TypeScript to make flows that return a promise return the actual promise result.
  3422. *
  3423. * @param val
  3424. * @returns
  3425. */
  3426. function castFlowReturn(val) {
  3427. return val;
  3428. }
  3429. /**
  3430. * @internal
  3431. * @hidden
  3432. */
  3433. function createFlowSpawner(name, generator) {
  3434. var spawner = function flowSpawner() {
  3435. // Implementation based on https://github.com/tj/co/blob/master/index.js
  3436. var runId = getNextActionId();
  3437. var parentContext = getCurrentActionContext();
  3438. if (!parentContext) {
  3439. throw fail$1("a mst flow must always have a parent context");
  3440. }
  3441. var parentActionContext = getParentActionContext(parentContext);
  3442. if (!parentActionContext) {
  3443. throw fail$1("a mst flow must always have a parent action context");
  3444. }
  3445. var contextBase = {
  3446. name: name,
  3447. id: runId,
  3448. tree: parentContext.tree,
  3449. context: parentContext.context,
  3450. parentId: parentContext.id,
  3451. allParentIds: __spread(parentContext.allParentIds, [parentContext.id]),
  3452. rootId: parentContext.rootId,
  3453. parentEvent: parentContext,
  3454. parentActionEvent: parentActionContext
  3455. };
  3456. var args = arguments;
  3457. function wrap(fn, type, arg) {
  3458. fn.$mst_middleware = spawner.$mst_middleware; // pick up any middleware attached to the flow
  3459. runWithActionContext(__assign(__assign({}, contextBase), { type: type, args: [arg] }), fn);
  3460. }
  3461. return new Promise(function (resolve, reject) {
  3462. var gen;
  3463. var init = function asyncActionInit() {
  3464. gen = generator.apply(null, arguments);
  3465. onFulfilled(undefined); // kick off the flow
  3466. };
  3467. init.$mst_middleware = spawner.$mst_middleware;
  3468. runWithActionContext(__assign(__assign({}, contextBase), { type: "flow_spawn", args: argsToArray(args) }), init);
  3469. function onFulfilled(res) {
  3470. var ret;
  3471. try {
  3472. // prettier-ignore
  3473. wrap(function (r) { ret = gen.next(r); }, "flow_resume", res);
  3474. }
  3475. catch (e) {
  3476. // prettier-ignore
  3477. setImmediate(function () {
  3478. wrap(function (r) { reject(e); }, "flow_throw", e);
  3479. });
  3480. return;
  3481. }
  3482. next(ret);
  3483. return;
  3484. }
  3485. function onRejected(err) {
  3486. var ret;
  3487. try {
  3488. // prettier-ignore
  3489. wrap(function (r) { ret = gen.throw(r); }, "flow_resume_error", err); // or yieldError?
  3490. }
  3491. catch (e) {
  3492. // prettier-ignore
  3493. setImmediate(function () {
  3494. wrap(function (r) { reject(e); }, "flow_throw", e);
  3495. });
  3496. return;
  3497. }
  3498. next(ret);
  3499. }
  3500. function next(ret) {
  3501. if (ret.done) {
  3502. // prettier-ignore
  3503. setImmediate(function () {
  3504. wrap(function (r) { resolve(r); }, "flow_return", ret.value);
  3505. });
  3506. return;
  3507. }
  3508. // TODO: support more type of values? See https://github.com/tj/co/blob/249bbdc72da24ae44076afd716349d2089b31c4c/index.js#L100
  3509. if (!ret.value || typeof ret.value.then !== "function") {
  3510. // istanbul ignore next
  3511. throw fail$1("Only promises can be yielded to `async`, got: " + ret);
  3512. }
  3513. return ret.value.then(onFulfilled, onRejected);
  3514. }
  3515. });
  3516. };
  3517. return spawner;
  3518. }
  3519. /**
  3520. * @internal
  3521. * @hidden
  3522. */
  3523. function splitPatch(patch) {
  3524. if (!("oldValue" in patch))
  3525. throw fail$1("Patches without `oldValue` field cannot be inversed");
  3526. return [stripPatch(patch), invertPatch(patch)];
  3527. }
  3528. /**
  3529. * @internal
  3530. * @hidden
  3531. */
  3532. function stripPatch(patch) {
  3533. // strips `oldvalue` information from the patch, so that it becomes a patch conform the json-patch spec
  3534. // this removes the ability to undo the patch
  3535. switch (patch.op) {
  3536. case "add":
  3537. return { op: "add", path: patch.path, value: patch.value };
  3538. case "remove":
  3539. return { op: "remove", path: patch.path };
  3540. case "replace":
  3541. return { op: "replace", path: patch.path, value: patch.value };
  3542. }
  3543. }
  3544. function invertPatch(patch) {
  3545. switch (patch.op) {
  3546. case "add":
  3547. return {
  3548. op: "remove",
  3549. path: patch.path
  3550. };
  3551. case "remove":
  3552. return {
  3553. op: "add",
  3554. path: patch.path,
  3555. value: patch.oldValue
  3556. };
  3557. case "replace":
  3558. return {
  3559. op: "replace",
  3560. path: patch.path,
  3561. value: patch.oldValue
  3562. };
  3563. }
  3564. }
  3565. /**
  3566. * Simple simple check to check it is a number.
  3567. */
  3568. function isNumber(x) {
  3569. return typeof x === "number";
  3570. }
  3571. /**
  3572. * Escape slashes and backslashes.
  3573. *
  3574. * http://tools.ietf.org/html/rfc6901
  3575. */
  3576. function escapeJsonPath(path) {
  3577. if (isNumber(path) === true) {
  3578. return "" + path;
  3579. }
  3580. if (path.indexOf("/") === -1 && path.indexOf("~") === -1)
  3581. return path;
  3582. return path.replace(/~/g, "~0").replace(/\//g, "~1");
  3583. }
  3584. /**
  3585. * Unescape slashes and backslashes.
  3586. */
  3587. function unescapeJsonPath(path) {
  3588. return path.replace(/~1/g, "/").replace(/~0/g, "~");
  3589. }
  3590. /**
  3591. * Generates a json-path compliant json path from path parts.
  3592. *
  3593. * @param path
  3594. * @returns
  3595. */
  3596. function joinJsonPath(path) {
  3597. // `/` refers to property with an empty name, while `` refers to root itself!
  3598. if (path.length === 0)
  3599. return "";
  3600. var getPathStr = function (p) { return p.map(escapeJsonPath).join("/"); };
  3601. if (path[0] === "." || path[0] === "..") {
  3602. // relative
  3603. return getPathStr(path);
  3604. }
  3605. else {
  3606. // absolute
  3607. return "/" + getPathStr(path);
  3608. }
  3609. }
  3610. /**
  3611. * Splits and decodes a json path into several parts.
  3612. *
  3613. * @param path
  3614. * @returns
  3615. */
  3616. function splitJsonPath(path) {
  3617. // `/` refers to property with an empty name, while `` refers to root itself!
  3618. var parts = path.split("/").map(unescapeJsonPath);
  3619. var valid = path === "" ||
  3620. path === "." ||
  3621. path === ".." ||
  3622. stringStartsWith(path, "/") ||
  3623. stringStartsWith(path, "./") ||
  3624. stringStartsWith(path, "../");
  3625. if (!valid) {
  3626. throw fail$1("a json path must be either rooted, empty or relative, but got '" + path + "'");
  3627. }
  3628. // '/a/b/c' -> ["a", "b", "c"]
  3629. // '../../b/c' -> ["..", "..", "b", "c"]
  3630. // '' -> []
  3631. // '/' -> ['']
  3632. // './a' -> [".", "a"]
  3633. // /./a' -> [".", "a"] equivalent to './a'
  3634. if (parts[0] === "") {
  3635. parts.shift();
  3636. }
  3637. return parts;
  3638. }
  3639. var SnapshotProcessor = /** @class */ (function (_super) {
  3640. __extends(SnapshotProcessor, _super);
  3641. function SnapshotProcessor(_subtype, _processors, name) {
  3642. var _this = _super.call(this, name || _subtype.name) || this;
  3643. _this._subtype = _subtype;
  3644. _this._processors = _processors;
  3645. return _this;
  3646. }
  3647. Object.defineProperty(SnapshotProcessor.prototype, "flags", {
  3648. get: function () {
  3649. return this._subtype.flags | TypeFlags.SnapshotProcessor;
  3650. },
  3651. enumerable: true,
  3652. configurable: true
  3653. });
  3654. SnapshotProcessor.prototype.describe = function () {
  3655. return "snapshotProcessor(" + this._subtype.describe() + ")";
  3656. };
  3657. SnapshotProcessor.prototype.preProcessSnapshot = function (sn) {
  3658. if (this._processors.preProcessor) {
  3659. return this._processors.preProcessor.call(null, sn);
  3660. }
  3661. return sn;
  3662. };
  3663. SnapshotProcessor.prototype.postProcessSnapshot = function (sn) {
  3664. if (this._processors.postProcessor) {
  3665. return this._processors.postProcessor.call(null, sn);
  3666. }
  3667. return sn;
  3668. };
  3669. SnapshotProcessor.prototype._fixNode = function (node) {
  3670. var _this = this;
  3671. // the node has to use these methods rather than the original type ones
  3672. proxyNodeTypeMethods(node.type, this, "isAssignableFrom", "create");
  3673. var oldGetSnapshot = node.getSnapshot;
  3674. node.getSnapshot = function () {
  3675. return _this.postProcessSnapshot(oldGetSnapshot.call(node));
  3676. };
  3677. };
  3678. SnapshotProcessor.prototype.instantiate = function (parent, subpath, environment, initialValue) {
  3679. var processedInitialValue = isStateTreeNode(initialValue)
  3680. ? initialValue
  3681. : this.preProcessSnapshot(initialValue);
  3682. var node = this._subtype.instantiate(parent, subpath, environment, processedInitialValue);
  3683. this._fixNode(node);
  3684. return node;
  3685. };
  3686. SnapshotProcessor.prototype.reconcile = function (current, newValue, parent, subpath) {
  3687. var node = this._subtype.reconcile(current, isStateTreeNode(newValue) ? newValue : this.preProcessSnapshot(newValue), parent, subpath);
  3688. if (node !== current) {
  3689. this._fixNode(node);
  3690. }
  3691. return node;
  3692. };
  3693. SnapshotProcessor.prototype.getSnapshot = function (node, applyPostProcess) {
  3694. if (applyPostProcess === void 0) { applyPostProcess = true; }
  3695. var sn = this._subtype.getSnapshot(node);
  3696. return applyPostProcess ? this.postProcessSnapshot(sn) : sn;
  3697. };
  3698. SnapshotProcessor.prototype.isValidSnapshot = function (value, context) {
  3699. var processedSn = this.preProcessSnapshot(value);
  3700. return this._subtype.validate(processedSn, context);
  3701. };
  3702. SnapshotProcessor.prototype.getSubTypes = function () {
  3703. return this._subtype;
  3704. };
  3705. SnapshotProcessor.prototype.is = function (thing) {
  3706. return (this._subtype.validate(isType(thing) ? this._subtype : this.preProcessSnapshot(thing), [
  3707. { path: "", type: this._subtype }
  3708. ]).length === 0);
  3709. };
  3710. return SnapshotProcessor;
  3711. }(BaseType));
  3712. function proxyNodeTypeMethods(nodeType, snapshotProcessorType) {
  3713. var e_1, _a;
  3714. var methods = [];
  3715. for (var _i = 2; _i < arguments.length; _i++) {
  3716. methods[_i - 2] = arguments[_i];
  3717. }
  3718. try {
  3719. for (var methods_1 = __values(methods), methods_1_1 = methods_1.next(); !methods_1_1.done; methods_1_1 = methods_1.next()) {
  3720. var method = methods_1_1.value;
  3721. nodeType[method] = snapshotProcessorType[method].bind(snapshotProcessorType);
  3722. }
  3723. }
  3724. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  3725. finally {
  3726. try {
  3727. if (methods_1_1 && !methods_1_1.done && (_a = methods_1.return)) _a.call(methods_1);
  3728. }
  3729. finally { if (e_1) throw e_1.error; }
  3730. }
  3731. }
  3732. /**
  3733. * `types.snapshotProcessor` - Runs a pre/post snapshot processor before/after serializing a given type.
  3734. *
  3735. * Example:
  3736. * ```ts
  3737. * const Todo1 = types.model({ text: types.string })
  3738. * // in the backend the text type must be null when empty
  3739. * interface BackendTodo {
  3740. * text: string | null
  3741. * }
  3742. * const Todo2 = types.snapshotProcessor(Todo1, {
  3743. * // from snapshot to instance
  3744. * preProcessor(sn: BackendTodo) {
  3745. * return {
  3746. * text: sn.text || "";
  3747. * }
  3748. * },
  3749. * // from instance to snapshot
  3750. * postProcessor(sn): BackendTodo {
  3751. * return {
  3752. * text: !sn.text ? null : sn.text
  3753. * }
  3754. * }
  3755. * })
  3756. * ```
  3757. *
  3758. * @param type Type to run the processors over.
  3759. * @param processors Processors to run.
  3760. * @param name Type name, or undefined to inherit the inner type one.
  3761. * @returns
  3762. */
  3763. function snapshotProcessor(type, processors, name) {
  3764. assertIsType(type, 1);
  3765. {
  3766. if (processors.postProcessor && typeof processors.postProcessor !== "function") {
  3767. // istanbul ignore next
  3768. throw fail("postSnapshotProcessor must be a function");
  3769. }
  3770. if (processors.preProcessor && typeof processors.preProcessor !== "function") {
  3771. // istanbul ignore next
  3772. throw fail("preSnapshotProcessor must be a function");
  3773. }
  3774. }
  3775. return new SnapshotProcessor(type, processors, name);
  3776. }
  3777. var needsIdentifierError = "Map.put can only be used to store complex values that have an identifier type attribute";
  3778. function tryCollectModelTypes(type, modelTypes) {
  3779. var e_1, _a;
  3780. var subtypes = type.getSubTypes();
  3781. if (subtypes === cannotDetermineSubtype) {
  3782. return false;
  3783. }
  3784. if (subtypes) {
  3785. var subtypesArray = asArray(subtypes);
  3786. try {
  3787. for (var subtypesArray_1 = __values(subtypesArray), subtypesArray_1_1 = subtypesArray_1.next(); !subtypesArray_1_1.done; subtypesArray_1_1 = subtypesArray_1.next()) {
  3788. var subtype = subtypesArray_1_1.value;
  3789. if (!tryCollectModelTypes(subtype, modelTypes))
  3790. return false;
  3791. }
  3792. }
  3793. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  3794. finally {
  3795. try {
  3796. if (subtypesArray_1_1 && !subtypesArray_1_1.done && (_a = subtypesArray_1.return)) _a.call(subtypesArray_1);
  3797. }
  3798. finally { if (e_1) throw e_1.error; }
  3799. }
  3800. }
  3801. if (type instanceof ModelType) {
  3802. modelTypes.push(type);
  3803. }
  3804. return true;
  3805. }
  3806. /**
  3807. * @internal
  3808. * @hidden
  3809. */
  3810. var MapIdentifierMode;
  3811. (function (MapIdentifierMode) {
  3812. MapIdentifierMode[MapIdentifierMode["UNKNOWN"] = 0] = "UNKNOWN";
  3813. MapIdentifierMode[MapIdentifierMode["YES"] = 1] = "YES";
  3814. MapIdentifierMode[MapIdentifierMode["NO"] = 2] = "NO";
  3815. })(MapIdentifierMode || (MapIdentifierMode = {}));
  3816. var MSTMap = /** @class */ (function (_super) {
  3817. __extends(MSTMap, _super);
  3818. function MSTMap(initialData) {
  3819. return _super.call(this, initialData, mobx.observable.ref.enhancer) || this;
  3820. }
  3821. MSTMap.prototype.get = function (key) {
  3822. // maybe this is over-enthousiastic? normalize numeric keys to strings
  3823. return _super.prototype.get.call(this, "" + key);
  3824. };
  3825. MSTMap.prototype.has = function (key) {
  3826. return _super.prototype.has.call(this, "" + key);
  3827. };
  3828. MSTMap.prototype.delete = function (key) {
  3829. return _super.prototype.delete.call(this, "" + key);
  3830. };
  3831. MSTMap.prototype.set = function (key, value) {
  3832. return _super.prototype.set.call(this, "" + key, value);
  3833. };
  3834. MSTMap.prototype.put = function (value) {
  3835. if (!value)
  3836. throw fail$1("Map.put cannot be used to set empty values");
  3837. if (isStateTreeNode(value)) {
  3838. var node = getStateTreeNode(value);
  3839. {
  3840. if (!node.identifierAttribute) {
  3841. throw fail$1(needsIdentifierError);
  3842. }
  3843. }
  3844. if (node.identifier === null) {
  3845. throw fail$1(needsIdentifierError);
  3846. }
  3847. this.set(node.identifier, value);
  3848. return value;
  3849. }
  3850. else if (!isMutable(value)) {
  3851. throw fail$1("Map.put can only be used to store complex values");
  3852. }
  3853. else {
  3854. var mapNode = getStateTreeNode(this);
  3855. var mapType = mapNode.type;
  3856. if (mapType.identifierMode !== MapIdentifierMode.YES) {
  3857. throw fail$1(needsIdentifierError);
  3858. }
  3859. var idAttr = mapType.mapIdentifierAttribute;
  3860. var id = value[idAttr];
  3861. if (!isValidIdentifier(id)) {
  3862. // try again but this time after creating a node for the value
  3863. // since it might be an optional identifier
  3864. var newNode = this.put(mapType.getChildType().create(value, mapNode.environment));
  3865. return this.put(getSnapshot(newNode));
  3866. }
  3867. var key = normalizeIdentifier(id);
  3868. this.set(key, value);
  3869. return this.get(key);
  3870. }
  3871. };
  3872. return MSTMap;
  3873. }(mobx.ObservableMap));
  3874. /**
  3875. * @internal
  3876. * @hidden
  3877. */
  3878. var MapType = /** @class */ (function (_super) {
  3879. __extends(MapType, _super);
  3880. function MapType(name, _subType, hookInitializers) {
  3881. if (hookInitializers === void 0) { hookInitializers = []; }
  3882. var _this = _super.call(this, name) || this;
  3883. _this._subType = _subType;
  3884. _this.identifierMode = MapIdentifierMode.UNKNOWN;
  3885. _this.mapIdentifierAttribute = undefined;
  3886. _this.flags = TypeFlags.Map;
  3887. _this.hookInitializers = [];
  3888. _this._determineIdentifierMode();
  3889. _this.hookInitializers = hookInitializers;
  3890. return _this;
  3891. }
  3892. MapType.prototype.hooks = function (hooks) {
  3893. var hookInitializers = this.hookInitializers.length > 0 ? this.hookInitializers.concat(hooks) : [hooks];
  3894. return new MapType(this.name, this._subType, hookInitializers);
  3895. };
  3896. MapType.prototype.instantiate = function (parent, subpath, environment, initialValue) {
  3897. this._determineIdentifierMode();
  3898. return createObjectNode(this, parent, subpath, environment, initialValue);
  3899. };
  3900. MapType.prototype._determineIdentifierMode = function () {
  3901. if (this.identifierMode !== MapIdentifierMode.UNKNOWN) {
  3902. return;
  3903. }
  3904. var modelTypes = [];
  3905. if (tryCollectModelTypes(this._subType, modelTypes)) {
  3906. var identifierAttribute_1 = undefined;
  3907. modelTypes.forEach(function (type) {
  3908. if (type.identifierAttribute) {
  3909. if (identifierAttribute_1 && identifierAttribute_1 !== type.identifierAttribute) {
  3910. throw fail$1("The objects in a map should all have the same identifier attribute, expected '" + identifierAttribute_1 + "', but child of type '" + type.name + "' declared attribute '" + type.identifierAttribute + "' as identifier");
  3911. }
  3912. identifierAttribute_1 = type.identifierAttribute;
  3913. }
  3914. });
  3915. if (identifierAttribute_1) {
  3916. this.identifierMode = MapIdentifierMode.YES;
  3917. this.mapIdentifierAttribute = identifierAttribute_1;
  3918. }
  3919. else {
  3920. this.identifierMode = MapIdentifierMode.NO;
  3921. }
  3922. }
  3923. };
  3924. MapType.prototype.initializeChildNodes = function (objNode, initialSnapshot) {
  3925. if (initialSnapshot === void 0) { initialSnapshot = {}; }
  3926. var subType = objNode.type._subType;
  3927. var result = {};
  3928. Object.keys(initialSnapshot).forEach(function (name) {
  3929. result[name] = subType.instantiate(objNode, name, undefined, initialSnapshot[name]);
  3930. });
  3931. return result;
  3932. };
  3933. MapType.prototype.createNewInstance = function (childNodes) {
  3934. return new MSTMap(childNodes);
  3935. };
  3936. MapType.prototype.finalizeNewInstance = function (node, instance) {
  3937. mobx._interceptReads(instance, node.unbox);
  3938. var type = node.type;
  3939. type.hookInitializers.forEach(function (initializer) {
  3940. var hooks = initializer(instance);
  3941. Object.keys(hooks).forEach(function (name) {
  3942. var hook = hooks[name];
  3943. var actionInvoker = createActionInvoker(instance, name, hook);
  3944. ( addHiddenWritableProp)(instance, name, actionInvoker);
  3945. });
  3946. });
  3947. mobx.intercept(instance, this.willChange);
  3948. mobx.observe(instance, this.didChange);
  3949. };
  3950. MapType.prototype.describe = function () {
  3951. return "Map<string, " + this._subType.describe() + ">";
  3952. };
  3953. MapType.prototype.getChildren = function (node) {
  3954. // return (node.storedValue as ObservableMap<any>).values()
  3955. return mobx.values(node.storedValue);
  3956. };
  3957. MapType.prototype.getChildNode = function (node, key) {
  3958. var childNode = node.storedValue.get("" + key);
  3959. if (!childNode)
  3960. throw fail$1("Not a child " + key);
  3961. return childNode;
  3962. };
  3963. MapType.prototype.willChange = function (change) {
  3964. var node = getStateTreeNode(change.object);
  3965. var key = change.name;
  3966. node.assertWritable({ subpath: key });
  3967. var mapType = node.type;
  3968. var subType = mapType._subType;
  3969. switch (change.type) {
  3970. case "update":
  3971. {
  3972. var newValue = change.newValue;
  3973. var oldValue = change.object.get(key);
  3974. if (newValue === oldValue)
  3975. return null;
  3976. typecheckInternal(subType, newValue);
  3977. change.newValue = subType.reconcile(node.getChildNode(key), change.newValue, node, key);
  3978. mapType.processIdentifier(key, change.newValue);
  3979. }
  3980. break;
  3981. case "add":
  3982. {
  3983. typecheckInternal(subType, change.newValue);
  3984. change.newValue = subType.instantiate(node, key, undefined, change.newValue);
  3985. mapType.processIdentifier(key, change.newValue);
  3986. }
  3987. break;
  3988. }
  3989. return change;
  3990. };
  3991. MapType.prototype.processIdentifier = function (expected, node) {
  3992. if (this.identifierMode === MapIdentifierMode.YES && node instanceof ObjectNode) {
  3993. var identifier = node.identifier;
  3994. if (identifier !== expected)
  3995. throw fail$1("A map of objects containing an identifier should always store the object under their own identifier. Trying to store key '" + identifier + "', but expected: '" + expected + "'");
  3996. }
  3997. };
  3998. MapType.prototype.getSnapshot = function (node) {
  3999. var res = {};
  4000. node.getChildren().forEach(function (childNode) {
  4001. res[childNode.subpath] = childNode.snapshot;
  4002. });
  4003. return res;
  4004. };
  4005. MapType.prototype.processInitialSnapshot = function (childNodes) {
  4006. var processed = {};
  4007. Object.keys(childNodes).forEach(function (key) {
  4008. processed[key] = childNodes[key].getSnapshot();
  4009. });
  4010. return processed;
  4011. };
  4012. MapType.prototype.didChange = function (change) {
  4013. var node = getStateTreeNode(change.object);
  4014. switch (change.type) {
  4015. case "update":
  4016. return void node.emitPatch({
  4017. op: "replace",
  4018. path: escapeJsonPath(change.name),
  4019. value: change.newValue.snapshot,
  4020. oldValue: change.oldValue ? change.oldValue.snapshot : undefined
  4021. }, node);
  4022. case "add":
  4023. return void node.emitPatch({
  4024. op: "add",
  4025. path: escapeJsonPath(change.name),
  4026. value: change.newValue.snapshot,
  4027. oldValue: undefined
  4028. }, node);
  4029. case "delete":
  4030. // a node got deleted, get the old snapshot and make the node die
  4031. var oldSnapshot = change.oldValue.snapshot;
  4032. change.oldValue.die();
  4033. // emit the patch
  4034. return void node.emitPatch({
  4035. op: "remove",
  4036. path: escapeJsonPath(change.name),
  4037. oldValue: oldSnapshot
  4038. }, node);
  4039. }
  4040. };
  4041. MapType.prototype.applyPatchLocally = function (node, subpath, patch) {
  4042. var target = node.storedValue;
  4043. switch (patch.op) {
  4044. case "add":
  4045. case "replace":
  4046. target.set(subpath, patch.value);
  4047. break;
  4048. case "remove":
  4049. target.delete(subpath);
  4050. break;
  4051. }
  4052. };
  4053. MapType.prototype.applySnapshot = function (node, snapshot) {
  4054. typecheckInternal(this, snapshot);
  4055. var target = node.storedValue;
  4056. var currentKeys = {};
  4057. Array.from(target.keys()).forEach(function (key) {
  4058. currentKeys[key] = false;
  4059. });
  4060. if (snapshot) {
  4061. // Don't use target.replace, as it will throw away all existing items first
  4062. for (var key in snapshot) {
  4063. target.set(key, snapshot[key]);
  4064. currentKeys["" + key] = true;
  4065. }
  4066. }
  4067. Object.keys(currentKeys).forEach(function (key) {
  4068. if (currentKeys[key] === false)
  4069. target.delete(key);
  4070. });
  4071. };
  4072. MapType.prototype.getChildType = function () {
  4073. return this._subType;
  4074. };
  4075. MapType.prototype.isValidSnapshot = function (value, context) {
  4076. var _this = this;
  4077. if (!isPlainObject(value)) {
  4078. return typeCheckFailure(context, value, "Value is not a plain object");
  4079. }
  4080. return flattenTypeErrors(Object.keys(value).map(function (path) {
  4081. return _this._subType.validate(value[path], getContextForPath(context, path, _this._subType));
  4082. }));
  4083. };
  4084. MapType.prototype.getDefaultSnapshot = function () {
  4085. return EMPTY_OBJECT;
  4086. };
  4087. MapType.prototype.removeChild = function (node, subpath) {
  4088. node.storedValue.delete(subpath);
  4089. };
  4090. __decorate([
  4091. mobx.action
  4092. ], MapType.prototype, "applySnapshot", null);
  4093. return MapType;
  4094. }(ComplexType));
  4095. /**
  4096. * `types.map` - Creates a key based collection type who's children are all of a uniform declared type.
  4097. * If the type stored in a map has an identifier, it is mandatory to store the child under that identifier in the map.
  4098. *
  4099. * This type will always produce [observable maps](https://mobx.js.org/refguide/map.html)
  4100. *
  4101. * Example:
  4102. * ```ts
  4103. * const Todo = types.model({
  4104. * id: types.identifier,
  4105. * task: types.string
  4106. * })
  4107. *
  4108. * const TodoStore = types.model({
  4109. * todos: types.map(Todo)
  4110. * })
  4111. *
  4112. * const s = TodoStore.create({ todos: {} })
  4113. * unprotect(s)
  4114. * s.todos.set(17, { task: "Grab coffee", id: 17 })
  4115. * s.todos.put({ task: "Grab cookie", id: 18 }) // put will infer key from the identifier
  4116. * console.log(s.todos.get(17).task) // prints: "Grab coffee"
  4117. * ```
  4118. *
  4119. * @param subtype
  4120. * @returns
  4121. */
  4122. function map(subtype) {
  4123. return new MapType("map<string, " + subtype.name + ">", subtype);
  4124. }
  4125. /**
  4126. * Returns if a given value represents a map type.
  4127. *
  4128. * @param type
  4129. * @returns `true` if it is a map type.
  4130. */
  4131. function isMapType(type) {
  4132. return isType(type) && (type.flags & TypeFlags.Map) > 0;
  4133. }
  4134. /**
  4135. * @internal
  4136. * @hidden
  4137. */
  4138. var ArrayType = /** @class */ (function (_super) {
  4139. __extends(ArrayType, _super);
  4140. function ArrayType(name, _subType, hookInitializers) {
  4141. if (hookInitializers === void 0) { hookInitializers = []; }
  4142. var _this = _super.call(this, name) || this;
  4143. _this._subType = _subType;
  4144. _this.flags = TypeFlags.Array;
  4145. _this.hookInitializers = [];
  4146. _this.hookInitializers = hookInitializers;
  4147. return _this;
  4148. }
  4149. ArrayType.prototype.hooks = function (hooks) {
  4150. var hookInitializers = this.hookInitializers.length > 0 ? this.hookInitializers.concat(hooks) : [hooks];
  4151. return new ArrayType(this.name, this._subType, hookInitializers);
  4152. };
  4153. ArrayType.prototype.instantiate = function (parent, subpath, environment, initialValue) {
  4154. return createObjectNode(this, parent, subpath, environment, initialValue);
  4155. };
  4156. ArrayType.prototype.initializeChildNodes = function (objNode, snapshot) {
  4157. if (snapshot === void 0) { snapshot = []; }
  4158. var subType = objNode.type._subType;
  4159. var result = {};
  4160. snapshot.forEach(function (item, index) {
  4161. var subpath = "" + index;
  4162. result[subpath] = subType.instantiate(objNode, subpath, undefined, item);
  4163. });
  4164. return result;
  4165. };
  4166. ArrayType.prototype.createNewInstance = function (childNodes) {
  4167. return mobx.observable.array(convertChildNodesToArray(childNodes), mobxShallow);
  4168. };
  4169. ArrayType.prototype.finalizeNewInstance = function (node, instance) {
  4170. mobx._getAdministration(instance).dehancer = node.unbox;
  4171. var type = node.type;
  4172. type.hookInitializers.forEach(function (initializer) {
  4173. var hooks = initializer(instance);
  4174. Object.keys(hooks).forEach(function (name) {
  4175. var hook = hooks[name];
  4176. var actionInvoker = createActionInvoker(instance, name, hook);
  4177. ( addHiddenWritableProp)(instance, name, actionInvoker);
  4178. });
  4179. });
  4180. mobx.intercept(instance, this.willChange);
  4181. mobx.observe(instance, this.didChange);
  4182. };
  4183. ArrayType.prototype.describe = function () {
  4184. return this._subType.describe() + "[]";
  4185. };
  4186. ArrayType.prototype.getChildren = function (node) {
  4187. return node.storedValue.slice();
  4188. };
  4189. ArrayType.prototype.getChildNode = function (node, key) {
  4190. var index = Number(key);
  4191. if (index < node.storedValue.length)
  4192. return node.storedValue[index];
  4193. throw fail$1("Not a child: " + key);
  4194. };
  4195. ArrayType.prototype.willChange = function (change) {
  4196. var node = getStateTreeNode(change.object);
  4197. node.assertWritable({ subpath: "" + change.index });
  4198. var subType = node.type._subType;
  4199. var childNodes = node.getChildren();
  4200. switch (change.type) {
  4201. case "update":
  4202. {
  4203. if (change.newValue === change.object[change.index])
  4204. return null;
  4205. var updatedNodes = reconcileArrayChildren(node, subType, [childNodes[change.index]], [change.newValue], [change.index]);
  4206. if (!updatedNodes) {
  4207. return null;
  4208. }
  4209. change.newValue = updatedNodes[0];
  4210. }
  4211. break;
  4212. case "splice":
  4213. {
  4214. var index_1 = change.index, removedCount = change.removedCount, added = change.added;
  4215. var addedNodes = reconcileArrayChildren(node, subType, childNodes.slice(index_1, index_1 + removedCount), added, added.map(function (_, i) { return index_1 + i; }));
  4216. if (!addedNodes) {
  4217. return null;
  4218. }
  4219. change.added = addedNodes;
  4220. // update paths of remaining items
  4221. for (var i = index_1 + removedCount; i < childNodes.length; i++) {
  4222. childNodes[i].setParent(node, "" + (i + added.length - removedCount));
  4223. }
  4224. }
  4225. break;
  4226. }
  4227. return change;
  4228. };
  4229. ArrayType.prototype.getSnapshot = function (node) {
  4230. return node.getChildren().map(function (childNode) { return childNode.snapshot; });
  4231. };
  4232. ArrayType.prototype.processInitialSnapshot = function (childNodes) {
  4233. var processed = [];
  4234. Object.keys(childNodes).forEach(function (key) {
  4235. processed.push(childNodes[key].getSnapshot());
  4236. });
  4237. return processed;
  4238. };
  4239. ArrayType.prototype.didChange = function (change) {
  4240. var node = getStateTreeNode(change.object);
  4241. switch (change.type) {
  4242. case "update":
  4243. return void node.emitPatch({
  4244. op: "replace",
  4245. path: "" + change.index,
  4246. value: change.newValue.snapshot,
  4247. oldValue: change.oldValue ? change.oldValue.snapshot : undefined
  4248. }, node);
  4249. case "splice":
  4250. for (var i = change.removedCount - 1; i >= 0; i--)
  4251. node.emitPatch({
  4252. op: "remove",
  4253. path: "" + (change.index + i),
  4254. oldValue: change.removed[i].snapshot
  4255. }, node);
  4256. for (var i = 0; i < change.addedCount; i++)
  4257. node.emitPatch({
  4258. op: "add",
  4259. path: "" + (change.index + i),
  4260. value: node.getChildNode("" + (change.index + i)).snapshot,
  4261. oldValue: undefined
  4262. }, node);
  4263. return;
  4264. }
  4265. };
  4266. ArrayType.prototype.applyPatchLocally = function (node, subpath, patch) {
  4267. var target = node.storedValue;
  4268. var index = subpath === "-" ? target.length : Number(subpath);
  4269. switch (patch.op) {
  4270. case "replace":
  4271. target[index] = patch.value;
  4272. break;
  4273. case "add":
  4274. target.splice(index, 0, patch.value);
  4275. break;
  4276. case "remove":
  4277. target.splice(index, 1);
  4278. break;
  4279. }
  4280. };
  4281. ArrayType.prototype.applySnapshot = function (node, snapshot) {
  4282. typecheckInternal(this, snapshot);
  4283. var target = node.storedValue;
  4284. target.replace(snapshot);
  4285. };
  4286. ArrayType.prototype.getChildType = function () {
  4287. return this._subType;
  4288. };
  4289. ArrayType.prototype.isValidSnapshot = function (value, context) {
  4290. var _this = this;
  4291. if (!isArray(value)) {
  4292. return typeCheckFailure(context, value, "Value is not an array");
  4293. }
  4294. return flattenTypeErrors(value.map(function (item, index) {
  4295. return _this._subType.validate(item, getContextForPath(context, "" + index, _this._subType));
  4296. }));
  4297. };
  4298. ArrayType.prototype.getDefaultSnapshot = function () {
  4299. return EMPTY_ARRAY;
  4300. };
  4301. ArrayType.prototype.removeChild = function (node, subpath) {
  4302. node.storedValue.splice(Number(subpath), 1);
  4303. };
  4304. __decorate([
  4305. mobx.action
  4306. ], ArrayType.prototype, "applySnapshot", null);
  4307. return ArrayType;
  4308. }(ComplexType));
  4309. /**
  4310. * `types.array` - Creates an index based collection type who's children are all of a uniform declared type.
  4311. *
  4312. * This type will always produce [observable arrays](https://mobx.js.org/refguide/array.html)
  4313. *
  4314. * Example:
  4315. * ```ts
  4316. * const Todo = types.model({
  4317. * task: types.string
  4318. * })
  4319. *
  4320. * const TodoStore = types.model({
  4321. * todos: types.array(Todo)
  4322. * })
  4323. *
  4324. * const s = TodoStore.create({ todos: [] })
  4325. * unprotect(s) // needed to allow modifying outside of an action
  4326. * s.todos.push({ task: "Grab coffee" })
  4327. * console.log(s.todos[0]) // prints: "Grab coffee"
  4328. * ```
  4329. *
  4330. * @param subtype
  4331. * @returns
  4332. */
  4333. function array(subtype) {
  4334. assertIsType(subtype, 1);
  4335. return new ArrayType(subtype.name + "[]", subtype);
  4336. }
  4337. function reconcileArrayChildren(parent, childType, oldNodes, newValues, newPaths) {
  4338. var nothingChanged = true;
  4339. for (var i = 0;; i++) {
  4340. var hasNewNode = i <= newValues.length - 1;
  4341. var oldNode = oldNodes[i];
  4342. var newValue = hasNewNode ? newValues[i] : undefined;
  4343. var newPath = "" + newPaths[i];
  4344. // for some reason, instead of newValue we got a node, fallback to the storedValue
  4345. // TODO: https://github.com/mobxjs/mobx-state-tree/issues/340#issuecomment-325581681
  4346. if (isNode(newValue))
  4347. newValue = newValue.storedValue;
  4348. if (!oldNode && !hasNewNode) {
  4349. // both are empty, end
  4350. break;
  4351. }
  4352. else if (!hasNewNode) {
  4353. // new one does not exists
  4354. nothingChanged = false;
  4355. oldNodes.splice(i, 1);
  4356. if (oldNode instanceof ObjectNode) {
  4357. // since it is going to be returned by pop/splice/shift better create it before killing it
  4358. // so it doesn't end up in an undead state
  4359. oldNode.createObservableInstanceIfNeeded();
  4360. }
  4361. oldNode.die();
  4362. i--;
  4363. }
  4364. else if (!oldNode) {
  4365. // there is no old node, create it
  4366. // check if already belongs to the same parent. if so, avoid pushing item in. only swapping can occur.
  4367. if (isStateTreeNode(newValue) && getStateTreeNode(newValue).parent === parent) {
  4368. // this node is owned by this parent, but not in the reconcilable set, so it must be double
  4369. throw fail$1("Cannot add an object to a state tree if it is already part of the same or another state tree. Tried to assign an object to '" + parent.path + "/" + newPath + "', but it lives already at '" + getStateTreeNode(newValue).path + "'");
  4370. }
  4371. nothingChanged = false;
  4372. var newNode = valueAsNode(childType, parent, newPath, newValue);
  4373. oldNodes.splice(i, 0, newNode);
  4374. }
  4375. else if (areSame(oldNode, newValue)) {
  4376. // both are the same, reconcile
  4377. oldNodes[i] = valueAsNode(childType, parent, newPath, newValue, oldNode);
  4378. }
  4379. else {
  4380. // nothing to do, try to reorder
  4381. var oldMatch = undefined;
  4382. // find a possible candidate to reuse
  4383. for (var j = i; j < oldNodes.length; j++) {
  4384. if (areSame(oldNodes[j], newValue)) {
  4385. oldMatch = oldNodes.splice(j, 1)[0];
  4386. break;
  4387. }
  4388. }
  4389. nothingChanged = false;
  4390. var newNode = valueAsNode(childType, parent, newPath, newValue, oldMatch);
  4391. oldNodes.splice(i, 0, newNode);
  4392. }
  4393. }
  4394. return nothingChanged ? null : oldNodes;
  4395. }
  4396. /**
  4397. * Convert a value to a node at given parent and subpath. Attempts to reuse old node if possible and given.
  4398. */
  4399. function valueAsNode(childType, parent, subpath, newValue, oldNode) {
  4400. // ensure the value is valid-ish
  4401. typecheckInternal(childType, newValue);
  4402. function getNewNode() {
  4403. // the new value has a MST node
  4404. if (isStateTreeNode(newValue)) {
  4405. var childNode = getStateTreeNode(newValue);
  4406. childNode.assertAlive(EMPTY_OBJECT);
  4407. // the node lives here
  4408. if (childNode.parent !== null && childNode.parent === parent) {
  4409. childNode.setParent(parent, subpath);
  4410. return childNode;
  4411. }
  4412. }
  4413. // there is old node and new one is a value/snapshot
  4414. if (oldNode) {
  4415. return childType.reconcile(oldNode, newValue, parent, subpath);
  4416. }
  4417. // nothing to do, create from scratch
  4418. return childType.instantiate(parent, subpath, undefined, newValue);
  4419. }
  4420. var newNode = getNewNode();
  4421. if (oldNode && oldNode !== newNode) {
  4422. if (oldNode instanceof ObjectNode) {
  4423. // since it is going to be returned by pop/splice/shift better create it before killing it
  4424. // so it doesn't end up in an undead state
  4425. oldNode.createObservableInstanceIfNeeded();
  4426. }
  4427. oldNode.die();
  4428. }
  4429. return newNode;
  4430. }
  4431. /**
  4432. * Check if a node holds a value.
  4433. */
  4434. function areSame(oldNode, newValue) {
  4435. // never consider dead old nodes for reconciliation
  4436. if (!oldNode.isAlive) {
  4437. return false;
  4438. }
  4439. // the new value has the same node
  4440. if (isStateTreeNode(newValue)) {
  4441. var newNode = getStateTreeNode(newValue);
  4442. return newNode.isAlive && newNode === oldNode;
  4443. }
  4444. // the provided value is the snapshot of the old node
  4445. if (oldNode.snapshot === newValue) {
  4446. return true;
  4447. }
  4448. // new value is a snapshot with the correct identifier
  4449. return (oldNode instanceof ObjectNode &&
  4450. oldNode.identifier !== null &&
  4451. oldNode.identifierAttribute &&
  4452. isPlainObject(newValue) &&
  4453. oldNode.identifier === normalizeIdentifier(newValue[oldNode.identifierAttribute]) &&
  4454. oldNode.type.is(newValue));
  4455. }
  4456. /**
  4457. * Returns if a given value represents an array type.
  4458. *
  4459. * @param type
  4460. * @returns `true` if the type is an array type.
  4461. */
  4462. function isArrayType(type) {
  4463. return isType(type) && (type.flags & TypeFlags.Array) > 0;
  4464. }
  4465. var PRE_PROCESS_SNAPSHOT = "preProcessSnapshot";
  4466. var POST_PROCESS_SNAPSHOT = "postProcessSnapshot";
  4467. function objectTypeToString() {
  4468. return getStateTreeNode(this).toString();
  4469. }
  4470. var defaultObjectOptions = {
  4471. name: "AnonymousModel",
  4472. properties: {},
  4473. initializers: EMPTY_ARRAY
  4474. };
  4475. function toPropertiesObject(declaredProps) {
  4476. // loop through properties and ensures that all items are types
  4477. return Object.keys(declaredProps).reduce(function (props, key) {
  4478. var _a, _b, _c;
  4479. // warn if user intended a HOOK
  4480. if (key in Hook)
  4481. throw fail$1("Hook '" + key + "' was defined as property. Hooks should be defined as part of the actions");
  4482. // the user intended to use a view
  4483. var descriptor = Object.getOwnPropertyDescriptor(props, key);
  4484. if ("get" in descriptor) {
  4485. throw fail$1("Getters are not supported as properties. Please use views instead");
  4486. }
  4487. // undefined and null are not valid
  4488. var value = descriptor.value;
  4489. if (value === null || value === undefined) {
  4490. throw fail$1("The default value of an attribute cannot be null or undefined as the type cannot be inferred. Did you mean `types.maybe(someType)`?");
  4491. // its a primitive, convert to its type
  4492. }
  4493. else if (isPrimitive(value)) {
  4494. return Object.assign({}, props, (_a = {},
  4495. _a[key] = optional(getPrimitiveFactoryFromValue(value), value),
  4496. _a));
  4497. // map defaults to empty object automatically for models
  4498. }
  4499. else if (value instanceof MapType) {
  4500. return Object.assign({}, props, (_b = {},
  4501. _b[key] = optional(value, {}),
  4502. _b));
  4503. }
  4504. else if (value instanceof ArrayType) {
  4505. return Object.assign({}, props, (_c = {}, _c[key] = optional(value, []), _c));
  4506. // its already a type
  4507. }
  4508. else if (isType(value)) {
  4509. return props;
  4510. // its a function, maybe the user wanted a view?
  4511. }
  4512. else if ( typeof value === "function") {
  4513. throw fail$1("Invalid type definition for property '" + key + "', it looks like you passed a function. Did you forget to invoke it, or did you intend to declare a view / action?");
  4514. // no other complex values
  4515. }
  4516. else if ( typeof value === "object") {
  4517. throw fail$1("Invalid type definition for property '" + key + "', it looks like you passed an object. Try passing another model type or a types.frozen.");
  4518. // WTF did you pass in mate?
  4519. }
  4520. else {
  4521. throw fail$1("Invalid type definition for property '" + key + "', cannot infer a type from a value like '" + value + "' (" + typeof value + ")");
  4522. }
  4523. }, declaredProps);
  4524. }
  4525. /**
  4526. * @internal
  4527. * @hidden
  4528. */
  4529. var ModelType = /** @class */ (function (_super) {
  4530. __extends(ModelType, _super);
  4531. function ModelType(opts) {
  4532. var _this = _super.call(this, opts.name || defaultObjectOptions.name) || this;
  4533. _this.flags = TypeFlags.Object;
  4534. _this.named = function (name) {
  4535. return _this.cloneAndEnhance({ name: name });
  4536. };
  4537. _this.props = function (properties) {
  4538. return _this.cloneAndEnhance({ properties: properties });
  4539. };
  4540. _this.preProcessSnapshot = function (preProcessor) {
  4541. var currentPreprocessor = _this.preProcessor;
  4542. if (!currentPreprocessor)
  4543. return _this.cloneAndEnhance({ preProcessor: preProcessor });
  4544. else
  4545. return _this.cloneAndEnhance({
  4546. preProcessor: function (snapshot) { return currentPreprocessor(preProcessor(snapshot)); }
  4547. });
  4548. };
  4549. _this.postProcessSnapshot = function (postProcessor) {
  4550. var currentPostprocessor = _this.postProcessor;
  4551. if (!currentPostprocessor)
  4552. return _this.cloneAndEnhance({ postProcessor: postProcessor });
  4553. else
  4554. return _this.cloneAndEnhance({
  4555. postProcessor: function (snapshot) { return postProcessor(currentPostprocessor(snapshot)); }
  4556. });
  4557. };
  4558. Object.assign(_this, defaultObjectOptions, opts);
  4559. // ensures that any default value gets converted to its related type
  4560. _this.properties = toPropertiesObject(_this.properties);
  4561. freeze(_this.properties); // make sure nobody messes with it
  4562. _this.propertyNames = Object.keys(_this.properties);
  4563. _this.identifierAttribute = _this._getIdentifierAttribute();
  4564. return _this;
  4565. }
  4566. ModelType.prototype._getIdentifierAttribute = function () {
  4567. var identifierAttribute = undefined;
  4568. this.forAllProps(function (propName, propType) {
  4569. if (propType.flags & TypeFlags.Identifier) {
  4570. if (identifierAttribute)
  4571. throw fail$1("Cannot define property '" + propName + "' as object identifier, property '" + identifierAttribute + "' is already defined as identifier property");
  4572. identifierAttribute = propName;
  4573. }
  4574. });
  4575. return identifierAttribute;
  4576. };
  4577. ModelType.prototype.cloneAndEnhance = function (opts) {
  4578. return new ModelType({
  4579. name: opts.name || this.name,
  4580. properties: Object.assign({}, this.properties, opts.properties),
  4581. initializers: this.initializers.concat(opts.initializers || []),
  4582. preProcessor: opts.preProcessor || this.preProcessor,
  4583. postProcessor: opts.postProcessor || this.postProcessor
  4584. });
  4585. };
  4586. ModelType.prototype.actions = function (fn) {
  4587. var _this = this;
  4588. var actionInitializer = function (self) {
  4589. _this.instantiateActions(self, fn(self));
  4590. return self;
  4591. };
  4592. return this.cloneAndEnhance({ initializers: [actionInitializer] });
  4593. };
  4594. ModelType.prototype.instantiateActions = function (self, actions) {
  4595. // check if return is correct
  4596. if (!isPlainObject(actions))
  4597. throw fail$1("actions initializer should return a plain object containing actions");
  4598. // bind actions to the object created
  4599. Object.keys(actions).forEach(function (name) {
  4600. // warn if preprocessor was given
  4601. if (name === PRE_PROCESS_SNAPSHOT)
  4602. throw fail$1("Cannot define action '" + PRE_PROCESS_SNAPSHOT + "', it should be defined using 'type.preProcessSnapshot(fn)' instead");
  4603. // warn if postprocessor was given
  4604. if (name === POST_PROCESS_SNAPSHOT)
  4605. throw fail$1("Cannot define action '" + POST_PROCESS_SNAPSHOT + "', it should be defined using 'type.postProcessSnapshot(fn)' instead");
  4606. var action2 = actions[name];
  4607. // apply hook composition
  4608. var baseAction = self[name];
  4609. if (name in Hook && baseAction) {
  4610. var specializedAction_1 = action2;
  4611. action2 = function () {
  4612. baseAction.apply(null, arguments);
  4613. specializedAction_1.apply(null, arguments);
  4614. };
  4615. }
  4616. // the goal of this is to make sure actions using "this" can call themselves,
  4617. // while still allowing the middlewares to register them
  4618. var middlewares = action2.$mst_middleware; // make sure middlewares are not lost
  4619. var boundAction = action2.bind(actions);
  4620. boundAction.$mst_middleware = middlewares;
  4621. var actionInvoker = createActionInvoker(self, name, boundAction);
  4622. actions[name] = actionInvoker;
  4623. ( addHiddenWritableProp)(self, name, actionInvoker);
  4624. });
  4625. };
  4626. ModelType.prototype.volatile = function (fn) {
  4627. var _this = this;
  4628. var stateInitializer = function (self) {
  4629. _this.instantiateVolatileState(self, fn(self));
  4630. return self;
  4631. };
  4632. return this.cloneAndEnhance({ initializers: [stateInitializer] });
  4633. };
  4634. ModelType.prototype.instantiateVolatileState = function (self, state) {
  4635. // check views return
  4636. if (!isPlainObject(state))
  4637. throw fail$1("volatile state initializer should return a plain object containing state");
  4638. mobx.set(self, state);
  4639. };
  4640. ModelType.prototype.extend = function (fn) {
  4641. var _this = this;
  4642. var initializer = function (self) {
  4643. var _a = fn(self), actions = _a.actions, views = _a.views, state = _a.state, rest = __rest(_a, ["actions", "views", "state"]);
  4644. for (var key in rest)
  4645. throw fail$1("The `extend` function should return an object with a subset of the fields 'actions', 'views' and 'state'. Found invalid key '" + key + "'");
  4646. if (state)
  4647. _this.instantiateVolatileState(self, state);
  4648. if (views)
  4649. _this.instantiateViews(self, views);
  4650. if (actions)
  4651. _this.instantiateActions(self, actions);
  4652. return self;
  4653. };
  4654. return this.cloneAndEnhance({ initializers: [initializer] });
  4655. };
  4656. ModelType.prototype.views = function (fn) {
  4657. var _this = this;
  4658. var viewInitializer = function (self) {
  4659. _this.instantiateViews(self, fn(self));
  4660. return self;
  4661. };
  4662. return this.cloneAndEnhance({ initializers: [viewInitializer] });
  4663. };
  4664. ModelType.prototype.instantiateViews = function (self, views) {
  4665. // check views return
  4666. if (!isPlainObject(views))
  4667. throw fail$1("views initializer should return a plain object containing views");
  4668. Object.keys(views).forEach(function (key) {
  4669. // is this a computed property?
  4670. var descriptor = Object.getOwnPropertyDescriptor(views, key);
  4671. if ("get" in descriptor) {
  4672. if (mobx.isComputedProp(self, key)) {
  4673. var computedValue = mobx._getAdministration(self, key);
  4674. // TODO: mobx currently does not allow redefining computes yet, pending #1121
  4675. // FIXME: this binds to the internals of mobx!
  4676. computedValue.derivation = descriptor.get;
  4677. computedValue.scope = self;
  4678. if (descriptor.set)
  4679. computedValue.setter = mobx.action(computedValue.name + "-setter", descriptor.set);
  4680. }
  4681. else {
  4682. mobx.computed(self, key, descriptor, true);
  4683. }
  4684. }
  4685. else if (typeof descriptor.value === "function") {
  4686. ( addHiddenWritableProp)(self, key, descriptor.value);
  4687. }
  4688. else {
  4689. throw fail$1("A view member should either be a function or getter based property");
  4690. }
  4691. });
  4692. };
  4693. ModelType.prototype.instantiate = function (parent, subpath, environment, initialValue) {
  4694. var value = isStateTreeNode(initialValue)
  4695. ? initialValue
  4696. : this.applySnapshotPreProcessor(initialValue);
  4697. return createObjectNode(this, parent, subpath, environment, value);
  4698. // Optimization: record all prop- view- and action names after first construction, and generate an optimal base class
  4699. // that pre-reserves all these fields for fast object-member lookups
  4700. };
  4701. ModelType.prototype.initializeChildNodes = function (objNode, initialSnapshot) {
  4702. if (initialSnapshot === void 0) { initialSnapshot = {}; }
  4703. var type = objNode.type;
  4704. var result = {};
  4705. type.forAllProps(function (name, childType) {
  4706. result[name] = childType.instantiate(objNode, name, undefined, initialSnapshot[name]);
  4707. });
  4708. return result;
  4709. };
  4710. ModelType.prototype.createNewInstance = function (childNodes) {
  4711. return mobx.observable.object(childNodes, EMPTY_OBJECT, mobxShallow);
  4712. };
  4713. ModelType.prototype.finalizeNewInstance = function (node, instance) {
  4714. addHiddenFinalProp(instance, "toString", objectTypeToString);
  4715. this.forAllProps(function (name) {
  4716. mobx._interceptReads(instance, name, node.unbox);
  4717. });
  4718. this.initializers.reduce(function (self, fn) { return fn(self); }, instance);
  4719. mobx.intercept(instance, this.willChange);
  4720. mobx.observe(instance, this.didChange);
  4721. };
  4722. ModelType.prototype.willChange = function (chg) {
  4723. // TODO: mobx typings don't seem to take into account that newValue can be set even when removing a prop
  4724. var change = chg;
  4725. var node = getStateTreeNode(change.object);
  4726. var subpath = change.name;
  4727. node.assertWritable({ subpath: subpath });
  4728. var childType = node.type.properties[subpath];
  4729. // only properties are typed, state are stored as-is references
  4730. if (childType) {
  4731. typecheckInternal(childType, change.newValue);
  4732. change.newValue = childType.reconcile(node.getChildNode(subpath), change.newValue, node, subpath);
  4733. }
  4734. return change;
  4735. };
  4736. ModelType.prototype.didChange = function (chg) {
  4737. // TODO: mobx typings don't seem to take into account that newValue can be set even when removing a prop
  4738. var change = chg;
  4739. var childNode = getStateTreeNode(change.object);
  4740. var childType = childNode.type.properties[change.name];
  4741. if (!childType) {
  4742. // don't emit patches for volatile state
  4743. return;
  4744. }
  4745. var oldChildValue = change.oldValue ? change.oldValue.snapshot : undefined;
  4746. childNode.emitPatch({
  4747. op: "replace",
  4748. path: escapeJsonPath(change.name),
  4749. value: change.newValue.snapshot,
  4750. oldValue: oldChildValue
  4751. }, childNode);
  4752. };
  4753. ModelType.prototype.getChildren = function (node) {
  4754. var _this = this;
  4755. var res = [];
  4756. this.forAllProps(function (name) {
  4757. res.push(_this.getChildNode(node, name));
  4758. });
  4759. return res;
  4760. };
  4761. ModelType.prototype.getChildNode = function (node, key) {
  4762. if (!(key in this.properties))
  4763. throw fail$1("Not a value property: " + key);
  4764. var childNode = mobx._getAdministration(node.storedValue, key).value; // TODO: blegh!
  4765. if (!childNode)
  4766. throw fail$1("Node not available for property " + key);
  4767. return childNode;
  4768. };
  4769. ModelType.prototype.getSnapshot = function (node, applyPostProcess) {
  4770. var _this = this;
  4771. if (applyPostProcess === void 0) { applyPostProcess = true; }
  4772. var res = {};
  4773. this.forAllProps(function (name, type) {
  4774. mobx.getAtom(node.storedValue, name).reportObserved();
  4775. res[name] = _this.getChildNode(node, name).snapshot;
  4776. });
  4777. if (applyPostProcess) {
  4778. return this.applySnapshotPostProcessor(res);
  4779. }
  4780. return res;
  4781. };
  4782. ModelType.prototype.processInitialSnapshot = function (childNodes) {
  4783. var processed = {};
  4784. Object.keys(childNodes).forEach(function (key) {
  4785. processed[key] = childNodes[key].getSnapshot();
  4786. });
  4787. return this.applySnapshotPostProcessor(processed);
  4788. };
  4789. ModelType.prototype.applyPatchLocally = function (node, subpath, patch) {
  4790. if (!(patch.op === "replace" || patch.op === "add")) {
  4791. throw fail$1("object does not support operation " + patch.op);
  4792. }
  4793. node.storedValue[subpath] = patch.value;
  4794. };
  4795. ModelType.prototype.applySnapshot = function (node, snapshot) {
  4796. var preProcessedSnapshot = this.applySnapshotPreProcessor(snapshot);
  4797. typecheckInternal(this, preProcessedSnapshot);
  4798. this.forAllProps(function (name) {
  4799. node.storedValue[name] = preProcessedSnapshot[name];
  4800. });
  4801. };
  4802. ModelType.prototype.applySnapshotPreProcessor = function (snapshot) {
  4803. var processor = this.preProcessor;
  4804. return processor ? processor.call(null, snapshot) : snapshot;
  4805. };
  4806. ModelType.prototype.applySnapshotPostProcessor = function (snapshot) {
  4807. var postProcessor = this.postProcessor;
  4808. if (postProcessor)
  4809. return postProcessor.call(null, snapshot);
  4810. return snapshot;
  4811. };
  4812. ModelType.prototype.getChildType = function (propertyName) {
  4813. assertIsString(propertyName, 1);
  4814. return this.properties[propertyName];
  4815. };
  4816. ModelType.prototype.isValidSnapshot = function (value, context) {
  4817. var _this = this;
  4818. var snapshot = this.applySnapshotPreProcessor(value);
  4819. if (!isPlainObject(snapshot)) {
  4820. return typeCheckFailure(context, snapshot, "Value is not a plain object");
  4821. }
  4822. return flattenTypeErrors(this.propertyNames.map(function (key) {
  4823. return _this.properties[key].validate(snapshot[key], getContextForPath(context, key, _this.properties[key]));
  4824. }));
  4825. };
  4826. ModelType.prototype.forAllProps = function (fn) {
  4827. var _this = this;
  4828. this.propertyNames.forEach(function (key) { return fn(key, _this.properties[key]); });
  4829. };
  4830. ModelType.prototype.describe = function () {
  4831. var _this = this;
  4832. // optimization: cache
  4833. return ("{ " +
  4834. this.propertyNames.map(function (key) { return key + ": " + _this.properties[key].describe(); }).join("; ") +
  4835. " }");
  4836. };
  4837. ModelType.prototype.getDefaultSnapshot = function () {
  4838. return EMPTY_OBJECT;
  4839. };
  4840. ModelType.prototype.removeChild = function (node, subpath) {
  4841. node.storedValue[subpath] = undefined;
  4842. };
  4843. __decorate([
  4844. mobx.action
  4845. ], ModelType.prototype, "applySnapshot", null);
  4846. return ModelType;
  4847. }(ComplexType));
  4848. /**
  4849. * `types.model` - Creates a new model type by providing a name, properties, volatile state and actions.
  4850. *
  4851. * See the [model type](https://github.com/mobxjs/mobx-state-tree#creating-models) description or the [getting started](https://github.com/mobxjs/mobx-state-tree/blob/master/docs/getting-started.md#getting-started-1) tutorial.
  4852. */
  4853. function model() {
  4854. var args = [];
  4855. for (var _i = 0; _i < arguments.length; _i++) {
  4856. args[_i] = arguments[_i];
  4857. }
  4858. var name = typeof args[0] === "string" ? args.shift() : "AnonymousModel";
  4859. var properties = args.shift() || {};
  4860. return new ModelType({ name: name, properties: properties });
  4861. }
  4862. /**
  4863. * `types.compose` - Composes a new model from one or more existing model types.
  4864. * This method can be invoked in two forms:
  4865. * Given 2 or more model types, the types are composed into a new Type.
  4866. * Given first parameter as a string and 2 or more model types,
  4867. * the types are composed into a new Type with the given name
  4868. */
  4869. function compose() {
  4870. var args = [];
  4871. for (var _i = 0; _i < arguments.length; _i++) {
  4872. args[_i] = arguments[_i];
  4873. }
  4874. // TODO: just join the base type names if no name is provided
  4875. var hasTypename = typeof args[0] === "string";
  4876. var typeName = hasTypename ? args[0] : "AnonymousModel";
  4877. if (hasTypename) {
  4878. args.shift();
  4879. }
  4880. // check all parameters
  4881. {
  4882. args.forEach(function (type, i) {
  4883. assertArg(type, isModelType, "mobx-state-tree model type", hasTypename ? i + 2 : i + 1);
  4884. });
  4885. }
  4886. return args
  4887. .reduce(function (prev, cur) {
  4888. return prev.cloneAndEnhance({
  4889. name: prev.name + "_" + cur.name,
  4890. properties: cur.properties,
  4891. initializers: cur.initializers,
  4892. preProcessor: function (snapshot) {
  4893. return cur.applySnapshotPreProcessor(prev.applySnapshotPreProcessor(snapshot));
  4894. },
  4895. postProcessor: function (snapshot) {
  4896. return cur.applySnapshotPostProcessor(prev.applySnapshotPostProcessor(snapshot));
  4897. }
  4898. });
  4899. })
  4900. .named(typeName);
  4901. }
  4902. /**
  4903. * Returns if a given value represents a model type.
  4904. *
  4905. * @param type
  4906. * @returns
  4907. */
  4908. function isModelType(type) {
  4909. return isType(type) && (type.flags & TypeFlags.Object) > 0;
  4910. }
  4911. // TODO: implement CoreType using types.custom ?
  4912. /**
  4913. * @internal
  4914. * @hidden
  4915. */
  4916. var CoreType = /** @class */ (function (_super) {
  4917. __extends(CoreType, _super);
  4918. function CoreType(name, flags, checker, initializer) {
  4919. if (initializer === void 0) { initializer = identity; }
  4920. var _this = _super.call(this, name) || this;
  4921. _this.flags = flags;
  4922. _this.checker = checker;
  4923. _this.initializer = initializer;
  4924. _this.flags = flags;
  4925. return _this;
  4926. }
  4927. CoreType.prototype.describe = function () {
  4928. return this.name;
  4929. };
  4930. CoreType.prototype.instantiate = function (parent, subpath, environment, initialValue) {
  4931. return createScalarNode(this, parent, subpath, environment, initialValue);
  4932. };
  4933. CoreType.prototype.createNewInstance = function (snapshot) {
  4934. return this.initializer(snapshot);
  4935. };
  4936. CoreType.prototype.isValidSnapshot = function (value, context) {
  4937. if (isPrimitive(value) && this.checker(value)) {
  4938. return typeCheckSuccess();
  4939. }
  4940. var typeName = this.name === "Date" ? "Date or a unix milliseconds timestamp" : this.name;
  4941. return typeCheckFailure(context, value, "Value is not a " + typeName);
  4942. };
  4943. return CoreType;
  4944. }(SimpleType));
  4945. /**
  4946. * `types.string` - Creates a type that can only contain a string value.
  4947. * This type is used for string values by default
  4948. *
  4949. * Example:
  4950. * ```ts
  4951. * const Person = types.model({
  4952. * firstName: types.string,
  4953. * lastName: "Doe"
  4954. * })
  4955. * ```
  4956. */
  4957. // tslint:disable-next-line:variable-name
  4958. var string = new CoreType("string", TypeFlags.String, function (v) { return typeof v === "string"; });
  4959. /**
  4960. * `types.number` - Creates a type that can only contain a numeric value.
  4961. * This type is used for numeric values by default
  4962. *
  4963. * Example:
  4964. * ```ts
  4965. * const Vector = types.model({
  4966. * x: types.number,
  4967. * y: 1.5
  4968. * })
  4969. * ```
  4970. */
  4971. // tslint:disable-next-line:variable-name
  4972. var number = new CoreType("number", TypeFlags.Number, function (v) { return typeof v === "number"; });
  4973. /**
  4974. * `types.integer` - Creates a type that can only contain an integer value.
  4975. * This type is used for integer values by default
  4976. *
  4977. * Example:
  4978. * ```ts
  4979. * const Size = types.model({
  4980. * width: types.integer,
  4981. * height: 10
  4982. * })
  4983. * ```
  4984. */
  4985. // tslint:disable-next-line:variable-name
  4986. var integer = new CoreType("integer", TypeFlags.Integer, function (v) { return isInteger(v); });
  4987. /**
  4988. * `types.boolean` - Creates a type that can only contain a boolean value.
  4989. * This type is used for boolean values by default
  4990. *
  4991. * Example:
  4992. * ```ts
  4993. * const Thing = types.model({
  4994. * isCool: types.boolean,
  4995. * isAwesome: false
  4996. * })
  4997. * ```
  4998. */
  4999. // tslint:disable-next-line:variable-name
  5000. var boolean = new CoreType("boolean", TypeFlags.Boolean, function (v) { return typeof v === "boolean"; });
  5001. /**
  5002. * `types.null` - The type of the value `null`
  5003. */
  5004. var nullType = new CoreType("null", TypeFlags.Null, function (v) { return v === null; });
  5005. /**
  5006. * `types.undefined` - The type of the value `undefined`
  5007. */
  5008. var undefinedType = new CoreType("undefined", TypeFlags.Undefined, function (v) { return v === undefined; });
  5009. var _DatePrimitive = new CoreType("Date", TypeFlags.Date, function (v) { return typeof v === "number" || v instanceof Date; }, function (v) { return (v instanceof Date ? v : new Date(v)); });
  5010. _DatePrimitive.getSnapshot = function (node) {
  5011. return node.storedValue.getTime();
  5012. };
  5013. /**
  5014. * `types.Date` - Creates a type that can only contain a javascript Date value.
  5015. *
  5016. * Example:
  5017. * ```ts
  5018. * const LogLine = types.model({
  5019. * timestamp: types.Date,
  5020. * })
  5021. *
  5022. * LogLine.create({ timestamp: new Date() })
  5023. * ```
  5024. */
  5025. var DatePrimitive = _DatePrimitive;
  5026. /**
  5027. * @internal
  5028. * @hidden
  5029. */
  5030. function getPrimitiveFactoryFromValue(value) {
  5031. switch (typeof value) {
  5032. case "string":
  5033. return string;
  5034. case "number":
  5035. return number; // In the future, isInteger(value) ? integer : number would be interesting, but would be too breaking for now
  5036. case "boolean":
  5037. return boolean;
  5038. case "object":
  5039. if (value instanceof Date)
  5040. return DatePrimitive;
  5041. }
  5042. throw fail$1("Cannot determine primitive type from value " + value);
  5043. }
  5044. /**
  5045. * Returns if a given value represents a primitive type.
  5046. *
  5047. * @param type
  5048. * @returns
  5049. */
  5050. function isPrimitiveType(type) {
  5051. return (isType(type) &&
  5052. (type.flags &
  5053. (TypeFlags.String |
  5054. TypeFlags.Number |
  5055. TypeFlags.Integer |
  5056. TypeFlags.Boolean |
  5057. TypeFlags.Date)) >
  5058. 0);
  5059. }
  5060. /**
  5061. * @internal
  5062. * @hidden
  5063. */
  5064. var Literal = /** @class */ (function (_super) {
  5065. __extends(Literal, _super);
  5066. function Literal(value) {
  5067. var _this = _super.call(this, JSON.stringify(value)) || this;
  5068. _this.flags = TypeFlags.Literal;
  5069. _this.value = value;
  5070. return _this;
  5071. }
  5072. Literal.prototype.instantiate = function (parent, subpath, environment, initialValue) {
  5073. return createScalarNode(this, parent, subpath, environment, initialValue);
  5074. };
  5075. Literal.prototype.describe = function () {
  5076. return JSON.stringify(this.value);
  5077. };
  5078. Literal.prototype.isValidSnapshot = function (value, context) {
  5079. if (isPrimitive(value) && value === this.value) {
  5080. return typeCheckSuccess();
  5081. }
  5082. return typeCheckFailure(context, value, "Value is not a literal " + JSON.stringify(this.value));
  5083. };
  5084. return Literal;
  5085. }(SimpleType));
  5086. /**
  5087. * `types.literal` - The literal type will return a type that will match only the exact given type.
  5088. * The given value must be a primitive, in order to be serialized to a snapshot correctly.
  5089. * You can use literal to match exact strings for example the exact male or female string.
  5090. *
  5091. * Example:
  5092. * ```ts
  5093. * const Person = types.model({
  5094. * name: types.string,
  5095. * gender: types.union(types.literal('male'), types.literal('female'))
  5096. * })
  5097. * ```
  5098. *
  5099. * @param value The value to use in the strict equal check
  5100. * @returns
  5101. */
  5102. function literal(value) {
  5103. // check that the given value is a primitive
  5104. assertArg(value, isPrimitive, "primitive", 1);
  5105. return new Literal(value);
  5106. }
  5107. /**
  5108. * Returns if a given value represents a literal type.
  5109. *
  5110. * @param type
  5111. * @returns
  5112. */
  5113. function isLiteralType(type) {
  5114. return isType(type) && (type.flags & TypeFlags.Literal) > 0;
  5115. }
  5116. var Refinement = /** @class */ (function (_super) {
  5117. __extends(Refinement, _super);
  5118. function Refinement(name, _subtype, _predicate, _message) {
  5119. var _this = _super.call(this, name) || this;
  5120. _this._subtype = _subtype;
  5121. _this._predicate = _predicate;
  5122. _this._message = _message;
  5123. return _this;
  5124. }
  5125. Object.defineProperty(Refinement.prototype, "flags", {
  5126. get: function () {
  5127. return this._subtype.flags | TypeFlags.Refinement;
  5128. },
  5129. enumerable: true,
  5130. configurable: true
  5131. });
  5132. Refinement.prototype.describe = function () {
  5133. return this.name;
  5134. };
  5135. Refinement.prototype.instantiate = function (parent, subpath, environment, initialValue) {
  5136. // create the child type
  5137. return this._subtype.instantiate(parent, subpath, environment, initialValue);
  5138. };
  5139. Refinement.prototype.isAssignableFrom = function (type) {
  5140. return this._subtype.isAssignableFrom(type);
  5141. };
  5142. Refinement.prototype.isValidSnapshot = function (value, context) {
  5143. var subtypeErrors = this._subtype.validate(value, context);
  5144. if (subtypeErrors.length > 0)
  5145. return subtypeErrors;
  5146. var snapshot = isStateTreeNode(value) ? getStateTreeNode(value).snapshot : value;
  5147. if (!this._predicate(snapshot)) {
  5148. return typeCheckFailure(context, value, this._message(value));
  5149. }
  5150. return typeCheckSuccess();
  5151. };
  5152. Refinement.prototype.reconcile = function (current, newValue, parent, subpath) {
  5153. return this._subtype.reconcile(current, newValue, parent, subpath);
  5154. };
  5155. Refinement.prototype.getSubTypes = function () {
  5156. return this._subtype;
  5157. };
  5158. return Refinement;
  5159. }(BaseType));
  5160. /**
  5161. * `types.refinement` - Creates a type that is more specific than the base type, e.g. `types.refinement(types.string, value => value.length > 5)` to create a type of strings that can only be longer then 5.
  5162. *
  5163. * @param name
  5164. * @param type
  5165. * @param predicate
  5166. * @returns
  5167. */
  5168. function refinement() {
  5169. var args = [];
  5170. for (var _i = 0; _i < arguments.length; _i++) {
  5171. args[_i] = arguments[_i];
  5172. }
  5173. var name = typeof args[0] === "string" ? args.shift() : isType(args[0]) ? args[0].name : null;
  5174. var type = args[0];
  5175. var predicate = args[1];
  5176. var message = args[2]
  5177. ? args[2]
  5178. : function (v) { return "Value does not respect the refinement predicate"; };
  5179. // ensures all parameters are correct
  5180. assertIsType(type, [1, 2]);
  5181. assertIsString(name, 1);
  5182. assertIsFunction(predicate, [2, 3]);
  5183. assertIsFunction(message, [3, 4]);
  5184. return new Refinement(name, type, predicate, message);
  5185. }
  5186. /**
  5187. * Returns if a given value is a refinement type.
  5188. *
  5189. * @param type
  5190. * @returns
  5191. */
  5192. function isRefinementType(type) {
  5193. return (type.flags & TypeFlags.Refinement) > 0;
  5194. }
  5195. /**
  5196. * `types.enumeration` - Can be used to create an string based enumeration.
  5197. * (note: this methods is just sugar for a union of string literals)
  5198. *
  5199. * Example:
  5200. * ```ts
  5201. * const TrafficLight = types.model({
  5202. * color: types.enumeration("Color", ["Red", "Orange", "Green"])
  5203. * })
  5204. * ```
  5205. *
  5206. * @param name descriptive name of the enumeration (optional)
  5207. * @param options possible values this enumeration can have
  5208. * @returns
  5209. */
  5210. function enumeration(name, options) {
  5211. var realOptions = typeof name === "string" ? options : name;
  5212. // check all options
  5213. {
  5214. realOptions.forEach(function (option, i) {
  5215. assertIsString(option, i + 1);
  5216. });
  5217. }
  5218. var type = union.apply(void 0, __spread(realOptions.map(function (option) { return literal("" + option); })));
  5219. if (typeof name === "string")
  5220. type.name = name;
  5221. return type;
  5222. }
  5223. /**
  5224. * @internal
  5225. * @hidden
  5226. */
  5227. var Union = /** @class */ (function (_super) {
  5228. __extends(Union, _super);
  5229. function Union(name, _types, options) {
  5230. var _this = _super.call(this, name) || this;
  5231. _this._types = _types;
  5232. _this._eager = true;
  5233. options = __assign({ eager: true, dispatcher: undefined }, options);
  5234. _this._dispatcher = options.dispatcher;
  5235. if (!options.eager)
  5236. _this._eager = false;
  5237. return _this;
  5238. }
  5239. Object.defineProperty(Union.prototype, "flags", {
  5240. get: function () {
  5241. var result = TypeFlags.Union;
  5242. this._types.forEach(function (type) {
  5243. result |= type.flags;
  5244. });
  5245. return result;
  5246. },
  5247. enumerable: true,
  5248. configurable: true
  5249. });
  5250. Union.prototype.isAssignableFrom = function (type) {
  5251. return this._types.some(function (subType) { return subType.isAssignableFrom(type); });
  5252. };
  5253. Union.prototype.describe = function () {
  5254. return "(" + this._types.map(function (factory) { return factory.describe(); }).join(" | ") + ")";
  5255. };
  5256. Union.prototype.instantiate = function (parent, subpath, environment, initialValue) {
  5257. var type = this.determineType(initialValue, undefined);
  5258. if (!type)
  5259. throw fail$1("No matching type for union " + this.describe()); // can happen in prod builds
  5260. return type.instantiate(parent, subpath, environment, initialValue);
  5261. };
  5262. Union.prototype.reconcile = function (current, newValue, parent, subpath) {
  5263. var type = this.determineType(newValue, current.type);
  5264. if (!type)
  5265. throw fail$1("No matching type for union " + this.describe()); // can happen in prod builds
  5266. return type.reconcile(current, newValue, parent, subpath);
  5267. };
  5268. Union.prototype.determineType = function (value, reconcileCurrentType) {
  5269. // try the dispatcher, if defined
  5270. if (this._dispatcher) {
  5271. return this._dispatcher(value);
  5272. }
  5273. // find the most accomodating type
  5274. // if we are using reconciliation try the current node type first (fix for #1045)
  5275. if (reconcileCurrentType) {
  5276. if (reconcileCurrentType.is(value)) {
  5277. return reconcileCurrentType;
  5278. }
  5279. return this._types.filter(function (t) { return t !== reconcileCurrentType; }).find(function (type) { return type.is(value); });
  5280. }
  5281. else {
  5282. return this._types.find(function (type) { return type.is(value); });
  5283. }
  5284. };
  5285. Union.prototype.isValidSnapshot = function (value, context) {
  5286. if (this._dispatcher) {
  5287. return this._dispatcher(value).validate(value, context);
  5288. }
  5289. var allErrors = [];
  5290. var applicableTypes = 0;
  5291. for (var i = 0; i < this._types.length; i++) {
  5292. var type = this._types[i];
  5293. var errors = type.validate(value, context);
  5294. if (errors.length === 0) {
  5295. if (this._eager)
  5296. return typeCheckSuccess();
  5297. else
  5298. applicableTypes++;
  5299. }
  5300. else {
  5301. allErrors.push(errors);
  5302. }
  5303. }
  5304. if (applicableTypes === 1)
  5305. return typeCheckSuccess();
  5306. return typeCheckFailure(context, value, "No type is applicable for the union").concat(flattenTypeErrors(allErrors));
  5307. };
  5308. Union.prototype.getSubTypes = function () {
  5309. return this._types;
  5310. };
  5311. return Union;
  5312. }(BaseType));
  5313. /**
  5314. * `types.union` - Create a union of multiple types. If the correct type cannot be inferred unambiguously from a snapshot, provide a dispatcher function of the form `(snapshot) => Type`.
  5315. *
  5316. * @param optionsOrType
  5317. * @param otherTypes
  5318. * @returns
  5319. */
  5320. function union(optionsOrType) {
  5321. var otherTypes = [];
  5322. for (var _i = 1; _i < arguments.length; _i++) {
  5323. otherTypes[_i - 1] = arguments[_i];
  5324. }
  5325. var options = isType(optionsOrType) ? undefined : optionsOrType;
  5326. var types = isType(optionsOrType) ? __spread([optionsOrType], otherTypes) : otherTypes;
  5327. var name = "(" + types.map(function (type) { return type.name; }).join(" | ") + ")";
  5328. // check all options
  5329. {
  5330. if (options) {
  5331. assertArg(options, function (o) { return isPlainObject(o); }, "object { eager?: boolean, dispatcher?: Function }", 1);
  5332. }
  5333. types.forEach(function (type, i) {
  5334. assertIsType(type, options ? i + 2 : i + 1);
  5335. });
  5336. }
  5337. return new Union(name, types, options);
  5338. }
  5339. /**
  5340. * Returns if a given value represents a union type.
  5341. *
  5342. * @param type
  5343. * @returns
  5344. */
  5345. function isUnionType(type) {
  5346. return (type.flags & TypeFlags.Union) > 0;
  5347. }
  5348. /**
  5349. * @hidden
  5350. * @internal
  5351. */
  5352. var OptionalValue = /** @class */ (function (_super) {
  5353. __extends(OptionalValue, _super);
  5354. function OptionalValue(_subtype, _defaultValue, optionalValues) {
  5355. var _this = _super.call(this, _subtype.name) || this;
  5356. _this._subtype = _subtype;
  5357. _this._defaultValue = _defaultValue;
  5358. _this.optionalValues = optionalValues;
  5359. return _this;
  5360. }
  5361. Object.defineProperty(OptionalValue.prototype, "flags", {
  5362. get: function () {
  5363. return this._subtype.flags | TypeFlags.Optional;
  5364. },
  5365. enumerable: true,
  5366. configurable: true
  5367. });
  5368. OptionalValue.prototype.describe = function () {
  5369. return this._subtype.describe() + "?";
  5370. };
  5371. OptionalValue.prototype.instantiate = function (parent, subpath, environment, initialValue) {
  5372. if (this.optionalValues.indexOf(initialValue) >= 0) {
  5373. var defaultInstanceOrSnapshot = this.getDefaultInstanceOrSnapshot();
  5374. return this._subtype.instantiate(parent, subpath, environment, defaultInstanceOrSnapshot);
  5375. }
  5376. return this._subtype.instantiate(parent, subpath, environment, initialValue);
  5377. };
  5378. OptionalValue.prototype.reconcile = function (current, newValue, parent, subpath) {
  5379. return this._subtype.reconcile(current, this.optionalValues.indexOf(newValue) < 0 && this._subtype.is(newValue)
  5380. ? newValue
  5381. : this.getDefaultInstanceOrSnapshot(), parent, subpath);
  5382. };
  5383. OptionalValue.prototype.getDefaultInstanceOrSnapshot = function () {
  5384. var defaultInstanceOrSnapshot = typeof this._defaultValue === "function"
  5385. ? this._defaultValue()
  5386. : this._defaultValue;
  5387. // while static values are already snapshots and checked on types.optional
  5388. // generator functions must always be rechecked just in case
  5389. if (typeof this._defaultValue === "function") {
  5390. typecheckInternal(this, defaultInstanceOrSnapshot);
  5391. }
  5392. return defaultInstanceOrSnapshot;
  5393. };
  5394. OptionalValue.prototype.isValidSnapshot = function (value, context) {
  5395. // defaulted values can be skipped
  5396. if (this.optionalValues.indexOf(value) >= 0) {
  5397. return typeCheckSuccess();
  5398. }
  5399. // bounce validation to the sub-type
  5400. return this._subtype.validate(value, context);
  5401. };
  5402. OptionalValue.prototype.isAssignableFrom = function (type) {
  5403. return this._subtype.isAssignableFrom(type);
  5404. };
  5405. OptionalValue.prototype.getSubTypes = function () {
  5406. return this._subtype;
  5407. };
  5408. return OptionalValue;
  5409. }(BaseType));
  5410. function checkOptionalPreconditions(type, defaultValueOrFunction) {
  5411. // make sure we never pass direct instances
  5412. if (typeof defaultValueOrFunction !== "function" && isStateTreeNode(defaultValueOrFunction)) {
  5413. throw fail$1("default value cannot be an instance, pass a snapshot or a function that creates an instance/snapshot instead");
  5414. }
  5415. assertIsType(type, 1);
  5416. {
  5417. // we only check default values if they are passed directly
  5418. // if they are generator functions they will be checked once they are generated
  5419. // we don't check generator function results here to avoid generating a node just for type-checking purposes
  5420. // which might generate side-effects
  5421. if (typeof defaultValueOrFunction !== "function") {
  5422. typecheckInternal(type, defaultValueOrFunction);
  5423. }
  5424. }
  5425. }
  5426. /**
  5427. * `types.optional` - Can be used to create a property with a default value.
  5428. *
  5429. * Depending on the third argument (`optionalValues`) there are two ways of operation:
  5430. * - If the argument is not provided, then if a value is not provided in the snapshot (`undefined` or missing),
  5431. * it will default to the provided `defaultValue`
  5432. * - If the argument is provided, then if the value in the snapshot matches one of the optional values inside the array then it will
  5433. * default to the provided `defaultValue`. Additionally, if one of the optional values inside the array is `undefined` then a missing
  5434. * property is also valid.
  5435. *
  5436. * Note that it is also possible to include values of the same type as the intended subtype as optional values,
  5437. * in this case the optional value will be transformed into the `defaultValue` (e.g. `types.optional(types.string, "unnamed", [undefined, ""])`
  5438. * will transform the snapshot values `undefined` (and therefore missing) and empty strings into the string `"unnamed"` when it gets
  5439. * instantiated).
  5440. *
  5441. * If `defaultValue` is a function, the function will be invoked for every new instance.
  5442. * Applying a snapshot in which the optional value is one of the optional values (or `undefined`/_not_ present if none are provided) causes the
  5443. * value to be reset.
  5444. *
  5445. * Example:
  5446. * ```ts
  5447. * const Todo = types.model({
  5448. * title: types.string,
  5449. * subtitle1: types.optional(types.string, "", [null]),
  5450. * subtitle2: types.optional(types.string, "", [null, undefined]),
  5451. * done: types.optional(types.boolean, false),
  5452. * created: types.optional(types.Date, () => new Date()),
  5453. * })
  5454. *
  5455. * // if done is missing / undefined it will become false
  5456. * // if created is missing / undefined it will get a freshly generated timestamp
  5457. * // if subtitle1 is null it will default to "", but it cannot be missing or undefined
  5458. * // if subtitle2 is null or undefined it will default to ""; since it can be undefined it can also be missing
  5459. * const todo = Todo.create({ title: "Get coffee", subtitle1: null })
  5460. * ```
  5461. *
  5462. * @param type
  5463. * @param defaultValueOrFunction
  5464. * @param optionalValues an optional array with zero or more primitive values (string, number, boolean, null or undefined)
  5465. * that will be converted into the default. `[ undefined ]` is assumed when none is provided
  5466. * @returns
  5467. */
  5468. function optional(type, defaultValueOrFunction, optionalValues) {
  5469. checkOptionalPreconditions(type, defaultValueOrFunction);
  5470. return new OptionalValue(type, defaultValueOrFunction, optionalValues ? optionalValues : undefinedAsOptionalValues);
  5471. }
  5472. var undefinedAsOptionalValues = [undefined];
  5473. /**
  5474. * Returns if a value represents an optional type.
  5475. *
  5476. * @template IT
  5477. * @param type
  5478. * @returns
  5479. */
  5480. function isOptionalType(type) {
  5481. return isType(type) && (type.flags & TypeFlags.Optional) > 0;
  5482. }
  5483. var optionalUndefinedType = optional(undefinedType, undefined);
  5484. var optionalNullType = optional(nullType, null);
  5485. /**
  5486. * `types.maybe` - Maybe will make a type nullable, and also optional.
  5487. * The value `undefined` will be used to represent nullability.
  5488. *
  5489. * @param type
  5490. * @returns
  5491. */
  5492. function maybe(type) {
  5493. assertIsType(type, 1);
  5494. return union(type, optionalUndefinedType);
  5495. }
  5496. /**
  5497. * `types.maybeNull` - Maybe will make a type nullable, and also optional.
  5498. * The value `null` will be used to represent no value.
  5499. *
  5500. * @param type
  5501. * @returns
  5502. */
  5503. function maybeNull(type) {
  5504. assertIsType(type, 1);
  5505. return union(type, optionalNullType);
  5506. }
  5507. var Late = /** @class */ (function (_super) {
  5508. __extends(Late, _super);
  5509. function Late(name, _definition) {
  5510. var _this = _super.call(this, name) || this;
  5511. _this._definition = _definition;
  5512. return _this;
  5513. }
  5514. Object.defineProperty(Late.prototype, "flags", {
  5515. get: function () {
  5516. return (this._subType ? this._subType.flags : 0) | TypeFlags.Late;
  5517. },
  5518. enumerable: true,
  5519. configurable: true
  5520. });
  5521. Late.prototype.getSubType = function (mustSucceed) {
  5522. if (!this._subType) {
  5523. var t = undefined;
  5524. try {
  5525. t = this._definition();
  5526. }
  5527. catch (e) {
  5528. if (e instanceof ReferenceError)
  5529. // can happen in strict ES5 code when a definition is self refering
  5530. t = undefined;
  5531. else
  5532. throw e;
  5533. }
  5534. if (mustSucceed && t === undefined)
  5535. throw fail$1("Late type seems to be used too early, the definition (still) returns undefined");
  5536. if (t) {
  5537. if ( !isType(t))
  5538. throw fail$1("Failed to determine subtype, make sure types.late returns a type definition.");
  5539. this._subType = t;
  5540. }
  5541. }
  5542. return this._subType;
  5543. };
  5544. Late.prototype.instantiate = function (parent, subpath, environment, initialValue) {
  5545. return this.getSubType(true).instantiate(parent, subpath, environment, initialValue);
  5546. };
  5547. Late.prototype.reconcile = function (current, newValue, parent, subpath) {
  5548. return this.getSubType(true).reconcile(current, newValue, parent, subpath);
  5549. };
  5550. Late.prototype.describe = function () {
  5551. var t = this.getSubType(false);
  5552. return t ? t.name : "<uknown late type>";
  5553. };
  5554. Late.prototype.isValidSnapshot = function (value, context) {
  5555. var t = this.getSubType(false);
  5556. if (!t) {
  5557. // See #916; the variable the definition closure is pointing to wasn't defined yet, so can't be evaluted yet here
  5558. return typeCheckSuccess();
  5559. }
  5560. return t.validate(value, context);
  5561. };
  5562. Late.prototype.isAssignableFrom = function (type) {
  5563. var t = this.getSubType(false);
  5564. return t ? t.isAssignableFrom(type) : false;
  5565. };
  5566. Late.prototype.getSubTypes = function () {
  5567. var subtype = this.getSubType(false);
  5568. return subtype ? subtype : cannotDetermineSubtype;
  5569. };
  5570. return Late;
  5571. }(BaseType));
  5572. /**
  5573. * `types.late` - Defines a type that gets implemented later. This is useful when you have to deal with circular dependencies.
  5574. * Please notice that when defining circular dependencies TypeScript isn't smart enough to inference them.
  5575. *
  5576. * Example:
  5577. * ```ts
  5578. * // TypeScript isn't smart enough to infer self referencing types.
  5579. * const Node = types.model({
  5580. * children: types.array(types.late((): IAnyModelType => Node)) // then typecast each array element to Instance<typeof Node>
  5581. * })
  5582. * ```
  5583. *
  5584. * @param name The name to use for the type that will be returned.
  5585. * @param type A function that returns the type that will be defined.
  5586. * @returns
  5587. */
  5588. function late(nameOrType, maybeType) {
  5589. var name = typeof nameOrType === "string" ? nameOrType : "late(" + nameOrType.toString() + ")";
  5590. var type = typeof nameOrType === "string" ? maybeType : nameOrType;
  5591. // checks that the type is actually a late type
  5592. {
  5593. if (!(typeof type === "function" && type.length === 0))
  5594. throw fail$1("Invalid late type, expected a function with zero arguments that returns a type, got: " +
  5595. type);
  5596. }
  5597. return new Late(name, type);
  5598. }
  5599. /**
  5600. * Returns if a given value represents a late type.
  5601. *
  5602. * @param type
  5603. * @returns
  5604. */
  5605. function isLateType(type) {
  5606. return isType(type) && (type.flags & TypeFlags.Late) > 0;
  5607. }
  5608. /**
  5609. * @internal
  5610. * @hidden
  5611. */
  5612. var Frozen = /** @class */ (function (_super) {
  5613. __extends(Frozen, _super);
  5614. function Frozen(subType) {
  5615. var _this = _super.call(this, subType ? "frozen(" + subType.name + ")" : "frozen") || this;
  5616. _this.subType = subType;
  5617. _this.flags = TypeFlags.Frozen;
  5618. return _this;
  5619. }
  5620. Frozen.prototype.describe = function () {
  5621. return "<any immutable value>";
  5622. };
  5623. Frozen.prototype.instantiate = function (parent, subpath, environment, value) {
  5624. // create the node
  5625. return createScalarNode(this, parent, subpath, environment, deepFreeze(value));
  5626. };
  5627. Frozen.prototype.isValidSnapshot = function (value, context) {
  5628. if (!isSerializable(value)) {
  5629. return typeCheckFailure(context, value, "Value is not serializable and cannot be frozen");
  5630. }
  5631. if (this.subType)
  5632. return this.subType.validate(value, context);
  5633. return typeCheckSuccess();
  5634. };
  5635. return Frozen;
  5636. }(SimpleType));
  5637. var untypedFrozenInstance = new Frozen();
  5638. /**
  5639. * `types.frozen` - Frozen can be used to store any value that is serializable in itself (that is valid JSON).
  5640. * Frozen values need to be immutable or treated as if immutable. They need be serializable as well.
  5641. * Values stored in frozen will snapshotted as-is by MST, and internal changes will not be tracked.
  5642. *
  5643. * This is useful to store complex, but immutable values like vectors etc. It can form a powerful bridge to parts of your application that should be immutable, or that assume data to be immutable.
  5644. *
  5645. * Note: if you want to store free-form state that is mutable, or not serializeable, consider using volatile state instead.
  5646. *
  5647. * Frozen properties can be defined in three different ways
  5648. * 1. `types.frozen(SubType)` - provide a valid MST type and frozen will check if the provided data conforms the snapshot for that type
  5649. * 2. `types.frozen({ someDefaultValue: true})` - provide a primitive value, object or array, and MST will infer the type from that object, and also make it the default value for the field
  5650. * 3. `types.frozen<TypeScriptType>()` - provide a typescript type, to help in strongly typing the field (design time only)
  5651. *
  5652. * Example:
  5653. * ```ts
  5654. * const GameCharacter = types.model({
  5655. * name: string,
  5656. * location: types.frozen({ x: 0, y: 0})
  5657. * })
  5658. *
  5659. * const hero = GameCharacter.create({
  5660. * name: "Mario",
  5661. * location: { x: 7, y: 4 }
  5662. * })
  5663. *
  5664. * hero.location = { x: 10, y: 2 } // OK
  5665. * hero.location.x = 7 // Not ok!
  5666. * ```
  5667. *
  5668. * ```ts
  5669. * type Point = { x: number, y: number }
  5670. * const Mouse = types.model({
  5671. * loc: types.frozen<Point>()
  5672. * })
  5673. * ```
  5674. *
  5675. * @param defaultValueOrType
  5676. * @returns
  5677. */
  5678. function frozen(arg) {
  5679. if (arguments.length === 0)
  5680. return untypedFrozenInstance;
  5681. else if (isType(arg))
  5682. return new Frozen(arg);
  5683. else
  5684. return optional(untypedFrozenInstance, arg);
  5685. }
  5686. /**
  5687. * Returns if a given value represents a frozen type.
  5688. *
  5689. * @param type
  5690. * @returns
  5691. */
  5692. function isFrozenType(type) {
  5693. return isType(type) && (type.flags & TypeFlags.Frozen) > 0;
  5694. }
  5695. function getInvalidationCause(hook) {
  5696. switch (hook) {
  5697. case Hook.beforeDestroy:
  5698. return "destroy";
  5699. case Hook.beforeDetach:
  5700. return "detach";
  5701. default:
  5702. return undefined;
  5703. }
  5704. }
  5705. var StoredReference = /** @class */ (function () {
  5706. function StoredReference(value, targetType) {
  5707. this.targetType = targetType;
  5708. if (isValidIdentifier(value)) {
  5709. this.identifier = value;
  5710. }
  5711. else if (isStateTreeNode(value)) {
  5712. var targetNode = getStateTreeNode(value);
  5713. if (!targetNode.identifierAttribute)
  5714. throw fail$1("Can only store references with a defined identifier attribute.");
  5715. var id = targetNode.unnormalizedIdentifier;
  5716. if (id === null || id === undefined) {
  5717. throw fail$1("Can only store references to tree nodes with a defined identifier.");
  5718. }
  5719. this.identifier = id;
  5720. }
  5721. else {
  5722. throw fail$1("Can only store references to tree nodes or identifiers, got: '" + value + "'");
  5723. }
  5724. }
  5725. StoredReference.prototype.updateResolvedReference = function (node) {
  5726. var normalizedId = normalizeIdentifier(this.identifier);
  5727. var root = node.root;
  5728. var lastCacheModification = root.identifierCache.getLastCacheModificationPerId(normalizedId);
  5729. if (!this.resolvedReference ||
  5730. this.resolvedReference.lastCacheModification !== lastCacheModification) {
  5731. var targetType = this.targetType;
  5732. // reference was initialized with the identifier of the target
  5733. var target = root.identifierCache.resolve(targetType, normalizedId);
  5734. if (!target) {
  5735. throw new InvalidReferenceError("[mobx-state-tree] Failed to resolve reference '" + this.identifier + "' to type '" + this.targetType.name + "' (from node: " + node.path + ")");
  5736. }
  5737. this.resolvedReference = {
  5738. node: target,
  5739. lastCacheModification: lastCacheModification
  5740. };
  5741. }
  5742. };
  5743. Object.defineProperty(StoredReference.prototype, "resolvedValue", {
  5744. get: function () {
  5745. this.updateResolvedReference(this.node);
  5746. return this.resolvedReference.node.value;
  5747. },
  5748. enumerable: true,
  5749. configurable: true
  5750. });
  5751. return StoredReference;
  5752. }());
  5753. /**
  5754. * @internal
  5755. * @hidden
  5756. */
  5757. var InvalidReferenceError = /** @class */ (function (_super) {
  5758. __extends(InvalidReferenceError, _super);
  5759. function InvalidReferenceError(m) {
  5760. var _this = _super.call(this, m) || this;
  5761. Object.setPrototypeOf(_this, InvalidReferenceError.prototype);
  5762. return _this;
  5763. }
  5764. return InvalidReferenceError;
  5765. }(Error));
  5766. /**
  5767. * @internal
  5768. * @hidden
  5769. */
  5770. var BaseReferenceType = /** @class */ (function (_super) {
  5771. __extends(BaseReferenceType, _super);
  5772. function BaseReferenceType(targetType, onInvalidated) {
  5773. var _this = _super.call(this, "reference(" + targetType.name + ")") || this;
  5774. _this.targetType = targetType;
  5775. _this.onInvalidated = onInvalidated;
  5776. _this.flags = TypeFlags.Reference;
  5777. return _this;
  5778. }
  5779. BaseReferenceType.prototype.describe = function () {
  5780. return this.name;
  5781. };
  5782. BaseReferenceType.prototype.isAssignableFrom = function (type) {
  5783. return this.targetType.isAssignableFrom(type);
  5784. };
  5785. BaseReferenceType.prototype.isValidSnapshot = function (value, context) {
  5786. return isValidIdentifier(value)
  5787. ? typeCheckSuccess()
  5788. : typeCheckFailure(context, value, "Value is not a valid identifier, which is a string or a number");
  5789. };
  5790. BaseReferenceType.prototype.fireInvalidated = function (cause, storedRefNode, referenceId, refTargetNode) {
  5791. // to actually invalidate a reference we need an alive parent,
  5792. // since it is a scalar value (immutable-ish) and we need to change it
  5793. // from the parent
  5794. var storedRefParentNode = storedRefNode.parent;
  5795. if (!storedRefParentNode || !storedRefParentNode.isAlive) {
  5796. return;
  5797. }
  5798. var storedRefParentValue = storedRefParentNode.storedValue;
  5799. if (!storedRefParentValue) {
  5800. return;
  5801. }
  5802. this.onInvalidated({
  5803. cause: cause,
  5804. parent: storedRefParentValue,
  5805. invalidTarget: refTargetNode ? refTargetNode.storedValue : undefined,
  5806. invalidId: referenceId,
  5807. replaceRef: function (newRef) {
  5808. applyPatch(storedRefNode.root.storedValue, {
  5809. op: "replace",
  5810. value: newRef,
  5811. path: storedRefNode.path
  5812. });
  5813. },
  5814. removeRef: function () {
  5815. if (isModelType(storedRefParentNode.type)) {
  5816. this.replaceRef(undefined);
  5817. }
  5818. else {
  5819. applyPatch(storedRefNode.root.storedValue, {
  5820. op: "remove",
  5821. path: storedRefNode.path
  5822. });
  5823. }
  5824. }
  5825. });
  5826. };
  5827. BaseReferenceType.prototype.addTargetNodeWatcher = function (storedRefNode, referenceId) {
  5828. var _this = this;
  5829. // this will make sure the target node becomes created
  5830. var refTargetValue = this.getValue(storedRefNode);
  5831. if (!refTargetValue) {
  5832. return undefined;
  5833. }
  5834. var refTargetNode = getStateTreeNode(refTargetValue);
  5835. var hookHandler = function (_, refTargetNodeHook) {
  5836. var cause = getInvalidationCause(refTargetNodeHook);
  5837. if (!cause) {
  5838. return;
  5839. }
  5840. _this.fireInvalidated(cause, storedRefNode, referenceId, refTargetNode);
  5841. };
  5842. var refTargetDetachHookDisposer = refTargetNode.registerHook(Hook.beforeDetach, hookHandler);
  5843. var refTargetDestroyHookDisposer = refTargetNode.registerHook(Hook.beforeDestroy, hookHandler);
  5844. return function () {
  5845. refTargetDetachHookDisposer();
  5846. refTargetDestroyHookDisposer();
  5847. };
  5848. };
  5849. BaseReferenceType.prototype.watchTargetNodeForInvalidations = function (storedRefNode, identifier, customGetSet) {
  5850. var _this = this;
  5851. if (!this.onInvalidated) {
  5852. return;
  5853. }
  5854. var onRefTargetDestroyedHookDisposer;
  5855. // get rid of the watcher hook when the stored ref node is destroyed
  5856. // detached is ignored since scalar nodes (where the reference resides) cannot be detached
  5857. storedRefNode.registerHook(Hook.beforeDestroy, function () {
  5858. if (onRefTargetDestroyedHookDisposer) {
  5859. onRefTargetDestroyedHookDisposer();
  5860. }
  5861. });
  5862. var startWatching = function (sync) {
  5863. // re-create hook in case the stored ref gets reattached
  5864. if (onRefTargetDestroyedHookDisposer) {
  5865. onRefTargetDestroyedHookDisposer();
  5866. }
  5867. // make sure the target node is actually there and initialized
  5868. var storedRefParentNode = storedRefNode.parent;
  5869. var storedRefParentValue = storedRefParentNode && storedRefParentNode.storedValue;
  5870. if (storedRefParentNode && storedRefParentNode.isAlive && storedRefParentValue) {
  5871. var refTargetNodeExists = void 0;
  5872. if (customGetSet) {
  5873. refTargetNodeExists = !!customGetSet.get(identifier, storedRefParentValue);
  5874. }
  5875. else {
  5876. refTargetNodeExists = storedRefNode.root.identifierCache.has(_this.targetType, normalizeIdentifier(identifier));
  5877. }
  5878. if (!refTargetNodeExists) {
  5879. // we cannot change the reference in sync mode
  5880. // since we are in the middle of a reconciliation/instantiation and the change would be overwritten
  5881. // for those cases just let the wrong reference be assigned and fail upon usage
  5882. // (like current references do)
  5883. // this means that effectively this code will only run when it is created from a snapshot
  5884. if (!sync) {
  5885. _this.fireInvalidated("invalidSnapshotReference", storedRefNode, identifier, null);
  5886. }
  5887. }
  5888. else {
  5889. onRefTargetDestroyedHookDisposer = _this.addTargetNodeWatcher(storedRefNode, identifier);
  5890. }
  5891. }
  5892. };
  5893. if (storedRefNode.state === NodeLifeCycle.FINALIZED) {
  5894. // already attached, so the whole tree is ready
  5895. startWatching(true);
  5896. }
  5897. else {
  5898. if (!storedRefNode.isRoot) {
  5899. // start watching once the whole tree is ready
  5900. storedRefNode.root.registerHook(Hook.afterCreationFinalization, function () {
  5901. // make sure to attach it so it can start listening
  5902. if (storedRefNode.parent) {
  5903. storedRefNode.parent.createObservableInstanceIfNeeded();
  5904. }
  5905. });
  5906. }
  5907. // start watching once the node is attached somewhere / parent changes
  5908. storedRefNode.registerHook(Hook.afterAttach, function () {
  5909. startWatching(false);
  5910. });
  5911. }
  5912. };
  5913. return BaseReferenceType;
  5914. }(SimpleType));
  5915. /**
  5916. * @internal
  5917. * @hidden
  5918. */
  5919. var IdentifierReferenceType = /** @class */ (function (_super) {
  5920. __extends(IdentifierReferenceType, _super);
  5921. function IdentifierReferenceType(targetType, onInvalidated) {
  5922. return _super.call(this, targetType, onInvalidated) || this;
  5923. }
  5924. IdentifierReferenceType.prototype.getValue = function (storedRefNode) {
  5925. if (!storedRefNode.isAlive)
  5926. return undefined;
  5927. var storedRef = storedRefNode.storedValue;
  5928. return storedRef.resolvedValue;
  5929. };
  5930. IdentifierReferenceType.prototype.getSnapshot = function (storedRefNode) {
  5931. var ref = storedRefNode.storedValue;
  5932. return ref.identifier;
  5933. };
  5934. IdentifierReferenceType.prototype.instantiate = function (parent, subpath, environment, initialValue) {
  5935. var identifier = isStateTreeNode(initialValue)
  5936. ? getIdentifier(initialValue)
  5937. : initialValue;
  5938. var storedRef = new StoredReference(initialValue, this.targetType);
  5939. var storedRefNode = createScalarNode(this, parent, subpath, environment, storedRef);
  5940. storedRef.node = storedRefNode;
  5941. this.watchTargetNodeForInvalidations(storedRefNode, identifier, undefined);
  5942. return storedRefNode;
  5943. };
  5944. IdentifierReferenceType.prototype.reconcile = function (current, newValue, parent, subpath) {
  5945. if (!current.isDetaching && current.type === this) {
  5946. var compareByValue = isStateTreeNode(newValue);
  5947. var ref = current.storedValue;
  5948. if ((!compareByValue && ref.identifier === newValue) ||
  5949. (compareByValue && ref.resolvedValue === newValue)) {
  5950. current.setParent(parent, subpath);
  5951. return current;
  5952. }
  5953. }
  5954. var newNode = this.instantiate(parent, subpath, undefined, newValue);
  5955. current.die(); // noop if detaching
  5956. return newNode;
  5957. };
  5958. return IdentifierReferenceType;
  5959. }(BaseReferenceType));
  5960. /**
  5961. * @internal
  5962. * @hidden
  5963. */
  5964. var CustomReferenceType = /** @class */ (function (_super) {
  5965. __extends(CustomReferenceType, _super);
  5966. function CustomReferenceType(targetType, options, onInvalidated) {
  5967. var _this = _super.call(this, targetType, onInvalidated) || this;
  5968. _this.options = options;
  5969. return _this;
  5970. }
  5971. CustomReferenceType.prototype.getValue = function (storedRefNode) {
  5972. if (!storedRefNode.isAlive)
  5973. return undefined;
  5974. var referencedNode = this.options.get(storedRefNode.storedValue, storedRefNode.parent ? storedRefNode.parent.storedValue : null);
  5975. return referencedNode;
  5976. };
  5977. CustomReferenceType.prototype.getSnapshot = function (storedRefNode) {
  5978. return storedRefNode.storedValue;
  5979. };
  5980. CustomReferenceType.prototype.instantiate = function (parent, subpath, environment, newValue) {
  5981. var identifier = isStateTreeNode(newValue)
  5982. ? this.options.set(newValue, parent ? parent.storedValue : null)
  5983. : newValue;
  5984. var storedRefNode = createScalarNode(this, parent, subpath, environment, identifier);
  5985. this.watchTargetNodeForInvalidations(storedRefNode, identifier, this.options);
  5986. return storedRefNode;
  5987. };
  5988. CustomReferenceType.prototype.reconcile = function (current, newValue, parent, subpath) {
  5989. var newIdentifier = isStateTreeNode(newValue)
  5990. ? this.options.set(newValue, current ? current.storedValue : null)
  5991. : newValue;
  5992. if (!current.isDetaching &&
  5993. current.type === this &&
  5994. current.storedValue === newIdentifier) {
  5995. current.setParent(parent, subpath);
  5996. return current;
  5997. }
  5998. var newNode = this.instantiate(parent, subpath, undefined, newIdentifier);
  5999. current.die(); // noop if detaching
  6000. return newNode;
  6001. };
  6002. return CustomReferenceType;
  6003. }(BaseReferenceType));
  6004. /**
  6005. * `types.reference` - Creates a reference to another type, which should have defined an identifier.
  6006. * See also the [reference and identifiers](https://github.com/mobxjs/mobx-state-tree#references-and-identifiers) section.
  6007. */
  6008. function reference(subType, options) {
  6009. assertIsType(subType, 1);
  6010. {
  6011. if (arguments.length === 2 && typeof arguments[1] === "string") {
  6012. // istanbul ignore next
  6013. throw fail$1("References with base path are no longer supported. Please remove the base path.");
  6014. }
  6015. }
  6016. var getSetOptions = options ? options : undefined;
  6017. var onInvalidated = options
  6018. ? options.onInvalidated
  6019. : undefined;
  6020. if (getSetOptions && (getSetOptions.get || getSetOptions.set)) {
  6021. {
  6022. if (!getSetOptions.get || !getSetOptions.set) {
  6023. throw fail$1("reference options must either contain both a 'get' and a 'set' method or none of them");
  6024. }
  6025. }
  6026. return new CustomReferenceType(subType, {
  6027. get: getSetOptions.get,
  6028. set: getSetOptions.set
  6029. }, onInvalidated);
  6030. }
  6031. else {
  6032. return new IdentifierReferenceType(subType, onInvalidated);
  6033. }
  6034. }
  6035. /**
  6036. * Returns if a given value represents a reference type.
  6037. *
  6038. * @param type
  6039. * @returns
  6040. */
  6041. function isReferenceType(type) {
  6042. return (type.flags & TypeFlags.Reference) > 0;
  6043. }
  6044. /**
  6045. * `types.safeReference` - A safe reference is like a standard reference, except that it accepts the undefined value by default
  6046. * and automatically sets itself to undefined (when the parent is a model) / removes itself from arrays and maps
  6047. * when the reference it is pointing to gets detached/destroyed.
  6048. *
  6049. * The optional options parameter object accepts a parameter named `acceptsUndefined`, which is set to true by default, so it is suitable
  6050. * for model properties.
  6051. * When used inside collections (arrays/maps), it is recommended to set this option to false so it can't take undefined as value,
  6052. * which is usually the desired in those cases.
  6053. *
  6054. * Strictly speaking it is a `types.maybe(types.reference(X))` (when `acceptsUndefined` is set to true, the default) and
  6055. * `types.reference(X)` (when `acceptsUndefined` is set to false), both of them with a customized `onInvalidated` option.
  6056. *
  6057. * @param subType
  6058. * @param options
  6059. * @returns
  6060. */
  6061. function safeReference(subType, options) {
  6062. var refType = reference(subType, __assign(__assign({}, options), { onInvalidated: function (ev) {
  6063. ev.removeRef();
  6064. } }));
  6065. if (options && options.acceptsUndefined === false) {
  6066. return refType;
  6067. }
  6068. else {
  6069. return maybe(refType);
  6070. }
  6071. }
  6072. var BaseIdentifierType = /** @class */ (function (_super) {
  6073. __extends(BaseIdentifierType, _super);
  6074. function BaseIdentifierType(name, validType) {
  6075. var _this = _super.call(this, name) || this;
  6076. _this.validType = validType;
  6077. _this.flags = TypeFlags.Identifier;
  6078. return _this;
  6079. }
  6080. BaseIdentifierType.prototype.instantiate = function (parent, subpath, environment, initialValue) {
  6081. if (!parent || !(parent.type instanceof ModelType))
  6082. throw fail$1("Identifier types can only be instantiated as direct child of a model type");
  6083. return createScalarNode(this, parent, subpath, environment, initialValue);
  6084. };
  6085. BaseIdentifierType.prototype.reconcile = function (current, newValue, parent, subpath) {
  6086. // we don't consider detaching here since identifier are scalar nodes, and scalar nodes cannot be detached
  6087. if (current.storedValue !== newValue)
  6088. throw fail$1("Tried to change identifier from '" + current.storedValue + "' to '" + newValue + "'. Changing identifiers is not allowed.");
  6089. current.setParent(parent, subpath);
  6090. return current;
  6091. };
  6092. BaseIdentifierType.prototype.isValidSnapshot = function (value, context) {
  6093. if (typeof value !== this.validType) {
  6094. return typeCheckFailure(context, value, "Value is not a valid " + this.describe() + ", expected a " + this.validType);
  6095. }
  6096. return typeCheckSuccess();
  6097. };
  6098. return BaseIdentifierType;
  6099. }(SimpleType));
  6100. /**
  6101. * @internal
  6102. * @hidden
  6103. */
  6104. var IdentifierType = /** @class */ (function (_super) {
  6105. __extends(IdentifierType, _super);
  6106. function IdentifierType() {
  6107. var _this = _super.call(this, "identifier", "string") || this;
  6108. _this.flags = TypeFlags.Identifier;
  6109. return _this;
  6110. }
  6111. IdentifierType.prototype.describe = function () {
  6112. return "identifier";
  6113. };
  6114. return IdentifierType;
  6115. }(BaseIdentifierType));
  6116. /**
  6117. * @internal
  6118. * @hidden
  6119. */
  6120. var IdentifierNumberType = /** @class */ (function (_super) {
  6121. __extends(IdentifierNumberType, _super);
  6122. function IdentifierNumberType() {
  6123. return _super.call(this, "identifierNumber", "number") || this;
  6124. }
  6125. IdentifierNumberType.prototype.getSnapshot = function (node) {
  6126. return node.storedValue;
  6127. };
  6128. IdentifierNumberType.prototype.describe = function () {
  6129. return "identifierNumber";
  6130. };
  6131. return IdentifierNumberType;
  6132. }(BaseIdentifierType));
  6133. /**
  6134. * `types.identifier` - Identifiers are used to make references, lifecycle events and reconciling works.
  6135. * Inside a state tree, for each type can exist only one instance for each given identifier.
  6136. * For example there couldn't be 2 instances of user with id 1. If you need more, consider using references.
  6137. * Identifier can be used only as type property of a model.
  6138. * This type accepts as parameter the value type of the identifier field that can be either string or number.
  6139. *
  6140. * Example:
  6141. * ```ts
  6142. * const Todo = types.model("Todo", {
  6143. * id: types.identifier,
  6144. * title: types.string
  6145. * })
  6146. * ```
  6147. *
  6148. * @returns
  6149. */
  6150. var identifier = new IdentifierType();
  6151. /**
  6152. * `types.identifierNumber` - Similar to `types.identifier`. This one will serialize from / to a number when applying snapshots
  6153. *
  6154. * Example:
  6155. * ```ts
  6156. * const Todo = types.model("Todo", {
  6157. * id: types.identifierNumber,
  6158. * title: types.string
  6159. * })
  6160. * ```
  6161. *
  6162. * @returns
  6163. */
  6164. var identifierNumber = new IdentifierNumberType();
  6165. /**
  6166. * Returns if a given value represents an identifier type.
  6167. *
  6168. * @param type
  6169. * @returns
  6170. */
  6171. function isIdentifierType(type) {
  6172. return isType(type) && (type.flags & TypeFlags.Identifier) > 0;
  6173. }
  6174. /**
  6175. * @internal
  6176. * @hidden
  6177. */
  6178. function normalizeIdentifier(id) {
  6179. return "" + id;
  6180. }
  6181. /**
  6182. * @internal
  6183. * @hidden
  6184. */
  6185. function isValidIdentifier(id) {
  6186. return typeof id === "string" || typeof id === "number";
  6187. }
  6188. /**
  6189. * @internal
  6190. * @hidden
  6191. */
  6192. function assertIsValidIdentifier(id, argNumber) {
  6193. assertArg(id, isValidIdentifier, "string or number (identifier)", argNumber);
  6194. }
  6195. /**
  6196. * `types.custom` - Creates a custom type. Custom types can be used for arbitrary immutable values, that have a serializable representation. For example, to create your own Date representation, Decimal type etc.
  6197. *
  6198. * The signature of the options is:
  6199. * ```ts
  6200. * export interface CustomTypeOptions<S, T> {
  6201. * // Friendly name
  6202. * name: string
  6203. * // given a serialized value, how to turn it into the target type
  6204. * fromSnapshot(snapshot: S): T
  6205. * // return the serialization of the current value
  6206. * toSnapshot(value: T): S
  6207. * // if true, this is a converted value, if false, it's a snapshot
  6208. * isTargetType(value: T | S): value is T
  6209. * // a non empty string is assumed to be a validation error
  6210. * getValidationMessage?(snapshot: S): string
  6211. * }
  6212. * ```
  6213. *
  6214. * Example:
  6215. * ```ts
  6216. * const DecimalPrimitive = types.custom<string, Decimal>({
  6217. * name: "Decimal",
  6218. * fromSnapshot(value: string) {
  6219. * return new Decimal(value)
  6220. * },
  6221. * toSnapshot(value: Decimal) {
  6222. * return value.toString()
  6223. * },
  6224. * isTargetType(value: string | Decimal): boolean {
  6225. * return value instanceof Decimal
  6226. * },
  6227. * getValidationMessage(value: string): string {
  6228. * if (/^-?\d+\.\d+$/.test(value)) return "" // OK
  6229. * return `'${value}' doesn't look like a valid decimal number`
  6230. * }
  6231. * })
  6232. *
  6233. * const Wallet = types.model({
  6234. * balance: DecimalPrimitive
  6235. * })
  6236. * ```
  6237. *
  6238. * @param options
  6239. * @returns
  6240. */
  6241. function custom(options) {
  6242. return new CustomType(options);
  6243. }
  6244. /**
  6245. * @internal
  6246. * @hidden
  6247. */
  6248. var CustomType = /** @class */ (function (_super) {
  6249. __extends(CustomType, _super);
  6250. function CustomType(options) {
  6251. var _this = _super.call(this, options.name) || this;
  6252. _this.options = options;
  6253. _this.flags = TypeFlags.Custom;
  6254. return _this;
  6255. }
  6256. CustomType.prototype.describe = function () {
  6257. return this.name;
  6258. };
  6259. CustomType.prototype.isValidSnapshot = function (value, context) {
  6260. if (this.options.isTargetType(value))
  6261. return typeCheckSuccess();
  6262. var typeError = this.options.getValidationMessage(value);
  6263. if (typeError) {
  6264. return typeCheckFailure(context, value, "Invalid value for type '" + this.name + "': " + typeError);
  6265. }
  6266. return typeCheckSuccess();
  6267. };
  6268. CustomType.prototype.getSnapshot = function (node) {
  6269. return this.options.toSnapshot(node.storedValue);
  6270. };
  6271. CustomType.prototype.instantiate = function (parent, subpath, environment, initialValue) {
  6272. var valueToStore = this.options.isTargetType(initialValue)
  6273. ? initialValue
  6274. : this.options.fromSnapshot(initialValue);
  6275. return createScalarNode(this, parent, subpath, environment, valueToStore);
  6276. };
  6277. CustomType.prototype.reconcile = function (current, value, parent, subpath) {
  6278. var isSnapshot = !this.options.isTargetType(value);
  6279. // in theory customs use scalar nodes which cannot be detached, but still...
  6280. if (!current.isDetaching) {
  6281. var unchanged = current.type === this &&
  6282. (isSnapshot ? value === current.snapshot : value === current.storedValue);
  6283. if (unchanged) {
  6284. current.setParent(parent, subpath);
  6285. return current;
  6286. }
  6287. }
  6288. var valueToStore = isSnapshot ? this.options.fromSnapshot(value) : value;
  6289. var newNode = this.instantiate(parent, subpath, undefined, valueToStore);
  6290. current.die(); // noop if detaching
  6291. return newNode;
  6292. };
  6293. return CustomType;
  6294. }(SimpleType));
  6295. // we import the types to re-export them inside types.
  6296. var types = {
  6297. enumeration: enumeration,
  6298. model: model,
  6299. compose: compose,
  6300. custom: custom,
  6301. reference: reference,
  6302. safeReference: safeReference,
  6303. union: union,
  6304. optional: optional,
  6305. literal: literal,
  6306. maybe: maybe,
  6307. maybeNull: maybeNull,
  6308. refinement: refinement,
  6309. string: string,
  6310. boolean: boolean,
  6311. number: number,
  6312. integer: integer,
  6313. Date: DatePrimitive,
  6314. map: map,
  6315. array: array,
  6316. frozen: frozen,
  6317. identifier: identifier,
  6318. identifierNumber: identifierNumber,
  6319. late: late,
  6320. undefined: undefinedType,
  6321. null: nullType,
  6322. snapshotProcessor: snapshotProcessor
  6323. };
  6324. exports.addDisposer = addDisposer;
  6325. exports.addMiddleware = addMiddleware;
  6326. exports.applyAction = applyAction;
  6327. exports.applyPatch = applyPatch;
  6328. exports.applySnapshot = applySnapshot;
  6329. exports.cast = cast;
  6330. exports.castFlowReturn = castFlowReturn;
  6331. exports.castToReferenceSnapshot = castToReferenceSnapshot;
  6332. exports.castToSnapshot = castToSnapshot;
  6333. exports.clone = clone;
  6334. exports.createActionTrackingMiddleware = createActionTrackingMiddleware;
  6335. exports.createActionTrackingMiddleware2 = createActionTrackingMiddleware2;
  6336. exports.decorate = decorate;
  6337. exports.destroy = destroy;
  6338. exports.detach = detach;
  6339. exports.escapeJsonPath = escapeJsonPath;
  6340. exports.flow = flow;
  6341. exports.getChildType = getChildType;
  6342. exports.getEnv = getEnv;
  6343. exports.getIdentifier = getIdentifier;
  6344. exports.getLivelinessChecking = getLivelinessChecking;
  6345. exports.getMembers = getMembers;
  6346. exports.getNodeId = getNodeId;
  6347. exports.getParent = getParent;
  6348. exports.getParentOfType = getParentOfType;
  6349. exports.getPath = getPath;
  6350. exports.getPathParts = getPathParts;
  6351. exports.getPropertyMembers = getPropertyMembers;
  6352. exports.getRelativePath = getRelativePath;
  6353. exports.getRoot = getRoot;
  6354. exports.getRunningActionContext = getRunningActionContext;
  6355. exports.getSnapshot = getSnapshot;
  6356. exports.getType = getType;
  6357. exports.hasParent = hasParent;
  6358. exports.hasParentOfType = hasParentOfType;
  6359. exports.isActionContextChildOf = isActionContextChildOf;
  6360. exports.isActionContextThisOrChildOf = isActionContextThisOrChildOf;
  6361. exports.isAlive = isAlive;
  6362. exports.isArrayType = isArrayType;
  6363. exports.isFrozenType = isFrozenType;
  6364. exports.isIdentifierType = isIdentifierType;
  6365. exports.isLateType = isLateType;
  6366. exports.isLiteralType = isLiteralType;
  6367. exports.isMapType = isMapType;
  6368. exports.isModelType = isModelType;
  6369. exports.isOptionalType = isOptionalType;
  6370. exports.isPrimitiveType = isPrimitiveType;
  6371. exports.isProtected = isProtected;
  6372. exports.isReferenceType = isReferenceType;
  6373. exports.isRefinementType = isRefinementType;
  6374. exports.isRoot = isRoot;
  6375. exports.isStateTreeNode = isStateTreeNode;
  6376. exports.isType = isType;
  6377. exports.isUnionType = isUnionType;
  6378. exports.isValidReference = isValidReference;
  6379. exports.joinJsonPath = joinJsonPath;
  6380. exports.onAction = onAction;
  6381. exports.onPatch = onPatch;
  6382. exports.onSnapshot = onSnapshot;
  6383. exports.process = process;
  6384. exports.protect = protect;
  6385. exports.recordActions = recordActions;
  6386. exports.recordPatches = recordPatches;
  6387. exports.resolveIdentifier = resolveIdentifier;
  6388. exports.resolvePath = resolvePath;
  6389. exports.setLivelinessChecking = setLivelinessChecking;
  6390. exports.setLivelynessChecking = setLivelynessChecking;
  6391. exports.splitJsonPath = splitJsonPath;
  6392. exports.tryReference = tryReference;
  6393. exports.tryResolve = tryResolve;
  6394. exports.typecheck = typecheck;
  6395. exports.types = types;
  6396. exports.unescapeJsonPath = unescapeJsonPath;
  6397. exports.unprotect = unprotect;
  6398. exports.walk = walk;
  6399. Object.defineProperty(exports, '__esModule', { value: true });
  6400. }));