reactivity.cjs.js 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  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) {
  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" || 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 get$1 = /* @__PURE__ */ createGetter();
  382. const shallowGet = /* @__PURE__ */ createGetter(false, true);
  383. const readonlyGet = /* @__PURE__ */ createGetter(true);
  384. const shallowReadonlyGet = /* @__PURE__ */ createGetter(true, true);
  385. const arrayInstrumentations = /* @__PURE__ */ createArrayInstrumentations();
  386. function createArrayInstrumentations() {
  387. const instrumentations = {};
  388. ["includes", "indexOf", "lastIndexOf"].forEach((key) => {
  389. instrumentations[key] = function(...args) {
  390. const arr = toRaw(this);
  391. for (let i = 0, l = this.length; i < l; i++) {
  392. track(arr, "get", i + "");
  393. }
  394. const res = arr[key](...args);
  395. if (res === -1 || res === false) {
  396. return arr[key](...args.map(toRaw));
  397. } else {
  398. return res;
  399. }
  400. };
  401. });
  402. ["push", "pop", "shift", "unshift", "splice"].forEach((key) => {
  403. instrumentations[key] = function(...args) {
  404. pauseTracking();
  405. const res = toRaw(this)[key].apply(this, args);
  406. resetTracking();
  407. return res;
  408. };
  409. });
  410. return instrumentations;
  411. }
  412. function hasOwnProperty(key) {
  413. const obj = toRaw(this);
  414. track(obj, "has", key);
  415. return obj.hasOwnProperty(key);
  416. }
  417. function createGetter(isReadonly2 = false, shallow = false) {
  418. return function get2(target, key, receiver) {
  419. if (key === "__v_isReactive") {
  420. return !isReadonly2;
  421. } else if (key === "__v_isReadonly") {
  422. return isReadonly2;
  423. } else if (key === "__v_isShallow") {
  424. return shallow;
  425. } else if (key === "__v_raw" && receiver === (isReadonly2 ? shallow ? shallowReadonlyMap : readonlyMap : shallow ? shallowReactiveMap : reactiveMap).get(target)) {
  426. return target;
  427. }
  428. const targetIsArray = shared.isArray(target);
  429. if (!isReadonly2) {
  430. if (targetIsArray && shared.hasOwn(arrayInstrumentations, key)) {
  431. return Reflect.get(arrayInstrumentations, key, receiver);
  432. }
  433. if (key === "hasOwnProperty") {
  434. return hasOwnProperty;
  435. }
  436. }
  437. const res = Reflect.get(target, key, receiver);
  438. if (shared.isSymbol(key) ? builtInSymbols.has(key) : isNonTrackableKeys(key)) {
  439. return res;
  440. }
  441. if (!isReadonly2) {
  442. track(target, "get", key);
  443. }
  444. if (shallow) {
  445. return res;
  446. }
  447. if (isRef(res)) {
  448. return targetIsArray && shared.isIntegerKey(key) ? res : res.value;
  449. }
  450. if (shared.isObject(res)) {
  451. return isReadonly2 ? readonly(res) : reactive(res);
  452. }
  453. return res;
  454. };
  455. }
  456. const set$1 = /* @__PURE__ */ createSetter();
  457. const shallowSet = /* @__PURE__ */ createSetter(true);
  458. function createSetter(shallow = false) {
  459. return function set2(target, key, value, receiver) {
  460. let oldValue = target[key];
  461. if (isReadonly(oldValue) && isRef(oldValue) && !isRef(value)) {
  462. return false;
  463. }
  464. if (!shallow) {
  465. if (!isShallow(value) && !isReadonly(value)) {
  466. oldValue = toRaw(oldValue);
  467. value = toRaw(value);
  468. }
  469. if (!shared.isArray(target) && isRef(oldValue) && !isRef(value)) {
  470. oldValue.value = value;
  471. return true;
  472. }
  473. }
  474. const hadKey = shared.isArray(target) && shared.isIntegerKey(key) ? Number(key) < target.length : shared.hasOwn(target, key);
  475. const result = Reflect.set(target, key, value, receiver);
  476. if (target === toRaw(receiver)) {
  477. if (!hadKey) {
  478. trigger(target, "add", key, value);
  479. } else if (shared.hasChanged(value, oldValue)) {
  480. trigger(target, "set", key, value, oldValue);
  481. }
  482. }
  483. return result;
  484. };
  485. }
  486. function deleteProperty(target, key) {
  487. const hadKey = shared.hasOwn(target, key);
  488. const oldValue = target[key];
  489. const result = Reflect.deleteProperty(target, key);
  490. if (result && hadKey) {
  491. trigger(target, "delete", key, void 0, oldValue);
  492. }
  493. return result;
  494. }
  495. function has$1(target, key) {
  496. const result = Reflect.has(target, key);
  497. if (!shared.isSymbol(key) || !builtInSymbols.has(key)) {
  498. track(target, "has", key);
  499. }
  500. return result;
  501. }
  502. function ownKeys(target) {
  503. track(target, "iterate", shared.isArray(target) ? "length" : ITERATE_KEY);
  504. return Reflect.ownKeys(target);
  505. }
  506. const mutableHandlers = {
  507. get: get$1,
  508. set: set$1,
  509. deleteProperty,
  510. has: has$1,
  511. ownKeys
  512. };
  513. const readonlyHandlers = {
  514. get: readonlyGet,
  515. set(target, key) {
  516. {
  517. warn(
  518. `Set operation on key "${String(key)}" failed: target is readonly.`,
  519. target
  520. );
  521. }
  522. return true;
  523. },
  524. deleteProperty(target, key) {
  525. {
  526. warn(
  527. `Delete operation on key "${String(key)}" failed: target is readonly.`,
  528. target
  529. );
  530. }
  531. return true;
  532. }
  533. };
  534. const shallowReactiveHandlers = /* @__PURE__ */ shared.extend(
  535. {},
  536. mutableHandlers,
  537. {
  538. get: shallowGet,
  539. set: shallowSet
  540. }
  541. );
  542. const shallowReadonlyHandlers = /* @__PURE__ */ shared.extend(
  543. {},
  544. readonlyHandlers,
  545. {
  546. get: shallowReadonlyGet
  547. }
  548. );
  549. const toShallow = (value) => value;
  550. const getProto = (v) => Reflect.getPrototypeOf(v);
  551. function get(target, key, isReadonly = false, isShallow = false) {
  552. target = target["__v_raw"];
  553. const rawTarget = toRaw(target);
  554. const rawKey = toRaw(key);
  555. if (!isReadonly) {
  556. if (key !== rawKey) {
  557. track(rawTarget, "get", key);
  558. }
  559. track(rawTarget, "get", rawKey);
  560. }
  561. const { has: has2 } = getProto(rawTarget);
  562. const wrap = isShallow ? toShallow : isReadonly ? toReadonly : toReactive;
  563. if (has2.call(rawTarget, key)) {
  564. return wrap(target.get(key));
  565. } else if (has2.call(rawTarget, rawKey)) {
  566. return wrap(target.get(rawKey));
  567. } else if (target !== rawTarget) {
  568. target.get(key);
  569. }
  570. }
  571. function has(key, isReadonly = false) {
  572. const target = this["__v_raw"];
  573. const rawTarget = toRaw(target);
  574. const rawKey = toRaw(key);
  575. if (!isReadonly) {
  576. if (key !== rawKey) {
  577. track(rawTarget, "has", key);
  578. }
  579. track(rawTarget, "has", rawKey);
  580. }
  581. return key === rawKey ? target.has(key) : target.has(key) || target.has(rawKey);
  582. }
  583. function size(target, isReadonly = false) {
  584. target = target["__v_raw"];
  585. !isReadonly && track(toRaw(target), "iterate", ITERATE_KEY);
  586. return Reflect.get(target, "size", target);
  587. }
  588. function add(value) {
  589. value = toRaw(value);
  590. const target = toRaw(this);
  591. const proto = getProto(target);
  592. const hadKey = proto.has.call(target, value);
  593. if (!hadKey) {
  594. target.add(value);
  595. trigger(target, "add", value, value);
  596. }
  597. return this;
  598. }
  599. function set(key, value) {
  600. value = toRaw(value);
  601. const target = toRaw(this);
  602. const { has: has2, get: get2 } = getProto(target);
  603. let hadKey = has2.call(target, key);
  604. if (!hadKey) {
  605. key = toRaw(key);
  606. hadKey = has2.call(target, key);
  607. } else {
  608. checkIdentityKeys(target, has2, key);
  609. }
  610. const oldValue = get2.call(target, key);
  611. target.set(key, value);
  612. if (!hadKey) {
  613. trigger(target, "add", key, value);
  614. } else if (shared.hasChanged(value, oldValue)) {
  615. trigger(target, "set", key, value, oldValue);
  616. }
  617. return this;
  618. }
  619. function deleteEntry(key) {
  620. const target = toRaw(this);
  621. const { has: has2, get: get2 } = getProto(target);
  622. let hadKey = has2.call(target, key);
  623. if (!hadKey) {
  624. key = toRaw(key);
  625. hadKey = has2.call(target, key);
  626. } else {
  627. checkIdentityKeys(target, has2, key);
  628. }
  629. const oldValue = get2 ? get2.call(target, key) : void 0;
  630. const result = target.delete(key);
  631. if (hadKey) {
  632. trigger(target, "delete", key, void 0, oldValue);
  633. }
  634. return result;
  635. }
  636. function clear() {
  637. const target = toRaw(this);
  638. const hadItems = target.size !== 0;
  639. const oldTarget = shared.isMap(target) ? new Map(target) : new Set(target) ;
  640. const result = target.clear();
  641. if (hadItems) {
  642. trigger(target, "clear", void 0, void 0, oldTarget);
  643. }
  644. return result;
  645. }
  646. function createForEach(isReadonly, isShallow) {
  647. return function forEach(callback, thisArg) {
  648. const observed = this;
  649. const target = observed["__v_raw"];
  650. const rawTarget = toRaw(target);
  651. const wrap = isShallow ? toShallow : isReadonly ? toReadonly : toReactive;
  652. !isReadonly && track(rawTarget, "iterate", ITERATE_KEY);
  653. return target.forEach((value, key) => {
  654. return callback.call(thisArg, wrap(value), wrap(key), observed);
  655. });
  656. };
  657. }
  658. function createIterableMethod(method, isReadonly, isShallow) {
  659. return function(...args) {
  660. const target = this["__v_raw"];
  661. const rawTarget = toRaw(target);
  662. const targetIsMap = shared.isMap(rawTarget);
  663. const isPair = method === "entries" || method === Symbol.iterator && targetIsMap;
  664. const isKeyOnly = method === "keys" && targetIsMap;
  665. const innerIterator = target[method](...args);
  666. const wrap = isShallow ? toShallow : isReadonly ? toReadonly : toReactive;
  667. !isReadonly && track(
  668. rawTarget,
  669. "iterate",
  670. isKeyOnly ? MAP_KEY_ITERATE_KEY : ITERATE_KEY
  671. );
  672. return {
  673. // iterator protocol
  674. next() {
  675. const { value, done } = innerIterator.next();
  676. return done ? { value, done } : {
  677. value: isPair ? [wrap(value[0]), wrap(value[1])] : wrap(value),
  678. done
  679. };
  680. },
  681. // iterable protocol
  682. [Symbol.iterator]() {
  683. return this;
  684. }
  685. };
  686. };
  687. }
  688. function createReadonlyMethod(type) {
  689. return function(...args) {
  690. {
  691. const key = args[0] ? `on key "${args[0]}" ` : ``;
  692. console.warn(
  693. `${shared.capitalize(type)} operation ${key}failed: target is readonly.`,
  694. toRaw(this)
  695. );
  696. }
  697. return type === "delete" ? false : this;
  698. };
  699. }
  700. function createInstrumentations() {
  701. const mutableInstrumentations2 = {
  702. get(key) {
  703. return get(this, key);
  704. },
  705. get size() {
  706. return size(this);
  707. },
  708. has,
  709. add,
  710. set,
  711. delete: deleteEntry,
  712. clear,
  713. forEach: createForEach(false, false)
  714. };
  715. const shallowInstrumentations2 = {
  716. get(key) {
  717. return get(this, key, false, true);
  718. },
  719. get size() {
  720. return size(this);
  721. },
  722. has,
  723. add,
  724. set,
  725. delete: deleteEntry,
  726. clear,
  727. forEach: createForEach(false, true)
  728. };
  729. const readonlyInstrumentations2 = {
  730. get(key) {
  731. return get(this, key, true);
  732. },
  733. get size() {
  734. return size(this, true);
  735. },
  736. has(key) {
  737. return has.call(this, key, true);
  738. },
  739. add: createReadonlyMethod("add"),
  740. set: createReadonlyMethod("set"),
  741. delete: createReadonlyMethod("delete"),
  742. clear: createReadonlyMethod("clear"),
  743. forEach: createForEach(true, false)
  744. };
  745. const shallowReadonlyInstrumentations2 = {
  746. get(key) {
  747. return get(this, key, true, true);
  748. },
  749. get size() {
  750. return size(this, true);
  751. },
  752. has(key) {
  753. return has.call(this, key, true);
  754. },
  755. add: createReadonlyMethod("add"),
  756. set: createReadonlyMethod("set"),
  757. delete: createReadonlyMethod("delete"),
  758. clear: createReadonlyMethod("clear"),
  759. forEach: createForEach(true, true)
  760. };
  761. const iteratorMethods = ["keys", "values", "entries", Symbol.iterator];
  762. iteratorMethods.forEach((method) => {
  763. mutableInstrumentations2[method] = createIterableMethod(
  764. method,
  765. false,
  766. false
  767. );
  768. readonlyInstrumentations2[method] = createIterableMethod(
  769. method,
  770. true,
  771. false
  772. );
  773. shallowInstrumentations2[method] = createIterableMethod(
  774. method,
  775. false,
  776. true
  777. );
  778. shallowReadonlyInstrumentations2[method] = createIterableMethod(
  779. method,
  780. true,
  781. true
  782. );
  783. });
  784. return [
  785. mutableInstrumentations2,
  786. readonlyInstrumentations2,
  787. shallowInstrumentations2,
  788. shallowReadonlyInstrumentations2
  789. ];
  790. }
  791. const [
  792. mutableInstrumentations,
  793. readonlyInstrumentations,
  794. shallowInstrumentations,
  795. shallowReadonlyInstrumentations
  796. ] = /* @__PURE__ */ createInstrumentations();
  797. function createInstrumentationGetter(isReadonly, shallow) {
  798. const instrumentations = shallow ? isReadonly ? shallowReadonlyInstrumentations : shallowInstrumentations : isReadonly ? readonlyInstrumentations : mutableInstrumentations;
  799. return (target, key, receiver) => {
  800. if (key === "__v_isReactive") {
  801. return !isReadonly;
  802. } else if (key === "__v_isReadonly") {
  803. return isReadonly;
  804. } else if (key === "__v_raw") {
  805. return target;
  806. }
  807. return Reflect.get(
  808. shared.hasOwn(instrumentations, key) && key in target ? instrumentations : target,
  809. key,
  810. receiver
  811. );
  812. };
  813. }
  814. const mutableCollectionHandlers = {
  815. get: /* @__PURE__ */ createInstrumentationGetter(false, false)
  816. };
  817. const shallowCollectionHandlers = {
  818. get: /* @__PURE__ */ createInstrumentationGetter(false, true)
  819. };
  820. const readonlyCollectionHandlers = {
  821. get: /* @__PURE__ */ createInstrumentationGetter(true, false)
  822. };
  823. const shallowReadonlyCollectionHandlers = {
  824. get: /* @__PURE__ */ createInstrumentationGetter(true, true)
  825. };
  826. function checkIdentityKeys(target, has2, key) {
  827. const rawKey = toRaw(key);
  828. if (rawKey !== key && has2.call(target, rawKey)) {
  829. const type = shared.toRawType(target);
  830. console.warn(
  831. `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.`
  832. );
  833. }
  834. }
  835. const reactiveMap = /* @__PURE__ */ new WeakMap();
  836. const shallowReactiveMap = /* @__PURE__ */ new WeakMap();
  837. const readonlyMap = /* @__PURE__ */ new WeakMap();
  838. const shallowReadonlyMap = /* @__PURE__ */ new WeakMap();
  839. function targetTypeMap(rawType) {
  840. switch (rawType) {
  841. case "Object":
  842. case "Array":
  843. return 1 /* COMMON */;
  844. case "Map":
  845. case "Set":
  846. case "WeakMap":
  847. case "WeakSet":
  848. return 2 /* COLLECTION */;
  849. default:
  850. return 0 /* INVALID */;
  851. }
  852. }
  853. function getTargetType(value) {
  854. return value["__v_skip"] || !Object.isExtensible(value) ? 0 /* INVALID */ : targetTypeMap(shared.toRawType(value));
  855. }
  856. function reactive(target) {
  857. if (isReadonly(target)) {
  858. return target;
  859. }
  860. return createReactiveObject(
  861. target,
  862. false,
  863. mutableHandlers,
  864. mutableCollectionHandlers,
  865. reactiveMap
  866. );
  867. }
  868. function shallowReactive(target) {
  869. return createReactiveObject(
  870. target,
  871. false,
  872. shallowReactiveHandlers,
  873. shallowCollectionHandlers,
  874. shallowReactiveMap
  875. );
  876. }
  877. function readonly(target) {
  878. return createReactiveObject(
  879. target,
  880. true,
  881. readonlyHandlers,
  882. readonlyCollectionHandlers,
  883. readonlyMap
  884. );
  885. }
  886. function shallowReadonly(target) {
  887. return createReactiveObject(
  888. target,
  889. true,
  890. shallowReadonlyHandlers,
  891. shallowReadonlyCollectionHandlers,
  892. shallowReadonlyMap
  893. );
  894. }
  895. function createReactiveObject(target, isReadonly2, baseHandlers, collectionHandlers, proxyMap) {
  896. if (!shared.isObject(target)) {
  897. {
  898. console.warn(`value cannot be made reactive: ${String(target)}`);
  899. }
  900. return target;
  901. }
  902. if (target["__v_raw"] && !(isReadonly2 && target["__v_isReactive"])) {
  903. return target;
  904. }
  905. const existingProxy = proxyMap.get(target);
  906. if (existingProxy) {
  907. return existingProxy;
  908. }
  909. const targetType = getTargetType(target);
  910. if (targetType === 0 /* INVALID */) {
  911. return target;
  912. }
  913. const proxy = new Proxy(
  914. target,
  915. targetType === 2 /* COLLECTION */ ? collectionHandlers : baseHandlers
  916. );
  917. proxyMap.set(target, proxy);
  918. return proxy;
  919. }
  920. function isReactive(value) {
  921. if (isReadonly(value)) {
  922. return isReactive(value["__v_raw"]);
  923. }
  924. return !!(value && value["__v_isReactive"]);
  925. }
  926. function isReadonly(value) {
  927. return !!(value && value["__v_isReadonly"]);
  928. }
  929. function isShallow(value) {
  930. return !!(value && value["__v_isShallow"]);
  931. }
  932. function isProxy(value) {
  933. return isReactive(value) || isReadonly(value);
  934. }
  935. function toRaw(observed) {
  936. const raw = observed && observed["__v_raw"];
  937. return raw ? toRaw(raw) : observed;
  938. }
  939. function markRaw(value) {
  940. shared.def(value, "__v_skip", true);
  941. return value;
  942. }
  943. const toReactive = (value) => shared.isObject(value) ? reactive(value) : value;
  944. const toReadonly = (value) => shared.isObject(value) ? readonly(value) : value;
  945. function trackRefValue(ref2) {
  946. if (shouldTrack && activeEffect) {
  947. ref2 = toRaw(ref2);
  948. {
  949. trackEffects(ref2.dep || (ref2.dep = createDep()), {
  950. target: ref2,
  951. type: "get",
  952. key: "value"
  953. });
  954. }
  955. }
  956. }
  957. function triggerRefValue(ref2, newVal) {
  958. ref2 = toRaw(ref2);
  959. const dep = ref2.dep;
  960. if (dep) {
  961. {
  962. triggerEffects(dep, {
  963. target: ref2,
  964. type: "set",
  965. key: "value",
  966. newValue: newVal
  967. });
  968. }
  969. }
  970. }
  971. function isRef(r) {
  972. return !!(r && r.__v_isRef === true);
  973. }
  974. function ref(value) {
  975. return createRef(value, false);
  976. }
  977. function shallowRef(value) {
  978. return createRef(value, true);
  979. }
  980. function createRef(rawValue, shallow) {
  981. if (isRef(rawValue)) {
  982. return rawValue;
  983. }
  984. return new RefImpl(rawValue, shallow);
  985. }
  986. class RefImpl {
  987. constructor(value, __v_isShallow) {
  988. this.__v_isShallow = __v_isShallow;
  989. this.dep = void 0;
  990. this.__v_isRef = true;
  991. this._rawValue = __v_isShallow ? value : toRaw(value);
  992. this._value = __v_isShallow ? value : toReactive(value);
  993. }
  994. get value() {
  995. trackRefValue(this);
  996. return this._value;
  997. }
  998. set value(newVal) {
  999. const useDirectValue = this.__v_isShallow || isShallow(newVal) || isReadonly(newVal);
  1000. newVal = useDirectValue ? newVal : toRaw(newVal);
  1001. if (shared.hasChanged(newVal, this._rawValue)) {
  1002. this._rawValue = newVal;
  1003. this._value = useDirectValue ? newVal : toReactive(newVal);
  1004. triggerRefValue(this, newVal);
  1005. }
  1006. }
  1007. }
  1008. function triggerRef(ref2) {
  1009. triggerRefValue(ref2, ref2.value );
  1010. }
  1011. function unref(ref2) {
  1012. return isRef(ref2) ? ref2.value : ref2;
  1013. }
  1014. function toValue(source) {
  1015. return shared.isFunction(source) ? source() : unref(source);
  1016. }
  1017. const shallowUnwrapHandlers = {
  1018. get: (target, key, receiver) => unref(Reflect.get(target, key, receiver)),
  1019. set: (target, key, value, receiver) => {
  1020. const oldValue = target[key];
  1021. if (isRef(oldValue) && !isRef(value)) {
  1022. oldValue.value = value;
  1023. return true;
  1024. } else {
  1025. return Reflect.set(target, key, value, receiver);
  1026. }
  1027. }
  1028. };
  1029. function proxyRefs(objectWithRefs) {
  1030. return isReactive(objectWithRefs) ? objectWithRefs : new Proxy(objectWithRefs, shallowUnwrapHandlers);
  1031. }
  1032. class CustomRefImpl {
  1033. constructor(factory) {
  1034. this.dep = void 0;
  1035. this.__v_isRef = true;
  1036. const { get, set } = factory(
  1037. () => trackRefValue(this),
  1038. () => triggerRefValue(this)
  1039. );
  1040. this._get = get;
  1041. this._set = set;
  1042. }
  1043. get value() {
  1044. return this._get();
  1045. }
  1046. set value(newVal) {
  1047. this._set(newVal);
  1048. }
  1049. }
  1050. function customRef(factory) {
  1051. return new CustomRefImpl(factory);
  1052. }
  1053. function toRefs(object) {
  1054. if (!isProxy(object)) {
  1055. console.warn(`toRefs() expects a reactive object but received a plain one.`);
  1056. }
  1057. const ret = shared.isArray(object) ? new Array(object.length) : {};
  1058. for (const key in object) {
  1059. ret[key] = propertyToRef(object, key);
  1060. }
  1061. return ret;
  1062. }
  1063. class ObjectRefImpl {
  1064. constructor(_object, _key, _defaultValue) {
  1065. this._object = _object;
  1066. this._key = _key;
  1067. this._defaultValue = _defaultValue;
  1068. this.__v_isRef = true;
  1069. }
  1070. get value() {
  1071. const val = this._object[this._key];
  1072. return val === void 0 ? this._defaultValue : val;
  1073. }
  1074. set value(newVal) {
  1075. this._object[this._key] = newVal;
  1076. }
  1077. get dep() {
  1078. return getDepFromReactive(toRaw(this._object), this._key);
  1079. }
  1080. }
  1081. class GetterRefImpl {
  1082. constructor(_getter) {
  1083. this._getter = _getter;
  1084. this.__v_isRef = true;
  1085. this.__v_isReadonly = true;
  1086. }
  1087. get value() {
  1088. return this._getter();
  1089. }
  1090. }
  1091. function toRef(source, key, defaultValue) {
  1092. if (isRef(source)) {
  1093. return source;
  1094. } else if (shared.isFunction(source)) {
  1095. return new GetterRefImpl(source);
  1096. } else if (shared.isObject(source) && arguments.length > 1) {
  1097. return propertyToRef(source, key, defaultValue);
  1098. } else {
  1099. return ref(source);
  1100. }
  1101. }
  1102. function propertyToRef(source, key, defaultValue) {
  1103. const val = source[key];
  1104. return isRef(val) ? val : new ObjectRefImpl(
  1105. source,
  1106. key,
  1107. defaultValue
  1108. );
  1109. }
  1110. class ComputedRefImpl {
  1111. constructor(getter, _setter, isReadonly, isSSR) {
  1112. this._setter = _setter;
  1113. this.dep = void 0;
  1114. this.__v_isRef = true;
  1115. this["__v_isReadonly"] = false;
  1116. this._dirty = true;
  1117. this.effect = new ReactiveEffect(getter, () => {
  1118. if (!this._dirty) {
  1119. this._dirty = true;
  1120. triggerRefValue(this);
  1121. }
  1122. });
  1123. this.effect.computed = this;
  1124. this.effect.active = this._cacheable = !isSSR;
  1125. this["__v_isReadonly"] = isReadonly;
  1126. }
  1127. get value() {
  1128. const self = toRaw(this);
  1129. trackRefValue(self);
  1130. if (self._dirty || !self._cacheable) {
  1131. self._dirty = false;
  1132. self._value = self.effect.run();
  1133. }
  1134. return self._value;
  1135. }
  1136. set value(newValue) {
  1137. this._setter(newValue);
  1138. }
  1139. }
  1140. function computed(getterOrOptions, debugOptions, isSSR = false) {
  1141. let getter;
  1142. let setter;
  1143. const onlyGetter = shared.isFunction(getterOrOptions);
  1144. if (onlyGetter) {
  1145. getter = getterOrOptions;
  1146. setter = () => {
  1147. console.warn("Write operation failed: computed value is readonly");
  1148. } ;
  1149. } else {
  1150. getter = getterOrOptions.get;
  1151. setter = getterOrOptions.set;
  1152. }
  1153. const cRef = new ComputedRefImpl(getter, setter, onlyGetter || !setter, isSSR);
  1154. if (debugOptions && !isSSR) {
  1155. cRef.effect.onTrack = debugOptions.onTrack;
  1156. cRef.effect.onTrigger = debugOptions.onTrigger;
  1157. }
  1158. return cRef;
  1159. }
  1160. const tick = /* @__PURE__ */ Promise.resolve();
  1161. const queue = [];
  1162. let queued = false;
  1163. const scheduler = (fn) => {
  1164. queue.push(fn);
  1165. if (!queued) {
  1166. queued = true;
  1167. tick.then(flush);
  1168. }
  1169. };
  1170. const flush = () => {
  1171. for (let i = 0; i < queue.length; i++) {
  1172. queue[i]();
  1173. }
  1174. queue.length = 0;
  1175. queued = false;
  1176. };
  1177. class DeferredComputedRefImpl {
  1178. constructor(getter) {
  1179. this.dep = void 0;
  1180. this._dirty = true;
  1181. this.__v_isRef = true;
  1182. this["__v_isReadonly"] = true;
  1183. let compareTarget;
  1184. let hasCompareTarget = false;
  1185. let scheduled = false;
  1186. this.effect = new ReactiveEffect(getter, (computedTrigger) => {
  1187. if (this.dep) {
  1188. if (computedTrigger) {
  1189. compareTarget = this._value;
  1190. hasCompareTarget = true;
  1191. } else if (!scheduled) {
  1192. const valueToCompare = hasCompareTarget ? compareTarget : this._value;
  1193. scheduled = true;
  1194. hasCompareTarget = false;
  1195. scheduler(() => {
  1196. if (this.effect.active && this._get() !== valueToCompare) {
  1197. triggerRefValue(this);
  1198. }
  1199. scheduled = false;
  1200. });
  1201. }
  1202. for (const e of this.dep) {
  1203. if (e.computed instanceof DeferredComputedRefImpl) {
  1204. e.scheduler(
  1205. true
  1206. /* computedTrigger */
  1207. );
  1208. }
  1209. }
  1210. }
  1211. this._dirty = true;
  1212. });
  1213. this.effect.computed = this;
  1214. }
  1215. _get() {
  1216. if (this._dirty) {
  1217. this._dirty = false;
  1218. return this._value = this.effect.run();
  1219. }
  1220. return this._value;
  1221. }
  1222. get value() {
  1223. trackRefValue(this);
  1224. return toRaw(this)._get();
  1225. }
  1226. }
  1227. function deferredComputed(getter) {
  1228. return new DeferredComputedRefImpl(getter);
  1229. }
  1230. exports.EffectScope = EffectScope;
  1231. exports.ITERATE_KEY = ITERATE_KEY;
  1232. exports.ReactiveEffect = ReactiveEffect;
  1233. exports.computed = computed;
  1234. exports.customRef = customRef;
  1235. exports.deferredComputed = deferredComputed;
  1236. exports.effect = effect;
  1237. exports.effectScope = effectScope;
  1238. exports.enableTracking = enableTracking;
  1239. exports.getCurrentScope = getCurrentScope;
  1240. exports.isProxy = isProxy;
  1241. exports.isReactive = isReactive;
  1242. exports.isReadonly = isReadonly;
  1243. exports.isRef = isRef;
  1244. exports.isShallow = isShallow;
  1245. exports.markRaw = markRaw;
  1246. exports.onScopeDispose = onScopeDispose;
  1247. exports.pauseTracking = pauseTracking;
  1248. exports.proxyRefs = proxyRefs;
  1249. exports.reactive = reactive;
  1250. exports.readonly = readonly;
  1251. exports.ref = ref;
  1252. exports.resetTracking = resetTracking;
  1253. exports.shallowReactive = shallowReactive;
  1254. exports.shallowReadonly = shallowReadonly;
  1255. exports.shallowRef = shallowRef;
  1256. exports.stop = stop;
  1257. exports.toRaw = toRaw;
  1258. exports.toRef = toRef;
  1259. exports.toRefs = toRefs;
  1260. exports.toValue = toValue;
  1261. exports.track = track;
  1262. exports.trigger = trigger;
  1263. exports.triggerRef = triggerRef;
  1264. exports.unref = unref;