reactivity.esm-bundler.js 50 KB

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