reactivity.esm-browser.js 50 KB

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