reactivity.esm-bundler.js 50 KB

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