reactivity.cjs.prod.js 46 KB

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