reactivity.cjs.prod.js 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var shared = require('@vue/shared');
  4. let activeEffectScope;
  5. class EffectScope {
  6. constructor(detached = false) {
  7. this.detached = detached;
  8. /**
  9. * @internal
  10. */
  11. this._active = true;
  12. /**
  13. * @internal
  14. */
  15. this.effects = [];
  16. /**
  17. * @internal
  18. */
  19. this.cleanups = [];
  20. this.parent = activeEffectScope;
  21. if (!detached && activeEffectScope) {
  22. this.index = (activeEffectScope.scopes || (activeEffectScope.scopes = [])).push(
  23. this
  24. ) - 1;
  25. }
  26. }
  27. get active() {
  28. return this._active;
  29. }
  30. run(fn) {
  31. if (this._active) {
  32. const currentEffectScope = activeEffectScope;
  33. try {
  34. activeEffectScope = this;
  35. return fn();
  36. } finally {
  37. activeEffectScope = currentEffectScope;
  38. }
  39. }
  40. }
  41. /**
  42. * This should only be called on non-detached scopes
  43. * @internal
  44. */
  45. on() {
  46. activeEffectScope = this;
  47. }
  48. /**
  49. * This should only be called on non-detached scopes
  50. * @internal
  51. */
  52. off() {
  53. activeEffectScope = this.parent;
  54. }
  55. stop(fromParent) {
  56. if (this._active) {
  57. let i, l;
  58. for (i = 0, l = this.effects.length; i < l; i++) {
  59. this.effects[i].stop();
  60. }
  61. for (i = 0, l = this.cleanups.length; i < l; i++) {
  62. this.cleanups[i]();
  63. }
  64. if (this.scopes) {
  65. for (i = 0, l = this.scopes.length; i < l; i++) {
  66. this.scopes[i].stop(true);
  67. }
  68. }
  69. if (!this.detached && this.parent && !fromParent) {
  70. const last = this.parent.scopes.pop();
  71. if (last && last !== this) {
  72. this.parent.scopes[this.index] = last;
  73. last.index = this.index;
  74. }
  75. }
  76. this.parent = void 0;
  77. this._active = false;
  78. }
  79. }
  80. }
  81. function effectScope(detached) {
  82. return new EffectScope(detached);
  83. }
  84. function recordEffectScope(effect, scope = activeEffectScope) {
  85. if (scope && scope.active) {
  86. scope.effects.push(effect);
  87. }
  88. }
  89. function getCurrentScope() {
  90. return activeEffectScope;
  91. }
  92. function onScopeDispose(fn) {
  93. if (activeEffectScope) {
  94. activeEffectScope.cleanups.push(fn);
  95. }
  96. }
  97. const createDep = (effects) => {
  98. const dep = new Set(effects);
  99. dep.w = 0;
  100. dep.n = 0;
  101. return dep;
  102. };
  103. const wasTracked = (dep) => (dep.w & trackOpBit) > 0;
  104. const newTracked = (dep) => (dep.n & trackOpBit) > 0;
  105. const initDepMarkers = ({ deps }) => {
  106. if (deps.length) {
  107. for (let i = 0; i < deps.length; i++) {
  108. deps[i].w |= trackOpBit;
  109. }
  110. }
  111. };
  112. const finalizeDepMarkers = (effect) => {
  113. const { deps } = effect;
  114. if (deps.length) {
  115. let ptr = 0;
  116. for (let i = 0; i < deps.length; i++) {
  117. const dep = deps[i];
  118. if (wasTracked(dep) && !newTracked(dep)) {
  119. dep.delete(effect);
  120. } else {
  121. deps[ptr++] = dep;
  122. }
  123. dep.w &= ~trackOpBit;
  124. dep.n &= ~trackOpBit;
  125. }
  126. deps.length = ptr;
  127. }
  128. };
  129. const targetMap = /* @__PURE__ */ new WeakMap();
  130. let effectTrackDepth = 0;
  131. let trackOpBit = 1;
  132. const maxMarkerBits = 30;
  133. let activeEffect;
  134. const ITERATE_KEY = Symbol("");
  135. const MAP_KEY_ITERATE_KEY = Symbol("");
  136. class ReactiveEffect {
  137. constructor(fn, scheduler = null, scope) {
  138. this.fn = fn;
  139. this.scheduler = scheduler;
  140. this.active = true;
  141. this.deps = [];
  142. this.parent = void 0;
  143. recordEffectScope(this, scope);
  144. }
  145. run() {
  146. if (!this.active) {
  147. return this.fn();
  148. }
  149. let parent = activeEffect;
  150. let lastShouldTrack = shouldTrack;
  151. while (parent) {
  152. if (parent === this) {
  153. return;
  154. }
  155. parent = parent.parent;
  156. }
  157. try {
  158. this.parent = activeEffect;
  159. activeEffect = this;
  160. shouldTrack = true;
  161. trackOpBit = 1 << ++effectTrackDepth;
  162. if (effectTrackDepth <= maxMarkerBits) {
  163. initDepMarkers(this);
  164. } else {
  165. cleanupEffect(this);
  166. }
  167. return this.fn();
  168. } finally {
  169. if (effectTrackDepth <= maxMarkerBits) {
  170. finalizeDepMarkers(this);
  171. }
  172. trackOpBit = 1 << --effectTrackDepth;
  173. activeEffect = this.parent;
  174. shouldTrack = lastShouldTrack;
  175. this.parent = void 0;
  176. if (this.deferStop) {
  177. this.stop();
  178. }
  179. }
  180. }
  181. stop() {
  182. if (activeEffect === this) {
  183. this.deferStop = true;
  184. } else if (this.active) {
  185. cleanupEffect(this);
  186. if (this.onStop) {
  187. this.onStop();
  188. }
  189. this.active = false;
  190. }
  191. }
  192. }
  193. function cleanupEffect(effect2) {
  194. const { deps } = effect2;
  195. if (deps.length) {
  196. for (let i = 0; i < deps.length; i++) {
  197. deps[i].delete(effect2);
  198. }
  199. deps.length = 0;
  200. }
  201. }
  202. function effect(fn, options) {
  203. if (fn.effect) {
  204. fn = fn.effect.fn;
  205. }
  206. const _effect = new ReactiveEffect(fn);
  207. if (options) {
  208. shared.extend(_effect, options);
  209. if (options.scope)
  210. recordEffectScope(_effect, options.scope);
  211. }
  212. if (!options || !options.lazy) {
  213. _effect.run();
  214. }
  215. const runner = _effect.run.bind(_effect);
  216. runner.effect = _effect;
  217. return runner;
  218. }
  219. function stop(runner) {
  220. runner.effect.stop();
  221. }
  222. let shouldTrack = true;
  223. const trackStack = [];
  224. function pauseTracking() {
  225. trackStack.push(shouldTrack);
  226. shouldTrack = false;
  227. }
  228. function enableTracking() {
  229. trackStack.push(shouldTrack);
  230. shouldTrack = true;
  231. }
  232. function resetTracking() {
  233. const last = trackStack.pop();
  234. shouldTrack = last === void 0 ? true : last;
  235. }
  236. function track(target, type, key) {
  237. if (shouldTrack && activeEffect) {
  238. let depsMap = targetMap.get(target);
  239. if (!depsMap) {
  240. targetMap.set(target, depsMap = /* @__PURE__ */ new Map());
  241. }
  242. let dep = depsMap.get(key);
  243. if (!dep) {
  244. depsMap.set(key, dep = createDep());
  245. }
  246. trackEffects(dep);
  247. }
  248. }
  249. function trackEffects(dep, debuggerEventExtraInfo) {
  250. let shouldTrack2 = false;
  251. if (effectTrackDepth <= maxMarkerBits) {
  252. if (!newTracked(dep)) {
  253. dep.n |= trackOpBit;
  254. shouldTrack2 = !wasTracked(dep);
  255. }
  256. } else {
  257. shouldTrack2 = !dep.has(activeEffect);
  258. }
  259. if (shouldTrack2) {
  260. dep.add(activeEffect);
  261. activeEffect.deps.push(dep);
  262. }
  263. }
  264. function trigger(target, type, key, newValue, oldValue, oldTarget) {
  265. const depsMap = targetMap.get(target);
  266. if (!depsMap) {
  267. return;
  268. }
  269. let deps = [];
  270. if (type === "clear") {
  271. deps = [...depsMap.values()];
  272. } else if (key === "length" && shared.isArray(target)) {
  273. const newLength = Number(newValue);
  274. depsMap.forEach((dep, key2) => {
  275. if (key2 === "length" || key2 >= newLength) {
  276. deps.push(dep);
  277. }
  278. });
  279. } else {
  280. if (key !== void 0) {
  281. deps.push(depsMap.get(key));
  282. }
  283. switch (type) {
  284. case "add":
  285. if (!shared.isArray(target)) {
  286. deps.push(depsMap.get(ITERATE_KEY));
  287. if (shared.isMap(target)) {
  288. deps.push(depsMap.get(MAP_KEY_ITERATE_KEY));
  289. }
  290. } else if (shared.isIntegerKey(key)) {
  291. deps.push(depsMap.get("length"));
  292. }
  293. break;
  294. case "delete":
  295. if (!shared.isArray(target)) {
  296. deps.push(depsMap.get(ITERATE_KEY));
  297. if (shared.isMap(target)) {
  298. deps.push(depsMap.get(MAP_KEY_ITERATE_KEY));
  299. }
  300. }
  301. break;
  302. case "set":
  303. if (shared.isMap(target)) {
  304. deps.push(depsMap.get(ITERATE_KEY));
  305. }
  306. break;
  307. }
  308. }
  309. if (deps.length === 1) {
  310. if (deps[0]) {
  311. {
  312. triggerEffects(deps[0]);
  313. }
  314. }
  315. } else {
  316. const effects = [];
  317. for (const dep of deps) {
  318. if (dep) {
  319. effects.push(...dep);
  320. }
  321. }
  322. {
  323. triggerEffects(createDep(effects));
  324. }
  325. }
  326. }
  327. function triggerEffects(dep, debuggerEventExtraInfo) {
  328. const effects = shared.isArray(dep) ? dep : [...dep];
  329. for (const effect2 of effects) {
  330. if (effect2.computed) {
  331. triggerEffect(effect2);
  332. }
  333. }
  334. for (const effect2 of effects) {
  335. if (!effect2.computed) {
  336. triggerEffect(effect2);
  337. }
  338. }
  339. }
  340. function triggerEffect(effect2, debuggerEventExtraInfo) {
  341. if (effect2 !== activeEffect || effect2.allowRecurse) {
  342. if (effect2.scheduler) {
  343. effect2.scheduler();
  344. } else {
  345. effect2.run();
  346. }
  347. }
  348. }
  349. function getDepFromReactive(object, key) {
  350. var _a;
  351. return (_a = targetMap.get(object)) == null ? void 0 : _a.get(key);
  352. }
  353. const isNonTrackableKeys = /* @__PURE__ */ shared.makeMap(`__proto__,__v_isRef,__isVue`);
  354. const builtInSymbols = new Set(
  355. /* @__PURE__ */ Object.getOwnPropertyNames(Symbol).filter((key) => key !== "arguments" && key !== "caller").map((key) => Symbol[key]).filter(shared.isSymbol)
  356. );
  357. const get$1 = /* @__PURE__ */ createGetter();
  358. const shallowGet = /* @__PURE__ */ createGetter(false, true);
  359. const readonlyGet = /* @__PURE__ */ createGetter(true);
  360. const shallowReadonlyGet = /* @__PURE__ */ createGetter(true, true);
  361. const arrayInstrumentations = /* @__PURE__ */ createArrayInstrumentations();
  362. function createArrayInstrumentations() {
  363. const instrumentations = {};
  364. ["includes", "indexOf", "lastIndexOf"].forEach((key) => {
  365. instrumentations[key] = function(...args) {
  366. const arr = toRaw(this);
  367. for (let i = 0, l = this.length; i < l; i++) {
  368. track(arr, "get", i + "");
  369. }
  370. const res = arr[key](...args);
  371. if (res === -1 || res === false) {
  372. return arr[key](...args.map(toRaw));
  373. } else {
  374. return res;
  375. }
  376. };
  377. });
  378. ["push", "pop", "shift", "unshift", "splice"].forEach((key) => {
  379. instrumentations[key] = function(...args) {
  380. pauseTracking();
  381. const res = toRaw(this)[key].apply(this, args);
  382. resetTracking();
  383. return res;
  384. };
  385. });
  386. return instrumentations;
  387. }
  388. function hasOwnProperty(key) {
  389. const obj = toRaw(this);
  390. track(obj, "has", key);
  391. return obj.hasOwnProperty(key);
  392. }
  393. function createGetter(isReadonly2 = false, shallow = false) {
  394. return function get2(target, key, receiver) {
  395. if (key === "__v_isReactive") {
  396. return !isReadonly2;
  397. } else if (key === "__v_isReadonly") {
  398. return isReadonly2;
  399. } else if (key === "__v_isShallow") {
  400. return shallow;
  401. } else if (key === "__v_raw" && receiver === (isReadonly2 ? shallow ? shallowReadonlyMap : readonlyMap : shallow ? shallowReactiveMap : reactiveMap).get(target)) {
  402. return target;
  403. }
  404. const targetIsArray = shared.isArray(target);
  405. if (!isReadonly2) {
  406. if (targetIsArray && shared.hasOwn(arrayInstrumentations, key)) {
  407. return Reflect.get(arrayInstrumentations, key, receiver);
  408. }
  409. if (key === "hasOwnProperty") {
  410. return hasOwnProperty;
  411. }
  412. }
  413. const res = Reflect.get(target, key, receiver);
  414. if (shared.isSymbol(key) ? builtInSymbols.has(key) : isNonTrackableKeys(key)) {
  415. return res;
  416. }
  417. if (!isReadonly2) {
  418. track(target, "get", key);
  419. }
  420. if (shallow) {
  421. return res;
  422. }
  423. if (isRef(res)) {
  424. return targetIsArray && shared.isIntegerKey(key) ? res : res.value;
  425. }
  426. if (shared.isObject(res)) {
  427. return isReadonly2 ? readonly(res) : reactive(res);
  428. }
  429. return res;
  430. };
  431. }
  432. const set$1 = /* @__PURE__ */ createSetter();
  433. const shallowSet = /* @__PURE__ */ createSetter(true);
  434. function createSetter(shallow = false) {
  435. return function set2(target, key, value, receiver) {
  436. let oldValue = target[key];
  437. if (isReadonly(oldValue) && isRef(oldValue) && !isRef(value)) {
  438. return false;
  439. }
  440. if (!shallow) {
  441. if (!isShallow(value) && !isReadonly(value)) {
  442. oldValue = toRaw(oldValue);
  443. value = toRaw(value);
  444. }
  445. if (!shared.isArray(target) && isRef(oldValue) && !isRef(value)) {
  446. oldValue.value = value;
  447. return true;
  448. }
  449. }
  450. const hadKey = shared.isArray(target) && shared.isIntegerKey(key) ? Number(key) < target.length : shared.hasOwn(target, key);
  451. const result = Reflect.set(target, key, value, receiver);
  452. if (target === toRaw(receiver)) {
  453. if (!hadKey) {
  454. trigger(target, "add", key, value);
  455. } else if (shared.hasChanged(value, oldValue)) {
  456. trigger(target, "set", key, value);
  457. }
  458. }
  459. return result;
  460. };
  461. }
  462. function deleteProperty(target, key) {
  463. const hadKey = shared.hasOwn(target, key);
  464. target[key];
  465. const result = Reflect.deleteProperty(target, key);
  466. if (result && hadKey) {
  467. trigger(target, "delete", key, void 0);
  468. }
  469. return result;
  470. }
  471. function has$1(target, key) {
  472. const result = Reflect.has(target, key);
  473. if (!shared.isSymbol(key) || !builtInSymbols.has(key)) {
  474. track(target, "has", key);
  475. }
  476. return result;
  477. }
  478. function ownKeys(target) {
  479. track(target, "iterate", shared.isArray(target) ? "length" : ITERATE_KEY);
  480. return Reflect.ownKeys(target);
  481. }
  482. const mutableHandlers = {
  483. get: get$1,
  484. set: set$1,
  485. deleteProperty,
  486. has: has$1,
  487. ownKeys
  488. };
  489. const readonlyHandlers = {
  490. get: readonlyGet,
  491. set(target, key) {
  492. return true;
  493. },
  494. deleteProperty(target, key) {
  495. return true;
  496. }
  497. };
  498. const shallowReactiveHandlers = /* @__PURE__ */ shared.extend(
  499. {},
  500. mutableHandlers,
  501. {
  502. get: shallowGet,
  503. set: shallowSet
  504. }
  505. );
  506. const shallowReadonlyHandlers = /* @__PURE__ */ shared.extend(
  507. {},
  508. readonlyHandlers,
  509. {
  510. get: shallowReadonlyGet
  511. }
  512. );
  513. const toShallow = (value) => value;
  514. const getProto = (v) => Reflect.getPrototypeOf(v);
  515. function get(target, key, isReadonly = false, isShallow = false) {
  516. target = target["__v_raw"];
  517. const rawTarget = toRaw(target);
  518. const rawKey = toRaw(key);
  519. if (!isReadonly) {
  520. if (key !== rawKey) {
  521. track(rawTarget, "get", key);
  522. }
  523. track(rawTarget, "get", rawKey);
  524. }
  525. const { has: has2 } = getProto(rawTarget);
  526. const wrap = isShallow ? toShallow : isReadonly ? toReadonly : toReactive;
  527. if (has2.call(rawTarget, key)) {
  528. return wrap(target.get(key));
  529. } else if (has2.call(rawTarget, rawKey)) {
  530. return wrap(target.get(rawKey));
  531. } else if (target !== rawTarget) {
  532. target.get(key);
  533. }
  534. }
  535. function has(key, isReadonly = false) {
  536. const target = this["__v_raw"];
  537. const rawTarget = toRaw(target);
  538. const rawKey = toRaw(key);
  539. if (!isReadonly) {
  540. if (key !== rawKey) {
  541. track(rawTarget, "has", key);
  542. }
  543. track(rawTarget, "has", rawKey);
  544. }
  545. return key === rawKey ? target.has(key) : target.has(key) || target.has(rawKey);
  546. }
  547. function size(target, isReadonly = false) {
  548. target = target["__v_raw"];
  549. !isReadonly && track(toRaw(target), "iterate", ITERATE_KEY);
  550. return Reflect.get(target, "size", target);
  551. }
  552. function add(value) {
  553. value = toRaw(value);
  554. const target = toRaw(this);
  555. const proto = getProto(target);
  556. const hadKey = proto.has.call(target, value);
  557. if (!hadKey) {
  558. target.add(value);
  559. trigger(target, "add", value, value);
  560. }
  561. return this;
  562. }
  563. function set(key, value) {
  564. value = toRaw(value);
  565. const target = toRaw(this);
  566. const { has: has2, get: get2 } = getProto(target);
  567. let hadKey = has2.call(target, key);
  568. if (!hadKey) {
  569. key = toRaw(key);
  570. hadKey = has2.call(target, key);
  571. }
  572. const oldValue = get2.call(target, key);
  573. target.set(key, value);
  574. if (!hadKey) {
  575. trigger(target, "add", key, value);
  576. } else if (shared.hasChanged(value, oldValue)) {
  577. trigger(target, "set", key, value);
  578. }
  579. return this;
  580. }
  581. function deleteEntry(key) {
  582. const target = toRaw(this);
  583. const { has: has2, get: get2 } = getProto(target);
  584. let hadKey = has2.call(target, key);
  585. if (!hadKey) {
  586. key = toRaw(key);
  587. hadKey = has2.call(target, key);
  588. }
  589. get2 ? get2.call(target, key) : void 0;
  590. const result = target.delete(key);
  591. if (hadKey) {
  592. trigger(target, "delete", key, void 0);
  593. }
  594. return result;
  595. }
  596. function clear() {
  597. const target = toRaw(this);
  598. const hadItems = target.size !== 0;
  599. const result = target.clear();
  600. if (hadItems) {
  601. trigger(target, "clear", void 0, void 0);
  602. }
  603. return result;
  604. }
  605. function createForEach(isReadonly, isShallow) {
  606. return function forEach(callback, thisArg) {
  607. const observed = this;
  608. const target = observed["__v_raw"];
  609. const rawTarget = toRaw(target);
  610. const wrap = isShallow ? toShallow : isReadonly ? toReadonly : toReactive;
  611. !isReadonly && track(rawTarget, "iterate", ITERATE_KEY);
  612. return target.forEach((value, key) => {
  613. return callback.call(thisArg, wrap(value), wrap(key), observed);
  614. });
  615. };
  616. }
  617. function createIterableMethod(method, isReadonly, isShallow) {
  618. return function(...args) {
  619. const target = this["__v_raw"];
  620. const rawTarget = toRaw(target);
  621. const targetIsMap = shared.isMap(rawTarget);
  622. const isPair = method === "entries" || method === Symbol.iterator && targetIsMap;
  623. const isKeyOnly = method === "keys" && targetIsMap;
  624. const innerIterator = target[method](...args);
  625. const wrap = isShallow ? toShallow : isReadonly ? toReadonly : toReactive;
  626. !isReadonly && track(
  627. rawTarget,
  628. "iterate",
  629. isKeyOnly ? MAP_KEY_ITERATE_KEY : ITERATE_KEY
  630. );
  631. return {
  632. // iterator protocol
  633. next() {
  634. const { value, done } = innerIterator.next();
  635. return done ? { value, done } : {
  636. value: isPair ? [wrap(value[0]), wrap(value[1])] : wrap(value),
  637. done
  638. };
  639. },
  640. // iterable protocol
  641. [Symbol.iterator]() {
  642. return this;
  643. }
  644. };
  645. };
  646. }
  647. function createReadonlyMethod(type) {
  648. return function(...args) {
  649. return type === "delete" ? false : this;
  650. };
  651. }
  652. function createInstrumentations() {
  653. const mutableInstrumentations2 = {
  654. get(key) {
  655. return get(this, key);
  656. },
  657. get size() {
  658. return size(this);
  659. },
  660. has,
  661. add,
  662. set,
  663. delete: deleteEntry,
  664. clear,
  665. forEach: createForEach(false, false)
  666. };
  667. const shallowInstrumentations2 = {
  668. get(key) {
  669. return get(this, key, false, true);
  670. },
  671. get size() {
  672. return size(this);
  673. },
  674. has,
  675. add,
  676. set,
  677. delete: deleteEntry,
  678. clear,
  679. forEach: createForEach(false, true)
  680. };
  681. const readonlyInstrumentations2 = {
  682. get(key) {
  683. return get(this, key, true);
  684. },
  685. get size() {
  686. return size(this, true);
  687. },
  688. has(key) {
  689. return has.call(this, key, true);
  690. },
  691. add: createReadonlyMethod("add"),
  692. set: createReadonlyMethod("set"),
  693. delete: createReadonlyMethod("delete"),
  694. clear: createReadonlyMethod("clear"),
  695. forEach: createForEach(true, false)
  696. };
  697. const shallowReadonlyInstrumentations2 = {
  698. get(key) {
  699. return get(this, key, true, true);
  700. },
  701. get size() {
  702. return size(this, true);
  703. },
  704. has(key) {
  705. return has.call(this, key, true);
  706. },
  707. add: createReadonlyMethod("add"),
  708. set: createReadonlyMethod("set"),
  709. delete: createReadonlyMethod("delete"),
  710. clear: createReadonlyMethod("clear"),
  711. forEach: createForEach(true, true)
  712. };
  713. const iteratorMethods = ["keys", "values", "entries", Symbol.iterator];
  714. iteratorMethods.forEach((method) => {
  715. mutableInstrumentations2[method] = createIterableMethod(
  716. method,
  717. false,
  718. false
  719. );
  720. readonlyInstrumentations2[method] = createIterableMethod(
  721. method,
  722. true,
  723. false
  724. );
  725. shallowInstrumentations2[method] = createIterableMethod(
  726. method,
  727. false,
  728. true
  729. );
  730. shallowReadonlyInstrumentations2[method] = createIterableMethod(
  731. method,
  732. true,
  733. true
  734. );
  735. });
  736. return [
  737. mutableInstrumentations2,
  738. readonlyInstrumentations2,
  739. shallowInstrumentations2,
  740. shallowReadonlyInstrumentations2
  741. ];
  742. }
  743. const [
  744. mutableInstrumentations,
  745. readonlyInstrumentations,
  746. shallowInstrumentations,
  747. shallowReadonlyInstrumentations
  748. ] = /* @__PURE__ */ createInstrumentations();
  749. function createInstrumentationGetter(isReadonly, shallow) {
  750. const instrumentations = shallow ? isReadonly ? shallowReadonlyInstrumentations : shallowInstrumentations : isReadonly ? readonlyInstrumentations : mutableInstrumentations;
  751. return (target, key, receiver) => {
  752. if (key === "__v_isReactive") {
  753. return !isReadonly;
  754. } else if (key === "__v_isReadonly") {
  755. return isReadonly;
  756. } else if (key === "__v_raw") {
  757. return target;
  758. }
  759. return Reflect.get(
  760. shared.hasOwn(instrumentations, key) && key in target ? instrumentations : target,
  761. key,
  762. receiver
  763. );
  764. };
  765. }
  766. const mutableCollectionHandlers = {
  767. get: /* @__PURE__ */ createInstrumentationGetter(false, false)
  768. };
  769. const shallowCollectionHandlers = {
  770. get: /* @__PURE__ */ createInstrumentationGetter(false, true)
  771. };
  772. const readonlyCollectionHandlers = {
  773. get: /* @__PURE__ */ createInstrumentationGetter(true, false)
  774. };
  775. const shallowReadonlyCollectionHandlers = {
  776. get: /* @__PURE__ */ createInstrumentationGetter(true, true)
  777. };
  778. const reactiveMap = /* @__PURE__ */ new WeakMap();
  779. const shallowReactiveMap = /* @__PURE__ */ new WeakMap();
  780. const readonlyMap = /* @__PURE__ */ new WeakMap();
  781. const shallowReadonlyMap = /* @__PURE__ */ new WeakMap();
  782. function targetTypeMap(rawType) {
  783. switch (rawType) {
  784. case "Object":
  785. case "Array":
  786. return 1 /* COMMON */;
  787. case "Map":
  788. case "Set":
  789. case "WeakMap":
  790. case "WeakSet":
  791. return 2 /* COLLECTION */;
  792. default:
  793. return 0 /* INVALID */;
  794. }
  795. }
  796. function getTargetType(value) {
  797. return value["__v_skip"] || !Object.isExtensible(value) ? 0 /* INVALID */ : targetTypeMap(shared.toRawType(value));
  798. }
  799. function reactive(target) {
  800. if (isReadonly(target)) {
  801. return target;
  802. }
  803. return createReactiveObject(
  804. target,
  805. false,
  806. mutableHandlers,
  807. mutableCollectionHandlers,
  808. reactiveMap
  809. );
  810. }
  811. function shallowReactive(target) {
  812. return createReactiveObject(
  813. target,
  814. false,
  815. shallowReactiveHandlers,
  816. shallowCollectionHandlers,
  817. shallowReactiveMap
  818. );
  819. }
  820. function readonly(target) {
  821. return createReactiveObject(
  822. target,
  823. true,
  824. readonlyHandlers,
  825. readonlyCollectionHandlers,
  826. readonlyMap
  827. );
  828. }
  829. function shallowReadonly(target) {
  830. return createReactiveObject(
  831. target,
  832. true,
  833. shallowReadonlyHandlers,
  834. shallowReadonlyCollectionHandlers,
  835. shallowReadonlyMap
  836. );
  837. }
  838. function createReactiveObject(target, isReadonly2, baseHandlers, collectionHandlers, proxyMap) {
  839. if (!shared.isObject(target)) {
  840. return target;
  841. }
  842. if (target["__v_raw"] && !(isReadonly2 && target["__v_isReactive"])) {
  843. return target;
  844. }
  845. const existingProxy = proxyMap.get(target);
  846. if (existingProxy) {
  847. return existingProxy;
  848. }
  849. const targetType = getTargetType(target);
  850. if (targetType === 0 /* INVALID */) {
  851. return target;
  852. }
  853. const proxy = new Proxy(
  854. target,
  855. targetType === 2 /* COLLECTION */ ? collectionHandlers : baseHandlers
  856. );
  857. proxyMap.set(target, proxy);
  858. return proxy;
  859. }
  860. function isReactive(value) {
  861. if (isReadonly(value)) {
  862. return isReactive(value["__v_raw"]);
  863. }
  864. return !!(value && value["__v_isReactive"]);
  865. }
  866. function isReadonly(value) {
  867. return !!(value && value["__v_isReadonly"]);
  868. }
  869. function isShallow(value) {
  870. return !!(value && value["__v_isShallow"]);
  871. }
  872. function isProxy(value) {
  873. return isReactive(value) || isReadonly(value);
  874. }
  875. function toRaw(observed) {
  876. const raw = observed && observed["__v_raw"];
  877. return raw ? toRaw(raw) : observed;
  878. }
  879. function markRaw(value) {
  880. shared.def(value, "__v_skip", true);
  881. return value;
  882. }
  883. const toReactive = (value) => shared.isObject(value) ? reactive(value) : value;
  884. const toReadonly = (value) => shared.isObject(value) ? readonly(value) : value;
  885. function trackRefValue(ref2) {
  886. if (shouldTrack && activeEffect) {
  887. ref2 = toRaw(ref2);
  888. {
  889. trackEffects(ref2.dep || (ref2.dep = createDep()));
  890. }
  891. }
  892. }
  893. function triggerRefValue(ref2, newVal) {
  894. ref2 = toRaw(ref2);
  895. const dep = ref2.dep;
  896. if (dep) {
  897. {
  898. triggerEffects(dep);
  899. }
  900. }
  901. }
  902. function isRef(r) {
  903. return !!(r && r.__v_isRef === true);
  904. }
  905. function ref(value) {
  906. return createRef(value, false);
  907. }
  908. function shallowRef(value) {
  909. return createRef(value, true);
  910. }
  911. function createRef(rawValue, shallow) {
  912. if (isRef(rawValue)) {
  913. return rawValue;
  914. }
  915. return new RefImpl(rawValue, shallow);
  916. }
  917. class RefImpl {
  918. constructor(value, __v_isShallow) {
  919. this.__v_isShallow = __v_isShallow;
  920. this.dep = void 0;
  921. this.__v_isRef = true;
  922. this._rawValue = __v_isShallow ? value : toRaw(value);
  923. this._value = __v_isShallow ? value : toReactive(value);
  924. }
  925. get value() {
  926. trackRefValue(this);
  927. return this._value;
  928. }
  929. set value(newVal) {
  930. const useDirectValue = this.__v_isShallow || isShallow(newVal) || isReadonly(newVal);
  931. newVal = useDirectValue ? newVal : toRaw(newVal);
  932. if (shared.hasChanged(newVal, this._rawValue)) {
  933. this._rawValue = newVal;
  934. this._value = useDirectValue ? newVal : toReactive(newVal);
  935. triggerRefValue(this);
  936. }
  937. }
  938. }
  939. function triggerRef(ref2) {
  940. triggerRefValue(ref2);
  941. }
  942. function unref(ref2) {
  943. return isRef(ref2) ? ref2.value : ref2;
  944. }
  945. function toValue(source) {
  946. return shared.isFunction(source) ? source() : unref(source);
  947. }
  948. const shallowUnwrapHandlers = {
  949. get: (target, key, receiver) => unref(Reflect.get(target, key, receiver)),
  950. set: (target, key, value, receiver) => {
  951. const oldValue = target[key];
  952. if (isRef(oldValue) && !isRef(value)) {
  953. oldValue.value = value;
  954. return true;
  955. } else {
  956. return Reflect.set(target, key, value, receiver);
  957. }
  958. }
  959. };
  960. function proxyRefs(objectWithRefs) {
  961. return isReactive(objectWithRefs) ? objectWithRefs : new Proxy(objectWithRefs, shallowUnwrapHandlers);
  962. }
  963. class CustomRefImpl {
  964. constructor(factory) {
  965. this.dep = void 0;
  966. this.__v_isRef = true;
  967. const { get, set } = factory(
  968. () => trackRefValue(this),
  969. () => triggerRefValue(this)
  970. );
  971. this._get = get;
  972. this._set = set;
  973. }
  974. get value() {
  975. return this._get();
  976. }
  977. set value(newVal) {
  978. this._set(newVal);
  979. }
  980. }
  981. function customRef(factory) {
  982. return new CustomRefImpl(factory);
  983. }
  984. function toRefs(object) {
  985. const ret = shared.isArray(object) ? new Array(object.length) : {};
  986. for (const key in object) {
  987. ret[key] = propertyToRef(object, key);
  988. }
  989. return ret;
  990. }
  991. class ObjectRefImpl {
  992. constructor(_object, _key, _defaultValue) {
  993. this._object = _object;
  994. this._key = _key;
  995. this._defaultValue = _defaultValue;
  996. this.__v_isRef = true;
  997. }
  998. get value() {
  999. const val = this._object[this._key];
  1000. return val === void 0 ? this._defaultValue : val;
  1001. }
  1002. set value(newVal) {
  1003. this._object[this._key] = newVal;
  1004. }
  1005. get dep() {
  1006. return getDepFromReactive(toRaw(this._object), this._key);
  1007. }
  1008. }
  1009. class GetterRefImpl {
  1010. constructor(_getter) {
  1011. this._getter = _getter;
  1012. this.__v_isRef = true;
  1013. this.__v_isReadonly = true;
  1014. }
  1015. get value() {
  1016. return this._getter();
  1017. }
  1018. }
  1019. function toRef(source, key, defaultValue) {
  1020. if (isRef(source)) {
  1021. return source;
  1022. } else if (shared.isFunction(source)) {
  1023. return new GetterRefImpl(source);
  1024. } else if (shared.isObject(source) && arguments.length > 1) {
  1025. return propertyToRef(source, key, defaultValue);
  1026. } else {
  1027. return ref(source);
  1028. }
  1029. }
  1030. function propertyToRef(source, key, defaultValue) {
  1031. const val = source[key];
  1032. return isRef(val) ? val : new ObjectRefImpl(
  1033. source,
  1034. key,
  1035. defaultValue
  1036. );
  1037. }
  1038. class ComputedRefImpl {
  1039. constructor(getter, _setter, isReadonly, isSSR) {
  1040. this._setter = _setter;
  1041. this.dep = void 0;
  1042. this.__v_isRef = true;
  1043. this["__v_isReadonly"] = false;
  1044. this._dirty = true;
  1045. this.effect = new ReactiveEffect(getter, () => {
  1046. if (!this._dirty) {
  1047. this._dirty = true;
  1048. triggerRefValue(this);
  1049. }
  1050. });
  1051. this.effect.computed = this;
  1052. this.effect.active = this._cacheable = !isSSR;
  1053. this["__v_isReadonly"] = isReadonly;
  1054. }
  1055. get value() {
  1056. const self = toRaw(this);
  1057. trackRefValue(self);
  1058. if (self._dirty || !self._cacheable) {
  1059. self._dirty = false;
  1060. self._value = self.effect.run();
  1061. }
  1062. return self._value;
  1063. }
  1064. set value(newValue) {
  1065. this._setter(newValue);
  1066. }
  1067. }
  1068. function computed(getterOrOptions, debugOptions, isSSR = false) {
  1069. let getter;
  1070. let setter;
  1071. const onlyGetter = shared.isFunction(getterOrOptions);
  1072. if (onlyGetter) {
  1073. getter = getterOrOptions;
  1074. setter = shared.NOOP;
  1075. } else {
  1076. getter = getterOrOptions.get;
  1077. setter = getterOrOptions.set;
  1078. }
  1079. const cRef = new ComputedRefImpl(getter, setter, onlyGetter || !setter, isSSR);
  1080. return cRef;
  1081. }
  1082. const tick = /* @__PURE__ */ Promise.resolve();
  1083. const queue = [];
  1084. let queued = false;
  1085. const scheduler = (fn) => {
  1086. queue.push(fn);
  1087. if (!queued) {
  1088. queued = true;
  1089. tick.then(flush);
  1090. }
  1091. };
  1092. const flush = () => {
  1093. for (let i = 0; i < queue.length; i++) {
  1094. queue[i]();
  1095. }
  1096. queue.length = 0;
  1097. queued = false;
  1098. };
  1099. class DeferredComputedRefImpl {
  1100. constructor(getter) {
  1101. this.dep = void 0;
  1102. this._dirty = true;
  1103. this.__v_isRef = true;
  1104. this["__v_isReadonly"] = true;
  1105. let compareTarget;
  1106. let hasCompareTarget = false;
  1107. let scheduled = false;
  1108. this.effect = new ReactiveEffect(getter, (computedTrigger) => {
  1109. if (this.dep) {
  1110. if (computedTrigger) {
  1111. compareTarget = this._value;
  1112. hasCompareTarget = true;
  1113. } else if (!scheduled) {
  1114. const valueToCompare = hasCompareTarget ? compareTarget : this._value;
  1115. scheduled = true;
  1116. hasCompareTarget = false;
  1117. scheduler(() => {
  1118. if (this.effect.active && this._get() !== valueToCompare) {
  1119. triggerRefValue(this);
  1120. }
  1121. scheduled = false;
  1122. });
  1123. }
  1124. for (const e of this.dep) {
  1125. if (e.computed instanceof DeferredComputedRefImpl) {
  1126. e.scheduler(
  1127. true
  1128. /* computedTrigger */
  1129. );
  1130. }
  1131. }
  1132. }
  1133. this._dirty = true;
  1134. });
  1135. this.effect.computed = this;
  1136. }
  1137. _get() {
  1138. if (this._dirty) {
  1139. this._dirty = false;
  1140. return this._value = this.effect.run();
  1141. }
  1142. return this._value;
  1143. }
  1144. get value() {
  1145. trackRefValue(this);
  1146. return toRaw(this)._get();
  1147. }
  1148. }
  1149. function deferredComputed(getter) {
  1150. return new DeferredComputedRefImpl(getter);
  1151. }
  1152. exports.EffectScope = EffectScope;
  1153. exports.ITERATE_KEY = ITERATE_KEY;
  1154. exports.ReactiveEffect = ReactiveEffect;
  1155. exports.computed = computed;
  1156. exports.customRef = customRef;
  1157. exports.deferredComputed = deferredComputed;
  1158. exports.effect = effect;
  1159. exports.effectScope = effectScope;
  1160. exports.enableTracking = enableTracking;
  1161. exports.getCurrentScope = getCurrentScope;
  1162. exports.isProxy = isProxy;
  1163. exports.isReactive = isReactive;
  1164. exports.isReadonly = isReadonly;
  1165. exports.isRef = isRef;
  1166. exports.isShallow = isShallow;
  1167. exports.markRaw = markRaw;
  1168. exports.onScopeDispose = onScopeDispose;
  1169. exports.pauseTracking = pauseTracking;
  1170. exports.proxyRefs = proxyRefs;
  1171. exports.reactive = reactive;
  1172. exports.readonly = readonly;
  1173. exports.ref = ref;
  1174. exports.resetTracking = resetTracking;
  1175. exports.shallowReactive = shallowReactive;
  1176. exports.shallowReadonly = shallowReadonly;
  1177. exports.shallowRef = shallowRef;
  1178. exports.stop = stop;
  1179. exports.toRaw = toRaw;
  1180. exports.toRef = toRef;
  1181. exports.toRefs = toRefs;
  1182. exports.toValue = toValue;
  1183. exports.track = track;
  1184. exports.trigger = trigger;
  1185. exports.triggerRef = triggerRef;
  1186. exports.unref = unref;