reactivity.esm-browser.js 50 KB

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