mobx-state-tree.module.js 228 KB

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