reactivity.global.js 55 KB

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