12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238 |
- /**
- * Make a map and return a function for checking if a key
- * is in that map.
- * IMPORTANT: all calls of this function must be prefixed with
- * \/\*#\_\_PURE\_\_\*\/
- * So that rollup can tree-shake them if necessary.
- */
- function makeMap(str, expectsLowerCase) {
- const map = Object.create(null);
- const list = str.split(',');
- for (let i = 0; i < list.length; i++) {
- map[list[i]] = true;
- }
- return expectsLowerCase ? val => !!map[val.toLowerCase()] : val => !!map[val];
- }
- Object.freeze({})
- ;
- Object.freeze([]) ;
- const extend = Object.assign;
- const hasOwnProperty = Object.prototype.hasOwnProperty;
- const hasOwn = (val, key) => hasOwnProperty.call(val, key);
- const isArray = Array.isArray;
- const isMap = (val) => toTypeString(val) === '[object Map]';
- const isFunction = (val) => typeof val === 'function';
- const isString = (val) => typeof val === 'string';
- const isSymbol = (val) => typeof val === 'symbol';
- const isObject = (val) => val !== null && typeof val === 'object';
- const objectToString = Object.prototype.toString;
- const toTypeString = (value) => objectToString.call(value);
- const toRawType = (value) => {
- // extract "RawType" from strings like "[object RawType]"
- return toTypeString(value).slice(8, -1);
- };
- const isIntegerKey = (key) => isString(key) &&
- key !== 'NaN' &&
- key[0] !== '-' &&
- '' + parseInt(key, 10) === key;
- const cacheStringFunction = (fn) => {
- const cache = Object.create(null);
- return ((str) => {
- const hit = cache[str];
- return hit || (cache[str] = fn(str));
- });
- };
- /**
- * @private
- */
- const capitalize = cacheStringFunction((str) => str.charAt(0).toUpperCase() + str.slice(1));
- // compare whether a value has changed, accounting for NaN.
- const hasChanged = (value, oldValue) => !Object.is(value, oldValue);
- const def = (obj, key, value) => {
- Object.defineProperty(obj, key, {
- configurable: true,
- enumerable: false,
- value
- });
- };
- function warn(msg, ...args) {
- console.warn(`[Vue warn] ${msg}`, ...args);
- }
- let activeEffectScope;
- const effectScopeStack = [];
- class EffectScope {
- constructor(detached = false) {
- this.active = true;
- this.effects = [];
- this.cleanups = [];
- if (!detached && activeEffectScope) {
- this.parent = activeEffectScope;
- this.index =
- (activeEffectScope.scopes || (activeEffectScope.scopes = [])).push(this) - 1;
- }
- }
- run(fn) {
- if (this.active) {
- try {
- this.on();
- return fn();
- }
- finally {
- this.off();
- }
- }
- else {
- warn(`cannot run an inactive effect scope.`);
- }
- }
- on() {
- if (this.active) {
- effectScopeStack.push(this);
- activeEffectScope = this;
- }
- }
- off() {
- if (this.active) {
- effectScopeStack.pop();
- activeEffectScope = effectScopeStack[effectScopeStack.length - 1];
- }
- }
- stop(fromParent) {
- if (this.active) {
- this.effects.forEach(e => e.stop());
- this.cleanups.forEach(cleanup => cleanup());
- if (this.scopes) {
- this.scopes.forEach(e => e.stop(true));
- }
- // nested scope, dereference from parent to avoid memory leaks
- if (this.parent && !fromParent) {
- // optimized O(1) removal
- const last = this.parent.scopes.pop();
- if (last && last !== this) {
- this.parent.scopes[this.index] = last;
- last.index = this.index;
- }
- }
- this.active = false;
- }
- }
- }
- function effectScope(detached) {
- return new EffectScope(detached);
- }
- function recordEffectScope(effect, scope) {
- scope = scope || activeEffectScope;
- if (scope && scope.active) {
- scope.effects.push(effect);
- }
- }
- function getCurrentScope() {
- return activeEffectScope;
- }
- function onScopeDispose(fn) {
- if (activeEffectScope) {
- activeEffectScope.cleanups.push(fn);
- }
- else {
- warn(`onScopeDispose() is called when there is no active effect scope` +
- ` to be associated with.`);
- }
- }
- const createDep = (effects) => {
- const dep = new Set(effects);
- dep.w = 0;
- dep.n = 0;
- return dep;
- };
- const wasTracked = (dep) => (dep.w & trackOpBit) > 0;
- const newTracked = (dep) => (dep.n & trackOpBit) > 0;
- const initDepMarkers = ({ deps }) => {
- if (deps.length) {
- for (let i = 0; i < deps.length; i++) {
- deps[i].w |= trackOpBit; // set was tracked
- }
- }
- };
- const finalizeDepMarkers = (effect) => {
- const { deps } = effect;
- if (deps.length) {
- let ptr = 0;
- for (let i = 0; i < deps.length; i++) {
- const dep = deps[i];
- if (wasTracked(dep) && !newTracked(dep)) {
- dep.delete(effect);
- }
- else {
- deps[ptr++] = dep;
- }
- // clear bits
- dep.w &= ~trackOpBit;
- dep.n &= ~trackOpBit;
- }
- deps.length = ptr;
- }
- };
- const targetMap = new WeakMap();
- // The number of effects currently being tracked recursively.
- let effectTrackDepth = 0;
- let trackOpBit = 1;
- /**
- * The bitwise track markers support at most 30 levels of recursion.
- * This value is chosen to enable modern JS engines to use a SMI on all platforms.
- * When recursion depth is greater, fall back to using a full cleanup.
- */
- const maxMarkerBits = 30;
- const effectStack = [];
- let activeEffect;
- const ITERATE_KEY = Symbol('iterate' );
- const MAP_KEY_ITERATE_KEY = Symbol('Map key iterate' );
- class ReactiveEffect {
- constructor(fn, scheduler = null, scope) {
- this.fn = fn;
- this.scheduler = scheduler;
- this.active = true;
- this.deps = [];
- recordEffectScope(this, scope);
- }
- run() {
- if (!this.active) {
- return this.fn();
- }
- if (!effectStack.includes(this)) {
- try {
- effectStack.push((activeEffect = this));
- enableTracking();
- trackOpBit = 1 << ++effectTrackDepth;
- if (effectTrackDepth <= maxMarkerBits) {
- initDepMarkers(this);
- }
- else {
- cleanupEffect(this);
- }
- return this.fn();
- }
- finally {
- if (effectTrackDepth <= maxMarkerBits) {
- finalizeDepMarkers(this);
- }
- trackOpBit = 1 << --effectTrackDepth;
- resetTracking();
- effectStack.pop();
- const n = effectStack.length;
- activeEffect = n > 0 ? effectStack[n - 1] : undefined;
- }
- }
- }
- stop() {
- if (this.active) {
- cleanupEffect(this);
- if (this.onStop) {
- this.onStop();
- }
- this.active = false;
- }
- }
- }
- function cleanupEffect(effect) {
- const { deps } = effect;
- if (deps.length) {
- for (let i = 0; i < deps.length; i++) {
- deps[i].delete(effect);
- }
- deps.length = 0;
- }
- }
- function effect(fn, options) {
- if (fn.effect) {
- fn = fn.effect.fn;
- }
- const _effect = new ReactiveEffect(fn);
- if (options) {
- extend(_effect, options);
- if (options.scope)
- recordEffectScope(_effect, options.scope);
- }
- if (!options || !options.lazy) {
- _effect.run();
- }
- const runner = _effect.run.bind(_effect);
- runner.effect = _effect;
- return runner;
- }
- function stop(runner) {
- runner.effect.stop();
- }
- let shouldTrack = true;
- const trackStack = [];
- function pauseTracking() {
- trackStack.push(shouldTrack);
- shouldTrack = false;
- }
- function enableTracking() {
- trackStack.push(shouldTrack);
- shouldTrack = true;
- }
- function resetTracking() {
- const last = trackStack.pop();
- shouldTrack = last === undefined ? true : last;
- }
- function track(target, type, key) {
- if (!isTracking()) {
- return;
- }
- let depsMap = targetMap.get(target);
- if (!depsMap) {
- targetMap.set(target, (depsMap = new Map()));
- }
- let dep = depsMap.get(key);
- if (!dep) {
- depsMap.set(key, (dep = createDep()));
- }
- const eventInfo = { effect: activeEffect, target, type, key }
- ;
- trackEffects(dep, eventInfo);
- }
- function isTracking() {
- return shouldTrack && activeEffect !== undefined;
- }
- function trackEffects(dep, debuggerEventExtraInfo) {
- let shouldTrack = false;
- if (effectTrackDepth <= maxMarkerBits) {
- if (!newTracked(dep)) {
- dep.n |= trackOpBit; // set newly tracked
- shouldTrack = !wasTracked(dep);
- }
- }
- else {
- // Full cleanup mode.
- shouldTrack = !dep.has(activeEffect);
- }
- if (shouldTrack) {
- dep.add(activeEffect);
- activeEffect.deps.push(dep);
- if (activeEffect.onTrack) {
- activeEffect.onTrack(Object.assign({
- effect: activeEffect
- }, debuggerEventExtraInfo));
- }
- }
- }
- function trigger(target, type, key, newValue, oldValue, oldTarget) {
- const depsMap = targetMap.get(target);
- if (!depsMap) {
- // never been tracked
- return;
- }
- let deps = [];
- if (type === "clear" /* CLEAR */) {
- // collection being cleared
- // trigger all effects for target
- deps = [...depsMap.values()];
- }
- else if (key === 'length' && isArray(target)) {
- depsMap.forEach((dep, key) => {
- if (key === 'length' || key >= newValue) {
- deps.push(dep);
- }
- });
- }
- else {
- // schedule runs for SET | ADD | DELETE
- if (key !== void 0) {
- deps.push(depsMap.get(key));
- }
- // also run for iteration key on ADD | DELETE | Map.SET
- switch (type) {
- case "add" /* ADD */:
- if (!isArray(target)) {
- deps.push(depsMap.get(ITERATE_KEY));
- if (isMap(target)) {
- deps.push(depsMap.get(MAP_KEY_ITERATE_KEY));
- }
- }
- else if (isIntegerKey(key)) {
- // new index added to array -> length changes
- deps.push(depsMap.get('length'));
- }
- break;
- case "delete" /* DELETE */:
- if (!isArray(target)) {
- deps.push(depsMap.get(ITERATE_KEY));
- if (isMap(target)) {
- deps.push(depsMap.get(MAP_KEY_ITERATE_KEY));
- }
- }
- break;
- case "set" /* SET */:
- if (isMap(target)) {
- deps.push(depsMap.get(ITERATE_KEY));
- }
- break;
- }
- }
- const eventInfo = { target, type, key, newValue, oldValue, oldTarget }
- ;
- if (deps.length === 1) {
- if (deps[0]) {
- {
- triggerEffects(deps[0], eventInfo);
- }
- }
- }
- else {
- const effects = [];
- for (const dep of deps) {
- if (dep) {
- effects.push(...dep);
- }
- }
- {
- triggerEffects(createDep(effects), eventInfo);
- }
- }
- }
- function triggerEffects(dep, debuggerEventExtraInfo) {
- // spread into array for stabilization
- for (const effect of isArray(dep) ? dep : [...dep]) {
- if (effect !== activeEffect || effect.allowRecurse) {
- if (effect.onTrigger) {
- effect.onTrigger(extend({ effect }, debuggerEventExtraInfo));
- }
- if (effect.scheduler) {
- effect.scheduler();
- }
- else {
- effect.run();
- }
- }
- }
- }
- const isNonTrackableKeys = /*#__PURE__*/ makeMap(`__proto__,__v_isRef,__isVue`);
- const builtInSymbols = new Set(Object.getOwnPropertyNames(Symbol)
- .map(key => Symbol[key])
- .filter(isSymbol));
- const get = /*#__PURE__*/ createGetter();
- const shallowGet = /*#__PURE__*/ createGetter(false, true);
- const readonlyGet = /*#__PURE__*/ createGetter(true);
- const shallowReadonlyGet = /*#__PURE__*/ createGetter(true, true);
- const arrayInstrumentations = /*#__PURE__*/ createArrayInstrumentations();
- function createArrayInstrumentations() {
- const instrumentations = {};
- ['includes', 'indexOf', 'lastIndexOf'].forEach(key => {
- instrumentations[key] = function (...args) {
- const arr = toRaw(this);
- for (let i = 0, l = this.length; i < l; i++) {
- track(arr, "get" /* GET */, i + '');
- }
- // we run the method using the original args first (which may be reactive)
- const res = arr[key](...args);
- if (res === -1 || res === false) {
- // if that didn't work, run it again using raw values.
- return arr[key](...args.map(toRaw));
- }
- else {
- return res;
- }
- };
- });
- ['push', 'pop', 'shift', 'unshift', 'splice'].forEach(key => {
- instrumentations[key] = function (...args) {
- pauseTracking();
- const res = toRaw(this)[key].apply(this, args);
- resetTracking();
- return res;
- };
- });
- return instrumentations;
- }
- function createGetter(isReadonly = false, shallow = false) {
- return function get(target, key, receiver) {
- if (key === "__v_isReactive" /* IS_REACTIVE */) {
- return !isReadonly;
- }
- else if (key === "__v_isReadonly" /* IS_READONLY */) {
- return isReadonly;
- }
- else if (key === "__v_raw" /* RAW */ &&
- receiver ===
- (isReadonly
- ? shallow
- ? shallowReadonlyMap
- : readonlyMap
- : shallow
- ? shallowReactiveMap
- : reactiveMap).get(target)) {
- return target;
- }
- const targetIsArray = isArray(target);
- if (!isReadonly && targetIsArray && hasOwn(arrayInstrumentations, key)) {
- return Reflect.get(arrayInstrumentations, key, receiver);
- }
- const res = Reflect.get(target, key, receiver);
- if (isSymbol(key) ? builtInSymbols.has(key) : isNonTrackableKeys(key)) {
- return res;
- }
- if (!isReadonly) {
- track(target, "get" /* GET */, key);
- }
- if (shallow) {
- return res;
- }
- if (isRef(res)) {
- // ref unwrapping - does not apply for Array + integer key.
- const shouldUnwrap = !targetIsArray || !isIntegerKey(key);
- return shouldUnwrap ? res.value : res;
- }
- if (isObject(res)) {
- // Convert returned value into a proxy as well. we do the isObject check
- // here to avoid invalid value warning. Also need to lazy access readonly
- // and reactive here to avoid circular dependency.
- return isReadonly ? readonly(res) : reactive(res);
- }
- return res;
- };
- }
- const set = /*#__PURE__*/ createSetter();
- const shallowSet = /*#__PURE__*/ createSetter(true);
- function createSetter(shallow = false) {
- return function set(target, key, value, receiver) {
- let oldValue = target[key];
- if (!shallow && !isReadonly(value)) {
- value = toRaw(value);
- oldValue = toRaw(oldValue);
- if (!isArray(target) && isRef(oldValue) && !isRef(value)) {
- oldValue.value = value;
- return true;
- }
- }
- const hadKey = isArray(target) && isIntegerKey(key)
- ? Number(key) < target.length
- : hasOwn(target, key);
- const result = Reflect.set(target, key, value, receiver);
- // don't trigger if target is something up in the prototype chain of original
- if (target === toRaw(receiver)) {
- if (!hadKey) {
- trigger(target, "add" /* ADD */, key, value);
- }
- else if (hasChanged(value, oldValue)) {
- trigger(target, "set" /* SET */, key, value, oldValue);
- }
- }
- return result;
- };
- }
- function deleteProperty(target, key) {
- const hadKey = hasOwn(target, key);
- const oldValue = target[key];
- const result = Reflect.deleteProperty(target, key);
- if (result && hadKey) {
- trigger(target, "delete" /* DELETE */, key, undefined, oldValue);
- }
- return result;
- }
- function has(target, key) {
- const result = Reflect.has(target, key);
- if (!isSymbol(key) || !builtInSymbols.has(key)) {
- track(target, "has" /* HAS */, key);
- }
- return result;
- }
- function ownKeys(target) {
- track(target, "iterate" /* ITERATE */, isArray(target) ? 'length' : ITERATE_KEY);
- return Reflect.ownKeys(target);
- }
- const mutableHandlers = {
- get,
- set,
- deleteProperty,
- has,
- ownKeys
- };
- const readonlyHandlers = {
- get: readonlyGet,
- set(target, key) {
- {
- console.warn(`Set operation on key "${String(key)}" failed: target is readonly.`, target);
- }
- return true;
- },
- deleteProperty(target, key) {
- {
- console.warn(`Delete operation on key "${String(key)}" failed: target is readonly.`, target);
- }
- return true;
- }
- };
- const shallowReactiveHandlers = /*#__PURE__*/ extend({}, mutableHandlers, {
- get: shallowGet,
- set: shallowSet
- });
- // Props handlers are special in the sense that it should not unwrap top-level
- // refs (in order to allow refs to be explicitly passed down), but should
- // retain the reactivity of the normal readonly object.
- const shallowReadonlyHandlers = /*#__PURE__*/ extend({}, readonlyHandlers, {
- get: shallowReadonlyGet
- });
- const toShallow = (value) => value;
- const getProto = (v) => Reflect.getPrototypeOf(v);
- function get$1(target, key, isReadonly = false, isShallow = false) {
- // #1772: readonly(reactive(Map)) should return readonly + reactive version
- // of the value
- target = target["__v_raw" /* RAW */];
- const rawTarget = toRaw(target);
- const rawKey = toRaw(key);
- if (key !== rawKey) {
- !isReadonly && track(rawTarget, "get" /* GET */, key);
- }
- !isReadonly && track(rawTarget, "get" /* GET */, rawKey);
- const { has } = getProto(rawTarget);
- const wrap = isShallow ? toShallow : isReadonly ? toReadonly : toReactive;
- if (has.call(rawTarget, key)) {
- return wrap(target.get(key));
- }
- else if (has.call(rawTarget, rawKey)) {
- return wrap(target.get(rawKey));
- }
- else if (target !== rawTarget) {
- // #3602 readonly(reactive(Map))
- // ensure that the nested reactive `Map` can do tracking for itself
- target.get(key);
- }
- }
- function has$1(key, isReadonly = false) {
- const target = this["__v_raw" /* RAW */];
- const rawTarget = toRaw(target);
- const rawKey = toRaw(key);
- if (key !== rawKey) {
- !isReadonly && track(rawTarget, "has" /* HAS */, key);
- }
- !isReadonly && track(rawTarget, "has" /* HAS */, rawKey);
- return key === rawKey
- ? target.has(key)
- : target.has(key) || target.has(rawKey);
- }
- function size(target, isReadonly = false) {
- target = target["__v_raw" /* RAW */];
- !isReadonly && track(toRaw(target), "iterate" /* ITERATE */, ITERATE_KEY);
- return Reflect.get(target, 'size', target);
- }
- function add(value) {
- value = toRaw(value);
- const target = toRaw(this);
- const proto = getProto(target);
- const hadKey = proto.has.call(target, value);
- if (!hadKey) {
- target.add(value);
- trigger(target, "add" /* ADD */, value, value);
- }
- return this;
- }
- function set$1(key, value) {
- value = toRaw(value);
- const target = toRaw(this);
- const { has, get } = getProto(target);
- let hadKey = has.call(target, key);
- if (!hadKey) {
- key = toRaw(key);
- hadKey = has.call(target, key);
- }
- else {
- checkIdentityKeys(target, has, key);
- }
- const oldValue = get.call(target, key);
- target.set(key, value);
- if (!hadKey) {
- trigger(target, "add" /* ADD */, key, value);
- }
- else if (hasChanged(value, oldValue)) {
- trigger(target, "set" /* SET */, key, value, oldValue);
- }
- return this;
- }
- function deleteEntry(key) {
- const target = toRaw(this);
- const { has, get } = getProto(target);
- let hadKey = has.call(target, key);
- if (!hadKey) {
- key = toRaw(key);
- hadKey = has.call(target, key);
- }
- else {
- checkIdentityKeys(target, has, key);
- }
- const oldValue = get ? get.call(target, key) : undefined;
- // forward the operation before queueing reactions
- const result = target.delete(key);
- if (hadKey) {
- trigger(target, "delete" /* DELETE */, key, undefined, oldValue);
- }
- return result;
- }
- function clear() {
- const target = toRaw(this);
- const hadItems = target.size !== 0;
- const oldTarget = isMap(target)
- ? new Map(target)
- : new Set(target)
- ;
- // forward the operation before queueing reactions
- const result = target.clear();
- if (hadItems) {
- trigger(target, "clear" /* CLEAR */, undefined, undefined, oldTarget);
- }
- return result;
- }
- function createForEach(isReadonly, isShallow) {
- return function forEach(callback, thisArg) {
- const observed = this;
- const target = observed["__v_raw" /* RAW */];
- const rawTarget = toRaw(target);
- const wrap = isShallow ? toShallow : isReadonly ? toReadonly : toReactive;
- !isReadonly && track(rawTarget, "iterate" /* ITERATE */, ITERATE_KEY);
- return target.forEach((value, key) => {
- // important: make sure the callback is
- // 1. invoked with the reactive map as `this` and 3rd arg
- // 2. the value received should be a corresponding reactive/readonly.
- return callback.call(thisArg, wrap(value), wrap(key), observed);
- });
- };
- }
- function createIterableMethod(method, isReadonly, isShallow) {
- return function (...args) {
- const target = this["__v_raw" /* RAW */];
- const rawTarget = toRaw(target);
- const targetIsMap = isMap(rawTarget);
- const isPair = method === 'entries' || (method === Symbol.iterator && targetIsMap);
- const isKeyOnly = method === 'keys' && targetIsMap;
- const innerIterator = target[method](...args);
- const wrap = isShallow ? toShallow : isReadonly ? toReadonly : toReactive;
- !isReadonly &&
- track(rawTarget, "iterate" /* ITERATE */, isKeyOnly ? MAP_KEY_ITERATE_KEY : ITERATE_KEY);
- // return a wrapped iterator which returns observed versions of the
- // values emitted from the real iterator
- return {
- // iterator protocol
- next() {
- const { value, done } = innerIterator.next();
- return done
- ? { value, done }
- : {
- value: isPair ? [wrap(value[0]), wrap(value[1])] : wrap(value),
- done
- };
- },
- // iterable protocol
- [Symbol.iterator]() {
- return this;
- }
- };
- };
- }
- function createReadonlyMethod(type) {
- return function (...args) {
- {
- const key = args[0] ? `on key "${args[0]}" ` : ``;
- console.warn(`${capitalize(type)} operation ${key}failed: target is readonly.`, toRaw(this));
- }
- return type === "delete" /* DELETE */ ? false : this;
- };
- }
- function createInstrumentations() {
- const mutableInstrumentations = {
- get(key) {
- return get$1(this, key);
- },
- get size() {
- return size(this);
- },
- has: has$1,
- add,
- set: set$1,
- delete: deleteEntry,
- clear,
- forEach: createForEach(false, false)
- };
- const shallowInstrumentations = {
- get(key) {
- return get$1(this, key, false, true);
- },
- get size() {
- return size(this);
- },
- has: has$1,
- add,
- set: set$1,
- delete: deleteEntry,
- clear,
- forEach: createForEach(false, true)
- };
- const readonlyInstrumentations = {
- get(key) {
- return get$1(this, key, true);
- },
- get size() {
- return size(this, true);
- },
- has(key) {
- return has$1.call(this, key, true);
- },
- add: createReadonlyMethod("add" /* ADD */),
- set: createReadonlyMethod("set" /* SET */),
- delete: createReadonlyMethod("delete" /* DELETE */),
- clear: createReadonlyMethod("clear" /* CLEAR */),
- forEach: createForEach(true, false)
- };
- const shallowReadonlyInstrumentations = {
- get(key) {
- return get$1(this, key, true, true);
- },
- get size() {
- return size(this, true);
- },
- has(key) {
- return has$1.call(this, key, true);
- },
- add: createReadonlyMethod("add" /* ADD */),
- set: createReadonlyMethod("set" /* SET */),
- delete: createReadonlyMethod("delete" /* DELETE */),
- clear: createReadonlyMethod("clear" /* CLEAR */),
- forEach: createForEach(true, true)
- };
- const iteratorMethods = ['keys', 'values', 'entries', Symbol.iterator];
- iteratorMethods.forEach(method => {
- mutableInstrumentations[method] = createIterableMethod(method, false, false);
- readonlyInstrumentations[method] = createIterableMethod(method, true, false);
- shallowInstrumentations[method] = createIterableMethod(method, false, true);
- shallowReadonlyInstrumentations[method] = createIterableMethod(method, true, true);
- });
- return [
- mutableInstrumentations,
- readonlyInstrumentations,
- shallowInstrumentations,
- shallowReadonlyInstrumentations
- ];
- }
- const [mutableInstrumentations, readonlyInstrumentations, shallowInstrumentations, shallowReadonlyInstrumentations] = /* #__PURE__*/ createInstrumentations();
- function createInstrumentationGetter(isReadonly, shallow) {
- const instrumentations = shallow
- ? isReadonly
- ? shallowReadonlyInstrumentations
- : shallowInstrumentations
- : isReadonly
- ? readonlyInstrumentations
- : mutableInstrumentations;
- return (target, key, receiver) => {
- if (key === "__v_isReactive" /* IS_REACTIVE */) {
- return !isReadonly;
- }
- else if (key === "__v_isReadonly" /* IS_READONLY */) {
- return isReadonly;
- }
- else if (key === "__v_raw" /* RAW */) {
- return target;
- }
- return Reflect.get(hasOwn(instrumentations, key) && key in target
- ? instrumentations
- : target, key, receiver);
- };
- }
- const mutableCollectionHandlers = {
- get: /*#__PURE__*/ createInstrumentationGetter(false, false)
- };
- const shallowCollectionHandlers = {
- get: /*#__PURE__*/ createInstrumentationGetter(false, true)
- };
- const readonlyCollectionHandlers = {
- get: /*#__PURE__*/ createInstrumentationGetter(true, false)
- };
- const shallowReadonlyCollectionHandlers = {
- get: /*#__PURE__*/ createInstrumentationGetter(true, true)
- };
- function checkIdentityKeys(target, has, key) {
- const rawKey = toRaw(key);
- if (rawKey !== key && has.call(target, rawKey)) {
- const type = toRawType(target);
- console.warn(`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.`);
- }
- }
- const reactiveMap = new WeakMap();
- const shallowReactiveMap = new WeakMap();
- const readonlyMap = new WeakMap();
- const shallowReadonlyMap = new WeakMap();
- function targetTypeMap(rawType) {
- switch (rawType) {
- case 'Object':
- case 'Array':
- return 1 /* COMMON */;
- case 'Map':
- case 'Set':
- case 'WeakMap':
- case 'WeakSet':
- return 2 /* COLLECTION */;
- default:
- return 0 /* INVALID */;
- }
- }
- function getTargetType(value) {
- return value["__v_skip" /* SKIP */] || !Object.isExtensible(value)
- ? 0 /* INVALID */
- : targetTypeMap(toRawType(value));
- }
- function reactive(target) {
- // if trying to observe a readonly proxy, return the readonly version.
- if (target && target["__v_isReadonly" /* IS_READONLY */]) {
- return target;
- }
- return createReactiveObject(target, false, mutableHandlers, mutableCollectionHandlers, reactiveMap);
- }
- /**
- * Return a shallowly-reactive copy of the original object, where only the root
- * level properties are reactive. It also does not auto-unwrap refs (even at the
- * root level).
- */
- function shallowReactive(target) {
- return createReactiveObject(target, false, shallowReactiveHandlers, shallowCollectionHandlers, shallowReactiveMap);
- }
- /**
- * Creates a readonly copy of the original object. Note the returned copy is not
- * made reactive, but `readonly` can be called on an already reactive object.
- */
- function readonly(target) {
- return createReactiveObject(target, true, readonlyHandlers, readonlyCollectionHandlers, readonlyMap);
- }
- /**
- * Returns a reactive-copy of the original object, where only the root level
- * properties are readonly, and does NOT unwrap refs nor recursively convert
- * returned properties.
- * This is used for creating the props proxy object for stateful components.
- */
- function shallowReadonly(target) {
- return createReactiveObject(target, true, shallowReadonlyHandlers, shallowReadonlyCollectionHandlers, shallowReadonlyMap);
- }
- function createReactiveObject(target, isReadonly, baseHandlers, collectionHandlers, proxyMap) {
- if (!isObject(target)) {
- {
- console.warn(`value cannot be made reactive: ${String(target)}`);
- }
- return target;
- }
- // target is already a Proxy, return it.
- // exception: calling readonly() on a reactive object
- if (target["__v_raw" /* RAW */] &&
- !(isReadonly && target["__v_isReactive" /* IS_REACTIVE */])) {
- return target;
- }
- // target already has corresponding Proxy
- const existingProxy = proxyMap.get(target);
- if (existingProxy) {
- return existingProxy;
- }
- // only a whitelist of value types can be observed.
- const targetType = getTargetType(target);
- if (targetType === 0 /* INVALID */) {
- return target;
- }
- const proxy = new Proxy(target, targetType === 2 /* COLLECTION */ ? collectionHandlers : baseHandlers);
- proxyMap.set(target, proxy);
- return proxy;
- }
- function isReactive(value) {
- if (isReadonly(value)) {
- return isReactive(value["__v_raw" /* RAW */]);
- }
- return !!(value && value["__v_isReactive" /* IS_REACTIVE */]);
- }
- function isReadonly(value) {
- return !!(value && value["__v_isReadonly" /* IS_READONLY */]);
- }
- function isProxy(value) {
- return isReactive(value) || isReadonly(value);
- }
- function toRaw(observed) {
- const raw = observed && observed["__v_raw" /* RAW */];
- return raw ? toRaw(raw) : observed;
- }
- function markRaw(value) {
- def(value, "__v_skip" /* SKIP */, true);
- return value;
- }
- const toReactive = (value) => isObject(value) ? reactive(value) : value;
- const toReadonly = (value) => isObject(value) ? readonly(value) : value;
- function trackRefValue(ref) {
- if (isTracking()) {
- ref = toRaw(ref);
- if (!ref.dep) {
- ref.dep = createDep();
- }
- {
- trackEffects(ref.dep, {
- target: ref,
- type: "get" /* GET */,
- key: 'value'
- });
- }
- }
- }
- function triggerRefValue(ref, newVal) {
- ref = toRaw(ref);
- if (ref.dep) {
- {
- triggerEffects(ref.dep, {
- target: ref,
- type: "set" /* SET */,
- key: 'value',
- newValue: newVal
- });
- }
- }
- }
- function isRef(r) {
- return Boolean(r && r.__v_isRef === true);
- }
- function ref(value) {
- return createRef(value, false);
- }
- function shallowRef(value) {
- return createRef(value, true);
- }
- function createRef(rawValue, shallow) {
- if (isRef(rawValue)) {
- return rawValue;
- }
- return new RefImpl(rawValue, shallow);
- }
- class RefImpl {
- constructor(value, _shallow) {
- this._shallow = _shallow;
- this.dep = undefined;
- this.__v_isRef = true;
- this._rawValue = _shallow ? value : toRaw(value);
- this._value = _shallow ? value : toReactive(value);
- }
- get value() {
- trackRefValue(this);
- return this._value;
- }
- set value(newVal) {
- newVal = this._shallow ? newVal : toRaw(newVal);
- if (hasChanged(newVal, this._rawValue)) {
- this._rawValue = newVal;
- this._value = this._shallow ? newVal : toReactive(newVal);
- triggerRefValue(this, newVal);
- }
- }
- }
- function triggerRef(ref) {
- triggerRefValue(ref, ref.value );
- }
- function unref(ref) {
- return isRef(ref) ? ref.value : ref;
- }
- const shallowUnwrapHandlers = {
- get: (target, key, receiver) => unref(Reflect.get(target, key, receiver)),
- set: (target, key, value, receiver) => {
- const oldValue = target[key];
- if (isRef(oldValue) && !isRef(value)) {
- oldValue.value = value;
- return true;
- }
- else {
- return Reflect.set(target, key, value, receiver);
- }
- }
- };
- function proxyRefs(objectWithRefs) {
- return isReactive(objectWithRefs)
- ? objectWithRefs
- : new Proxy(objectWithRefs, shallowUnwrapHandlers);
- }
- class CustomRefImpl {
- constructor(factory) {
- this.dep = undefined;
- this.__v_isRef = true;
- const { get, set } = factory(() => trackRefValue(this), () => triggerRefValue(this));
- this._get = get;
- this._set = set;
- }
- get value() {
- return this._get();
- }
- set value(newVal) {
- this._set(newVal);
- }
- }
- function customRef(factory) {
- return new CustomRefImpl(factory);
- }
- function toRefs(object) {
- if (!isProxy(object)) {
- console.warn(`toRefs() expects a reactive object but received a plain one.`);
- }
- const ret = isArray(object) ? new Array(object.length) : {};
- for (const key in object) {
- ret[key] = toRef(object, key);
- }
- return ret;
- }
- class ObjectRefImpl {
- constructor(_object, _key, _defaultValue) {
- this._object = _object;
- this._key = _key;
- this._defaultValue = _defaultValue;
- this.__v_isRef = true;
- }
- get value() {
- const val = this._object[this._key];
- return val === undefined ? this._defaultValue : val;
- }
- set value(newVal) {
- this._object[this._key] = newVal;
- }
- }
- function toRef(object, key, defaultValue) {
- const val = object[key];
- return isRef(val)
- ? val
- : new ObjectRefImpl(object, key, defaultValue);
- }
- class ComputedRefImpl {
- constructor(getter, _setter, isReadonly) {
- this._setter = _setter;
- this.dep = undefined;
- this._dirty = true;
- this.__v_isRef = true;
- this.effect = new ReactiveEffect(getter, () => {
- if (!this._dirty) {
- this._dirty = true;
- triggerRefValue(this);
- }
- });
- this["__v_isReadonly" /* IS_READONLY */] = isReadonly;
- }
- get value() {
- // the computed ref may get wrapped by other proxies e.g. readonly() #3376
- const self = toRaw(this);
- trackRefValue(self);
- if (self._dirty) {
- self._dirty = false;
- self._value = self.effect.run();
- }
- return self._value;
- }
- set value(newValue) {
- this._setter(newValue);
- }
- }
- function computed(getterOrOptions, debugOptions) {
- let getter;
- let setter;
- const onlyGetter = isFunction(getterOrOptions);
- if (onlyGetter) {
- getter = getterOrOptions;
- setter = () => {
- console.warn('Write operation failed: computed value is readonly');
- }
- ;
- }
- else {
- getter = getterOrOptions.get;
- setter = getterOrOptions.set;
- }
- const cRef = new ComputedRefImpl(getter, setter, onlyGetter || !setter);
- if (debugOptions) {
- cRef.effect.onTrack = debugOptions.onTrack;
- cRef.effect.onTrigger = debugOptions.onTrigger;
- }
- return cRef;
- }
- var _a;
- const tick = Promise.resolve();
- const queue = [];
- let queued = false;
- const scheduler = (fn) => {
- queue.push(fn);
- if (!queued) {
- queued = true;
- tick.then(flush);
- }
- };
- const flush = () => {
- for (let i = 0; i < queue.length; i++) {
- queue[i]();
- }
- queue.length = 0;
- queued = false;
- };
- class DeferredComputedRefImpl {
- constructor(getter) {
- this.dep = undefined;
- this._dirty = true;
- this.__v_isRef = true;
- this[_a] = true;
- let compareTarget;
- let hasCompareTarget = false;
- let scheduled = false;
- this.effect = new ReactiveEffect(getter, (computedTrigger) => {
- if (this.dep) {
- if (computedTrigger) {
- compareTarget = this._value;
- hasCompareTarget = true;
- }
- else if (!scheduled) {
- const valueToCompare = hasCompareTarget ? compareTarget : this._value;
- scheduled = true;
- hasCompareTarget = false;
- scheduler(() => {
- if (this.effect.active && this._get() !== valueToCompare) {
- triggerRefValue(this);
- }
- scheduled = false;
- });
- }
- // chained upstream computeds are notified synchronously to ensure
- // value invalidation in case of sync access; normal effects are
- // deferred to be triggered in scheduler.
- for (const e of this.dep) {
- if (e.computed) {
- e.scheduler(true /* computedTrigger */);
- }
- }
- }
- this._dirty = true;
- });
- this.effect.computed = true;
- }
- _get() {
- if (this._dirty) {
- this._dirty = false;
- return (this._value = this.effect.run());
- }
- return this._value;
- }
- get value() {
- trackRefValue(this);
- // the computed ref may get wrapped by other proxies e.g. readonly() #3376
- return toRaw(this)._get();
- }
- }
- _a = "__v_isReadonly" /* IS_READONLY */;
- function deferredComputed(getter) {
- return new DeferredComputedRefImpl(getter);
- }
- export { EffectScope, ITERATE_KEY, ReactiveEffect, computed, customRef, deferredComputed, effect, effectScope, enableTracking, getCurrentScope, isProxy, isReactive, isReadonly, isRef, markRaw, onScopeDispose, pauseTracking, proxyRefs, reactive, readonly, ref, resetTracking, shallowReactive, shallowReadonly, shallowRef, stop, toRaw, toRef, toRefs, track, trigger, triggerRef, unref };
|