reactivity.cjs.prod.js 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807
  1. /**
  2. * @vue/reactivity v3.5.22
  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 optimization 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.done) {
  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. const value = targetIsArray && shared.isIntegerKey(key) ? res : res.value;
  916. return isReadonly2 && shared.isObject(value) ? readonly(value) : value;
  917. }
  918. if (shared.isObject(res)) {
  919. return isReadonly2 ? readonly(res) : reactive(res);
  920. }
  921. return res;
  922. }
  923. }
  924. class MutableReactiveHandler extends BaseReactiveHandler {
  925. constructor(isShallow2 = false) {
  926. super(false, isShallow2);
  927. }
  928. set(target, key, value, receiver) {
  929. let oldValue = target[key];
  930. if (!this._isShallow) {
  931. const isOldValueReadonly = isReadonly(oldValue);
  932. if (!isShallow(value) && !isReadonly(value)) {
  933. oldValue = toRaw(oldValue);
  934. value = toRaw(value);
  935. }
  936. if (!shared.isArray(target) && isRef(oldValue) && !isRef(value)) {
  937. if (isOldValueReadonly) {
  938. return true;
  939. } else {
  940. oldValue.value = value;
  941. return true;
  942. }
  943. }
  944. }
  945. const hadKey = shared.isArray(target) && shared.isIntegerKey(key) ? Number(key) < target.length : shared.hasOwn(target, key);
  946. const result = Reflect.set(
  947. target,
  948. key,
  949. value,
  950. isRef(target) ? target : receiver
  951. );
  952. if (target === toRaw(receiver)) {
  953. if (!hadKey) {
  954. trigger(target, "add", key, value);
  955. } else if (shared.hasChanged(value, oldValue)) {
  956. trigger(target, "set", key, value);
  957. }
  958. }
  959. return result;
  960. }
  961. deleteProperty(target, key) {
  962. const hadKey = shared.hasOwn(target, key);
  963. target[key];
  964. const result = Reflect.deleteProperty(target, key);
  965. if (result && hadKey) {
  966. trigger(target, "delete", key, void 0);
  967. }
  968. return result;
  969. }
  970. has(target, key) {
  971. const result = Reflect.has(target, key);
  972. if (!shared.isSymbol(key) || !builtInSymbols.has(key)) {
  973. track(target, "has", key);
  974. }
  975. return result;
  976. }
  977. ownKeys(target) {
  978. track(
  979. target,
  980. "iterate",
  981. shared.isArray(target) ? "length" : ITERATE_KEY
  982. );
  983. return Reflect.ownKeys(target);
  984. }
  985. }
  986. class ReadonlyReactiveHandler extends BaseReactiveHandler {
  987. constructor(isShallow2 = false) {
  988. super(true, isShallow2);
  989. }
  990. set(target, key) {
  991. return true;
  992. }
  993. deleteProperty(target, key) {
  994. return true;
  995. }
  996. }
  997. const mutableHandlers = /* @__PURE__ */ new MutableReactiveHandler();
  998. const readonlyHandlers = /* @__PURE__ */ new ReadonlyReactiveHandler();
  999. const shallowReactiveHandlers = /* @__PURE__ */ new MutableReactiveHandler(true);
  1000. const shallowReadonlyHandlers = /* @__PURE__ */ new ReadonlyReactiveHandler(true);
  1001. const toShallow = (value) => value;
  1002. const getProto = (v) => Reflect.getPrototypeOf(v);
  1003. function createIterableMethod(method, isReadonly2, isShallow2) {
  1004. return function(...args) {
  1005. const target = this["__v_raw"];
  1006. const rawTarget = toRaw(target);
  1007. const targetIsMap = shared.isMap(rawTarget);
  1008. const isPair = method === "entries" || method === Symbol.iterator && targetIsMap;
  1009. const isKeyOnly = method === "keys" && targetIsMap;
  1010. const innerIterator = target[method](...args);
  1011. const wrap = isShallow2 ? toShallow : isReadonly2 ? toReadonly : toReactive;
  1012. !isReadonly2 && track(
  1013. rawTarget,
  1014. "iterate",
  1015. isKeyOnly ? MAP_KEY_ITERATE_KEY : ITERATE_KEY
  1016. );
  1017. return {
  1018. // iterator protocol
  1019. next() {
  1020. const { value, done } = innerIterator.next();
  1021. return done ? { value, done } : {
  1022. value: isPair ? [wrap(value[0]), wrap(value[1])] : wrap(value),
  1023. done
  1024. };
  1025. },
  1026. // iterable protocol
  1027. [Symbol.iterator]() {
  1028. return this;
  1029. }
  1030. };
  1031. };
  1032. }
  1033. function createReadonlyMethod(type) {
  1034. return function(...args) {
  1035. return type === "delete" ? false : type === "clear" ? void 0 : this;
  1036. };
  1037. }
  1038. function createInstrumentations(readonly, shallow) {
  1039. const instrumentations = {
  1040. get(key) {
  1041. const target = this["__v_raw"];
  1042. const rawTarget = toRaw(target);
  1043. const rawKey = toRaw(key);
  1044. if (!readonly) {
  1045. if (shared.hasChanged(key, rawKey)) {
  1046. track(rawTarget, "get", key);
  1047. }
  1048. track(rawTarget, "get", rawKey);
  1049. }
  1050. const { has } = getProto(rawTarget);
  1051. const wrap = shallow ? toShallow : readonly ? toReadonly : toReactive;
  1052. if (has.call(rawTarget, key)) {
  1053. return wrap(target.get(key));
  1054. } else if (has.call(rawTarget, rawKey)) {
  1055. return wrap(target.get(rawKey));
  1056. } else if (target !== rawTarget) {
  1057. target.get(key);
  1058. }
  1059. },
  1060. get size() {
  1061. const target = this["__v_raw"];
  1062. !readonly && track(toRaw(target), "iterate", ITERATE_KEY);
  1063. return target.size;
  1064. },
  1065. has(key) {
  1066. const target = this["__v_raw"];
  1067. const rawTarget = toRaw(target);
  1068. const rawKey = toRaw(key);
  1069. if (!readonly) {
  1070. if (shared.hasChanged(key, rawKey)) {
  1071. track(rawTarget, "has", key);
  1072. }
  1073. track(rawTarget, "has", rawKey);
  1074. }
  1075. return key === rawKey ? target.has(key) : target.has(key) || target.has(rawKey);
  1076. },
  1077. forEach(callback, thisArg) {
  1078. const observed = this;
  1079. const target = observed["__v_raw"];
  1080. const rawTarget = toRaw(target);
  1081. const wrap = shallow ? toShallow : readonly ? toReadonly : toReactive;
  1082. !readonly && track(rawTarget, "iterate", ITERATE_KEY);
  1083. return target.forEach((value, key) => {
  1084. return callback.call(thisArg, wrap(value), wrap(key), observed);
  1085. });
  1086. }
  1087. };
  1088. shared.extend(
  1089. instrumentations,
  1090. readonly ? {
  1091. add: createReadonlyMethod("add"),
  1092. set: createReadonlyMethod("set"),
  1093. delete: createReadonlyMethod("delete"),
  1094. clear: createReadonlyMethod("clear")
  1095. } : {
  1096. add(value) {
  1097. if (!shallow && !isShallow(value) && !isReadonly(value)) {
  1098. value = toRaw(value);
  1099. }
  1100. const target = toRaw(this);
  1101. const proto = getProto(target);
  1102. const hadKey = proto.has.call(target, value);
  1103. if (!hadKey) {
  1104. target.add(value);
  1105. trigger(target, "add", value, value);
  1106. }
  1107. return this;
  1108. },
  1109. set(key, value) {
  1110. if (!shallow && !isShallow(value) && !isReadonly(value)) {
  1111. value = toRaw(value);
  1112. }
  1113. const target = toRaw(this);
  1114. const { has, get } = getProto(target);
  1115. let hadKey = has.call(target, key);
  1116. if (!hadKey) {
  1117. key = toRaw(key);
  1118. hadKey = has.call(target, key);
  1119. }
  1120. const oldValue = get.call(target, key);
  1121. target.set(key, value);
  1122. if (!hadKey) {
  1123. trigger(target, "add", key, value);
  1124. } else if (shared.hasChanged(value, oldValue)) {
  1125. trigger(target, "set", key, value);
  1126. }
  1127. return this;
  1128. },
  1129. delete(key) {
  1130. const target = toRaw(this);
  1131. const { has, get } = getProto(target);
  1132. let hadKey = has.call(target, key);
  1133. if (!hadKey) {
  1134. key = toRaw(key);
  1135. hadKey = has.call(target, key);
  1136. }
  1137. get ? get.call(target, key) : void 0;
  1138. const result = target.delete(key);
  1139. if (hadKey) {
  1140. trigger(target, "delete", key, void 0);
  1141. }
  1142. return result;
  1143. },
  1144. clear() {
  1145. const target = toRaw(this);
  1146. const hadItems = target.size !== 0;
  1147. const result = target.clear();
  1148. if (hadItems) {
  1149. trigger(
  1150. target,
  1151. "clear",
  1152. void 0,
  1153. void 0);
  1154. }
  1155. return result;
  1156. }
  1157. }
  1158. );
  1159. const iteratorMethods = [
  1160. "keys",
  1161. "values",
  1162. "entries",
  1163. Symbol.iterator
  1164. ];
  1165. iteratorMethods.forEach((method) => {
  1166. instrumentations[method] = createIterableMethod(method, readonly, shallow);
  1167. });
  1168. return instrumentations;
  1169. }
  1170. function createInstrumentationGetter(isReadonly2, shallow) {
  1171. const instrumentations = createInstrumentations(isReadonly2, shallow);
  1172. return (target, key, receiver) => {
  1173. if (key === "__v_isReactive") {
  1174. return !isReadonly2;
  1175. } else if (key === "__v_isReadonly") {
  1176. return isReadonly2;
  1177. } else if (key === "__v_raw") {
  1178. return target;
  1179. }
  1180. return Reflect.get(
  1181. shared.hasOwn(instrumentations, key) && key in target ? instrumentations : target,
  1182. key,
  1183. receiver
  1184. );
  1185. };
  1186. }
  1187. const mutableCollectionHandlers = {
  1188. get: /* @__PURE__ */ createInstrumentationGetter(false, false)
  1189. };
  1190. const shallowCollectionHandlers = {
  1191. get: /* @__PURE__ */ createInstrumentationGetter(false, true)
  1192. };
  1193. const readonlyCollectionHandlers = {
  1194. get: /* @__PURE__ */ createInstrumentationGetter(true, false)
  1195. };
  1196. const shallowReadonlyCollectionHandlers = {
  1197. get: /* @__PURE__ */ createInstrumentationGetter(true, true)
  1198. };
  1199. const reactiveMap = /* @__PURE__ */ new WeakMap();
  1200. const shallowReactiveMap = /* @__PURE__ */ new WeakMap();
  1201. const readonlyMap = /* @__PURE__ */ new WeakMap();
  1202. const shallowReadonlyMap = /* @__PURE__ */ new WeakMap();
  1203. function targetTypeMap(rawType) {
  1204. switch (rawType) {
  1205. case "Object":
  1206. case "Array":
  1207. return 1 /* COMMON */;
  1208. case "Map":
  1209. case "Set":
  1210. case "WeakMap":
  1211. case "WeakSet":
  1212. return 2 /* COLLECTION */;
  1213. default:
  1214. return 0 /* INVALID */;
  1215. }
  1216. }
  1217. function getTargetType(value) {
  1218. return value["__v_skip"] || !Object.isExtensible(value) ? 0 /* INVALID */ : targetTypeMap(shared.toRawType(value));
  1219. }
  1220. function reactive(target) {
  1221. if (isReadonly(target)) {
  1222. return target;
  1223. }
  1224. return createReactiveObject(
  1225. target,
  1226. false,
  1227. mutableHandlers,
  1228. mutableCollectionHandlers,
  1229. reactiveMap
  1230. );
  1231. }
  1232. function shallowReactive(target) {
  1233. return createReactiveObject(
  1234. target,
  1235. false,
  1236. shallowReactiveHandlers,
  1237. shallowCollectionHandlers,
  1238. shallowReactiveMap
  1239. );
  1240. }
  1241. function readonly(target) {
  1242. return createReactiveObject(
  1243. target,
  1244. true,
  1245. readonlyHandlers,
  1246. readonlyCollectionHandlers,
  1247. readonlyMap
  1248. );
  1249. }
  1250. function shallowReadonly(target) {
  1251. return createReactiveObject(
  1252. target,
  1253. true,
  1254. shallowReadonlyHandlers,
  1255. shallowReadonlyCollectionHandlers,
  1256. shallowReadonlyMap
  1257. );
  1258. }
  1259. function createReactiveObject(target, isReadonly2, baseHandlers, collectionHandlers, proxyMap) {
  1260. if (!shared.isObject(target)) {
  1261. return target;
  1262. }
  1263. if (target["__v_raw"] && !(isReadonly2 && target["__v_isReactive"])) {
  1264. return target;
  1265. }
  1266. const targetType = getTargetType(target);
  1267. if (targetType === 0 /* INVALID */) {
  1268. return target;
  1269. }
  1270. const existingProxy = proxyMap.get(target);
  1271. if (existingProxy) {
  1272. return existingProxy;
  1273. }
  1274. const proxy = new Proxy(
  1275. target,
  1276. targetType === 2 /* COLLECTION */ ? collectionHandlers : baseHandlers
  1277. );
  1278. proxyMap.set(target, proxy);
  1279. return proxy;
  1280. }
  1281. function isReactive(value) {
  1282. if (isReadonly(value)) {
  1283. return isReactive(value["__v_raw"]);
  1284. }
  1285. return !!(value && value["__v_isReactive"]);
  1286. }
  1287. function isReadonly(value) {
  1288. return !!(value && value["__v_isReadonly"]);
  1289. }
  1290. function isShallow(value) {
  1291. return !!(value && value["__v_isShallow"]);
  1292. }
  1293. function isProxy(value) {
  1294. return value ? !!value["__v_raw"] : false;
  1295. }
  1296. function toRaw(observed) {
  1297. const raw = observed && observed["__v_raw"];
  1298. return raw ? toRaw(raw) : observed;
  1299. }
  1300. function markRaw(value) {
  1301. if (!shared.hasOwn(value, "__v_skip") && Object.isExtensible(value)) {
  1302. shared.def(value, "__v_skip", true);
  1303. }
  1304. return value;
  1305. }
  1306. const toReactive = (value) => shared.isObject(value) ? reactive(value) : value;
  1307. const toReadonly = (value) => shared.isObject(value) ? readonly(value) : value;
  1308. function isRef(r) {
  1309. return r ? r["__v_isRef"] === true : false;
  1310. }
  1311. function ref(value) {
  1312. return createRef(value, false);
  1313. }
  1314. function shallowRef(value) {
  1315. return createRef(value, true);
  1316. }
  1317. function createRef(rawValue, shallow) {
  1318. if (isRef(rawValue)) {
  1319. return rawValue;
  1320. }
  1321. return new RefImpl(rawValue, shallow);
  1322. }
  1323. class RefImpl {
  1324. constructor(value, isShallow2) {
  1325. this.dep = new Dep();
  1326. this["__v_isRef"] = true;
  1327. this["__v_isShallow"] = false;
  1328. this._rawValue = isShallow2 ? value : toRaw(value);
  1329. this._value = isShallow2 ? value : toReactive(value);
  1330. this["__v_isShallow"] = isShallow2;
  1331. }
  1332. get value() {
  1333. {
  1334. this.dep.track();
  1335. }
  1336. return this._value;
  1337. }
  1338. set value(newValue) {
  1339. const oldValue = this._rawValue;
  1340. const useDirectValue = this["__v_isShallow"] || isShallow(newValue) || isReadonly(newValue);
  1341. newValue = useDirectValue ? newValue : toRaw(newValue);
  1342. if (shared.hasChanged(newValue, oldValue)) {
  1343. this._rawValue = newValue;
  1344. this._value = useDirectValue ? newValue : toReactive(newValue);
  1345. {
  1346. this.dep.trigger();
  1347. }
  1348. }
  1349. }
  1350. }
  1351. function triggerRef(ref2) {
  1352. if (ref2.dep) {
  1353. {
  1354. ref2.dep.trigger();
  1355. }
  1356. }
  1357. }
  1358. function unref(ref2) {
  1359. return isRef(ref2) ? ref2.value : ref2;
  1360. }
  1361. function toValue(source) {
  1362. return shared.isFunction(source) ? source() : unref(source);
  1363. }
  1364. const shallowUnwrapHandlers = {
  1365. get: (target, key, receiver) => key === "__v_raw" ? target : unref(Reflect.get(target, key, receiver)),
  1366. set: (target, key, value, receiver) => {
  1367. const oldValue = target[key];
  1368. if (isRef(oldValue) && !isRef(value)) {
  1369. oldValue.value = value;
  1370. return true;
  1371. } else {
  1372. return Reflect.set(target, key, value, receiver);
  1373. }
  1374. }
  1375. };
  1376. function proxyRefs(objectWithRefs) {
  1377. return isReactive(objectWithRefs) ? objectWithRefs : new Proxy(objectWithRefs, shallowUnwrapHandlers);
  1378. }
  1379. class CustomRefImpl {
  1380. constructor(factory) {
  1381. this["__v_isRef"] = true;
  1382. this._value = void 0;
  1383. const dep = this.dep = new Dep();
  1384. const { get, set } = factory(dep.track.bind(dep), dep.trigger.bind(dep));
  1385. this._get = get;
  1386. this._set = set;
  1387. }
  1388. get value() {
  1389. return this._value = this._get();
  1390. }
  1391. set value(newVal) {
  1392. this._set(newVal);
  1393. }
  1394. }
  1395. function customRef(factory) {
  1396. return new CustomRefImpl(factory);
  1397. }
  1398. function toRefs(object) {
  1399. const ret = shared.isArray(object) ? new Array(object.length) : {};
  1400. for (const key in object) {
  1401. ret[key] = propertyToRef(object, key);
  1402. }
  1403. return ret;
  1404. }
  1405. class ObjectRefImpl {
  1406. constructor(_object, _key, _defaultValue) {
  1407. this._object = _object;
  1408. this._key = _key;
  1409. this._defaultValue = _defaultValue;
  1410. this["__v_isRef"] = true;
  1411. this._value = void 0;
  1412. }
  1413. get value() {
  1414. const val = this._object[this._key];
  1415. return this._value = val === void 0 ? this._defaultValue : val;
  1416. }
  1417. set value(newVal) {
  1418. this._object[this._key] = newVal;
  1419. }
  1420. get dep() {
  1421. return getDepFromReactive(toRaw(this._object), this._key);
  1422. }
  1423. }
  1424. class GetterRefImpl {
  1425. constructor(_getter) {
  1426. this._getter = _getter;
  1427. this["__v_isRef"] = true;
  1428. this["__v_isReadonly"] = true;
  1429. this._value = void 0;
  1430. }
  1431. get value() {
  1432. return this._value = this._getter();
  1433. }
  1434. }
  1435. function toRef(source, key, defaultValue) {
  1436. if (isRef(source)) {
  1437. return source;
  1438. } else if (shared.isFunction(source)) {
  1439. return new GetterRefImpl(source);
  1440. } else if (shared.isObject(source) && arguments.length > 1) {
  1441. return propertyToRef(source, key, defaultValue);
  1442. } else {
  1443. return ref(source);
  1444. }
  1445. }
  1446. function propertyToRef(source, key, defaultValue) {
  1447. const val = source[key];
  1448. return isRef(val) ? val : new ObjectRefImpl(source, key, defaultValue);
  1449. }
  1450. class ComputedRefImpl {
  1451. constructor(fn, setter, isSSR) {
  1452. this.fn = fn;
  1453. this.setter = setter;
  1454. /**
  1455. * @internal
  1456. */
  1457. this._value = void 0;
  1458. /**
  1459. * @internal
  1460. */
  1461. this.dep = new Dep(this);
  1462. /**
  1463. * @internal
  1464. */
  1465. this.__v_isRef = true;
  1466. // TODO isolatedDeclarations "__v_isReadonly"
  1467. // A computed is also a subscriber that tracks other deps
  1468. /**
  1469. * @internal
  1470. */
  1471. this.deps = void 0;
  1472. /**
  1473. * @internal
  1474. */
  1475. this.depsTail = void 0;
  1476. /**
  1477. * @internal
  1478. */
  1479. this.flags = 16;
  1480. /**
  1481. * @internal
  1482. */
  1483. this.globalVersion = globalVersion - 1;
  1484. /**
  1485. * @internal
  1486. */
  1487. this.next = void 0;
  1488. // for backwards compat
  1489. this.effect = this;
  1490. this["__v_isReadonly"] = !setter;
  1491. this.isSSR = isSSR;
  1492. }
  1493. /**
  1494. * @internal
  1495. */
  1496. notify() {
  1497. this.flags |= 16;
  1498. if (!(this.flags & 8) && // avoid infinite self recursion
  1499. activeSub !== this) {
  1500. batch(this, true);
  1501. return true;
  1502. }
  1503. }
  1504. get value() {
  1505. const link = this.dep.track();
  1506. refreshComputed(this);
  1507. if (link) {
  1508. link.version = this.dep.version;
  1509. }
  1510. return this._value;
  1511. }
  1512. set value(newValue) {
  1513. if (this.setter) {
  1514. this.setter(newValue);
  1515. }
  1516. }
  1517. }
  1518. function computed(getterOrOptions, debugOptions, isSSR = false) {
  1519. let getter;
  1520. let setter;
  1521. if (shared.isFunction(getterOrOptions)) {
  1522. getter = getterOrOptions;
  1523. } else {
  1524. getter = getterOrOptions.get;
  1525. setter = getterOrOptions.set;
  1526. }
  1527. const cRef = new ComputedRefImpl(getter, setter, isSSR);
  1528. return cRef;
  1529. }
  1530. const TrackOpTypes = {
  1531. "GET": "get",
  1532. "HAS": "has",
  1533. "ITERATE": "iterate"
  1534. };
  1535. const TriggerOpTypes = {
  1536. "SET": "set",
  1537. "ADD": "add",
  1538. "DELETE": "delete",
  1539. "CLEAR": "clear"
  1540. };
  1541. const ReactiveFlags = {
  1542. "SKIP": "__v_skip",
  1543. "IS_REACTIVE": "__v_isReactive",
  1544. "IS_READONLY": "__v_isReadonly",
  1545. "IS_SHALLOW": "__v_isShallow",
  1546. "RAW": "__v_raw",
  1547. "IS_REF": "__v_isRef"
  1548. };
  1549. const WatchErrorCodes = {
  1550. "WATCH_GETTER": 2,
  1551. "2": "WATCH_GETTER",
  1552. "WATCH_CALLBACK": 3,
  1553. "3": "WATCH_CALLBACK",
  1554. "WATCH_CLEANUP": 4,
  1555. "4": "WATCH_CLEANUP"
  1556. };
  1557. const INITIAL_WATCHER_VALUE = {};
  1558. const cleanupMap = /* @__PURE__ */ new WeakMap();
  1559. let activeWatcher = void 0;
  1560. function getCurrentWatcher() {
  1561. return activeWatcher;
  1562. }
  1563. function onWatcherCleanup(cleanupFn, failSilently = false, owner = activeWatcher) {
  1564. if (owner) {
  1565. let cleanups = cleanupMap.get(owner);
  1566. if (!cleanups) cleanupMap.set(owner, cleanups = []);
  1567. cleanups.push(cleanupFn);
  1568. }
  1569. }
  1570. function watch(source, cb, options = shared.EMPTY_OBJ) {
  1571. const { immediate, deep, once, scheduler, augmentJob, call } = options;
  1572. const reactiveGetter = (source2) => {
  1573. if (deep) return source2;
  1574. if (isShallow(source2) || deep === false || deep === 0)
  1575. return traverse(source2, 1);
  1576. return traverse(source2);
  1577. };
  1578. let effect;
  1579. let getter;
  1580. let cleanup;
  1581. let boundCleanup;
  1582. let forceTrigger = false;
  1583. let isMultiSource = false;
  1584. if (isRef(source)) {
  1585. getter = () => source.value;
  1586. forceTrigger = isShallow(source);
  1587. } else if (isReactive(source)) {
  1588. getter = () => reactiveGetter(source);
  1589. forceTrigger = true;
  1590. } else if (shared.isArray(source)) {
  1591. isMultiSource = true;
  1592. forceTrigger = source.some((s) => isReactive(s) || isShallow(s));
  1593. getter = () => source.map((s) => {
  1594. if (isRef(s)) {
  1595. return s.value;
  1596. } else if (isReactive(s)) {
  1597. return reactiveGetter(s);
  1598. } else if (shared.isFunction(s)) {
  1599. return call ? call(s, 2) : s();
  1600. } else ;
  1601. });
  1602. } else if (shared.isFunction(source)) {
  1603. if (cb) {
  1604. getter = call ? () => call(source, 2) : source;
  1605. } else {
  1606. getter = () => {
  1607. if (cleanup) {
  1608. pauseTracking();
  1609. try {
  1610. cleanup();
  1611. } finally {
  1612. resetTracking();
  1613. }
  1614. }
  1615. const currentEffect = activeWatcher;
  1616. activeWatcher = effect;
  1617. try {
  1618. return call ? call(source, 3, [boundCleanup]) : source(boundCleanup);
  1619. } finally {
  1620. activeWatcher = currentEffect;
  1621. }
  1622. };
  1623. }
  1624. } else {
  1625. getter = shared.NOOP;
  1626. }
  1627. if (cb && deep) {
  1628. const baseGetter = getter;
  1629. const depth = deep === true ? Infinity : deep;
  1630. getter = () => traverse(baseGetter(), depth);
  1631. }
  1632. const scope = getCurrentScope();
  1633. const watchHandle = () => {
  1634. effect.stop();
  1635. if (scope && scope.active) {
  1636. shared.remove(scope.effects, effect);
  1637. }
  1638. };
  1639. if (once && cb) {
  1640. const _cb = cb;
  1641. cb = (...args) => {
  1642. _cb(...args);
  1643. watchHandle();
  1644. };
  1645. }
  1646. let oldValue = isMultiSource ? new Array(source.length).fill(INITIAL_WATCHER_VALUE) : INITIAL_WATCHER_VALUE;
  1647. const job = (immediateFirstRun) => {
  1648. if (!(effect.flags & 1) || !effect.dirty && !immediateFirstRun) {
  1649. return;
  1650. }
  1651. if (cb) {
  1652. const newValue = effect.run();
  1653. if (deep || forceTrigger || (isMultiSource ? newValue.some((v, i) => shared.hasChanged(v, oldValue[i])) : shared.hasChanged(newValue, oldValue))) {
  1654. if (cleanup) {
  1655. cleanup();
  1656. }
  1657. const currentWatcher = activeWatcher;
  1658. activeWatcher = effect;
  1659. try {
  1660. const args = [
  1661. newValue,
  1662. // pass undefined as the old value when it's changed for the first time
  1663. oldValue === INITIAL_WATCHER_VALUE ? void 0 : isMultiSource && oldValue[0] === INITIAL_WATCHER_VALUE ? [] : oldValue,
  1664. boundCleanup
  1665. ];
  1666. oldValue = newValue;
  1667. call ? call(cb, 3, args) : (
  1668. // @ts-expect-error
  1669. cb(...args)
  1670. );
  1671. } finally {
  1672. activeWatcher = currentWatcher;
  1673. }
  1674. }
  1675. } else {
  1676. effect.run();
  1677. }
  1678. };
  1679. if (augmentJob) {
  1680. augmentJob(job);
  1681. }
  1682. effect = new ReactiveEffect(getter);
  1683. effect.scheduler = scheduler ? () => scheduler(job, false) : job;
  1684. boundCleanup = (fn) => onWatcherCleanup(fn, false, effect);
  1685. cleanup = effect.onStop = () => {
  1686. const cleanups = cleanupMap.get(effect);
  1687. if (cleanups) {
  1688. if (call) {
  1689. call(cleanups, 4);
  1690. } else {
  1691. for (const cleanup2 of cleanups) cleanup2();
  1692. }
  1693. cleanupMap.delete(effect);
  1694. }
  1695. };
  1696. if (cb) {
  1697. if (immediate) {
  1698. job(true);
  1699. } else {
  1700. oldValue = effect.run();
  1701. }
  1702. } else if (scheduler) {
  1703. scheduler(job.bind(null, true), true);
  1704. } else {
  1705. effect.run();
  1706. }
  1707. watchHandle.pause = effect.pause.bind(effect);
  1708. watchHandle.resume = effect.resume.bind(effect);
  1709. watchHandle.stop = watchHandle;
  1710. return watchHandle;
  1711. }
  1712. function traverse(value, depth = Infinity, seen) {
  1713. if (depth <= 0 || !shared.isObject(value) || value["__v_skip"]) {
  1714. return value;
  1715. }
  1716. seen = seen || /* @__PURE__ */ new Map();
  1717. if ((seen.get(value) || 0) >= depth) {
  1718. return value;
  1719. }
  1720. seen.set(value, depth);
  1721. depth--;
  1722. if (isRef(value)) {
  1723. traverse(value.value, depth, seen);
  1724. } else if (shared.isArray(value)) {
  1725. for (let i = 0; i < value.length; i++) {
  1726. traverse(value[i], depth, seen);
  1727. }
  1728. } else if (shared.isSet(value) || shared.isMap(value)) {
  1729. value.forEach((v) => {
  1730. traverse(v, depth, seen);
  1731. });
  1732. } else if (shared.isPlainObject(value)) {
  1733. for (const key in value) {
  1734. traverse(value[key], depth, seen);
  1735. }
  1736. for (const key of Object.getOwnPropertySymbols(value)) {
  1737. if (Object.prototype.propertyIsEnumerable.call(value, key)) {
  1738. traverse(value[key], depth, seen);
  1739. }
  1740. }
  1741. }
  1742. return value;
  1743. }
  1744. exports.ARRAY_ITERATE_KEY = ARRAY_ITERATE_KEY;
  1745. exports.EffectFlags = EffectFlags;
  1746. exports.EffectScope = EffectScope;
  1747. exports.ITERATE_KEY = ITERATE_KEY;
  1748. exports.MAP_KEY_ITERATE_KEY = MAP_KEY_ITERATE_KEY;
  1749. exports.ReactiveEffect = ReactiveEffect;
  1750. exports.ReactiveFlags = ReactiveFlags;
  1751. exports.TrackOpTypes = TrackOpTypes;
  1752. exports.TriggerOpTypes = TriggerOpTypes;
  1753. exports.WatchErrorCodes = WatchErrorCodes;
  1754. exports.computed = computed;
  1755. exports.customRef = customRef;
  1756. exports.effect = effect;
  1757. exports.effectScope = effectScope;
  1758. exports.enableTracking = enableTracking;
  1759. exports.getCurrentScope = getCurrentScope;
  1760. exports.getCurrentWatcher = getCurrentWatcher;
  1761. exports.isProxy = isProxy;
  1762. exports.isReactive = isReactive;
  1763. exports.isReadonly = isReadonly;
  1764. exports.isRef = isRef;
  1765. exports.isShallow = isShallow;
  1766. exports.markRaw = markRaw;
  1767. exports.onEffectCleanup = onEffectCleanup;
  1768. exports.onScopeDispose = onScopeDispose;
  1769. exports.onWatcherCleanup = onWatcherCleanup;
  1770. exports.pauseTracking = pauseTracking;
  1771. exports.proxyRefs = proxyRefs;
  1772. exports.reactive = reactive;
  1773. exports.reactiveReadArray = reactiveReadArray;
  1774. exports.readonly = readonly;
  1775. exports.ref = ref;
  1776. exports.resetTracking = resetTracking;
  1777. exports.shallowReactive = shallowReactive;
  1778. exports.shallowReadArray = shallowReadArray;
  1779. exports.shallowReadonly = shallowReadonly;
  1780. exports.shallowRef = shallowRef;
  1781. exports.stop = stop;
  1782. exports.toRaw = toRaw;
  1783. exports.toReactive = toReactive;
  1784. exports.toReadonly = toReadonly;
  1785. exports.toRef = toRef;
  1786. exports.toRefs = toRefs;
  1787. exports.toValue = toValue;
  1788. exports.track = track;
  1789. exports.traverse = traverse;
  1790. exports.trigger = trigger;
  1791. exports.triggerRef = triggerRef;
  1792. exports.unref = unref;
  1793. exports.watch = watch;