reactivity.cjs.js 49 KB

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