reactivity.cjs.prod.js 45 KB

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