reactivity.global.js 55 KB

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