reactivity.cjs.js 50 KB

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