reactivity.esm-bundler.js 50 KB

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