reactivity.esm-browser.js 50 KB

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