reactivity.esm-browser.js 50 KB

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