reactivity.cjs.js 49 KB

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