reactivity.cjs.js 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956
  1. /**
  2. * @vue/reactivity v3.5.18
  3. * (c) 2018-present Yuxi (Evan) You and Vue contributors
  4. * @license MIT
  5. **/
  6. 'use strict';
  7. Object.defineProperty(exports, '__esModule', { value: true });
  8. var shared = require('@vue/shared');
  9. function warn(msg, ...args) {
  10. console.warn(`[Vue warn] ${msg}`, ...args);
  11. }
  12. let activeEffectScope;
  13. class EffectScope {
  14. constructor(detached = false) {
  15. this.detached = detached;
  16. /**
  17. * @internal
  18. */
  19. this._active = true;
  20. /**
  21. * @internal track `on` calls, allow `on` call multiple times
  22. */
  23. this._on = 0;
  24. /**
  25. * @internal
  26. */
  27. this.effects = [];
  28. /**
  29. * @internal
  30. */
  31. this.cleanups = [];
  32. this._isPaused = false;
  33. this.parent = activeEffectScope;
  34. if (!detached && activeEffectScope) {
  35. this.index = (activeEffectScope.scopes || (activeEffectScope.scopes = [])).push(
  36. this
  37. ) - 1;
  38. }
  39. }
  40. get active() {
  41. return this._active;
  42. }
  43. pause() {
  44. if (this._active) {
  45. this._isPaused = true;
  46. let i, l;
  47. if (this.scopes) {
  48. for (i = 0, l = this.scopes.length; i < l; i++) {
  49. this.scopes[i].pause();
  50. }
  51. }
  52. for (i = 0, l = this.effects.length; i < l; i++) {
  53. this.effects[i].pause();
  54. }
  55. }
  56. }
  57. /**
  58. * Resumes the effect scope, including all child scopes and effects.
  59. */
  60. resume() {
  61. if (this._active) {
  62. if (this._isPaused) {
  63. this._isPaused = false;
  64. let i, l;
  65. if (this.scopes) {
  66. for (i = 0, l = this.scopes.length; i < l; i++) {
  67. this.scopes[i].resume();
  68. }
  69. }
  70. for (i = 0, l = this.effects.length; i < l; i++) {
  71. this.effects[i].resume();
  72. }
  73. }
  74. }
  75. }
  76. run(fn) {
  77. if (this._active) {
  78. const currentEffectScope = activeEffectScope;
  79. try {
  80. activeEffectScope = this;
  81. return fn();
  82. } finally {
  83. activeEffectScope = currentEffectScope;
  84. }
  85. } else {
  86. warn(`cannot run an inactive effect scope.`);
  87. }
  88. }
  89. /**
  90. * This should only be called on non-detached scopes
  91. * @internal
  92. */
  93. on() {
  94. if (++this._on === 1) {
  95. this.prevScope = activeEffectScope;
  96. activeEffectScope = this;
  97. }
  98. }
  99. /**
  100. * This should only be called on non-detached scopes
  101. * @internal
  102. */
  103. off() {
  104. if (this._on > 0 && --this._on === 0) {
  105. activeEffectScope = this.prevScope;
  106. this.prevScope = void 0;
  107. }
  108. }
  109. stop(fromParent) {
  110. if (this._active) {
  111. this._active = false;
  112. let i, l;
  113. for (i = 0, l = this.effects.length; i < l; i++) {
  114. this.effects[i].stop();
  115. }
  116. this.effects.length = 0;
  117. for (i = 0, l = this.cleanups.length; i < l; i++) {
  118. this.cleanups[i]();
  119. }
  120. this.cleanups.length = 0;
  121. if (this.scopes) {
  122. for (i = 0, l = this.scopes.length; i < l; i++) {
  123. this.scopes[i].stop(true);
  124. }
  125. this.scopes.length = 0;
  126. }
  127. if (!this.detached && this.parent && !fromParent) {
  128. const last = this.parent.scopes.pop();
  129. if (last && last !== this) {
  130. this.parent.scopes[this.index] = last;
  131. last.index = this.index;
  132. }
  133. }
  134. this.parent = void 0;
  135. }
  136. }
  137. }
  138. function effectScope(detached) {
  139. return new EffectScope(detached);
  140. }
  141. function getCurrentScope() {
  142. return activeEffectScope;
  143. }
  144. function onScopeDispose(fn, failSilently = false) {
  145. if (activeEffectScope) {
  146. activeEffectScope.cleanups.push(fn);
  147. } else if (!failSilently) {
  148. warn(
  149. `onScopeDispose() is called when there is no active effect scope to be associated with.`
  150. );
  151. }
  152. }
  153. let activeSub;
  154. const EffectFlags = {
  155. "ACTIVE": 1,
  156. "1": "ACTIVE",
  157. "RUNNING": 2,
  158. "2": "RUNNING",
  159. "TRACKING": 4,
  160. "4": "TRACKING",
  161. "NOTIFIED": 8,
  162. "8": "NOTIFIED",
  163. "DIRTY": 16,
  164. "16": "DIRTY",
  165. "ALLOW_RECURSE": 32,
  166. "32": "ALLOW_RECURSE",
  167. "PAUSED": 64,
  168. "64": "PAUSED",
  169. "EVALUATED": 128,
  170. "128": "EVALUATED"
  171. };
  172. const pausedQueueEffects = /* @__PURE__ */ new WeakSet();
  173. class ReactiveEffect {
  174. constructor(fn) {
  175. this.fn = fn;
  176. /**
  177. * @internal
  178. */
  179. this.deps = void 0;
  180. /**
  181. * @internal
  182. */
  183. this.depsTail = void 0;
  184. /**
  185. * @internal
  186. */
  187. this.flags = 1 | 4;
  188. /**
  189. * @internal
  190. */
  191. this.next = void 0;
  192. /**
  193. * @internal
  194. */
  195. this.cleanup = void 0;
  196. this.scheduler = void 0;
  197. if (activeEffectScope && activeEffectScope.active) {
  198. activeEffectScope.effects.push(this);
  199. }
  200. }
  201. pause() {
  202. this.flags |= 64;
  203. }
  204. resume() {
  205. if (this.flags & 64) {
  206. this.flags &= -65;
  207. if (pausedQueueEffects.has(this)) {
  208. pausedQueueEffects.delete(this);
  209. this.trigger();
  210. }
  211. }
  212. }
  213. /**
  214. * @internal
  215. */
  216. notify() {
  217. if (this.flags & 2 && !(this.flags & 32)) {
  218. return;
  219. }
  220. if (!(this.flags & 8)) {
  221. batch(this);
  222. }
  223. }
  224. run() {
  225. if (!(this.flags & 1)) {
  226. return this.fn();
  227. }
  228. this.flags |= 2;
  229. cleanupEffect(this);
  230. prepareDeps(this);
  231. const prevEffect = activeSub;
  232. const prevShouldTrack = shouldTrack;
  233. activeSub = this;
  234. shouldTrack = true;
  235. try {
  236. return this.fn();
  237. } finally {
  238. if (activeSub !== this) {
  239. warn(
  240. "Active effect was not restored correctly - this is likely a Vue internal bug."
  241. );
  242. }
  243. cleanupDeps(this);
  244. activeSub = prevEffect;
  245. shouldTrack = prevShouldTrack;
  246. this.flags &= -3;
  247. }
  248. }
  249. stop() {
  250. if (this.flags & 1) {
  251. for (let link = this.deps; link; link = link.nextDep) {
  252. removeSub(link);
  253. }
  254. this.deps = this.depsTail = void 0;
  255. cleanupEffect(this);
  256. this.onStop && this.onStop();
  257. this.flags &= -2;
  258. }
  259. }
  260. trigger() {
  261. if (this.flags & 64) {
  262. pausedQueueEffects.add(this);
  263. } else if (this.scheduler) {
  264. this.scheduler();
  265. } else {
  266. this.runIfDirty();
  267. }
  268. }
  269. /**
  270. * @internal
  271. */
  272. runIfDirty() {
  273. if (isDirty(this)) {
  274. this.run();
  275. }
  276. }
  277. get dirty() {
  278. return isDirty(this);
  279. }
  280. }
  281. let batchDepth = 0;
  282. let batchedSub;
  283. let batchedComputed;
  284. function batch(sub, isComputed = false) {
  285. sub.flags |= 8;
  286. if (isComputed) {
  287. sub.next = batchedComputed;
  288. batchedComputed = sub;
  289. return;
  290. }
  291. sub.next = batchedSub;
  292. batchedSub = sub;
  293. }
  294. function startBatch() {
  295. batchDepth++;
  296. }
  297. function endBatch() {
  298. if (--batchDepth > 0) {
  299. return;
  300. }
  301. if (batchedComputed) {
  302. let e = batchedComputed;
  303. batchedComputed = void 0;
  304. while (e) {
  305. const next = e.next;
  306. e.next = void 0;
  307. e.flags &= -9;
  308. e = next;
  309. }
  310. }
  311. let error;
  312. while (batchedSub) {
  313. let e = batchedSub;
  314. batchedSub = void 0;
  315. while (e) {
  316. const next = e.next;
  317. e.next = void 0;
  318. e.flags &= -9;
  319. if (e.flags & 1) {
  320. try {
  321. ;
  322. e.trigger();
  323. } catch (err) {
  324. if (!error) error = err;
  325. }
  326. }
  327. e = next;
  328. }
  329. }
  330. if (error) throw error;
  331. }
  332. function prepareDeps(sub) {
  333. for (let link = sub.deps; link; link = link.nextDep) {
  334. link.version = -1;
  335. link.prevActiveLink = link.dep.activeLink;
  336. link.dep.activeLink = link;
  337. }
  338. }
  339. function cleanupDeps(sub) {
  340. let head;
  341. let tail = sub.depsTail;
  342. let link = tail;
  343. while (link) {
  344. const prev = link.prevDep;
  345. if (link.version === -1) {
  346. if (link === tail) tail = prev;
  347. removeSub(link);
  348. removeDep(link);
  349. } else {
  350. head = link;
  351. }
  352. link.dep.activeLink = link.prevActiveLink;
  353. link.prevActiveLink = void 0;
  354. link = prev;
  355. }
  356. sub.deps = head;
  357. sub.depsTail = tail;
  358. }
  359. function isDirty(sub) {
  360. for (let link = sub.deps; link; link = link.nextDep) {
  361. if (link.dep.version !== link.version || link.dep.computed && (refreshComputed(link.dep.computed) || link.dep.version !== link.version)) {
  362. return true;
  363. }
  364. }
  365. if (sub._dirty) {
  366. return true;
  367. }
  368. return false;
  369. }
  370. function refreshComputed(computed) {
  371. if (computed.flags & 4 && !(computed.flags & 16)) {
  372. return;
  373. }
  374. computed.flags &= -17;
  375. if (computed.globalVersion === globalVersion) {
  376. return;
  377. }
  378. computed.globalVersion = globalVersion;
  379. if (!computed.isSSR && computed.flags & 128 && (!computed.deps && !computed._dirty || !isDirty(computed))) {
  380. return;
  381. }
  382. computed.flags |= 2;
  383. const dep = computed.dep;
  384. const prevSub = activeSub;
  385. const prevShouldTrack = shouldTrack;
  386. activeSub = computed;
  387. shouldTrack = true;
  388. try {
  389. prepareDeps(computed);
  390. const value = computed.fn(computed._value);
  391. if (dep.version === 0 || shared.hasChanged(value, computed._value)) {
  392. computed.flags |= 128;
  393. computed._value = value;
  394. dep.version++;
  395. }
  396. } catch (err) {
  397. dep.version++;
  398. throw err;
  399. } finally {
  400. activeSub = prevSub;
  401. shouldTrack = prevShouldTrack;
  402. cleanupDeps(computed);
  403. computed.flags &= -3;
  404. }
  405. }
  406. function removeSub(link, soft = false) {
  407. const { dep, prevSub, nextSub } = link;
  408. if (prevSub) {
  409. prevSub.nextSub = nextSub;
  410. link.prevSub = void 0;
  411. }
  412. if (nextSub) {
  413. nextSub.prevSub = prevSub;
  414. link.nextSub = void 0;
  415. }
  416. if (dep.subsHead === link) {
  417. dep.subsHead = nextSub;
  418. }
  419. if (dep.subs === link) {
  420. dep.subs = prevSub;
  421. if (!prevSub && dep.computed) {
  422. dep.computed.flags &= -5;
  423. for (let l = dep.computed.deps; l; l = l.nextDep) {
  424. removeSub(l, true);
  425. }
  426. }
  427. }
  428. if (!soft && !--dep.sc && dep.map) {
  429. dep.map.delete(dep.key);
  430. }
  431. }
  432. function removeDep(link) {
  433. const { prevDep, nextDep } = link;
  434. if (prevDep) {
  435. prevDep.nextDep = nextDep;
  436. link.prevDep = void 0;
  437. }
  438. if (nextDep) {
  439. nextDep.prevDep = prevDep;
  440. link.nextDep = void 0;
  441. }
  442. }
  443. function effect(fn, options) {
  444. if (fn.effect instanceof ReactiveEffect) {
  445. fn = fn.effect.fn;
  446. }
  447. const e = new ReactiveEffect(fn);
  448. if (options) {
  449. shared.extend(e, options);
  450. }
  451. try {
  452. e.run();
  453. } catch (err) {
  454. e.stop();
  455. throw err;
  456. }
  457. const runner = e.run.bind(e);
  458. runner.effect = e;
  459. return runner;
  460. }
  461. function stop(runner) {
  462. runner.effect.stop();
  463. }
  464. let shouldTrack = true;
  465. const trackStack = [];
  466. function pauseTracking() {
  467. trackStack.push(shouldTrack);
  468. shouldTrack = false;
  469. }
  470. function enableTracking() {
  471. trackStack.push(shouldTrack);
  472. shouldTrack = true;
  473. }
  474. function resetTracking() {
  475. const last = trackStack.pop();
  476. shouldTrack = last === void 0 ? true : last;
  477. }
  478. function onEffectCleanup(fn, failSilently = false) {
  479. if (activeSub instanceof ReactiveEffect) {
  480. activeSub.cleanup = fn;
  481. } else if (!failSilently) {
  482. warn(
  483. `onEffectCleanup() was called when there was no active effect to associate with.`
  484. );
  485. }
  486. }
  487. function cleanupEffect(e) {
  488. const { cleanup } = e;
  489. e.cleanup = void 0;
  490. if (cleanup) {
  491. const prevSub = activeSub;
  492. activeSub = void 0;
  493. try {
  494. cleanup();
  495. } finally {
  496. activeSub = prevSub;
  497. }
  498. }
  499. }
  500. let globalVersion = 0;
  501. class Link {
  502. constructor(sub, dep) {
  503. this.sub = sub;
  504. this.dep = dep;
  505. this.version = dep.version;
  506. this.nextDep = this.prevDep = this.nextSub = this.prevSub = this.prevActiveLink = void 0;
  507. }
  508. }
  509. class Dep {
  510. // TODO isolatedDeclarations "__v_skip"
  511. constructor(computed) {
  512. this.computed = computed;
  513. this.version = 0;
  514. /**
  515. * Link between this dep and the current active effect
  516. */
  517. this.activeLink = void 0;
  518. /**
  519. * Doubly linked list representing the subscribing effects (tail)
  520. */
  521. this.subs = void 0;
  522. /**
  523. * For object property deps cleanup
  524. */
  525. this.map = void 0;
  526. this.key = void 0;
  527. /**
  528. * Subscriber counter
  529. */
  530. this.sc = 0;
  531. /**
  532. * @internal
  533. */
  534. this.__v_skip = true;
  535. {
  536. this.subsHead = void 0;
  537. }
  538. }
  539. track(debugInfo) {
  540. if (!activeSub || !shouldTrack || activeSub === this.computed) {
  541. return;
  542. }
  543. let link = this.activeLink;
  544. if (link === void 0 || link.sub !== activeSub) {
  545. link = this.activeLink = new Link(activeSub, this);
  546. if (!activeSub.deps) {
  547. activeSub.deps = activeSub.depsTail = link;
  548. } else {
  549. link.prevDep = activeSub.depsTail;
  550. activeSub.depsTail.nextDep = link;
  551. activeSub.depsTail = link;
  552. }
  553. addSub(link);
  554. } else if (link.version === -1) {
  555. link.version = this.version;
  556. if (link.nextDep) {
  557. const next = link.nextDep;
  558. next.prevDep = link.prevDep;
  559. if (link.prevDep) {
  560. link.prevDep.nextDep = next;
  561. }
  562. link.prevDep = activeSub.depsTail;
  563. link.nextDep = void 0;
  564. activeSub.depsTail.nextDep = link;
  565. activeSub.depsTail = link;
  566. if (activeSub.deps === link) {
  567. activeSub.deps = next;
  568. }
  569. }
  570. }
  571. if (activeSub.onTrack) {
  572. activeSub.onTrack(
  573. shared.extend(
  574. {
  575. effect: activeSub
  576. },
  577. debugInfo
  578. )
  579. );
  580. }
  581. return link;
  582. }
  583. trigger(debugInfo) {
  584. this.version++;
  585. globalVersion++;
  586. this.notify(debugInfo);
  587. }
  588. notify(debugInfo) {
  589. startBatch();
  590. try {
  591. if (true) {
  592. for (let head = this.subsHead; head; head = head.nextSub) {
  593. if (head.sub.onTrigger && !(head.sub.flags & 8)) {
  594. head.sub.onTrigger(
  595. shared.extend(
  596. {
  597. effect: head.sub
  598. },
  599. debugInfo
  600. )
  601. );
  602. }
  603. }
  604. }
  605. for (let link = this.subs; link; link = link.prevSub) {
  606. if (link.sub.notify()) {
  607. ;
  608. link.sub.dep.notify();
  609. }
  610. }
  611. } finally {
  612. endBatch();
  613. }
  614. }
  615. }
  616. function addSub(link) {
  617. link.dep.sc++;
  618. if (link.sub.flags & 4) {
  619. const computed = link.dep.computed;
  620. if (computed && !link.dep.subs) {
  621. computed.flags |= 4 | 16;
  622. for (let l = computed.deps; l; l = l.nextDep) {
  623. addSub(l);
  624. }
  625. }
  626. const currentTail = link.dep.subs;
  627. if (currentTail !== link) {
  628. link.prevSub = currentTail;
  629. if (currentTail) currentTail.nextSub = link;
  630. }
  631. if (link.dep.subsHead === void 0) {
  632. link.dep.subsHead = link;
  633. }
  634. link.dep.subs = link;
  635. }
  636. }
  637. const targetMap = /* @__PURE__ */ new WeakMap();
  638. const ITERATE_KEY = Symbol(
  639. "Object iterate"
  640. );
  641. const MAP_KEY_ITERATE_KEY = Symbol(
  642. "Map keys iterate"
  643. );
  644. const ARRAY_ITERATE_KEY = Symbol(
  645. "Array iterate"
  646. );
  647. function track(target, type, key) {
  648. if (shouldTrack && activeSub) {
  649. let depsMap = targetMap.get(target);
  650. if (!depsMap) {
  651. targetMap.set(target, depsMap = /* @__PURE__ */ new Map());
  652. }
  653. let dep = depsMap.get(key);
  654. if (!dep) {
  655. depsMap.set(key, dep = new Dep());
  656. dep.map = depsMap;
  657. dep.key = key;
  658. }
  659. {
  660. dep.track({
  661. target,
  662. type,
  663. key
  664. });
  665. }
  666. }
  667. }
  668. function trigger(target, type, key, newValue, oldValue, oldTarget) {
  669. const depsMap = targetMap.get(target);
  670. if (!depsMap) {
  671. globalVersion++;
  672. return;
  673. }
  674. const run = (dep) => {
  675. if (dep) {
  676. {
  677. dep.trigger({
  678. target,
  679. type,
  680. key,
  681. newValue,
  682. oldValue,
  683. oldTarget
  684. });
  685. }
  686. }
  687. };
  688. startBatch();
  689. if (type === "clear") {
  690. depsMap.forEach(run);
  691. } else {
  692. const targetIsArray = shared.isArray(target);
  693. const isArrayIndex = targetIsArray && shared.isIntegerKey(key);
  694. if (targetIsArray && key === "length") {
  695. const newLength = Number(newValue);
  696. depsMap.forEach((dep, key2) => {
  697. if (key2 === "length" || key2 === ARRAY_ITERATE_KEY || !shared.isSymbol(key2) && key2 >= newLength) {
  698. run(dep);
  699. }
  700. });
  701. } else {
  702. if (key !== void 0 || depsMap.has(void 0)) {
  703. run(depsMap.get(key));
  704. }
  705. if (isArrayIndex) {
  706. run(depsMap.get(ARRAY_ITERATE_KEY));
  707. }
  708. switch (type) {
  709. case "add":
  710. if (!targetIsArray) {
  711. run(depsMap.get(ITERATE_KEY));
  712. if (shared.isMap(target)) {
  713. run(depsMap.get(MAP_KEY_ITERATE_KEY));
  714. }
  715. } else if (isArrayIndex) {
  716. run(depsMap.get("length"));
  717. }
  718. break;
  719. case "delete":
  720. if (!targetIsArray) {
  721. run(depsMap.get(ITERATE_KEY));
  722. if (shared.isMap(target)) {
  723. run(depsMap.get(MAP_KEY_ITERATE_KEY));
  724. }
  725. }
  726. break;
  727. case "set":
  728. if (shared.isMap(target)) {
  729. run(depsMap.get(ITERATE_KEY));
  730. }
  731. break;
  732. }
  733. }
  734. }
  735. endBatch();
  736. }
  737. function getDepFromReactive(object, key) {
  738. const depMap = targetMap.get(object);
  739. return depMap && depMap.get(key);
  740. }
  741. function reactiveReadArray(array) {
  742. const raw = toRaw(array);
  743. if (raw === array) return raw;
  744. track(raw, "iterate", ARRAY_ITERATE_KEY);
  745. return isShallow(array) ? raw : raw.map(toReactive);
  746. }
  747. function shallowReadArray(arr) {
  748. track(arr = toRaw(arr), "iterate", ARRAY_ITERATE_KEY);
  749. return arr;
  750. }
  751. const arrayInstrumentations = {
  752. __proto__: null,
  753. [Symbol.iterator]() {
  754. return iterator(this, Symbol.iterator, toReactive);
  755. },
  756. concat(...args) {
  757. return reactiveReadArray(this).concat(
  758. ...args.map((x) => shared.isArray(x) ? reactiveReadArray(x) : x)
  759. );
  760. },
  761. entries() {
  762. return iterator(this, "entries", (value) => {
  763. value[1] = toReactive(value[1]);
  764. return value;
  765. });
  766. },
  767. every(fn, thisArg) {
  768. return apply(this, "every", fn, thisArg, void 0, arguments);
  769. },
  770. filter(fn, thisArg) {
  771. return apply(this, "filter", fn, thisArg, (v) => v.map(toReactive), arguments);
  772. },
  773. find(fn, thisArg) {
  774. return apply(this, "find", fn, thisArg, toReactive, arguments);
  775. },
  776. findIndex(fn, thisArg) {
  777. return apply(this, "findIndex", fn, thisArg, void 0, arguments);
  778. },
  779. findLast(fn, thisArg) {
  780. return apply(this, "findLast", fn, thisArg, toReactive, arguments);
  781. },
  782. findLastIndex(fn, thisArg) {
  783. return apply(this, "findLastIndex", fn, thisArg, void 0, arguments);
  784. },
  785. // flat, flatMap could benefit from ARRAY_ITERATE but are not straight-forward to implement
  786. forEach(fn, thisArg) {
  787. return apply(this, "forEach", fn, thisArg, void 0, arguments);
  788. },
  789. includes(...args) {
  790. return searchProxy(this, "includes", args);
  791. },
  792. indexOf(...args) {
  793. return searchProxy(this, "indexOf", args);
  794. },
  795. join(separator) {
  796. return reactiveReadArray(this).join(separator);
  797. },
  798. // keys() iterator only reads `length`, no optimisation required
  799. lastIndexOf(...args) {
  800. return searchProxy(this, "lastIndexOf", args);
  801. },
  802. map(fn, thisArg) {
  803. return apply(this, "map", fn, thisArg, void 0, arguments);
  804. },
  805. pop() {
  806. return noTracking(this, "pop");
  807. },
  808. push(...args) {
  809. return noTracking(this, "push", args);
  810. },
  811. reduce(fn, ...args) {
  812. return reduce(this, "reduce", fn, args);
  813. },
  814. reduceRight(fn, ...args) {
  815. return reduce(this, "reduceRight", fn, args);
  816. },
  817. shift() {
  818. return noTracking(this, "shift");
  819. },
  820. // slice could use ARRAY_ITERATE but also seems to beg for range tracking
  821. some(fn, thisArg) {
  822. return apply(this, "some", fn, thisArg, void 0, arguments);
  823. },
  824. splice(...args) {
  825. return noTracking(this, "splice", args);
  826. },
  827. toReversed() {
  828. return reactiveReadArray(this).toReversed();
  829. },
  830. toSorted(comparer) {
  831. return reactiveReadArray(this).toSorted(comparer);
  832. },
  833. toSpliced(...args) {
  834. return reactiveReadArray(this).toSpliced(...args);
  835. },
  836. unshift(...args) {
  837. return noTracking(this, "unshift", args);
  838. },
  839. values() {
  840. return iterator(this, "values", toReactive);
  841. }
  842. };
  843. function iterator(self, method, wrapValue) {
  844. const arr = shallowReadArray(self);
  845. const iter = arr[method]();
  846. if (arr !== self && !isShallow(self)) {
  847. iter._next = iter.next;
  848. iter.next = () => {
  849. const result = iter._next();
  850. if (result.value) {
  851. result.value = wrapValue(result.value);
  852. }
  853. return result;
  854. };
  855. }
  856. return iter;
  857. }
  858. const arrayProto = Array.prototype;
  859. function apply(self, method, fn, thisArg, wrappedRetFn, args) {
  860. const arr = shallowReadArray(self);
  861. const needsWrap = arr !== self && !isShallow(self);
  862. const methodFn = arr[method];
  863. if (methodFn !== arrayProto[method]) {
  864. const result2 = methodFn.apply(self, args);
  865. return needsWrap ? toReactive(result2) : result2;
  866. }
  867. let wrappedFn = fn;
  868. if (arr !== self) {
  869. if (needsWrap) {
  870. wrappedFn = function(item, index) {
  871. return fn.call(this, toReactive(item), index, self);
  872. };
  873. } else if (fn.length > 2) {
  874. wrappedFn = function(item, index) {
  875. return fn.call(this, item, index, self);
  876. };
  877. }
  878. }
  879. const result = methodFn.call(arr, wrappedFn, thisArg);
  880. return needsWrap && wrappedRetFn ? wrappedRetFn(result) : result;
  881. }
  882. function reduce(self, method, fn, args) {
  883. const arr = shallowReadArray(self);
  884. let wrappedFn = fn;
  885. if (arr !== self) {
  886. if (!isShallow(self)) {
  887. wrappedFn = function(acc, item, index) {
  888. return fn.call(this, acc, toReactive(item), index, self);
  889. };
  890. } else if (fn.length > 3) {
  891. wrappedFn = function(acc, item, index) {
  892. return fn.call(this, acc, item, index, self);
  893. };
  894. }
  895. }
  896. return arr[method](wrappedFn, ...args);
  897. }
  898. function searchProxy(self, method, args) {
  899. const arr = toRaw(self);
  900. track(arr, "iterate", ARRAY_ITERATE_KEY);
  901. const res = arr[method](...args);
  902. if ((res === -1 || res === false) && isProxy(args[0])) {
  903. args[0] = toRaw(args[0]);
  904. return arr[method](...args);
  905. }
  906. return res;
  907. }
  908. function noTracking(self, method, args = []) {
  909. pauseTracking();
  910. startBatch();
  911. const res = toRaw(self)[method].apply(self, args);
  912. endBatch();
  913. resetTracking();
  914. return res;
  915. }
  916. const isNonTrackableKeys = /* @__PURE__ */ shared.makeMap(`__proto__,__v_isRef,__isVue`);
  917. const builtInSymbols = new Set(
  918. /* @__PURE__ */ Object.getOwnPropertyNames(Symbol).filter((key) => key !== "arguments" && key !== "caller").map((key) => Symbol[key]).filter(shared.isSymbol)
  919. );
  920. function hasOwnProperty(key) {
  921. if (!shared.isSymbol(key)) key = String(key);
  922. const obj = toRaw(this);
  923. track(obj, "has", key);
  924. return obj.hasOwnProperty(key);
  925. }
  926. class BaseReactiveHandler {
  927. constructor(_isReadonly = false, _isShallow = false) {
  928. this._isReadonly = _isReadonly;
  929. this._isShallow = _isShallow;
  930. }
  931. get(target, key, receiver) {
  932. if (key === "__v_skip") return target["__v_skip"];
  933. const isReadonly2 = this._isReadonly, isShallow2 = this._isShallow;
  934. if (key === "__v_isReactive") {
  935. return !isReadonly2;
  936. } else if (key === "__v_isReadonly") {
  937. return isReadonly2;
  938. } else if (key === "__v_isShallow") {
  939. return isShallow2;
  940. } else if (key === "__v_raw") {
  941. if (receiver === (isReadonly2 ? isShallow2 ? shallowReadonlyMap : readonlyMap : isShallow2 ? shallowReactiveMap : reactiveMap).get(target) || // receiver is not the reactive proxy, but has the same prototype
  942. // this means the receiver is a user proxy of the reactive proxy
  943. Object.getPrototypeOf(target) === Object.getPrototypeOf(receiver)) {
  944. return target;
  945. }
  946. return;
  947. }
  948. const targetIsArray = shared.isArray(target);
  949. if (!isReadonly2) {
  950. let fn;
  951. if (targetIsArray && (fn = arrayInstrumentations[key])) {
  952. return fn;
  953. }
  954. if (key === "hasOwnProperty") {
  955. return hasOwnProperty;
  956. }
  957. }
  958. const res = Reflect.get(
  959. target,
  960. key,
  961. // if this is a proxy wrapping a ref, return methods using the raw ref
  962. // as receiver so that we don't have to call `toRaw` on the ref in all
  963. // its class methods
  964. isRef(target) ? target : receiver
  965. );
  966. if (shared.isSymbol(key) ? builtInSymbols.has(key) : isNonTrackableKeys(key)) {
  967. return res;
  968. }
  969. if (!isReadonly2) {
  970. track(target, "get", key);
  971. }
  972. if (isShallow2) {
  973. return res;
  974. }
  975. if (isRef(res)) {
  976. return targetIsArray && shared.isIntegerKey(key) ? res : res.value;
  977. }
  978. if (shared.isObject(res)) {
  979. return isReadonly2 ? readonly(res) : reactive(res);
  980. }
  981. return res;
  982. }
  983. }
  984. class MutableReactiveHandler extends BaseReactiveHandler {
  985. constructor(isShallow2 = false) {
  986. super(false, isShallow2);
  987. }
  988. set(target, key, value, receiver) {
  989. let oldValue = target[key];
  990. if (!this._isShallow) {
  991. const isOldValueReadonly = isReadonly(oldValue);
  992. if (!isShallow(value) && !isReadonly(value)) {
  993. oldValue = toRaw(oldValue);
  994. value = toRaw(value);
  995. }
  996. if (!shared.isArray(target) && isRef(oldValue) && !isRef(value)) {
  997. if (isOldValueReadonly) {
  998. return false;
  999. } else {
  1000. oldValue.value = value;
  1001. return true;
  1002. }
  1003. }
  1004. }
  1005. const hadKey = shared.isArray(target) && shared.isIntegerKey(key) ? Number(key) < target.length : shared.hasOwn(target, key);
  1006. const result = Reflect.set(
  1007. target,
  1008. key,
  1009. value,
  1010. isRef(target) ? target : receiver
  1011. );
  1012. if (target === toRaw(receiver)) {
  1013. if (!hadKey) {
  1014. trigger(target, "add", key, value);
  1015. } else if (shared.hasChanged(value, oldValue)) {
  1016. trigger(target, "set", key, value, oldValue);
  1017. }
  1018. }
  1019. return result;
  1020. }
  1021. deleteProperty(target, key) {
  1022. const hadKey = shared.hasOwn(target, key);
  1023. const oldValue = target[key];
  1024. const result = Reflect.deleteProperty(target, key);
  1025. if (result && hadKey) {
  1026. trigger(target, "delete", key, void 0, oldValue);
  1027. }
  1028. return result;
  1029. }
  1030. has(target, key) {
  1031. const result = Reflect.has(target, key);
  1032. if (!shared.isSymbol(key) || !builtInSymbols.has(key)) {
  1033. track(target, "has", key);
  1034. }
  1035. return result;
  1036. }
  1037. ownKeys(target) {
  1038. track(
  1039. target,
  1040. "iterate",
  1041. shared.isArray(target) ? "length" : ITERATE_KEY
  1042. );
  1043. return Reflect.ownKeys(target);
  1044. }
  1045. }
  1046. class ReadonlyReactiveHandler extends BaseReactiveHandler {
  1047. constructor(isShallow2 = false) {
  1048. super(true, isShallow2);
  1049. }
  1050. set(target, key) {
  1051. {
  1052. warn(
  1053. `Set operation on key "${String(key)}" failed: target is readonly.`,
  1054. target
  1055. );
  1056. }
  1057. return true;
  1058. }
  1059. deleteProperty(target, key) {
  1060. {
  1061. warn(
  1062. `Delete operation on key "${String(key)}" failed: target is readonly.`,
  1063. target
  1064. );
  1065. }
  1066. return true;
  1067. }
  1068. }
  1069. const mutableHandlers = /* @__PURE__ */ new MutableReactiveHandler();
  1070. const readonlyHandlers = /* @__PURE__ */ new ReadonlyReactiveHandler();
  1071. const shallowReactiveHandlers = /* @__PURE__ */ new MutableReactiveHandler(true);
  1072. const shallowReadonlyHandlers = /* @__PURE__ */ new ReadonlyReactiveHandler(true);
  1073. const toShallow = (value) => value;
  1074. const getProto = (v) => Reflect.getPrototypeOf(v);
  1075. function createIterableMethod(method, isReadonly2, isShallow2) {
  1076. return function(...args) {
  1077. const target = this["__v_raw"];
  1078. const rawTarget = toRaw(target);
  1079. const targetIsMap = shared.isMap(rawTarget);
  1080. const isPair = method === "entries" || method === Symbol.iterator && targetIsMap;
  1081. const isKeyOnly = method === "keys" && targetIsMap;
  1082. const innerIterator = target[method](...args);
  1083. const wrap = isShallow2 ? toShallow : isReadonly2 ? toReadonly : toReactive;
  1084. !isReadonly2 && track(
  1085. rawTarget,
  1086. "iterate",
  1087. isKeyOnly ? MAP_KEY_ITERATE_KEY : ITERATE_KEY
  1088. );
  1089. return {
  1090. // iterator protocol
  1091. next() {
  1092. const { value, done } = innerIterator.next();
  1093. return done ? { value, done } : {
  1094. value: isPair ? [wrap(value[0]), wrap(value[1])] : wrap(value),
  1095. done
  1096. };
  1097. },
  1098. // iterable protocol
  1099. [Symbol.iterator]() {
  1100. return this;
  1101. }
  1102. };
  1103. };
  1104. }
  1105. function createReadonlyMethod(type) {
  1106. return function(...args) {
  1107. {
  1108. const key = args[0] ? `on key "${args[0]}" ` : ``;
  1109. warn(
  1110. `${shared.capitalize(type)} operation ${key}failed: target is readonly.`,
  1111. toRaw(this)
  1112. );
  1113. }
  1114. return type === "delete" ? false : type === "clear" ? void 0 : this;
  1115. };
  1116. }
  1117. function createInstrumentations(readonly, shallow) {
  1118. const instrumentations = {
  1119. get(key) {
  1120. const target = this["__v_raw"];
  1121. const rawTarget = toRaw(target);
  1122. const rawKey = toRaw(key);
  1123. if (!readonly) {
  1124. if (shared.hasChanged(key, rawKey)) {
  1125. track(rawTarget, "get", key);
  1126. }
  1127. track(rawTarget, "get", rawKey);
  1128. }
  1129. const { has } = getProto(rawTarget);
  1130. const wrap = shallow ? toShallow : readonly ? toReadonly : toReactive;
  1131. if (has.call(rawTarget, key)) {
  1132. return wrap(target.get(key));
  1133. } else if (has.call(rawTarget, rawKey)) {
  1134. return wrap(target.get(rawKey));
  1135. } else if (target !== rawTarget) {
  1136. target.get(key);
  1137. }
  1138. },
  1139. get size() {
  1140. const target = this["__v_raw"];
  1141. !readonly && track(toRaw(target), "iterate", ITERATE_KEY);
  1142. return Reflect.get(target, "size", target);
  1143. },
  1144. has(key) {
  1145. const target = this["__v_raw"];
  1146. const rawTarget = toRaw(target);
  1147. const rawKey = toRaw(key);
  1148. if (!readonly) {
  1149. if (shared.hasChanged(key, rawKey)) {
  1150. track(rawTarget, "has", key);
  1151. }
  1152. track(rawTarget, "has", rawKey);
  1153. }
  1154. return key === rawKey ? target.has(key) : target.has(key) || target.has(rawKey);
  1155. },
  1156. forEach(callback, thisArg) {
  1157. const observed = this;
  1158. const target = observed["__v_raw"];
  1159. const rawTarget = toRaw(target);
  1160. const wrap = shallow ? toShallow : readonly ? toReadonly : toReactive;
  1161. !readonly && track(rawTarget, "iterate", ITERATE_KEY);
  1162. return target.forEach((value, key) => {
  1163. return callback.call(thisArg, wrap(value), wrap(key), observed);
  1164. });
  1165. }
  1166. };
  1167. shared.extend(
  1168. instrumentations,
  1169. readonly ? {
  1170. add: createReadonlyMethod("add"),
  1171. set: createReadonlyMethod("set"),
  1172. delete: createReadonlyMethod("delete"),
  1173. clear: createReadonlyMethod("clear")
  1174. } : {
  1175. add(value) {
  1176. if (!shallow && !isShallow(value) && !isReadonly(value)) {
  1177. value = toRaw(value);
  1178. }
  1179. const target = toRaw(this);
  1180. const proto = getProto(target);
  1181. const hadKey = proto.has.call(target, value);
  1182. if (!hadKey) {
  1183. target.add(value);
  1184. trigger(target, "add", value, value);
  1185. }
  1186. return this;
  1187. },
  1188. set(key, value) {
  1189. if (!shallow && !isShallow(value) && !isReadonly(value)) {
  1190. value = toRaw(value);
  1191. }
  1192. const target = toRaw(this);
  1193. const { has, get } = getProto(target);
  1194. let hadKey = has.call(target, key);
  1195. if (!hadKey) {
  1196. key = toRaw(key);
  1197. hadKey = has.call(target, key);
  1198. } else {
  1199. checkIdentityKeys(target, has, key);
  1200. }
  1201. const oldValue = get.call(target, key);
  1202. target.set(key, value);
  1203. if (!hadKey) {
  1204. trigger(target, "add", key, value);
  1205. } else if (shared.hasChanged(value, oldValue)) {
  1206. trigger(target, "set", key, value, oldValue);
  1207. }
  1208. return this;
  1209. },
  1210. delete(key) {
  1211. const target = toRaw(this);
  1212. const { has, get } = getProto(target);
  1213. let hadKey = has.call(target, key);
  1214. if (!hadKey) {
  1215. key = toRaw(key);
  1216. hadKey = has.call(target, key);
  1217. } else {
  1218. checkIdentityKeys(target, has, key);
  1219. }
  1220. const oldValue = get ? get.call(target, key) : void 0;
  1221. const result = target.delete(key);
  1222. if (hadKey) {
  1223. trigger(target, "delete", key, void 0, oldValue);
  1224. }
  1225. return result;
  1226. },
  1227. clear() {
  1228. const target = toRaw(this);
  1229. const hadItems = target.size !== 0;
  1230. const oldTarget = shared.isMap(target) ? new Map(target) : new Set(target) ;
  1231. const result = target.clear();
  1232. if (hadItems) {
  1233. trigger(
  1234. target,
  1235. "clear",
  1236. void 0,
  1237. void 0,
  1238. oldTarget
  1239. );
  1240. }
  1241. return result;
  1242. }
  1243. }
  1244. );
  1245. const iteratorMethods = [
  1246. "keys",
  1247. "values",
  1248. "entries",
  1249. Symbol.iterator
  1250. ];
  1251. iteratorMethods.forEach((method) => {
  1252. instrumentations[method] = createIterableMethod(method, readonly, shallow);
  1253. });
  1254. return instrumentations;
  1255. }
  1256. function createInstrumentationGetter(isReadonly2, shallow) {
  1257. const instrumentations = createInstrumentations(isReadonly2, shallow);
  1258. return (target, key, receiver) => {
  1259. if (key === "__v_isReactive") {
  1260. return !isReadonly2;
  1261. } else if (key === "__v_isReadonly") {
  1262. return isReadonly2;
  1263. } else if (key === "__v_raw") {
  1264. return target;
  1265. }
  1266. return Reflect.get(
  1267. shared.hasOwn(instrumentations, key) && key in target ? instrumentations : target,
  1268. key,
  1269. receiver
  1270. );
  1271. };
  1272. }
  1273. const mutableCollectionHandlers = {
  1274. get: /* @__PURE__ */ createInstrumentationGetter(false, false)
  1275. };
  1276. const shallowCollectionHandlers = {
  1277. get: /* @__PURE__ */ createInstrumentationGetter(false, true)
  1278. };
  1279. const readonlyCollectionHandlers = {
  1280. get: /* @__PURE__ */ createInstrumentationGetter(true, false)
  1281. };
  1282. const shallowReadonlyCollectionHandlers = {
  1283. get: /* @__PURE__ */ createInstrumentationGetter(true, true)
  1284. };
  1285. function checkIdentityKeys(target, has, key) {
  1286. const rawKey = toRaw(key);
  1287. if (rawKey !== key && has.call(target, rawKey)) {
  1288. const type = shared.toRawType(target);
  1289. warn(
  1290. `Reactive ${type} contains both the raw and reactive versions of the same object${type === `Map` ? ` as keys` : ``}, which can lead to inconsistencies. Avoid differentiating between the raw and reactive versions of an object and only use the reactive version if possible.`
  1291. );
  1292. }
  1293. }
  1294. const reactiveMap = /* @__PURE__ */ new WeakMap();
  1295. const shallowReactiveMap = /* @__PURE__ */ new WeakMap();
  1296. const readonlyMap = /* @__PURE__ */ new WeakMap();
  1297. const shallowReadonlyMap = /* @__PURE__ */ new WeakMap();
  1298. function targetTypeMap(rawType) {
  1299. switch (rawType) {
  1300. case "Object":
  1301. case "Array":
  1302. return 1 /* COMMON */;
  1303. case "Map":
  1304. case "Set":
  1305. case "WeakMap":
  1306. case "WeakSet":
  1307. return 2 /* COLLECTION */;
  1308. default:
  1309. return 0 /* INVALID */;
  1310. }
  1311. }
  1312. function getTargetType(value) {
  1313. return value["__v_skip"] || !Object.isExtensible(value) ? 0 /* INVALID */ : targetTypeMap(shared.toRawType(value));
  1314. }
  1315. function reactive(target) {
  1316. if (isReadonly(target)) {
  1317. return target;
  1318. }
  1319. return createReactiveObject(
  1320. target,
  1321. false,
  1322. mutableHandlers,
  1323. mutableCollectionHandlers,
  1324. reactiveMap
  1325. );
  1326. }
  1327. function shallowReactive(target) {
  1328. return createReactiveObject(
  1329. target,
  1330. false,
  1331. shallowReactiveHandlers,
  1332. shallowCollectionHandlers,
  1333. shallowReactiveMap
  1334. );
  1335. }
  1336. function readonly(target) {
  1337. return createReactiveObject(
  1338. target,
  1339. true,
  1340. readonlyHandlers,
  1341. readonlyCollectionHandlers,
  1342. readonlyMap
  1343. );
  1344. }
  1345. function shallowReadonly(target) {
  1346. return createReactiveObject(
  1347. target,
  1348. true,
  1349. shallowReadonlyHandlers,
  1350. shallowReadonlyCollectionHandlers,
  1351. shallowReadonlyMap
  1352. );
  1353. }
  1354. function createReactiveObject(target, isReadonly2, baseHandlers, collectionHandlers, proxyMap) {
  1355. if (!shared.isObject(target)) {
  1356. {
  1357. warn(
  1358. `value cannot be made ${isReadonly2 ? "readonly" : "reactive"}: ${String(
  1359. target
  1360. )}`
  1361. );
  1362. }
  1363. return target;
  1364. }
  1365. if (target["__v_raw"] && !(isReadonly2 && target["__v_isReactive"])) {
  1366. return target;
  1367. }
  1368. const targetType = getTargetType(target);
  1369. if (targetType === 0 /* INVALID */) {
  1370. return target;
  1371. }
  1372. const existingProxy = proxyMap.get(target);
  1373. if (existingProxy) {
  1374. return existingProxy;
  1375. }
  1376. const proxy = new Proxy(
  1377. target,
  1378. targetType === 2 /* COLLECTION */ ? collectionHandlers : baseHandlers
  1379. );
  1380. proxyMap.set(target, proxy);
  1381. return proxy;
  1382. }
  1383. function isReactive(value) {
  1384. if (isReadonly(value)) {
  1385. return isReactive(value["__v_raw"]);
  1386. }
  1387. return !!(value && value["__v_isReactive"]);
  1388. }
  1389. function isReadonly(value) {
  1390. return !!(value && value["__v_isReadonly"]);
  1391. }
  1392. function isShallow(value) {
  1393. return !!(value && value["__v_isShallow"]);
  1394. }
  1395. function isProxy(value) {
  1396. return value ? !!value["__v_raw"] : false;
  1397. }
  1398. function toRaw(observed) {
  1399. const raw = observed && observed["__v_raw"];
  1400. return raw ? toRaw(raw) : observed;
  1401. }
  1402. function markRaw(value) {
  1403. if (!shared.hasOwn(value, "__v_skip") && Object.isExtensible(value)) {
  1404. shared.def(value, "__v_skip", true);
  1405. }
  1406. return value;
  1407. }
  1408. const toReactive = (value) => shared.isObject(value) ? reactive(value) : value;
  1409. const toReadonly = (value) => shared.isObject(value) ? readonly(value) : value;
  1410. function isRef(r) {
  1411. return r ? r["__v_isRef"] === true : false;
  1412. }
  1413. function ref(value) {
  1414. return createRef(value, false);
  1415. }
  1416. function shallowRef(value) {
  1417. return createRef(value, true);
  1418. }
  1419. function createRef(rawValue, shallow) {
  1420. if (isRef(rawValue)) {
  1421. return rawValue;
  1422. }
  1423. return new RefImpl(rawValue, shallow);
  1424. }
  1425. class RefImpl {
  1426. constructor(value, isShallow2) {
  1427. this.dep = new Dep();
  1428. this["__v_isRef"] = true;
  1429. this["__v_isShallow"] = false;
  1430. this._rawValue = isShallow2 ? value : toRaw(value);
  1431. this._value = isShallow2 ? value : toReactive(value);
  1432. this["__v_isShallow"] = isShallow2;
  1433. }
  1434. get value() {
  1435. {
  1436. this.dep.track({
  1437. target: this,
  1438. type: "get",
  1439. key: "value"
  1440. });
  1441. }
  1442. return this._value;
  1443. }
  1444. set value(newValue) {
  1445. const oldValue = this._rawValue;
  1446. const useDirectValue = this["__v_isShallow"] || isShallow(newValue) || isReadonly(newValue);
  1447. newValue = useDirectValue ? newValue : toRaw(newValue);
  1448. if (shared.hasChanged(newValue, oldValue)) {
  1449. this._rawValue = newValue;
  1450. this._value = useDirectValue ? newValue : toReactive(newValue);
  1451. {
  1452. this.dep.trigger({
  1453. target: this,
  1454. type: "set",
  1455. key: "value",
  1456. newValue,
  1457. oldValue
  1458. });
  1459. }
  1460. }
  1461. }
  1462. }
  1463. function triggerRef(ref2) {
  1464. if (ref2.dep) {
  1465. {
  1466. ref2.dep.trigger({
  1467. target: ref2,
  1468. type: "set",
  1469. key: "value",
  1470. newValue: ref2._value
  1471. });
  1472. }
  1473. }
  1474. }
  1475. function unref(ref2) {
  1476. return isRef(ref2) ? ref2.value : ref2;
  1477. }
  1478. function toValue(source) {
  1479. return shared.isFunction(source) ? source() : unref(source);
  1480. }
  1481. const shallowUnwrapHandlers = {
  1482. get: (target, key, receiver) => key === "__v_raw" ? target : unref(Reflect.get(target, key, receiver)),
  1483. set: (target, key, value, receiver) => {
  1484. const oldValue = target[key];
  1485. if (isRef(oldValue) && !isRef(value)) {
  1486. oldValue.value = value;
  1487. return true;
  1488. } else {
  1489. return Reflect.set(target, key, value, receiver);
  1490. }
  1491. }
  1492. };
  1493. function proxyRefs(objectWithRefs) {
  1494. return isReactive(objectWithRefs) ? objectWithRefs : new Proxy(objectWithRefs, shallowUnwrapHandlers);
  1495. }
  1496. class CustomRefImpl {
  1497. constructor(factory) {
  1498. this["__v_isRef"] = true;
  1499. this._value = void 0;
  1500. const dep = this.dep = new Dep();
  1501. const { get, set } = factory(dep.track.bind(dep), dep.trigger.bind(dep));
  1502. this._get = get;
  1503. this._set = set;
  1504. }
  1505. get value() {
  1506. return this._value = this._get();
  1507. }
  1508. set value(newVal) {
  1509. this._set(newVal);
  1510. }
  1511. }
  1512. function customRef(factory) {
  1513. return new CustomRefImpl(factory);
  1514. }
  1515. function toRefs(object) {
  1516. if (!isProxy(object)) {
  1517. warn(`toRefs() expects a reactive object but received a plain one.`);
  1518. }
  1519. const ret = shared.isArray(object) ? new Array(object.length) : {};
  1520. for (const key in object) {
  1521. ret[key] = propertyToRef(object, key);
  1522. }
  1523. return ret;
  1524. }
  1525. class ObjectRefImpl {
  1526. constructor(_object, _key, _defaultValue) {
  1527. this._object = _object;
  1528. this._key = _key;
  1529. this._defaultValue = _defaultValue;
  1530. this["__v_isRef"] = true;
  1531. this._value = void 0;
  1532. }
  1533. get value() {
  1534. const val = this._object[this._key];
  1535. return this._value = val === void 0 ? this._defaultValue : val;
  1536. }
  1537. set value(newVal) {
  1538. this._object[this._key] = newVal;
  1539. }
  1540. get dep() {
  1541. return getDepFromReactive(toRaw(this._object), this._key);
  1542. }
  1543. }
  1544. class GetterRefImpl {
  1545. constructor(_getter) {
  1546. this._getter = _getter;
  1547. this["__v_isRef"] = true;
  1548. this["__v_isReadonly"] = true;
  1549. this._value = void 0;
  1550. }
  1551. get value() {
  1552. return this._value = this._getter();
  1553. }
  1554. }
  1555. function toRef(source, key, defaultValue) {
  1556. if (isRef(source)) {
  1557. return source;
  1558. } else if (shared.isFunction(source)) {
  1559. return new GetterRefImpl(source);
  1560. } else if (shared.isObject(source) && arguments.length > 1) {
  1561. return propertyToRef(source, key, defaultValue);
  1562. } else {
  1563. return ref(source);
  1564. }
  1565. }
  1566. function propertyToRef(source, key, defaultValue) {
  1567. const val = source[key];
  1568. return isRef(val) ? val : new ObjectRefImpl(source, key, defaultValue);
  1569. }
  1570. class ComputedRefImpl {
  1571. constructor(fn, setter, isSSR) {
  1572. this.fn = fn;
  1573. this.setter = setter;
  1574. /**
  1575. * @internal
  1576. */
  1577. this._value = void 0;
  1578. /**
  1579. * @internal
  1580. */
  1581. this.dep = new Dep(this);
  1582. /**
  1583. * @internal
  1584. */
  1585. this.__v_isRef = true;
  1586. // TODO isolatedDeclarations "__v_isReadonly"
  1587. // A computed is also a subscriber that tracks other deps
  1588. /**
  1589. * @internal
  1590. */
  1591. this.deps = void 0;
  1592. /**
  1593. * @internal
  1594. */
  1595. this.depsTail = void 0;
  1596. /**
  1597. * @internal
  1598. */
  1599. this.flags = 16;
  1600. /**
  1601. * @internal
  1602. */
  1603. this.globalVersion = globalVersion - 1;
  1604. /**
  1605. * @internal
  1606. */
  1607. this.next = void 0;
  1608. // for backwards compat
  1609. this.effect = this;
  1610. this["__v_isReadonly"] = !setter;
  1611. this.isSSR = isSSR;
  1612. }
  1613. /**
  1614. * @internal
  1615. */
  1616. notify() {
  1617. this.flags |= 16;
  1618. if (!(this.flags & 8) && // avoid infinite self recursion
  1619. activeSub !== this) {
  1620. batch(this, true);
  1621. return true;
  1622. }
  1623. }
  1624. get value() {
  1625. const link = this.dep.track({
  1626. target: this,
  1627. type: "get",
  1628. key: "value"
  1629. }) ;
  1630. refreshComputed(this);
  1631. if (link) {
  1632. link.version = this.dep.version;
  1633. }
  1634. return this._value;
  1635. }
  1636. set value(newValue) {
  1637. if (this.setter) {
  1638. this.setter(newValue);
  1639. } else {
  1640. warn("Write operation failed: computed value is readonly");
  1641. }
  1642. }
  1643. }
  1644. function computed(getterOrOptions, debugOptions, isSSR = false) {
  1645. let getter;
  1646. let setter;
  1647. if (shared.isFunction(getterOrOptions)) {
  1648. getter = getterOrOptions;
  1649. } else {
  1650. getter = getterOrOptions.get;
  1651. setter = getterOrOptions.set;
  1652. }
  1653. const cRef = new ComputedRefImpl(getter, setter, isSSR);
  1654. if (debugOptions && !isSSR) {
  1655. cRef.onTrack = debugOptions.onTrack;
  1656. cRef.onTrigger = debugOptions.onTrigger;
  1657. }
  1658. return cRef;
  1659. }
  1660. const TrackOpTypes = {
  1661. "GET": "get",
  1662. "HAS": "has",
  1663. "ITERATE": "iterate"
  1664. };
  1665. const TriggerOpTypes = {
  1666. "SET": "set",
  1667. "ADD": "add",
  1668. "DELETE": "delete",
  1669. "CLEAR": "clear"
  1670. };
  1671. const ReactiveFlags = {
  1672. "SKIP": "__v_skip",
  1673. "IS_REACTIVE": "__v_isReactive",
  1674. "IS_READONLY": "__v_isReadonly",
  1675. "IS_SHALLOW": "__v_isShallow",
  1676. "RAW": "__v_raw",
  1677. "IS_REF": "__v_isRef"
  1678. };
  1679. const WatchErrorCodes = {
  1680. "WATCH_GETTER": 2,
  1681. "2": "WATCH_GETTER",
  1682. "WATCH_CALLBACK": 3,
  1683. "3": "WATCH_CALLBACK",
  1684. "WATCH_CLEANUP": 4,
  1685. "4": "WATCH_CLEANUP"
  1686. };
  1687. const INITIAL_WATCHER_VALUE = {};
  1688. const cleanupMap = /* @__PURE__ */ new WeakMap();
  1689. let activeWatcher = void 0;
  1690. function getCurrentWatcher() {
  1691. return activeWatcher;
  1692. }
  1693. function onWatcherCleanup(cleanupFn, failSilently = false, owner = activeWatcher) {
  1694. if (owner) {
  1695. let cleanups = cleanupMap.get(owner);
  1696. if (!cleanups) cleanupMap.set(owner, cleanups = []);
  1697. cleanups.push(cleanupFn);
  1698. } else if (!failSilently) {
  1699. warn(
  1700. `onWatcherCleanup() was called when there was no active watcher to associate with.`
  1701. );
  1702. }
  1703. }
  1704. function watch(source, cb, options = shared.EMPTY_OBJ) {
  1705. const { immediate, deep, once, scheduler, augmentJob, call } = options;
  1706. const warnInvalidSource = (s) => {
  1707. (options.onWarn || warn)(
  1708. `Invalid watch source: `,
  1709. s,
  1710. `A watch source can only be a getter/effect function, a ref, a reactive object, or an array of these types.`
  1711. );
  1712. };
  1713. const reactiveGetter = (source2) => {
  1714. if (deep) return source2;
  1715. if (isShallow(source2) || deep === false || deep === 0)
  1716. return traverse(source2, 1);
  1717. return traverse(source2);
  1718. };
  1719. let effect;
  1720. let getter;
  1721. let cleanup;
  1722. let boundCleanup;
  1723. let forceTrigger = false;
  1724. let isMultiSource = false;
  1725. if (isRef(source)) {
  1726. getter = () => source.value;
  1727. forceTrigger = isShallow(source);
  1728. } else if (isReactive(source)) {
  1729. getter = () => reactiveGetter(source);
  1730. forceTrigger = true;
  1731. } else if (shared.isArray(source)) {
  1732. isMultiSource = true;
  1733. forceTrigger = source.some((s) => isReactive(s) || isShallow(s));
  1734. getter = () => source.map((s) => {
  1735. if (isRef(s)) {
  1736. return s.value;
  1737. } else if (isReactive(s)) {
  1738. return reactiveGetter(s);
  1739. } else if (shared.isFunction(s)) {
  1740. return call ? call(s, 2) : s();
  1741. } else {
  1742. warnInvalidSource(s);
  1743. }
  1744. });
  1745. } else if (shared.isFunction(source)) {
  1746. if (cb) {
  1747. getter = call ? () => call(source, 2) : source;
  1748. } else {
  1749. getter = () => {
  1750. if (cleanup) {
  1751. pauseTracking();
  1752. try {
  1753. cleanup();
  1754. } finally {
  1755. resetTracking();
  1756. }
  1757. }
  1758. const currentEffect = activeWatcher;
  1759. activeWatcher = effect;
  1760. try {
  1761. return call ? call(source, 3, [boundCleanup]) : source(boundCleanup);
  1762. } finally {
  1763. activeWatcher = currentEffect;
  1764. }
  1765. };
  1766. }
  1767. } else {
  1768. getter = shared.NOOP;
  1769. warnInvalidSource(source);
  1770. }
  1771. if (cb && deep) {
  1772. const baseGetter = getter;
  1773. const depth = deep === true ? Infinity : deep;
  1774. getter = () => traverse(baseGetter(), depth);
  1775. }
  1776. const scope = getCurrentScope();
  1777. const watchHandle = () => {
  1778. effect.stop();
  1779. if (scope && scope.active) {
  1780. shared.remove(scope.effects, effect);
  1781. }
  1782. };
  1783. if (once && cb) {
  1784. const _cb = cb;
  1785. cb = (...args) => {
  1786. _cb(...args);
  1787. watchHandle();
  1788. };
  1789. }
  1790. let oldValue = isMultiSource ? new Array(source.length).fill(INITIAL_WATCHER_VALUE) : INITIAL_WATCHER_VALUE;
  1791. const job = (immediateFirstRun) => {
  1792. if (!(effect.flags & 1) || !effect.dirty && !immediateFirstRun) {
  1793. return;
  1794. }
  1795. if (cb) {
  1796. const newValue = effect.run();
  1797. if (deep || forceTrigger || (isMultiSource ? newValue.some((v, i) => shared.hasChanged(v, oldValue[i])) : shared.hasChanged(newValue, oldValue))) {
  1798. if (cleanup) {
  1799. cleanup();
  1800. }
  1801. const currentWatcher = activeWatcher;
  1802. activeWatcher = effect;
  1803. try {
  1804. const args = [
  1805. newValue,
  1806. // pass undefined as the old value when it's changed for the first time
  1807. oldValue === INITIAL_WATCHER_VALUE ? void 0 : isMultiSource && oldValue[0] === INITIAL_WATCHER_VALUE ? [] : oldValue,
  1808. boundCleanup
  1809. ];
  1810. oldValue = newValue;
  1811. call ? call(cb, 3, args) : (
  1812. // @ts-expect-error
  1813. cb(...args)
  1814. );
  1815. } finally {
  1816. activeWatcher = currentWatcher;
  1817. }
  1818. }
  1819. } else {
  1820. effect.run();
  1821. }
  1822. };
  1823. if (augmentJob) {
  1824. augmentJob(job);
  1825. }
  1826. effect = new ReactiveEffect(getter);
  1827. effect.scheduler = scheduler ? () => scheduler(job, false) : job;
  1828. boundCleanup = (fn) => onWatcherCleanup(fn, false, effect);
  1829. cleanup = effect.onStop = () => {
  1830. const cleanups = cleanupMap.get(effect);
  1831. if (cleanups) {
  1832. if (call) {
  1833. call(cleanups, 4);
  1834. } else {
  1835. for (const cleanup2 of cleanups) cleanup2();
  1836. }
  1837. cleanupMap.delete(effect);
  1838. }
  1839. };
  1840. {
  1841. effect.onTrack = options.onTrack;
  1842. effect.onTrigger = options.onTrigger;
  1843. }
  1844. if (cb) {
  1845. if (immediate) {
  1846. job(true);
  1847. } else {
  1848. oldValue = effect.run();
  1849. }
  1850. } else if (scheduler) {
  1851. scheduler(job.bind(null, true), true);
  1852. } else {
  1853. effect.run();
  1854. }
  1855. watchHandle.pause = effect.pause.bind(effect);
  1856. watchHandle.resume = effect.resume.bind(effect);
  1857. watchHandle.stop = watchHandle;
  1858. return watchHandle;
  1859. }
  1860. function traverse(value, depth = Infinity, seen) {
  1861. if (depth <= 0 || !shared.isObject(value) || value["__v_skip"]) {
  1862. return value;
  1863. }
  1864. seen = seen || /* @__PURE__ */ new Set();
  1865. if (seen.has(value)) {
  1866. return value;
  1867. }
  1868. seen.add(value);
  1869. depth--;
  1870. if (isRef(value)) {
  1871. traverse(value.value, depth, seen);
  1872. } else if (shared.isArray(value)) {
  1873. for (let i = 0; i < value.length; i++) {
  1874. traverse(value[i], depth, seen);
  1875. }
  1876. } else if (shared.isSet(value) || shared.isMap(value)) {
  1877. value.forEach((v) => {
  1878. traverse(v, depth, seen);
  1879. });
  1880. } else if (shared.isPlainObject(value)) {
  1881. for (const key in value) {
  1882. traverse(value[key], depth, seen);
  1883. }
  1884. for (const key of Object.getOwnPropertySymbols(value)) {
  1885. if (Object.prototype.propertyIsEnumerable.call(value, key)) {
  1886. traverse(value[key], depth, seen);
  1887. }
  1888. }
  1889. }
  1890. return value;
  1891. }
  1892. exports.ARRAY_ITERATE_KEY = ARRAY_ITERATE_KEY;
  1893. exports.EffectFlags = EffectFlags;
  1894. exports.EffectScope = EffectScope;
  1895. exports.ITERATE_KEY = ITERATE_KEY;
  1896. exports.MAP_KEY_ITERATE_KEY = MAP_KEY_ITERATE_KEY;
  1897. exports.ReactiveEffect = ReactiveEffect;
  1898. exports.ReactiveFlags = ReactiveFlags;
  1899. exports.TrackOpTypes = TrackOpTypes;
  1900. exports.TriggerOpTypes = TriggerOpTypes;
  1901. exports.WatchErrorCodes = WatchErrorCodes;
  1902. exports.computed = computed;
  1903. exports.customRef = customRef;
  1904. exports.effect = effect;
  1905. exports.effectScope = effectScope;
  1906. exports.enableTracking = enableTracking;
  1907. exports.getCurrentScope = getCurrentScope;
  1908. exports.getCurrentWatcher = getCurrentWatcher;
  1909. exports.isProxy = isProxy;
  1910. exports.isReactive = isReactive;
  1911. exports.isReadonly = isReadonly;
  1912. exports.isRef = isRef;
  1913. exports.isShallow = isShallow;
  1914. exports.markRaw = markRaw;
  1915. exports.onEffectCleanup = onEffectCleanup;
  1916. exports.onScopeDispose = onScopeDispose;
  1917. exports.onWatcherCleanup = onWatcherCleanup;
  1918. exports.pauseTracking = pauseTracking;
  1919. exports.proxyRefs = proxyRefs;
  1920. exports.reactive = reactive;
  1921. exports.reactiveReadArray = reactiveReadArray;
  1922. exports.readonly = readonly;
  1923. exports.ref = ref;
  1924. exports.resetTracking = resetTracking;
  1925. exports.shallowReactive = shallowReactive;
  1926. exports.shallowReadArray = shallowReadArray;
  1927. exports.shallowReadonly = shallowReadonly;
  1928. exports.shallowRef = shallowRef;
  1929. exports.stop = stop;
  1930. exports.toRaw = toRaw;
  1931. exports.toReactive = toReactive;
  1932. exports.toReadonly = toReadonly;
  1933. exports.toRef = toRef;
  1934. exports.toRefs = toRefs;
  1935. exports.toValue = toValue;
  1936. exports.track = track;
  1937. exports.traverse = traverse;
  1938. exports.trigger = trigger;
  1939. exports.triggerRef = triggerRef;
  1940. exports.unref = unref;
  1941. exports.watch = watch;