reactivity.cjs.prod.js 31 KB

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