reactivity.esm-browser.js 50 KB

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