reactivity.cjs.prod.js 46 KB

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