reactivity.cjs.prod.js 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807
  1. /**
  2. * @vue/reactivity v3.5.18
  3. * (c) 2018-present Yuxi (Evan) You and Vue contributors
  4. * @license MIT
  5. **/
  6. 'use strict';
  7. Object.defineProperty(exports, '__esModule', { value: true });
  8. var shared = require('@vue/shared');
  9. 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. // TODO isolatedDeclarations "__v_skip"
  490. constructor(computed) {
  491. this.computed = computed;
  492. this.version = 0;
  493. /**
  494. * Link between this dep and the current active effect
  495. */
  496. this.activeLink = void 0;
  497. /**
  498. * Doubly linked list representing the subscribing effects (tail)
  499. */
  500. this.subs = void 0;
  501. /**
  502. * For object property deps cleanup
  503. */
  504. this.map = void 0;
  505. this.key = void 0;
  506. /**
  507. * Subscriber counter
  508. */
  509. this.sc = 0;
  510. /**
  511. * @internal
  512. */
  513. this.__v_skip = true;
  514. }
  515. track(debugInfo) {
  516. if (!activeSub || !shouldTrack || activeSub === this.computed) {
  517. return;
  518. }
  519. let link = this.activeLink;
  520. if (link === void 0 || link.sub !== activeSub) {
  521. link = this.activeLink = new Link(activeSub, this);
  522. if (!activeSub.deps) {
  523. activeSub.deps = activeSub.depsTail = link;
  524. } else {
  525. link.prevDep = activeSub.depsTail;
  526. activeSub.depsTail.nextDep = link;
  527. activeSub.depsTail = link;
  528. }
  529. addSub(link);
  530. } else if (link.version === -1) {
  531. link.version = this.version;
  532. if (link.nextDep) {
  533. const next = link.nextDep;
  534. next.prevDep = link.prevDep;
  535. if (link.prevDep) {
  536. link.prevDep.nextDep = next;
  537. }
  538. link.prevDep = activeSub.depsTail;
  539. link.nextDep = void 0;
  540. activeSub.depsTail.nextDep = link;
  541. activeSub.depsTail = link;
  542. if (activeSub.deps === link) {
  543. activeSub.deps = next;
  544. }
  545. }
  546. }
  547. return link;
  548. }
  549. trigger(debugInfo) {
  550. this.version++;
  551. globalVersion++;
  552. this.notify(debugInfo);
  553. }
  554. notify(debugInfo) {
  555. startBatch();
  556. try {
  557. if (false) ;
  558. for (let link = this.subs; link; link = link.prevSub) {
  559. if (link.sub.notify()) {
  560. ;
  561. link.sub.dep.notify();
  562. }
  563. }
  564. } finally {
  565. endBatch();
  566. }
  567. }
  568. }
  569. function addSub(link) {
  570. link.dep.sc++;
  571. if (link.sub.flags & 4) {
  572. const computed = link.dep.computed;
  573. if (computed && !link.dep.subs) {
  574. computed.flags |= 4 | 16;
  575. for (let l = computed.deps; l; l = l.nextDep) {
  576. addSub(l);
  577. }
  578. }
  579. const currentTail = link.dep.subs;
  580. if (currentTail !== link) {
  581. link.prevSub = currentTail;
  582. if (currentTail) currentTail.nextSub = link;
  583. }
  584. link.dep.subs = link;
  585. }
  586. }
  587. const targetMap = /* @__PURE__ */ new WeakMap();
  588. const ITERATE_KEY = Symbol(
  589. ""
  590. );
  591. const MAP_KEY_ITERATE_KEY = Symbol(
  592. ""
  593. );
  594. const ARRAY_ITERATE_KEY = Symbol(
  595. ""
  596. );
  597. function track(target, type, key) {
  598. if (shouldTrack && activeSub) {
  599. let depsMap = targetMap.get(target);
  600. if (!depsMap) {
  601. targetMap.set(target, depsMap = /* @__PURE__ */ new Map());
  602. }
  603. let dep = depsMap.get(key);
  604. if (!dep) {
  605. depsMap.set(key, dep = new Dep());
  606. dep.map = depsMap;
  607. dep.key = key;
  608. }
  609. {
  610. dep.track();
  611. }
  612. }
  613. }
  614. function trigger(target, type, key, newValue, oldValue, oldTarget) {
  615. const depsMap = targetMap.get(target);
  616. if (!depsMap) {
  617. globalVersion++;
  618. return;
  619. }
  620. const run = (dep) => {
  621. if (dep) {
  622. {
  623. dep.trigger();
  624. }
  625. }
  626. };
  627. startBatch();
  628. if (type === "clear") {
  629. depsMap.forEach(run);
  630. } else {
  631. const targetIsArray = shared.isArray(target);
  632. const isArrayIndex = targetIsArray && shared.isIntegerKey(key);
  633. if (targetIsArray && key === "length") {
  634. const newLength = Number(newValue);
  635. depsMap.forEach((dep, key2) => {
  636. if (key2 === "length" || key2 === ARRAY_ITERATE_KEY || !shared.isSymbol(key2) && key2 >= newLength) {
  637. run(dep);
  638. }
  639. });
  640. } else {
  641. if (key !== void 0 || depsMap.has(void 0)) {
  642. run(depsMap.get(key));
  643. }
  644. if (isArrayIndex) {
  645. run(depsMap.get(ARRAY_ITERATE_KEY));
  646. }
  647. switch (type) {
  648. case "add":
  649. if (!targetIsArray) {
  650. run(depsMap.get(ITERATE_KEY));
  651. if (shared.isMap(target)) {
  652. run(depsMap.get(MAP_KEY_ITERATE_KEY));
  653. }
  654. } else if (isArrayIndex) {
  655. run(depsMap.get("length"));
  656. }
  657. break;
  658. case "delete":
  659. if (!targetIsArray) {
  660. run(depsMap.get(ITERATE_KEY));
  661. if (shared.isMap(target)) {
  662. run(depsMap.get(MAP_KEY_ITERATE_KEY));
  663. }
  664. }
  665. break;
  666. case "set":
  667. if (shared.isMap(target)) {
  668. run(depsMap.get(ITERATE_KEY));
  669. }
  670. break;
  671. }
  672. }
  673. }
  674. endBatch();
  675. }
  676. function getDepFromReactive(object, key) {
  677. const depMap = targetMap.get(object);
  678. return depMap && depMap.get(key);
  679. }
  680. function reactiveReadArray(array) {
  681. const raw = toRaw(array);
  682. if (raw === array) return raw;
  683. track(raw, "iterate", ARRAY_ITERATE_KEY);
  684. return isShallow(array) ? raw : raw.map(toReactive);
  685. }
  686. function shallowReadArray(arr) {
  687. track(arr = toRaw(arr), "iterate", ARRAY_ITERATE_KEY);
  688. return arr;
  689. }
  690. const arrayInstrumentations = {
  691. __proto__: null,
  692. [Symbol.iterator]() {
  693. return iterator(this, Symbol.iterator, toReactive);
  694. },
  695. concat(...args) {
  696. return reactiveReadArray(this).concat(
  697. ...args.map((x) => shared.isArray(x) ? reactiveReadArray(x) : x)
  698. );
  699. },
  700. entries() {
  701. return iterator(this, "entries", (value) => {
  702. value[1] = toReactive(value[1]);
  703. return value;
  704. });
  705. },
  706. every(fn, thisArg) {
  707. return apply(this, "every", fn, thisArg, void 0, arguments);
  708. },
  709. filter(fn, thisArg) {
  710. return apply(this, "filter", fn, thisArg, (v) => v.map(toReactive), arguments);
  711. },
  712. find(fn, thisArg) {
  713. return apply(this, "find", fn, thisArg, toReactive, arguments);
  714. },
  715. findIndex(fn, thisArg) {
  716. return apply(this, "findIndex", fn, thisArg, void 0, arguments);
  717. },
  718. findLast(fn, thisArg) {
  719. return apply(this, "findLast", fn, thisArg, toReactive, arguments);
  720. },
  721. findLastIndex(fn, thisArg) {
  722. return apply(this, "findLastIndex", fn, thisArg, void 0, arguments);
  723. },
  724. // flat, flatMap could benefit from ARRAY_ITERATE but are not straight-forward to implement
  725. forEach(fn, thisArg) {
  726. return apply(this, "forEach", fn, thisArg, void 0, arguments);
  727. },
  728. includes(...args) {
  729. return searchProxy(this, "includes", args);
  730. },
  731. indexOf(...args) {
  732. return searchProxy(this, "indexOf", args);
  733. },
  734. join(separator) {
  735. return reactiveReadArray(this).join(separator);
  736. },
  737. // keys() iterator only reads `length`, no optimisation required
  738. lastIndexOf(...args) {
  739. return searchProxy(this, "lastIndexOf", args);
  740. },
  741. map(fn, thisArg) {
  742. return apply(this, "map", fn, thisArg, void 0, arguments);
  743. },
  744. pop() {
  745. return noTracking(this, "pop");
  746. },
  747. push(...args) {
  748. return noTracking(this, "push", args);
  749. },
  750. reduce(fn, ...args) {
  751. return reduce(this, "reduce", fn, args);
  752. },
  753. reduceRight(fn, ...args) {
  754. return reduce(this, "reduceRight", fn, args);
  755. },
  756. shift() {
  757. return noTracking(this, "shift");
  758. },
  759. // slice could use ARRAY_ITERATE but also seems to beg for range tracking
  760. some(fn, thisArg) {
  761. return apply(this, "some", fn, thisArg, void 0, arguments);
  762. },
  763. splice(...args) {
  764. return noTracking(this, "splice", args);
  765. },
  766. toReversed() {
  767. return reactiveReadArray(this).toReversed();
  768. },
  769. toSorted(comparer) {
  770. return reactiveReadArray(this).toSorted(comparer);
  771. },
  772. toSpliced(...args) {
  773. return reactiveReadArray(this).toSpliced(...args);
  774. },
  775. unshift(...args) {
  776. return noTracking(this, "unshift", args);
  777. },
  778. values() {
  779. return iterator(this, "values", toReactive);
  780. }
  781. };
  782. function iterator(self, method, wrapValue) {
  783. const arr = shallowReadArray(self);
  784. const iter = arr[method]();
  785. if (arr !== self && !isShallow(self)) {
  786. iter._next = iter.next;
  787. iter.next = () => {
  788. const result = iter._next();
  789. if (result.value) {
  790. result.value = wrapValue(result.value);
  791. }
  792. return result;
  793. };
  794. }
  795. return iter;
  796. }
  797. const arrayProto = Array.prototype;
  798. function apply(self, method, fn, thisArg, wrappedRetFn, args) {
  799. const arr = shallowReadArray(self);
  800. const needsWrap = arr !== self && !isShallow(self);
  801. const methodFn = arr[method];
  802. if (methodFn !== arrayProto[method]) {
  803. const result2 = methodFn.apply(self, args);
  804. return needsWrap ? toReactive(result2) : result2;
  805. }
  806. let wrappedFn = fn;
  807. if (arr !== self) {
  808. if (needsWrap) {
  809. wrappedFn = function(item, index) {
  810. return fn.call(this, toReactive(item), index, self);
  811. };
  812. } else if (fn.length > 2) {
  813. wrappedFn = function(item, index) {
  814. return fn.call(this, item, index, self);
  815. };
  816. }
  817. }
  818. const result = methodFn.call(arr, wrappedFn, thisArg);
  819. return needsWrap && wrappedRetFn ? wrappedRetFn(result) : result;
  820. }
  821. function reduce(self, method, fn, args) {
  822. const arr = shallowReadArray(self);
  823. let wrappedFn = fn;
  824. if (arr !== self) {
  825. if (!isShallow(self)) {
  826. wrappedFn = function(acc, item, index) {
  827. return fn.call(this, acc, toReactive(item), index, self);
  828. };
  829. } else if (fn.length > 3) {
  830. wrappedFn = function(acc, item, index) {
  831. return fn.call(this, acc, item, index, self);
  832. };
  833. }
  834. }
  835. return arr[method](wrappedFn, ...args);
  836. }
  837. function searchProxy(self, method, args) {
  838. const arr = toRaw(self);
  839. track(arr, "iterate", ARRAY_ITERATE_KEY);
  840. const res = arr[method](...args);
  841. if ((res === -1 || res === false) && isProxy(args[0])) {
  842. args[0] = toRaw(args[0]);
  843. return arr[method](...args);
  844. }
  845. return res;
  846. }
  847. function noTracking(self, method, args = []) {
  848. pauseTracking();
  849. startBatch();
  850. const res = toRaw(self)[method].apply(self, args);
  851. endBatch();
  852. resetTracking();
  853. return res;
  854. }
  855. const isNonTrackableKeys = /* @__PURE__ */ shared.makeMap(`__proto__,__v_isRef,__isVue`);
  856. const builtInSymbols = new Set(
  857. /* @__PURE__ */ Object.getOwnPropertyNames(Symbol).filter((key) => key !== "arguments" && key !== "caller").map((key) => Symbol[key]).filter(shared.isSymbol)
  858. );
  859. function hasOwnProperty(key) {
  860. if (!shared.isSymbol(key)) key = String(key);
  861. const obj = toRaw(this);
  862. track(obj, "has", key);
  863. return obj.hasOwnProperty(key);
  864. }
  865. class BaseReactiveHandler {
  866. constructor(_isReadonly = false, _isShallow = false) {
  867. this._isReadonly = _isReadonly;
  868. this._isShallow = _isShallow;
  869. }
  870. get(target, key, receiver) {
  871. if (key === "__v_skip") return target["__v_skip"];
  872. const isReadonly2 = this._isReadonly, isShallow2 = this._isShallow;
  873. if (key === "__v_isReactive") {
  874. return !isReadonly2;
  875. } else if (key === "__v_isReadonly") {
  876. return isReadonly2;
  877. } else if (key === "__v_isShallow") {
  878. return isShallow2;
  879. } else if (key === "__v_raw") {
  880. if (receiver === (isReadonly2 ? isShallow2 ? shallowReadonlyMap : readonlyMap : isShallow2 ? shallowReactiveMap : reactiveMap).get(target) || // receiver is not the reactive proxy, but has the same prototype
  881. // this means the receiver is a user proxy of the reactive proxy
  882. Object.getPrototypeOf(target) === Object.getPrototypeOf(receiver)) {
  883. return target;
  884. }
  885. return;
  886. }
  887. const targetIsArray = shared.isArray(target);
  888. if (!isReadonly2) {
  889. let fn;
  890. if (targetIsArray && (fn = arrayInstrumentations[key])) {
  891. return fn;
  892. }
  893. if (key === "hasOwnProperty") {
  894. return hasOwnProperty;
  895. }
  896. }
  897. const res = Reflect.get(
  898. target,
  899. key,
  900. // if this is a proxy wrapping a ref, return methods using the raw ref
  901. // as receiver so that we don't have to call `toRaw` on the ref in all
  902. // its class methods
  903. isRef(target) ? target : receiver
  904. );
  905. if (shared.isSymbol(key) ? builtInSymbols.has(key) : isNonTrackableKeys(key)) {
  906. return res;
  907. }
  908. if (!isReadonly2) {
  909. track(target, "get", key);
  910. }
  911. if (isShallow2) {
  912. return res;
  913. }
  914. if (isRef(res)) {
  915. return targetIsArray && shared.isIntegerKey(key) ? res : res.value;
  916. }
  917. if (shared.isObject(res)) {
  918. return isReadonly2 ? readonly(res) : reactive(res);
  919. }
  920. return res;
  921. }
  922. }
  923. class MutableReactiveHandler extends BaseReactiveHandler {
  924. constructor(isShallow2 = false) {
  925. super(false, isShallow2);
  926. }
  927. set(target, key, value, receiver) {
  928. let oldValue = target[key];
  929. if (!this._isShallow) {
  930. const isOldValueReadonly = isReadonly(oldValue);
  931. if (!isShallow(value) && !isReadonly(value)) {
  932. oldValue = toRaw(oldValue);
  933. value = toRaw(value);
  934. }
  935. if (!shared.isArray(target) && isRef(oldValue) && !isRef(value)) {
  936. if (isOldValueReadonly) {
  937. return false;
  938. } else {
  939. oldValue.value = value;
  940. return true;
  941. }
  942. }
  943. }
  944. const hadKey = shared.isArray(target) && shared.isIntegerKey(key) ? Number(key) < target.length : shared.hasOwn(target, key);
  945. const result = Reflect.set(
  946. target,
  947. key,
  948. value,
  949. isRef(target) ? target : receiver
  950. );
  951. if (target === toRaw(receiver)) {
  952. if (!hadKey) {
  953. trigger(target, "add", key, value);
  954. } else if (shared.hasChanged(value, oldValue)) {
  955. trigger(target, "set", key, value);
  956. }
  957. }
  958. return result;
  959. }
  960. deleteProperty(target, key) {
  961. const hadKey = shared.hasOwn(target, key);
  962. target[key];
  963. const result = Reflect.deleteProperty(target, key);
  964. if (result && hadKey) {
  965. trigger(target, "delete", key, void 0);
  966. }
  967. return result;
  968. }
  969. has(target, key) {
  970. const result = Reflect.has(target, key);
  971. if (!shared.isSymbol(key) || !builtInSymbols.has(key)) {
  972. track(target, "has", key);
  973. }
  974. return result;
  975. }
  976. ownKeys(target) {
  977. track(
  978. target,
  979. "iterate",
  980. shared.isArray(target) ? "length" : ITERATE_KEY
  981. );
  982. return Reflect.ownKeys(target);
  983. }
  984. }
  985. class ReadonlyReactiveHandler extends BaseReactiveHandler {
  986. constructor(isShallow2 = false) {
  987. super(true, isShallow2);
  988. }
  989. set(target, key) {
  990. return true;
  991. }
  992. deleteProperty(target, key) {
  993. return true;
  994. }
  995. }
  996. const mutableHandlers = /* @__PURE__ */ new MutableReactiveHandler();
  997. const readonlyHandlers = /* @__PURE__ */ new ReadonlyReactiveHandler();
  998. const shallowReactiveHandlers = /* @__PURE__ */ new MutableReactiveHandler(true);
  999. const shallowReadonlyHandlers = /* @__PURE__ */ new ReadonlyReactiveHandler(true);
  1000. const toShallow = (value) => value;
  1001. const getProto = (v) => Reflect.getPrototypeOf(v);
  1002. function createIterableMethod(method, isReadonly2, isShallow2) {
  1003. return function(...args) {
  1004. const target = this["__v_raw"];
  1005. const rawTarget = toRaw(target);
  1006. const targetIsMap = shared.isMap(rawTarget);
  1007. const isPair = method === "entries" || method === Symbol.iterator && targetIsMap;
  1008. const isKeyOnly = method === "keys" && targetIsMap;
  1009. const innerIterator = target[method](...args);
  1010. const wrap = isShallow2 ? toShallow : isReadonly2 ? toReadonly : toReactive;
  1011. !isReadonly2 && track(
  1012. rawTarget,
  1013. "iterate",
  1014. isKeyOnly ? MAP_KEY_ITERATE_KEY : ITERATE_KEY
  1015. );
  1016. return {
  1017. // iterator protocol
  1018. next() {
  1019. const { value, done } = innerIterator.next();
  1020. return done ? { value, done } : {
  1021. value: isPair ? [wrap(value[0]), wrap(value[1])] : wrap(value),
  1022. done
  1023. };
  1024. },
  1025. // iterable protocol
  1026. [Symbol.iterator]() {
  1027. return this;
  1028. }
  1029. };
  1030. };
  1031. }
  1032. function createReadonlyMethod(type) {
  1033. return function(...args) {
  1034. return type === "delete" ? false : type === "clear" ? void 0 : this;
  1035. };
  1036. }
  1037. function createInstrumentations(readonly, shallow) {
  1038. const instrumentations = {
  1039. get(key) {
  1040. const target = this["__v_raw"];
  1041. const rawTarget = toRaw(target);
  1042. const rawKey = toRaw(key);
  1043. if (!readonly) {
  1044. if (shared.hasChanged(key, rawKey)) {
  1045. track(rawTarget, "get", key);
  1046. }
  1047. track(rawTarget, "get", rawKey);
  1048. }
  1049. const { has } = getProto(rawTarget);
  1050. const wrap = shallow ? toShallow : readonly ? toReadonly : toReactive;
  1051. if (has.call(rawTarget, key)) {
  1052. return wrap(target.get(key));
  1053. } else if (has.call(rawTarget, rawKey)) {
  1054. return wrap(target.get(rawKey));
  1055. } else if (target !== rawTarget) {
  1056. target.get(key);
  1057. }
  1058. },
  1059. get size() {
  1060. const target = this["__v_raw"];
  1061. !readonly && track(toRaw(target), "iterate", ITERATE_KEY);
  1062. return Reflect.get(target, "size", target);
  1063. },
  1064. has(key) {
  1065. const target = this["__v_raw"];
  1066. const rawTarget = toRaw(target);
  1067. const rawKey = toRaw(key);
  1068. if (!readonly) {
  1069. if (shared.hasChanged(key, rawKey)) {
  1070. track(rawTarget, "has", key);
  1071. }
  1072. track(rawTarget, "has", rawKey);
  1073. }
  1074. return key === rawKey ? target.has(key) : target.has(key) || target.has(rawKey);
  1075. },
  1076. forEach(callback, thisArg) {
  1077. const observed = this;
  1078. const target = observed["__v_raw"];
  1079. const rawTarget = toRaw(target);
  1080. const wrap = shallow ? toShallow : readonly ? toReadonly : toReactive;
  1081. !readonly && track(rawTarget, "iterate", ITERATE_KEY);
  1082. return target.forEach((value, key) => {
  1083. return callback.call(thisArg, wrap(value), wrap(key), observed);
  1084. });
  1085. }
  1086. };
  1087. shared.extend(
  1088. instrumentations,
  1089. readonly ? {
  1090. add: createReadonlyMethod("add"),
  1091. set: createReadonlyMethod("set"),
  1092. delete: createReadonlyMethod("delete"),
  1093. clear: createReadonlyMethod("clear")
  1094. } : {
  1095. add(value) {
  1096. if (!shallow && !isShallow(value) && !isReadonly(value)) {
  1097. value = toRaw(value);
  1098. }
  1099. const target = toRaw(this);
  1100. const proto = getProto(target);
  1101. const hadKey = proto.has.call(target, value);
  1102. if (!hadKey) {
  1103. target.add(value);
  1104. trigger(target, "add", value, value);
  1105. }
  1106. return this;
  1107. },
  1108. set(key, value) {
  1109. if (!shallow && !isShallow(value) && !isReadonly(value)) {
  1110. value = toRaw(value);
  1111. }
  1112. const target = toRaw(this);
  1113. const { has, get } = getProto(target);
  1114. let hadKey = has.call(target, key);
  1115. if (!hadKey) {
  1116. key = toRaw(key);
  1117. hadKey = has.call(target, key);
  1118. }
  1119. const oldValue = get.call(target, key);
  1120. target.set(key, value);
  1121. if (!hadKey) {
  1122. trigger(target, "add", key, value);
  1123. } else if (shared.hasChanged(value, oldValue)) {
  1124. trigger(target, "set", key, value);
  1125. }
  1126. return this;
  1127. },
  1128. delete(key) {
  1129. const target = toRaw(this);
  1130. const { has, get } = getProto(target);
  1131. let hadKey = has.call(target, key);
  1132. if (!hadKey) {
  1133. key = toRaw(key);
  1134. hadKey = has.call(target, key);
  1135. }
  1136. get ? get.call(target, key) : void 0;
  1137. const result = target.delete(key);
  1138. if (hadKey) {
  1139. trigger(target, "delete", key, void 0);
  1140. }
  1141. return result;
  1142. },
  1143. clear() {
  1144. const target = toRaw(this);
  1145. const hadItems = target.size !== 0;
  1146. const result = target.clear();
  1147. if (hadItems) {
  1148. trigger(
  1149. target,
  1150. "clear",
  1151. void 0,
  1152. void 0);
  1153. }
  1154. return result;
  1155. }
  1156. }
  1157. );
  1158. const iteratorMethods = [
  1159. "keys",
  1160. "values",
  1161. "entries",
  1162. Symbol.iterator
  1163. ];
  1164. iteratorMethods.forEach((method) => {
  1165. instrumentations[method] = createIterableMethod(method, readonly, shallow);
  1166. });
  1167. return instrumentations;
  1168. }
  1169. function createInstrumentationGetter(isReadonly2, shallow) {
  1170. const instrumentations = createInstrumentations(isReadonly2, shallow);
  1171. return (target, key, receiver) => {
  1172. if (key === "__v_isReactive") {
  1173. return !isReadonly2;
  1174. } else if (key === "__v_isReadonly") {
  1175. return isReadonly2;
  1176. } else if (key === "__v_raw") {
  1177. return target;
  1178. }
  1179. return Reflect.get(
  1180. shared.hasOwn(instrumentations, key) && key in target ? instrumentations : target,
  1181. key,
  1182. receiver
  1183. );
  1184. };
  1185. }
  1186. const mutableCollectionHandlers = {
  1187. get: /* @__PURE__ */ createInstrumentationGetter(false, false)
  1188. };
  1189. const shallowCollectionHandlers = {
  1190. get: /* @__PURE__ */ createInstrumentationGetter(false, true)
  1191. };
  1192. const readonlyCollectionHandlers = {
  1193. get: /* @__PURE__ */ createInstrumentationGetter(true, false)
  1194. };
  1195. const shallowReadonlyCollectionHandlers = {
  1196. get: /* @__PURE__ */ createInstrumentationGetter(true, true)
  1197. };
  1198. const reactiveMap = /* @__PURE__ */ new WeakMap();
  1199. const shallowReactiveMap = /* @__PURE__ */ new WeakMap();
  1200. const readonlyMap = /* @__PURE__ */ new WeakMap();
  1201. const shallowReadonlyMap = /* @__PURE__ */ new WeakMap();
  1202. function targetTypeMap(rawType) {
  1203. switch (rawType) {
  1204. case "Object":
  1205. case "Array":
  1206. return 1 /* COMMON */;
  1207. case "Map":
  1208. case "Set":
  1209. case "WeakMap":
  1210. case "WeakSet":
  1211. return 2 /* COLLECTION */;
  1212. default:
  1213. return 0 /* INVALID */;
  1214. }
  1215. }
  1216. function getTargetType(value) {
  1217. return value["__v_skip"] || !Object.isExtensible(value) ? 0 /* INVALID */ : targetTypeMap(shared.toRawType(value));
  1218. }
  1219. function reactive(target) {
  1220. if (isReadonly(target)) {
  1221. return target;
  1222. }
  1223. return createReactiveObject(
  1224. target,
  1225. false,
  1226. mutableHandlers,
  1227. mutableCollectionHandlers,
  1228. reactiveMap
  1229. );
  1230. }
  1231. function shallowReactive(target) {
  1232. return createReactiveObject(
  1233. target,
  1234. false,
  1235. shallowReactiveHandlers,
  1236. shallowCollectionHandlers,
  1237. shallowReactiveMap
  1238. );
  1239. }
  1240. function readonly(target) {
  1241. return createReactiveObject(
  1242. target,
  1243. true,
  1244. readonlyHandlers,
  1245. readonlyCollectionHandlers,
  1246. readonlyMap
  1247. );
  1248. }
  1249. function shallowReadonly(target) {
  1250. return createReactiveObject(
  1251. target,
  1252. true,
  1253. shallowReadonlyHandlers,
  1254. shallowReadonlyCollectionHandlers,
  1255. shallowReadonlyMap
  1256. );
  1257. }
  1258. function createReactiveObject(target, isReadonly2, baseHandlers, collectionHandlers, proxyMap) {
  1259. if (!shared.isObject(target)) {
  1260. return target;
  1261. }
  1262. if (target["__v_raw"] && !(isReadonly2 && target["__v_isReactive"])) {
  1263. return target;
  1264. }
  1265. const targetType = getTargetType(target);
  1266. if (targetType === 0 /* INVALID */) {
  1267. return target;
  1268. }
  1269. const existingProxy = proxyMap.get(target);
  1270. if (existingProxy) {
  1271. return existingProxy;
  1272. }
  1273. const proxy = new Proxy(
  1274. target,
  1275. targetType === 2 /* COLLECTION */ ? collectionHandlers : baseHandlers
  1276. );
  1277. proxyMap.set(target, proxy);
  1278. return proxy;
  1279. }
  1280. function isReactive(value) {
  1281. if (isReadonly(value)) {
  1282. return isReactive(value["__v_raw"]);
  1283. }
  1284. return !!(value && value["__v_isReactive"]);
  1285. }
  1286. function isReadonly(value) {
  1287. return !!(value && value["__v_isReadonly"]);
  1288. }
  1289. function isShallow(value) {
  1290. return !!(value && value["__v_isShallow"]);
  1291. }
  1292. function isProxy(value) {
  1293. return value ? !!value["__v_raw"] : false;
  1294. }
  1295. function toRaw(observed) {
  1296. const raw = observed && observed["__v_raw"];
  1297. return raw ? toRaw(raw) : observed;
  1298. }
  1299. function markRaw(value) {
  1300. if (!shared.hasOwn(value, "__v_skip") && Object.isExtensible(value)) {
  1301. shared.def(value, "__v_skip", true);
  1302. }
  1303. return value;
  1304. }
  1305. const toReactive = (value) => shared.isObject(value) ? reactive(value) : value;
  1306. const toReadonly = (value) => shared.isObject(value) ? readonly(value) : value;
  1307. function isRef(r) {
  1308. return r ? r["__v_isRef"] === true : false;
  1309. }
  1310. function ref(value) {
  1311. return createRef(value, false);
  1312. }
  1313. function shallowRef(value) {
  1314. return createRef(value, true);
  1315. }
  1316. function createRef(rawValue, shallow) {
  1317. if (isRef(rawValue)) {
  1318. return rawValue;
  1319. }
  1320. return new RefImpl(rawValue, shallow);
  1321. }
  1322. class RefImpl {
  1323. constructor(value, isShallow2) {
  1324. this.dep = new Dep();
  1325. this["__v_isRef"] = true;
  1326. this["__v_isShallow"] = false;
  1327. this._rawValue = isShallow2 ? value : toRaw(value);
  1328. this._value = isShallow2 ? value : toReactive(value);
  1329. this["__v_isShallow"] = isShallow2;
  1330. }
  1331. get value() {
  1332. {
  1333. this.dep.track();
  1334. }
  1335. return this._value;
  1336. }
  1337. set value(newValue) {
  1338. const oldValue = this._rawValue;
  1339. const useDirectValue = this["__v_isShallow"] || isShallow(newValue) || isReadonly(newValue);
  1340. newValue = useDirectValue ? newValue : toRaw(newValue);
  1341. if (shared.hasChanged(newValue, oldValue)) {
  1342. this._rawValue = newValue;
  1343. this._value = useDirectValue ? newValue : toReactive(newValue);
  1344. {
  1345. this.dep.trigger();
  1346. }
  1347. }
  1348. }
  1349. }
  1350. function triggerRef(ref2) {
  1351. if (ref2.dep) {
  1352. {
  1353. ref2.dep.trigger();
  1354. }
  1355. }
  1356. }
  1357. function unref(ref2) {
  1358. return isRef(ref2) ? ref2.value : ref2;
  1359. }
  1360. function toValue(source) {
  1361. return shared.isFunction(source) ? source() : unref(source);
  1362. }
  1363. const shallowUnwrapHandlers = {
  1364. get: (target, key, receiver) => key === "__v_raw" ? target : unref(Reflect.get(target, key, receiver)),
  1365. set: (target, key, value, receiver) => {
  1366. const oldValue = target[key];
  1367. if (isRef(oldValue) && !isRef(value)) {
  1368. oldValue.value = value;
  1369. return true;
  1370. } else {
  1371. return Reflect.set(target, key, value, receiver);
  1372. }
  1373. }
  1374. };
  1375. function proxyRefs(objectWithRefs) {
  1376. return isReactive(objectWithRefs) ? objectWithRefs : new Proxy(objectWithRefs, shallowUnwrapHandlers);
  1377. }
  1378. class CustomRefImpl {
  1379. constructor(factory) {
  1380. this["__v_isRef"] = true;
  1381. this._value = void 0;
  1382. const dep = this.dep = new Dep();
  1383. const { get, set } = factory(dep.track.bind(dep), dep.trigger.bind(dep));
  1384. this._get = get;
  1385. this._set = set;
  1386. }
  1387. get value() {
  1388. return this._value = this._get();
  1389. }
  1390. set value(newVal) {
  1391. this._set(newVal);
  1392. }
  1393. }
  1394. function customRef(factory) {
  1395. return new CustomRefImpl(factory);
  1396. }
  1397. function toRefs(object) {
  1398. const ret = shared.isArray(object) ? new Array(object.length) : {};
  1399. for (const key in object) {
  1400. ret[key] = propertyToRef(object, key);
  1401. }
  1402. return ret;
  1403. }
  1404. class ObjectRefImpl {
  1405. constructor(_object, _key, _defaultValue) {
  1406. this._object = _object;
  1407. this._key = _key;
  1408. this._defaultValue = _defaultValue;
  1409. this["__v_isRef"] = true;
  1410. this._value = void 0;
  1411. }
  1412. get value() {
  1413. const val = this._object[this._key];
  1414. return this._value = val === void 0 ? this._defaultValue : val;
  1415. }
  1416. set value(newVal) {
  1417. this._object[this._key] = newVal;
  1418. }
  1419. get dep() {
  1420. return getDepFromReactive(toRaw(this._object), this._key);
  1421. }
  1422. }
  1423. class GetterRefImpl {
  1424. constructor(_getter) {
  1425. this._getter = _getter;
  1426. this["__v_isRef"] = true;
  1427. this["__v_isReadonly"] = true;
  1428. this._value = void 0;
  1429. }
  1430. get value() {
  1431. return this._value = this._getter();
  1432. }
  1433. }
  1434. function toRef(source, key, defaultValue) {
  1435. if (isRef(source)) {
  1436. return source;
  1437. } else if (shared.isFunction(source)) {
  1438. return new GetterRefImpl(source);
  1439. } else if (shared.isObject(source) && arguments.length > 1) {
  1440. return propertyToRef(source, key, defaultValue);
  1441. } else {
  1442. return ref(source);
  1443. }
  1444. }
  1445. function propertyToRef(source, key, defaultValue) {
  1446. const val = source[key];
  1447. return isRef(val) ? val : new ObjectRefImpl(source, key, defaultValue);
  1448. }
  1449. class ComputedRefImpl {
  1450. constructor(fn, setter, isSSR) {
  1451. this.fn = fn;
  1452. this.setter = setter;
  1453. /**
  1454. * @internal
  1455. */
  1456. this._value = void 0;
  1457. /**
  1458. * @internal
  1459. */
  1460. this.dep = new Dep(this);
  1461. /**
  1462. * @internal
  1463. */
  1464. this.__v_isRef = true;
  1465. // TODO isolatedDeclarations "__v_isReadonly"
  1466. // A computed is also a subscriber that tracks other deps
  1467. /**
  1468. * @internal
  1469. */
  1470. this.deps = void 0;
  1471. /**
  1472. * @internal
  1473. */
  1474. this.depsTail = void 0;
  1475. /**
  1476. * @internal
  1477. */
  1478. this.flags = 16;
  1479. /**
  1480. * @internal
  1481. */
  1482. this.globalVersion = globalVersion - 1;
  1483. /**
  1484. * @internal
  1485. */
  1486. this.next = void 0;
  1487. // for backwards compat
  1488. this.effect = this;
  1489. this["__v_isReadonly"] = !setter;
  1490. this.isSSR = isSSR;
  1491. }
  1492. /**
  1493. * @internal
  1494. */
  1495. notify() {
  1496. this.flags |= 16;
  1497. if (!(this.flags & 8) && // avoid infinite self recursion
  1498. activeSub !== this) {
  1499. batch(this, true);
  1500. return true;
  1501. }
  1502. }
  1503. get value() {
  1504. const link = this.dep.track();
  1505. refreshComputed(this);
  1506. if (link) {
  1507. link.version = this.dep.version;
  1508. }
  1509. return this._value;
  1510. }
  1511. set value(newValue) {
  1512. if (this.setter) {
  1513. this.setter(newValue);
  1514. }
  1515. }
  1516. }
  1517. function computed(getterOrOptions, debugOptions, isSSR = false) {
  1518. let getter;
  1519. let setter;
  1520. if (shared.isFunction(getterOrOptions)) {
  1521. getter = getterOrOptions;
  1522. } else {
  1523. getter = getterOrOptions.get;
  1524. setter = getterOrOptions.set;
  1525. }
  1526. const cRef = new ComputedRefImpl(getter, setter, isSSR);
  1527. return cRef;
  1528. }
  1529. const TrackOpTypes = {
  1530. "GET": "get",
  1531. "HAS": "has",
  1532. "ITERATE": "iterate"
  1533. };
  1534. const TriggerOpTypes = {
  1535. "SET": "set",
  1536. "ADD": "add",
  1537. "DELETE": "delete",
  1538. "CLEAR": "clear"
  1539. };
  1540. const ReactiveFlags = {
  1541. "SKIP": "__v_skip",
  1542. "IS_REACTIVE": "__v_isReactive",
  1543. "IS_READONLY": "__v_isReadonly",
  1544. "IS_SHALLOW": "__v_isShallow",
  1545. "RAW": "__v_raw",
  1546. "IS_REF": "__v_isRef"
  1547. };
  1548. const WatchErrorCodes = {
  1549. "WATCH_GETTER": 2,
  1550. "2": "WATCH_GETTER",
  1551. "WATCH_CALLBACK": 3,
  1552. "3": "WATCH_CALLBACK",
  1553. "WATCH_CLEANUP": 4,
  1554. "4": "WATCH_CLEANUP"
  1555. };
  1556. const INITIAL_WATCHER_VALUE = {};
  1557. const cleanupMap = /* @__PURE__ */ new WeakMap();
  1558. let activeWatcher = void 0;
  1559. function getCurrentWatcher() {
  1560. return activeWatcher;
  1561. }
  1562. function onWatcherCleanup(cleanupFn, failSilently = false, owner = activeWatcher) {
  1563. if (owner) {
  1564. let cleanups = cleanupMap.get(owner);
  1565. if (!cleanups) cleanupMap.set(owner, cleanups = []);
  1566. cleanups.push(cleanupFn);
  1567. }
  1568. }
  1569. function watch(source, cb, options = shared.EMPTY_OBJ) {
  1570. const { immediate, deep, once, scheduler, augmentJob, call } = options;
  1571. const reactiveGetter = (source2) => {
  1572. if (deep) return source2;
  1573. if (isShallow(source2) || deep === false || deep === 0)
  1574. return traverse(source2, 1);
  1575. return traverse(source2);
  1576. };
  1577. let effect;
  1578. let getter;
  1579. let cleanup;
  1580. let boundCleanup;
  1581. let forceTrigger = false;
  1582. let isMultiSource = false;
  1583. if (isRef(source)) {
  1584. getter = () => source.value;
  1585. forceTrigger = isShallow(source);
  1586. } else if (isReactive(source)) {
  1587. getter = () => reactiveGetter(source);
  1588. forceTrigger = true;
  1589. } else if (shared.isArray(source)) {
  1590. isMultiSource = true;
  1591. forceTrigger = source.some((s) => isReactive(s) || isShallow(s));
  1592. getter = () => source.map((s) => {
  1593. if (isRef(s)) {
  1594. return s.value;
  1595. } else if (isReactive(s)) {
  1596. return reactiveGetter(s);
  1597. } else if (shared.isFunction(s)) {
  1598. return call ? call(s, 2) : s();
  1599. } else ;
  1600. });
  1601. } else if (shared.isFunction(source)) {
  1602. if (cb) {
  1603. getter = call ? () => call(source, 2) : source;
  1604. } else {
  1605. getter = () => {
  1606. if (cleanup) {
  1607. pauseTracking();
  1608. try {
  1609. cleanup();
  1610. } finally {
  1611. resetTracking();
  1612. }
  1613. }
  1614. const currentEffect = activeWatcher;
  1615. activeWatcher = effect;
  1616. try {
  1617. return call ? call(source, 3, [boundCleanup]) : source(boundCleanup);
  1618. } finally {
  1619. activeWatcher = currentEffect;
  1620. }
  1621. };
  1622. }
  1623. } else {
  1624. getter = shared.NOOP;
  1625. }
  1626. if (cb && deep) {
  1627. const baseGetter = getter;
  1628. const depth = deep === true ? Infinity : deep;
  1629. getter = () => traverse(baseGetter(), depth);
  1630. }
  1631. const scope = getCurrentScope();
  1632. const watchHandle = () => {
  1633. effect.stop();
  1634. if (scope && scope.active) {
  1635. shared.remove(scope.effects, effect);
  1636. }
  1637. };
  1638. if (once && cb) {
  1639. const _cb = cb;
  1640. cb = (...args) => {
  1641. _cb(...args);
  1642. watchHandle();
  1643. };
  1644. }
  1645. let oldValue = isMultiSource ? new Array(source.length).fill(INITIAL_WATCHER_VALUE) : INITIAL_WATCHER_VALUE;
  1646. const job = (immediateFirstRun) => {
  1647. if (!(effect.flags & 1) || !effect.dirty && !immediateFirstRun) {
  1648. return;
  1649. }
  1650. if (cb) {
  1651. const newValue = effect.run();
  1652. if (deep || forceTrigger || (isMultiSource ? newValue.some((v, i) => shared.hasChanged(v, oldValue[i])) : shared.hasChanged(newValue, oldValue))) {
  1653. if (cleanup) {
  1654. cleanup();
  1655. }
  1656. const currentWatcher = activeWatcher;
  1657. activeWatcher = effect;
  1658. try {
  1659. const args = [
  1660. newValue,
  1661. // pass undefined as the old value when it's changed for the first time
  1662. oldValue === INITIAL_WATCHER_VALUE ? void 0 : isMultiSource && oldValue[0] === INITIAL_WATCHER_VALUE ? [] : oldValue,
  1663. boundCleanup
  1664. ];
  1665. oldValue = newValue;
  1666. call ? call(cb, 3, args) : (
  1667. // @ts-expect-error
  1668. cb(...args)
  1669. );
  1670. } finally {
  1671. activeWatcher = currentWatcher;
  1672. }
  1673. }
  1674. } else {
  1675. effect.run();
  1676. }
  1677. };
  1678. if (augmentJob) {
  1679. augmentJob(job);
  1680. }
  1681. effect = new ReactiveEffect(getter);
  1682. effect.scheduler = scheduler ? () => scheduler(job, false) : job;
  1683. boundCleanup = (fn) => onWatcherCleanup(fn, false, effect);
  1684. cleanup = effect.onStop = () => {
  1685. const cleanups = cleanupMap.get(effect);
  1686. if (cleanups) {
  1687. if (call) {
  1688. call(cleanups, 4);
  1689. } else {
  1690. for (const cleanup2 of cleanups) cleanup2();
  1691. }
  1692. cleanupMap.delete(effect);
  1693. }
  1694. };
  1695. if (cb) {
  1696. if (immediate) {
  1697. job(true);
  1698. } else {
  1699. oldValue = effect.run();
  1700. }
  1701. } else if (scheduler) {
  1702. scheduler(job.bind(null, true), true);
  1703. } else {
  1704. effect.run();
  1705. }
  1706. watchHandle.pause = effect.pause.bind(effect);
  1707. watchHandle.resume = effect.resume.bind(effect);
  1708. watchHandle.stop = watchHandle;
  1709. return watchHandle;
  1710. }
  1711. function traverse(value, depth = Infinity, seen) {
  1712. if (depth <= 0 || !shared.isObject(value) || value["__v_skip"]) {
  1713. return value;
  1714. }
  1715. seen = seen || /* @__PURE__ */ new Set();
  1716. if (seen.has(value)) {
  1717. return value;
  1718. }
  1719. seen.add(value);
  1720. depth--;
  1721. if (isRef(value)) {
  1722. traverse(value.value, depth, seen);
  1723. } else if (shared.isArray(value)) {
  1724. for (let i = 0; i < value.length; i++) {
  1725. traverse(value[i], depth, seen);
  1726. }
  1727. } else if (shared.isSet(value) || shared.isMap(value)) {
  1728. value.forEach((v) => {
  1729. traverse(v, depth, seen);
  1730. });
  1731. } else if (shared.isPlainObject(value)) {
  1732. for (const key in value) {
  1733. traverse(value[key], depth, seen);
  1734. }
  1735. for (const key of Object.getOwnPropertySymbols(value)) {
  1736. if (Object.prototype.propertyIsEnumerable.call(value, key)) {
  1737. traverse(value[key], depth, seen);
  1738. }
  1739. }
  1740. }
  1741. return value;
  1742. }
  1743. exports.ARRAY_ITERATE_KEY = ARRAY_ITERATE_KEY;
  1744. exports.EffectFlags = EffectFlags;
  1745. exports.EffectScope = EffectScope;
  1746. exports.ITERATE_KEY = ITERATE_KEY;
  1747. exports.MAP_KEY_ITERATE_KEY = MAP_KEY_ITERATE_KEY;
  1748. exports.ReactiveEffect = ReactiveEffect;
  1749. exports.ReactiveFlags = ReactiveFlags;
  1750. exports.TrackOpTypes = TrackOpTypes;
  1751. exports.TriggerOpTypes = TriggerOpTypes;
  1752. exports.WatchErrorCodes = WatchErrorCodes;
  1753. exports.computed = computed;
  1754. exports.customRef = customRef;
  1755. exports.effect = effect;
  1756. exports.effectScope = effectScope;
  1757. exports.enableTracking = enableTracking;
  1758. exports.getCurrentScope = getCurrentScope;
  1759. exports.getCurrentWatcher = getCurrentWatcher;
  1760. exports.isProxy = isProxy;
  1761. exports.isReactive = isReactive;
  1762. exports.isReadonly = isReadonly;
  1763. exports.isRef = isRef;
  1764. exports.isShallow = isShallow;
  1765. exports.markRaw = markRaw;
  1766. exports.onEffectCleanup = onEffectCleanup;
  1767. exports.onScopeDispose = onScopeDispose;
  1768. exports.onWatcherCleanup = onWatcherCleanup;
  1769. exports.pauseTracking = pauseTracking;
  1770. exports.proxyRefs = proxyRefs;
  1771. exports.reactive = reactive;
  1772. exports.reactiveReadArray = reactiveReadArray;
  1773. exports.readonly = readonly;
  1774. exports.ref = ref;
  1775. exports.resetTracking = resetTracking;
  1776. exports.shallowReactive = shallowReactive;
  1777. exports.shallowReadArray = shallowReadArray;
  1778. exports.shallowReadonly = shallowReadonly;
  1779. exports.shallowRef = shallowRef;
  1780. exports.stop = stop;
  1781. exports.toRaw = toRaw;
  1782. exports.toReactive = toReactive;
  1783. exports.toReadonly = toReadonly;
  1784. exports.toRef = toRef;
  1785. exports.toRefs = toRefs;
  1786. exports.toValue = toValue;
  1787. exports.track = track;
  1788. exports.traverse = traverse;
  1789. exports.trigger = trigger;
  1790. exports.triggerRef = triggerRef;
  1791. exports.unref = unref;
  1792. exports.watch = watch;