reactivity.cjs.js 50 KB

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