reactivity.cjs.js 50 KB

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