reactivity.esm-bundler.js 50 KB

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