reactivity.esm-browser.js 50 KB

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