reactivity.cjs.js 50 KB

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