reactivity.esm-bundler.js 33 KB

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