reactivity.cjs.js 33 KB

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