[57e58a3] | 1 | /**
|
---|
| 2 | * @vue/reactivity v3.5.13
|
---|
| 3 | * (c) 2018-present Yuxi (Evan) You and Vue contributors
|
---|
| 4 | * @license MIT
|
---|
| 5 | **/
|
---|
| 6 | import { hasChanged, extend, isArray, isIntegerKey, isSymbol, isMap, hasOwn, isObject, makeMap, toRawType, capitalize, def, isFunction, EMPTY_OBJ, isSet, isPlainObject, NOOP, remove } from '@vue/shared';
|
---|
| 7 |
|
---|
| 8 | function warn(msg, ...args) {
|
---|
| 9 | console.warn(`[Vue warn] ${msg}`, ...args);
|
---|
| 10 | }
|
---|
| 11 |
|
---|
| 12 | let activeEffectScope;
|
---|
| 13 | class EffectScope {
|
---|
| 14 | constructor(detached = false) {
|
---|
| 15 | this.detached = detached;
|
---|
| 16 | /**
|
---|
| 17 | * @internal
|
---|
| 18 | */
|
---|
| 19 | this._active = true;
|
---|
| 20 | /**
|
---|
| 21 | * @internal
|
---|
| 22 | */
|
---|
| 23 | this.effects = [];
|
---|
| 24 | /**
|
---|
| 25 | * @internal
|
---|
| 26 | */
|
---|
| 27 | this.cleanups = [];
|
---|
| 28 | this._isPaused = false;
|
---|
| 29 | this.parent = activeEffectScope;
|
---|
| 30 | if (!detached && activeEffectScope) {
|
---|
| 31 | this.index = (activeEffectScope.scopes || (activeEffectScope.scopes = [])).push(
|
---|
| 32 | this
|
---|
| 33 | ) - 1;
|
---|
| 34 | }
|
---|
| 35 | }
|
---|
| 36 | get active() {
|
---|
| 37 | return this._active;
|
---|
| 38 | }
|
---|
| 39 | pause() {
|
---|
| 40 | if (this._active) {
|
---|
| 41 | this._isPaused = true;
|
---|
| 42 | let i, l;
|
---|
| 43 | if (this.scopes) {
|
---|
| 44 | for (i = 0, l = this.scopes.length; i < l; i++) {
|
---|
| 45 | this.scopes[i].pause();
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 | for (i = 0, l = this.effects.length; i < l; i++) {
|
---|
| 49 | this.effects[i].pause();
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 | /**
|
---|
| 54 | * Resumes the effect scope, including all child scopes and effects.
|
---|
| 55 | */
|
---|
| 56 | resume() {
|
---|
| 57 | if (this._active) {
|
---|
| 58 | if (this._isPaused) {
|
---|
| 59 | this._isPaused = false;
|
---|
| 60 | let i, l;
|
---|
| 61 | if (this.scopes) {
|
---|
| 62 | for (i = 0, l = this.scopes.length; i < l; i++) {
|
---|
| 63 | this.scopes[i].resume();
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 | for (i = 0, l = this.effects.length; i < l; i++) {
|
---|
| 67 | this.effects[i].resume();
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 | run(fn) {
|
---|
| 73 | if (this._active) {
|
---|
| 74 | const currentEffectScope = activeEffectScope;
|
---|
| 75 | try {
|
---|
| 76 | activeEffectScope = this;
|
---|
| 77 | return fn();
|
---|
| 78 | } finally {
|
---|
| 79 | activeEffectScope = currentEffectScope;
|
---|
| 80 | }
|
---|
| 81 | } else if (!!(process.env.NODE_ENV !== "production")) {
|
---|
| 82 | warn(`cannot run an inactive effect scope.`);
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | /**
|
---|
| 86 | * This should only be called on non-detached scopes
|
---|
| 87 | * @internal
|
---|
| 88 | */
|
---|
| 89 | on() {
|
---|
| 90 | activeEffectScope = this;
|
---|
| 91 | }
|
---|
| 92 | /**
|
---|
| 93 | * This should only be called on non-detached scopes
|
---|
| 94 | * @internal
|
---|
| 95 | */
|
---|
| 96 | off() {
|
---|
| 97 | activeEffectScope = this.parent;
|
---|
| 98 | }
|
---|
| 99 | stop(fromParent) {
|
---|
| 100 | if (this._active) {
|
---|
| 101 | this._active = false;
|
---|
| 102 | let i, l;
|
---|
| 103 | for (i = 0, l = this.effects.length; i < l; i++) {
|
---|
| 104 | this.effects[i].stop();
|
---|
| 105 | }
|
---|
| 106 | this.effects.length = 0;
|
---|
| 107 | for (i = 0, l = this.cleanups.length; i < l; i++) {
|
---|
| 108 | this.cleanups[i]();
|
---|
| 109 | }
|
---|
| 110 | this.cleanups.length = 0;
|
---|
| 111 | if (this.scopes) {
|
---|
| 112 | for (i = 0, l = this.scopes.length; i < l; i++) {
|
---|
| 113 | this.scopes[i].stop(true);
|
---|
| 114 | }
|
---|
| 115 | this.scopes.length = 0;
|
---|
| 116 | }
|
---|
| 117 | if (!this.detached && this.parent && !fromParent) {
|
---|
| 118 | const last = this.parent.scopes.pop();
|
---|
| 119 | if (last && last !== this) {
|
---|
| 120 | this.parent.scopes[this.index] = last;
|
---|
| 121 | last.index = this.index;
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 | this.parent = void 0;
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 | function effectScope(detached) {
|
---|
| 129 | return new EffectScope(detached);
|
---|
| 130 | }
|
---|
| 131 | function getCurrentScope() {
|
---|
| 132 | return activeEffectScope;
|
---|
| 133 | }
|
---|
| 134 | function onScopeDispose(fn, failSilently = false) {
|
---|
| 135 | if (activeEffectScope) {
|
---|
| 136 | activeEffectScope.cleanups.push(fn);
|
---|
| 137 | } else if (!!(process.env.NODE_ENV !== "production") && !failSilently) {
|
---|
| 138 | warn(
|
---|
| 139 | `onScopeDispose() is called when there is no active effect scope to be associated with.`
|
---|
| 140 | );
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | let activeSub;
|
---|
| 145 | const EffectFlags = {
|
---|
| 146 | "ACTIVE": 1,
|
---|
| 147 | "1": "ACTIVE",
|
---|
| 148 | "RUNNING": 2,
|
---|
| 149 | "2": "RUNNING",
|
---|
| 150 | "TRACKING": 4,
|
---|
| 151 | "4": "TRACKING",
|
---|
| 152 | "NOTIFIED": 8,
|
---|
| 153 | "8": "NOTIFIED",
|
---|
| 154 | "DIRTY": 16,
|
---|
| 155 | "16": "DIRTY",
|
---|
| 156 | "ALLOW_RECURSE": 32,
|
---|
| 157 | "32": "ALLOW_RECURSE",
|
---|
| 158 | "PAUSED": 64,
|
---|
| 159 | "64": "PAUSED"
|
---|
| 160 | };
|
---|
| 161 | const pausedQueueEffects = /* @__PURE__ */ new WeakSet();
|
---|
| 162 | class ReactiveEffect {
|
---|
| 163 | constructor(fn) {
|
---|
| 164 | this.fn = fn;
|
---|
| 165 | /**
|
---|
| 166 | * @internal
|
---|
| 167 | */
|
---|
| 168 | this.deps = void 0;
|
---|
| 169 | /**
|
---|
| 170 | * @internal
|
---|
| 171 | */
|
---|
| 172 | this.depsTail = void 0;
|
---|
| 173 | /**
|
---|
| 174 | * @internal
|
---|
| 175 | */
|
---|
| 176 | this.flags = 1 | 4;
|
---|
| 177 | /**
|
---|
| 178 | * @internal
|
---|
| 179 | */
|
---|
| 180 | this.next = void 0;
|
---|
| 181 | /**
|
---|
| 182 | * @internal
|
---|
| 183 | */
|
---|
| 184 | this.cleanup = void 0;
|
---|
| 185 | this.scheduler = void 0;
|
---|
| 186 | if (activeEffectScope && activeEffectScope.active) {
|
---|
| 187 | activeEffectScope.effects.push(this);
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 | pause() {
|
---|
| 191 | this.flags |= 64;
|
---|
| 192 | }
|
---|
| 193 | resume() {
|
---|
| 194 | if (this.flags & 64) {
|
---|
| 195 | this.flags &= ~64;
|
---|
| 196 | if (pausedQueueEffects.has(this)) {
|
---|
| 197 | pausedQueueEffects.delete(this);
|
---|
| 198 | this.trigger();
|
---|
| 199 | }
|
---|
| 200 | }
|
---|
| 201 | }
|
---|
| 202 | /**
|
---|
| 203 | * @internal
|
---|
| 204 | */
|
---|
| 205 | notify() {
|
---|
| 206 | if (this.flags & 2 && !(this.flags & 32)) {
|
---|
| 207 | return;
|
---|
| 208 | }
|
---|
| 209 | if (!(this.flags & 8)) {
|
---|
| 210 | batch(this);
|
---|
| 211 | }
|
---|
| 212 | }
|
---|
| 213 | run() {
|
---|
| 214 | if (!(this.flags & 1)) {
|
---|
| 215 | return this.fn();
|
---|
| 216 | }
|
---|
| 217 | this.flags |= 2;
|
---|
| 218 | cleanupEffect(this);
|
---|
| 219 | prepareDeps(this);
|
---|
| 220 | const prevEffect = activeSub;
|
---|
| 221 | const prevShouldTrack = shouldTrack;
|
---|
| 222 | activeSub = this;
|
---|
| 223 | shouldTrack = true;
|
---|
| 224 | try {
|
---|
| 225 | return this.fn();
|
---|
| 226 | } finally {
|
---|
| 227 | if (!!(process.env.NODE_ENV !== "production") && activeSub !== this) {
|
---|
| 228 | warn(
|
---|
| 229 | "Active effect was not restored correctly - this is likely a Vue internal bug."
|
---|
| 230 | );
|
---|
| 231 | }
|
---|
| 232 | cleanupDeps(this);
|
---|
| 233 | activeSub = prevEffect;
|
---|
| 234 | shouldTrack = prevShouldTrack;
|
---|
| 235 | this.flags &= ~2;
|
---|
| 236 | }
|
---|
| 237 | }
|
---|
| 238 | stop() {
|
---|
| 239 | if (this.flags & 1) {
|
---|
| 240 | for (let link = this.deps; link; link = link.nextDep) {
|
---|
| 241 | removeSub(link);
|
---|
| 242 | }
|
---|
| 243 | this.deps = this.depsTail = void 0;
|
---|
| 244 | cleanupEffect(this);
|
---|
| 245 | this.onStop && this.onStop();
|
---|
| 246 | this.flags &= ~1;
|
---|
| 247 | }
|
---|
| 248 | }
|
---|
| 249 | trigger() {
|
---|
| 250 | if (this.flags & 64) {
|
---|
| 251 | pausedQueueEffects.add(this);
|
---|
| 252 | } else if (this.scheduler) {
|
---|
| 253 | this.scheduler();
|
---|
| 254 | } else {
|
---|
| 255 | this.runIfDirty();
|
---|
| 256 | }
|
---|
| 257 | }
|
---|
| 258 | /**
|
---|
| 259 | * @internal
|
---|
| 260 | */
|
---|
| 261 | runIfDirty() {
|
---|
| 262 | if (isDirty(this)) {
|
---|
| 263 | this.run();
|
---|
| 264 | }
|
---|
| 265 | }
|
---|
| 266 | get dirty() {
|
---|
| 267 | return isDirty(this);
|
---|
| 268 | }
|
---|
| 269 | }
|
---|
| 270 | let batchDepth = 0;
|
---|
| 271 | let batchedSub;
|
---|
| 272 | let batchedComputed;
|
---|
| 273 | function batch(sub, isComputed = false) {
|
---|
| 274 | sub.flags |= 8;
|
---|
| 275 | if (isComputed) {
|
---|
| 276 | sub.next = batchedComputed;
|
---|
| 277 | batchedComputed = sub;
|
---|
| 278 | return;
|
---|
| 279 | }
|
---|
| 280 | sub.next = batchedSub;
|
---|
| 281 | batchedSub = sub;
|
---|
| 282 | }
|
---|
| 283 | function startBatch() {
|
---|
| 284 | batchDepth++;
|
---|
| 285 | }
|
---|
| 286 | function endBatch() {
|
---|
| 287 | if (--batchDepth > 0) {
|
---|
| 288 | return;
|
---|
| 289 | }
|
---|
| 290 | if (batchedComputed) {
|
---|
| 291 | let e = batchedComputed;
|
---|
| 292 | batchedComputed = void 0;
|
---|
| 293 | while (e) {
|
---|
| 294 | const next = e.next;
|
---|
| 295 | e.next = void 0;
|
---|
| 296 | e.flags &= ~8;
|
---|
| 297 | e = next;
|
---|
| 298 | }
|
---|
| 299 | }
|
---|
| 300 | let error;
|
---|
| 301 | while (batchedSub) {
|
---|
| 302 | let e = batchedSub;
|
---|
| 303 | batchedSub = void 0;
|
---|
| 304 | while (e) {
|
---|
| 305 | const next = e.next;
|
---|
| 306 | e.next = void 0;
|
---|
| 307 | e.flags &= ~8;
|
---|
| 308 | if (e.flags & 1) {
|
---|
| 309 | try {
|
---|
| 310 | ;
|
---|
| 311 | e.trigger();
|
---|
| 312 | } catch (err) {
|
---|
| 313 | if (!error) error = err;
|
---|
| 314 | }
|
---|
| 315 | }
|
---|
| 316 | e = next;
|
---|
| 317 | }
|
---|
| 318 | }
|
---|
| 319 | if (error) throw error;
|
---|
| 320 | }
|
---|
| 321 | function prepareDeps(sub) {
|
---|
| 322 | for (let link = sub.deps; link; link = link.nextDep) {
|
---|
| 323 | link.version = -1;
|
---|
| 324 | link.prevActiveLink = link.dep.activeLink;
|
---|
| 325 | link.dep.activeLink = link;
|
---|
| 326 | }
|
---|
| 327 | }
|
---|
| 328 | function cleanupDeps(sub) {
|
---|
| 329 | let head;
|
---|
| 330 | let tail = sub.depsTail;
|
---|
| 331 | let link = tail;
|
---|
| 332 | while (link) {
|
---|
| 333 | const prev = link.prevDep;
|
---|
| 334 | if (link.version === -1) {
|
---|
| 335 | if (link === tail) tail = prev;
|
---|
| 336 | removeSub(link);
|
---|
| 337 | removeDep(link);
|
---|
| 338 | } else {
|
---|
| 339 | head = link;
|
---|
| 340 | }
|
---|
| 341 | link.dep.activeLink = link.prevActiveLink;
|
---|
| 342 | link.prevActiveLink = void 0;
|
---|
| 343 | link = prev;
|
---|
| 344 | }
|
---|
| 345 | sub.deps = head;
|
---|
| 346 | sub.depsTail = tail;
|
---|
| 347 | }
|
---|
| 348 | function isDirty(sub) {
|
---|
| 349 | for (let link = sub.deps; link; link = link.nextDep) {
|
---|
| 350 | if (link.dep.version !== link.version || link.dep.computed && (refreshComputed(link.dep.computed) || link.dep.version !== link.version)) {
|
---|
| 351 | return true;
|
---|
| 352 | }
|
---|
| 353 | }
|
---|
| 354 | if (sub._dirty) {
|
---|
| 355 | return true;
|
---|
| 356 | }
|
---|
| 357 | return false;
|
---|
| 358 | }
|
---|
| 359 | function refreshComputed(computed) {
|
---|
| 360 | if (computed.flags & 4 && !(computed.flags & 16)) {
|
---|
| 361 | return;
|
---|
| 362 | }
|
---|
| 363 | computed.flags &= ~16;
|
---|
| 364 | if (computed.globalVersion === globalVersion) {
|
---|
| 365 | return;
|
---|
| 366 | }
|
---|
| 367 | computed.globalVersion = globalVersion;
|
---|
| 368 | const dep = computed.dep;
|
---|
| 369 | computed.flags |= 2;
|
---|
| 370 | if (dep.version > 0 && !computed.isSSR && computed.deps && !isDirty(computed)) {
|
---|
| 371 | computed.flags &= ~2;
|
---|
| 372 | return;
|
---|
| 373 | }
|
---|
| 374 | const prevSub = activeSub;
|
---|
| 375 | const prevShouldTrack = shouldTrack;
|
---|
| 376 | activeSub = computed;
|
---|
| 377 | shouldTrack = true;
|
---|
| 378 | try {
|
---|
| 379 | prepareDeps(computed);
|
---|
| 380 | const value = computed.fn(computed._value);
|
---|
| 381 | if (dep.version === 0 || hasChanged(value, computed._value)) {
|
---|
| 382 | computed._value = value;
|
---|
| 383 | dep.version++;
|
---|
| 384 | }
|
---|
| 385 | } catch (err) {
|
---|
| 386 | dep.version++;
|
---|
| 387 | throw err;
|
---|
| 388 | } finally {
|
---|
| 389 | activeSub = prevSub;
|
---|
| 390 | shouldTrack = prevShouldTrack;
|
---|
| 391 | cleanupDeps(computed);
|
---|
| 392 | computed.flags &= ~2;
|
---|
| 393 | }
|
---|
| 394 | }
|
---|
| 395 | function removeSub(link, soft = false) {
|
---|
| 396 | const { dep, prevSub, nextSub } = link;
|
---|
| 397 | if (prevSub) {
|
---|
| 398 | prevSub.nextSub = nextSub;
|
---|
| 399 | link.prevSub = void 0;
|
---|
| 400 | }
|
---|
| 401 | if (nextSub) {
|
---|
| 402 | nextSub.prevSub = prevSub;
|
---|
| 403 | link.nextSub = void 0;
|
---|
| 404 | }
|
---|
| 405 | if (!!(process.env.NODE_ENV !== "production") && dep.subsHead === link) {
|
---|
| 406 | dep.subsHead = nextSub;
|
---|
| 407 | }
|
---|
| 408 | if (dep.subs === link) {
|
---|
| 409 | dep.subs = prevSub;
|
---|
| 410 | if (!prevSub && dep.computed) {
|
---|
| 411 | dep.computed.flags &= ~4;
|
---|
| 412 | for (let l = dep.computed.deps; l; l = l.nextDep) {
|
---|
| 413 | removeSub(l, true);
|
---|
| 414 | }
|
---|
| 415 | }
|
---|
| 416 | }
|
---|
| 417 | if (!soft && !--dep.sc && dep.map) {
|
---|
| 418 | dep.map.delete(dep.key);
|
---|
| 419 | }
|
---|
| 420 | }
|
---|
| 421 | function removeDep(link) {
|
---|
| 422 | const { prevDep, nextDep } = link;
|
---|
| 423 | if (prevDep) {
|
---|
| 424 | prevDep.nextDep = nextDep;
|
---|
| 425 | link.prevDep = void 0;
|
---|
| 426 | }
|
---|
| 427 | if (nextDep) {
|
---|
| 428 | nextDep.prevDep = prevDep;
|
---|
| 429 | link.nextDep = void 0;
|
---|
| 430 | }
|
---|
| 431 | }
|
---|
| 432 | function effect(fn, options) {
|
---|
| 433 | if (fn.effect instanceof ReactiveEffect) {
|
---|
| 434 | fn = fn.effect.fn;
|
---|
| 435 | }
|
---|
| 436 | const e = new ReactiveEffect(fn);
|
---|
| 437 | if (options) {
|
---|
| 438 | extend(e, options);
|
---|
| 439 | }
|
---|
| 440 | try {
|
---|
| 441 | e.run();
|
---|
| 442 | } catch (err) {
|
---|
| 443 | e.stop();
|
---|
| 444 | throw err;
|
---|
| 445 | }
|
---|
| 446 | const runner = e.run.bind(e);
|
---|
| 447 | runner.effect = e;
|
---|
| 448 | return runner;
|
---|
| 449 | }
|
---|
| 450 | function stop(runner) {
|
---|
| 451 | runner.effect.stop();
|
---|
| 452 | }
|
---|
| 453 | let shouldTrack = true;
|
---|
| 454 | const trackStack = [];
|
---|
| 455 | function pauseTracking() {
|
---|
| 456 | trackStack.push(shouldTrack);
|
---|
| 457 | shouldTrack = false;
|
---|
| 458 | }
|
---|
| 459 | function enableTracking() {
|
---|
| 460 | trackStack.push(shouldTrack);
|
---|
| 461 | shouldTrack = true;
|
---|
| 462 | }
|
---|
| 463 | function resetTracking() {
|
---|
| 464 | const last = trackStack.pop();
|
---|
| 465 | shouldTrack = last === void 0 ? true : last;
|
---|
| 466 | }
|
---|
| 467 | function onEffectCleanup(fn, failSilently = false) {
|
---|
| 468 | if (activeSub instanceof ReactiveEffect) {
|
---|
| 469 | activeSub.cleanup = fn;
|
---|
| 470 | } else if (!!(process.env.NODE_ENV !== "production") && !failSilently) {
|
---|
| 471 | warn(
|
---|
| 472 | `onEffectCleanup() was called when there was no active effect to associate with.`
|
---|
| 473 | );
|
---|
| 474 | }
|
---|
| 475 | }
|
---|
| 476 | function cleanupEffect(e) {
|
---|
| 477 | const { cleanup } = e;
|
---|
| 478 | e.cleanup = void 0;
|
---|
| 479 | if (cleanup) {
|
---|
| 480 | const prevSub = activeSub;
|
---|
| 481 | activeSub = void 0;
|
---|
| 482 | try {
|
---|
| 483 | cleanup();
|
---|
| 484 | } finally {
|
---|
| 485 | activeSub = prevSub;
|
---|
| 486 | }
|
---|
| 487 | }
|
---|
| 488 | }
|
---|
| 489 |
|
---|
| 490 | let globalVersion = 0;
|
---|
| 491 | class Link {
|
---|
| 492 | constructor(sub, dep) {
|
---|
| 493 | this.sub = sub;
|
---|
| 494 | this.dep = dep;
|
---|
| 495 | this.version = dep.version;
|
---|
| 496 | this.nextDep = this.prevDep = this.nextSub = this.prevSub = this.prevActiveLink = void 0;
|
---|
| 497 | }
|
---|
| 498 | }
|
---|
| 499 | class Dep {
|
---|
| 500 | constructor(computed) {
|
---|
| 501 | this.computed = computed;
|
---|
| 502 | this.version = 0;
|
---|
| 503 | /**
|
---|
| 504 | * Link between this dep and the current active effect
|
---|
| 505 | */
|
---|
| 506 | this.activeLink = void 0;
|
---|
| 507 | /**
|
---|
| 508 | * Doubly linked list representing the subscribing effects (tail)
|
---|
| 509 | */
|
---|
| 510 | this.subs = void 0;
|
---|
| 511 | /**
|
---|
| 512 | * For object property deps cleanup
|
---|
| 513 | */
|
---|
| 514 | this.map = void 0;
|
---|
| 515 | this.key = void 0;
|
---|
| 516 | /**
|
---|
| 517 | * Subscriber counter
|
---|
| 518 | */
|
---|
| 519 | this.sc = 0;
|
---|
| 520 | if (!!(process.env.NODE_ENV !== "production")) {
|
---|
| 521 | this.subsHead = void 0;
|
---|
| 522 | }
|
---|
| 523 | }
|
---|
| 524 | track(debugInfo) {
|
---|
| 525 | if (!activeSub || !shouldTrack || activeSub === this.computed) {
|
---|
| 526 | return;
|
---|
| 527 | }
|
---|
| 528 | let link = this.activeLink;
|
---|
| 529 | if (link === void 0 || link.sub !== activeSub) {
|
---|
| 530 | link = this.activeLink = new Link(activeSub, this);
|
---|
| 531 | if (!activeSub.deps) {
|
---|
| 532 | activeSub.deps = activeSub.depsTail = link;
|
---|
| 533 | } else {
|
---|
| 534 | link.prevDep = activeSub.depsTail;
|
---|
| 535 | activeSub.depsTail.nextDep = link;
|
---|
| 536 | activeSub.depsTail = link;
|
---|
| 537 | }
|
---|
| 538 | addSub(link);
|
---|
| 539 | } else if (link.version === -1) {
|
---|
| 540 | link.version = this.version;
|
---|
| 541 | if (link.nextDep) {
|
---|
| 542 | const next = link.nextDep;
|
---|
| 543 | next.prevDep = link.prevDep;
|
---|
| 544 | if (link.prevDep) {
|
---|
| 545 | link.prevDep.nextDep = next;
|
---|
| 546 | }
|
---|
| 547 | link.prevDep = activeSub.depsTail;
|
---|
| 548 | link.nextDep = void 0;
|
---|
| 549 | activeSub.depsTail.nextDep = link;
|
---|
| 550 | activeSub.depsTail = link;
|
---|
| 551 | if (activeSub.deps === link) {
|
---|
| 552 | activeSub.deps = next;
|
---|
| 553 | }
|
---|
| 554 | }
|
---|
| 555 | }
|
---|
| 556 | if (!!(process.env.NODE_ENV !== "production") && activeSub.onTrack) {
|
---|
| 557 | activeSub.onTrack(
|
---|
| 558 | extend(
|
---|
| 559 | {
|
---|
| 560 | effect: activeSub
|
---|
| 561 | },
|
---|
| 562 | debugInfo
|
---|
| 563 | )
|
---|
| 564 | );
|
---|
| 565 | }
|
---|
| 566 | return link;
|
---|
| 567 | }
|
---|
| 568 | trigger(debugInfo) {
|
---|
| 569 | this.version++;
|
---|
| 570 | globalVersion++;
|
---|
| 571 | this.notify(debugInfo);
|
---|
| 572 | }
|
---|
| 573 | notify(debugInfo) {
|
---|
| 574 | startBatch();
|
---|
| 575 | try {
|
---|
| 576 | if (!!(process.env.NODE_ENV !== "production")) {
|
---|
| 577 | for (let head = this.subsHead; head; head = head.nextSub) {
|
---|
| 578 | if (head.sub.onTrigger && !(head.sub.flags & 8)) {
|
---|
| 579 | head.sub.onTrigger(
|
---|
| 580 | extend(
|
---|
| 581 | {
|
---|
| 582 | effect: head.sub
|
---|
| 583 | },
|
---|
| 584 | debugInfo
|
---|
| 585 | )
|
---|
| 586 | );
|
---|
| 587 | }
|
---|
| 588 | }
|
---|
| 589 | }
|
---|
| 590 | for (let link = this.subs; link; link = link.prevSub) {
|
---|
| 591 | if (link.sub.notify()) {
|
---|
| 592 | ;
|
---|
| 593 | link.sub.dep.notify();
|
---|
| 594 | }
|
---|
| 595 | }
|
---|
| 596 | } finally {
|
---|
| 597 | endBatch();
|
---|
| 598 | }
|
---|
| 599 | }
|
---|
| 600 | }
|
---|
| 601 | function addSub(link) {
|
---|
| 602 | link.dep.sc++;
|
---|
| 603 | if (link.sub.flags & 4) {
|
---|
| 604 | const computed = link.dep.computed;
|
---|
| 605 | if (computed && !link.dep.subs) {
|
---|
| 606 | computed.flags |= 4 | 16;
|
---|
| 607 | for (let l = computed.deps; l; l = l.nextDep) {
|
---|
| 608 | addSub(l);
|
---|
| 609 | }
|
---|
| 610 | }
|
---|
| 611 | const currentTail = link.dep.subs;
|
---|
| 612 | if (currentTail !== link) {
|
---|
| 613 | link.prevSub = currentTail;
|
---|
| 614 | if (currentTail) currentTail.nextSub = link;
|
---|
| 615 | }
|
---|
| 616 | if (!!(process.env.NODE_ENV !== "production") && link.dep.subsHead === void 0) {
|
---|
| 617 | link.dep.subsHead = link;
|
---|
| 618 | }
|
---|
| 619 | link.dep.subs = link;
|
---|
| 620 | }
|
---|
| 621 | }
|
---|
| 622 | const targetMap = /* @__PURE__ */ new WeakMap();
|
---|
| 623 | const ITERATE_KEY = Symbol(
|
---|
| 624 | !!(process.env.NODE_ENV !== "production") ? "Object iterate" : ""
|
---|
| 625 | );
|
---|
| 626 | const MAP_KEY_ITERATE_KEY = Symbol(
|
---|
| 627 | !!(process.env.NODE_ENV !== "production") ? "Map keys iterate" : ""
|
---|
| 628 | );
|
---|
| 629 | const ARRAY_ITERATE_KEY = Symbol(
|
---|
| 630 | !!(process.env.NODE_ENV !== "production") ? "Array iterate" : ""
|
---|
| 631 | );
|
---|
| 632 | function track(target, type, key) {
|
---|
| 633 | if (shouldTrack && activeSub) {
|
---|
| 634 | let depsMap = targetMap.get(target);
|
---|
| 635 | if (!depsMap) {
|
---|
| 636 | targetMap.set(target, depsMap = /* @__PURE__ */ new Map());
|
---|
| 637 | }
|
---|
| 638 | let dep = depsMap.get(key);
|
---|
| 639 | if (!dep) {
|
---|
| 640 | depsMap.set(key, dep = new Dep());
|
---|
| 641 | dep.map = depsMap;
|
---|
| 642 | dep.key = key;
|
---|
| 643 | }
|
---|
| 644 | if (!!(process.env.NODE_ENV !== "production")) {
|
---|
| 645 | dep.track({
|
---|
| 646 | target,
|
---|
| 647 | type,
|
---|
| 648 | key
|
---|
| 649 | });
|
---|
| 650 | } else {
|
---|
| 651 | dep.track();
|
---|
| 652 | }
|
---|
| 653 | }
|
---|
| 654 | }
|
---|
| 655 | function trigger(target, type, key, newValue, oldValue, oldTarget) {
|
---|
| 656 | const depsMap = targetMap.get(target);
|
---|
| 657 | if (!depsMap) {
|
---|
| 658 | globalVersion++;
|
---|
| 659 | return;
|
---|
| 660 | }
|
---|
| 661 | const run = (dep) => {
|
---|
| 662 | if (dep) {
|
---|
| 663 | if (!!(process.env.NODE_ENV !== "production")) {
|
---|
| 664 | dep.trigger({
|
---|
| 665 | target,
|
---|
| 666 | type,
|
---|
| 667 | key,
|
---|
| 668 | newValue,
|
---|
| 669 | oldValue,
|
---|
| 670 | oldTarget
|
---|
| 671 | });
|
---|
| 672 | } else {
|
---|
| 673 | dep.trigger();
|
---|
| 674 | }
|
---|
| 675 | }
|
---|
| 676 | };
|
---|
| 677 | startBatch();
|
---|
| 678 | if (type === "clear") {
|
---|
| 679 | depsMap.forEach(run);
|
---|
| 680 | } else {
|
---|
| 681 | const targetIsArray = isArray(target);
|
---|
| 682 | const isArrayIndex = targetIsArray && isIntegerKey(key);
|
---|
| 683 | if (targetIsArray && key === "length") {
|
---|
| 684 | const newLength = Number(newValue);
|
---|
| 685 | depsMap.forEach((dep, key2) => {
|
---|
| 686 | if (key2 === "length" || key2 === ARRAY_ITERATE_KEY || !isSymbol(key2) && key2 >= newLength) {
|
---|
| 687 | run(dep);
|
---|
| 688 | }
|
---|
| 689 | });
|
---|
| 690 | } else {
|
---|
| 691 | if (key !== void 0 || depsMap.has(void 0)) {
|
---|
| 692 | run(depsMap.get(key));
|
---|
| 693 | }
|
---|
| 694 | if (isArrayIndex) {
|
---|
| 695 | run(depsMap.get(ARRAY_ITERATE_KEY));
|
---|
| 696 | }
|
---|
| 697 | switch (type) {
|
---|
| 698 | case "add":
|
---|
| 699 | if (!targetIsArray) {
|
---|
| 700 | run(depsMap.get(ITERATE_KEY));
|
---|
| 701 | if (isMap(target)) {
|
---|
| 702 | run(depsMap.get(MAP_KEY_ITERATE_KEY));
|
---|
| 703 | }
|
---|
| 704 | } else if (isArrayIndex) {
|
---|
| 705 | run(depsMap.get("length"));
|
---|
| 706 | }
|
---|
| 707 | break;
|
---|
| 708 | case "delete":
|
---|
| 709 | if (!targetIsArray) {
|
---|
| 710 | run(depsMap.get(ITERATE_KEY));
|
---|
| 711 | if (isMap(target)) {
|
---|
| 712 | run(depsMap.get(MAP_KEY_ITERATE_KEY));
|
---|
| 713 | }
|
---|
| 714 | }
|
---|
| 715 | break;
|
---|
| 716 | case "set":
|
---|
| 717 | if (isMap(target)) {
|
---|
| 718 | run(depsMap.get(ITERATE_KEY));
|
---|
| 719 | }
|
---|
| 720 | break;
|
---|
| 721 | }
|
---|
| 722 | }
|
---|
| 723 | }
|
---|
| 724 | endBatch();
|
---|
| 725 | }
|
---|
| 726 | function getDepFromReactive(object, key) {
|
---|
| 727 | const depMap = targetMap.get(object);
|
---|
| 728 | return depMap && depMap.get(key);
|
---|
| 729 | }
|
---|
| 730 |
|
---|
| 731 | function reactiveReadArray(array) {
|
---|
| 732 | const raw = toRaw(array);
|
---|
| 733 | if (raw === array) return raw;
|
---|
| 734 | track(raw, "iterate", ARRAY_ITERATE_KEY);
|
---|
| 735 | return isShallow(array) ? raw : raw.map(toReactive);
|
---|
| 736 | }
|
---|
| 737 | function shallowReadArray(arr) {
|
---|
| 738 | track(arr = toRaw(arr), "iterate", ARRAY_ITERATE_KEY);
|
---|
| 739 | return arr;
|
---|
| 740 | }
|
---|
| 741 | const arrayInstrumentations = {
|
---|
| 742 | __proto__: null,
|
---|
| 743 | [Symbol.iterator]() {
|
---|
| 744 | return iterator(this, Symbol.iterator, toReactive);
|
---|
| 745 | },
|
---|
| 746 | concat(...args) {
|
---|
| 747 | return reactiveReadArray(this).concat(
|
---|
| 748 | ...args.map((x) => isArray(x) ? reactiveReadArray(x) : x)
|
---|
| 749 | );
|
---|
| 750 | },
|
---|
| 751 | entries() {
|
---|
| 752 | return iterator(this, "entries", (value) => {
|
---|
| 753 | value[1] = toReactive(value[1]);
|
---|
| 754 | return value;
|
---|
| 755 | });
|
---|
| 756 | },
|
---|
| 757 | every(fn, thisArg) {
|
---|
| 758 | return apply(this, "every", fn, thisArg, void 0, arguments);
|
---|
| 759 | },
|
---|
| 760 | filter(fn, thisArg) {
|
---|
| 761 | return apply(this, "filter", fn, thisArg, (v) => v.map(toReactive), arguments);
|
---|
| 762 | },
|
---|
| 763 | find(fn, thisArg) {
|
---|
| 764 | return apply(this, "find", fn, thisArg, toReactive, arguments);
|
---|
| 765 | },
|
---|
| 766 | findIndex(fn, thisArg) {
|
---|
| 767 | return apply(this, "findIndex", fn, thisArg, void 0, arguments);
|
---|
| 768 | },
|
---|
| 769 | findLast(fn, thisArg) {
|
---|
| 770 | return apply(this, "findLast", fn, thisArg, toReactive, arguments);
|
---|
| 771 | },
|
---|
| 772 | findLastIndex(fn, thisArg) {
|
---|
| 773 | return apply(this, "findLastIndex", fn, thisArg, void 0, arguments);
|
---|
| 774 | },
|
---|
| 775 | // flat, flatMap could benefit from ARRAY_ITERATE but are not straight-forward to implement
|
---|
| 776 | forEach(fn, thisArg) {
|
---|
| 777 | return apply(this, "forEach", fn, thisArg, void 0, arguments);
|
---|
| 778 | },
|
---|
| 779 | includes(...args) {
|
---|
| 780 | return searchProxy(this, "includes", args);
|
---|
| 781 | },
|
---|
| 782 | indexOf(...args) {
|
---|
| 783 | return searchProxy(this, "indexOf", args);
|
---|
| 784 | },
|
---|
| 785 | join(separator) {
|
---|
| 786 | return reactiveReadArray(this).join(separator);
|
---|
| 787 | },
|
---|
| 788 | // keys() iterator only reads `length`, no optimisation required
|
---|
| 789 | lastIndexOf(...args) {
|
---|
| 790 | return searchProxy(this, "lastIndexOf", args);
|
---|
| 791 | },
|
---|
| 792 | map(fn, thisArg) {
|
---|
| 793 | return apply(this, "map", fn, thisArg, void 0, arguments);
|
---|
| 794 | },
|
---|
| 795 | pop() {
|
---|
| 796 | return noTracking(this, "pop");
|
---|
| 797 | },
|
---|
| 798 | push(...args) {
|
---|
| 799 | return noTracking(this, "push", args);
|
---|
| 800 | },
|
---|
| 801 | reduce(fn, ...args) {
|
---|
| 802 | return reduce(this, "reduce", fn, args);
|
---|
| 803 | },
|
---|
| 804 | reduceRight(fn, ...args) {
|
---|
| 805 | return reduce(this, "reduceRight", fn, args);
|
---|
| 806 | },
|
---|
| 807 | shift() {
|
---|
| 808 | return noTracking(this, "shift");
|
---|
| 809 | },
|
---|
| 810 | // slice could use ARRAY_ITERATE but also seems to beg for range tracking
|
---|
| 811 | some(fn, thisArg) {
|
---|
| 812 | return apply(this, "some", fn, thisArg, void 0, arguments);
|
---|
| 813 | },
|
---|
| 814 | splice(...args) {
|
---|
| 815 | return noTracking(this, "splice", args);
|
---|
| 816 | },
|
---|
| 817 | toReversed() {
|
---|
| 818 | return reactiveReadArray(this).toReversed();
|
---|
| 819 | },
|
---|
| 820 | toSorted(comparer) {
|
---|
| 821 | return reactiveReadArray(this).toSorted(comparer);
|
---|
| 822 | },
|
---|
| 823 | toSpliced(...args) {
|
---|
| 824 | return reactiveReadArray(this).toSpliced(...args);
|
---|
| 825 | },
|
---|
| 826 | unshift(...args) {
|
---|
| 827 | return noTracking(this, "unshift", args);
|
---|
| 828 | },
|
---|
| 829 | values() {
|
---|
| 830 | return iterator(this, "values", toReactive);
|
---|
| 831 | }
|
---|
| 832 | };
|
---|
| 833 | function iterator(self, method, wrapValue) {
|
---|
| 834 | const arr = shallowReadArray(self);
|
---|
| 835 | const iter = arr[method]();
|
---|
| 836 | if (arr !== self && !isShallow(self)) {
|
---|
| 837 | iter._next = iter.next;
|
---|
| 838 | iter.next = () => {
|
---|
| 839 | const result = iter._next();
|
---|
| 840 | if (result.value) {
|
---|
| 841 | result.value = wrapValue(result.value);
|
---|
| 842 | }
|
---|
| 843 | return result;
|
---|
| 844 | };
|
---|
| 845 | }
|
---|
| 846 | return iter;
|
---|
| 847 | }
|
---|
| 848 | const arrayProto = Array.prototype;
|
---|
| 849 | function apply(self, method, fn, thisArg, wrappedRetFn, args) {
|
---|
| 850 | const arr = shallowReadArray(self);
|
---|
| 851 | const needsWrap = arr !== self && !isShallow(self);
|
---|
| 852 | const methodFn = arr[method];
|
---|
| 853 | if (methodFn !== arrayProto[method]) {
|
---|
| 854 | const result2 = methodFn.apply(self, args);
|
---|
| 855 | return needsWrap ? toReactive(result2) : result2;
|
---|
| 856 | }
|
---|
| 857 | let wrappedFn = fn;
|
---|
| 858 | if (arr !== self) {
|
---|
| 859 | if (needsWrap) {
|
---|
| 860 | wrappedFn = function(item, index) {
|
---|
| 861 | return fn.call(this, toReactive(item), index, self);
|
---|
| 862 | };
|
---|
| 863 | } else if (fn.length > 2) {
|
---|
| 864 | wrappedFn = function(item, index) {
|
---|
| 865 | return fn.call(this, item, index, self);
|
---|
| 866 | };
|
---|
| 867 | }
|
---|
| 868 | }
|
---|
| 869 | const result = methodFn.call(arr, wrappedFn, thisArg);
|
---|
| 870 | return needsWrap && wrappedRetFn ? wrappedRetFn(result) : result;
|
---|
| 871 | }
|
---|
| 872 | function reduce(self, method, fn, args) {
|
---|
| 873 | const arr = shallowReadArray(self);
|
---|
| 874 | let wrappedFn = fn;
|
---|
| 875 | if (arr !== self) {
|
---|
| 876 | if (!isShallow(self)) {
|
---|
| 877 | wrappedFn = function(acc, item, index) {
|
---|
| 878 | return fn.call(this, acc, toReactive(item), index, self);
|
---|
| 879 | };
|
---|
| 880 | } else if (fn.length > 3) {
|
---|
| 881 | wrappedFn = function(acc, item, index) {
|
---|
| 882 | return fn.call(this, acc, item, index, self);
|
---|
| 883 | };
|
---|
| 884 | }
|
---|
| 885 | }
|
---|
| 886 | return arr[method](wrappedFn, ...args);
|
---|
| 887 | }
|
---|
| 888 | function searchProxy(self, method, args) {
|
---|
| 889 | const arr = toRaw(self);
|
---|
| 890 | track(arr, "iterate", ARRAY_ITERATE_KEY);
|
---|
| 891 | const res = arr[method](...args);
|
---|
| 892 | if ((res === -1 || res === false) && isProxy(args[0])) {
|
---|
| 893 | args[0] = toRaw(args[0]);
|
---|
| 894 | return arr[method](...args);
|
---|
| 895 | }
|
---|
| 896 | return res;
|
---|
| 897 | }
|
---|
| 898 | function noTracking(self, method, args = []) {
|
---|
| 899 | pauseTracking();
|
---|
| 900 | startBatch();
|
---|
| 901 | const res = toRaw(self)[method].apply(self, args);
|
---|
| 902 | endBatch();
|
---|
| 903 | resetTracking();
|
---|
| 904 | return res;
|
---|
| 905 | }
|
---|
| 906 |
|
---|
| 907 | const isNonTrackableKeys = /* @__PURE__ */ makeMap(`__proto__,__v_isRef,__isVue`);
|
---|
| 908 | const builtInSymbols = new Set(
|
---|
| 909 | /* @__PURE__ */ Object.getOwnPropertyNames(Symbol).filter((key) => key !== "arguments" && key !== "caller").map((key) => Symbol[key]).filter(isSymbol)
|
---|
| 910 | );
|
---|
| 911 | function hasOwnProperty(key) {
|
---|
| 912 | if (!isSymbol(key)) key = String(key);
|
---|
| 913 | const obj = toRaw(this);
|
---|
| 914 | track(obj, "has", key);
|
---|
| 915 | return obj.hasOwnProperty(key);
|
---|
| 916 | }
|
---|
| 917 | class BaseReactiveHandler {
|
---|
| 918 | constructor(_isReadonly = false, _isShallow = false) {
|
---|
| 919 | this._isReadonly = _isReadonly;
|
---|
| 920 | this._isShallow = _isShallow;
|
---|
| 921 | }
|
---|
| 922 | get(target, key, receiver) {
|
---|
| 923 | if (key === "__v_skip") return target["__v_skip"];
|
---|
| 924 | const isReadonly2 = this._isReadonly, isShallow2 = this._isShallow;
|
---|
| 925 | if (key === "__v_isReactive") {
|
---|
| 926 | return !isReadonly2;
|
---|
| 927 | } else if (key === "__v_isReadonly") {
|
---|
| 928 | return isReadonly2;
|
---|
| 929 | } else if (key === "__v_isShallow") {
|
---|
| 930 | return isShallow2;
|
---|
| 931 | } else if (key === "__v_raw") {
|
---|
| 932 | if (receiver === (isReadonly2 ? isShallow2 ? shallowReadonlyMap : readonlyMap : isShallow2 ? shallowReactiveMap : reactiveMap).get(target) || // receiver is not the reactive proxy, but has the same prototype
|
---|
| 933 | // this means the receiver is a user proxy of the reactive proxy
|
---|
| 934 | Object.getPrototypeOf(target) === Object.getPrototypeOf(receiver)) {
|
---|
| 935 | return target;
|
---|
| 936 | }
|
---|
| 937 | return;
|
---|
| 938 | }
|
---|
| 939 | const targetIsArray = isArray(target);
|
---|
| 940 | if (!isReadonly2) {
|
---|
| 941 | let fn;
|
---|
| 942 | if (targetIsArray && (fn = arrayInstrumentations[key])) {
|
---|
| 943 | return fn;
|
---|
| 944 | }
|
---|
| 945 | if (key === "hasOwnProperty") {
|
---|
| 946 | return hasOwnProperty;
|
---|
| 947 | }
|
---|
| 948 | }
|
---|
| 949 | const res = Reflect.get(
|
---|
| 950 | target,
|
---|
| 951 | key,
|
---|
| 952 | // if this is a proxy wrapping a ref, return methods using the raw ref
|
---|
| 953 | // as receiver so that we don't have to call `toRaw` on the ref in all
|
---|
| 954 | // its class methods
|
---|
| 955 | isRef(target) ? target : receiver
|
---|
| 956 | );
|
---|
| 957 | if (isSymbol(key) ? builtInSymbols.has(key) : isNonTrackableKeys(key)) {
|
---|
| 958 | return res;
|
---|
| 959 | }
|
---|
| 960 | if (!isReadonly2) {
|
---|
| 961 | track(target, "get", key);
|
---|
| 962 | }
|
---|
| 963 | if (isShallow2) {
|
---|
| 964 | return res;
|
---|
| 965 | }
|
---|
| 966 | if (isRef(res)) {
|
---|
| 967 | return targetIsArray && isIntegerKey(key) ? res : res.value;
|
---|
| 968 | }
|
---|
| 969 | if (isObject(res)) {
|
---|
| 970 | return isReadonly2 ? readonly(res) : reactive(res);
|
---|
| 971 | }
|
---|
| 972 | return res;
|
---|
| 973 | }
|
---|
| 974 | }
|
---|
| 975 | class MutableReactiveHandler extends BaseReactiveHandler {
|
---|
| 976 | constructor(isShallow2 = false) {
|
---|
| 977 | super(false, isShallow2);
|
---|
| 978 | }
|
---|
| 979 | set(target, key, value, receiver) {
|
---|
| 980 | let oldValue = target[key];
|
---|
| 981 | if (!this._isShallow) {
|
---|
| 982 | const isOldValueReadonly = isReadonly(oldValue);
|
---|
| 983 | if (!isShallow(value) && !isReadonly(value)) {
|
---|
| 984 | oldValue = toRaw(oldValue);
|
---|
| 985 | value = toRaw(value);
|
---|
| 986 | }
|
---|
| 987 | if (!isArray(target) && isRef(oldValue) && !isRef(value)) {
|
---|
| 988 | if (isOldValueReadonly) {
|
---|
| 989 | return false;
|
---|
| 990 | } else {
|
---|
| 991 | oldValue.value = value;
|
---|
| 992 | return true;
|
---|
| 993 | }
|
---|
| 994 | }
|
---|
| 995 | }
|
---|
| 996 | const hadKey = isArray(target) && isIntegerKey(key) ? Number(key) < target.length : hasOwn(target, key);
|
---|
| 997 | const result = Reflect.set(
|
---|
| 998 | target,
|
---|
| 999 | key,
|
---|
| 1000 | value,
|
---|
| 1001 | isRef(target) ? target : receiver
|
---|
| 1002 | );
|
---|
| 1003 | if (target === toRaw(receiver)) {
|
---|
| 1004 | if (!hadKey) {
|
---|
| 1005 | trigger(target, "add", key, value);
|
---|
| 1006 | } else if (hasChanged(value, oldValue)) {
|
---|
| 1007 | trigger(target, "set", key, value, oldValue);
|
---|
| 1008 | }
|
---|
| 1009 | }
|
---|
| 1010 | return result;
|
---|
| 1011 | }
|
---|
| 1012 | deleteProperty(target, key) {
|
---|
| 1013 | const hadKey = hasOwn(target, key);
|
---|
| 1014 | const oldValue = target[key];
|
---|
| 1015 | const result = Reflect.deleteProperty(target, key);
|
---|
| 1016 | if (result && hadKey) {
|
---|
| 1017 | trigger(target, "delete", key, void 0, oldValue);
|
---|
| 1018 | }
|
---|
| 1019 | return result;
|
---|
| 1020 | }
|
---|
| 1021 | has(target, key) {
|
---|
| 1022 | const result = Reflect.has(target, key);
|
---|
| 1023 | if (!isSymbol(key) || !builtInSymbols.has(key)) {
|
---|
| 1024 | track(target, "has", key);
|
---|
| 1025 | }
|
---|
| 1026 | return result;
|
---|
| 1027 | }
|
---|
| 1028 | ownKeys(target) {
|
---|
| 1029 | track(
|
---|
| 1030 | target,
|
---|
| 1031 | "iterate",
|
---|
| 1032 | isArray(target) ? "length" : ITERATE_KEY
|
---|
| 1033 | );
|
---|
| 1034 | return Reflect.ownKeys(target);
|
---|
| 1035 | }
|
---|
| 1036 | }
|
---|
| 1037 | class ReadonlyReactiveHandler extends BaseReactiveHandler {
|
---|
| 1038 | constructor(isShallow2 = false) {
|
---|
| 1039 | super(true, isShallow2);
|
---|
| 1040 | }
|
---|
| 1041 | set(target, key) {
|
---|
| 1042 | if (!!(process.env.NODE_ENV !== "production")) {
|
---|
| 1043 | warn(
|
---|
| 1044 | `Set operation on key "${String(key)}" failed: target is readonly.`,
|
---|
| 1045 | target
|
---|
| 1046 | );
|
---|
| 1047 | }
|
---|
| 1048 | return true;
|
---|
| 1049 | }
|
---|
| 1050 | deleteProperty(target, key) {
|
---|
| 1051 | if (!!(process.env.NODE_ENV !== "production")) {
|
---|
| 1052 | warn(
|
---|
| 1053 | `Delete operation on key "${String(key)}" failed: target is readonly.`,
|
---|
| 1054 | target
|
---|
| 1055 | );
|
---|
| 1056 | }
|
---|
| 1057 | return true;
|
---|
| 1058 | }
|
---|
| 1059 | }
|
---|
| 1060 | const mutableHandlers = /* @__PURE__ */ new MutableReactiveHandler();
|
---|
| 1061 | const readonlyHandlers = /* @__PURE__ */ new ReadonlyReactiveHandler();
|
---|
| 1062 | const shallowReactiveHandlers = /* @__PURE__ */ new MutableReactiveHandler(true);
|
---|
| 1063 | const shallowReadonlyHandlers = /* @__PURE__ */ new ReadonlyReactiveHandler(true);
|
---|
| 1064 |
|
---|
| 1065 | const toShallow = (value) => value;
|
---|
| 1066 | const getProto = (v) => Reflect.getPrototypeOf(v);
|
---|
| 1067 | function createIterableMethod(method, isReadonly2, isShallow2) {
|
---|
| 1068 | return function(...args) {
|
---|
| 1069 | const target = this["__v_raw"];
|
---|
| 1070 | const rawTarget = toRaw(target);
|
---|
| 1071 | const targetIsMap = isMap(rawTarget);
|
---|
| 1072 | const isPair = method === "entries" || method === Symbol.iterator && targetIsMap;
|
---|
| 1073 | const isKeyOnly = method === "keys" && targetIsMap;
|
---|
| 1074 | const innerIterator = target[method](...args);
|
---|
| 1075 | const wrap = isShallow2 ? toShallow : isReadonly2 ? toReadonly : toReactive;
|
---|
| 1076 | !isReadonly2 && track(
|
---|
| 1077 | rawTarget,
|
---|
| 1078 | "iterate",
|
---|
| 1079 | isKeyOnly ? MAP_KEY_ITERATE_KEY : ITERATE_KEY
|
---|
| 1080 | );
|
---|
| 1081 | return {
|
---|
| 1082 | // iterator protocol
|
---|
| 1083 | next() {
|
---|
| 1084 | const { value, done } = innerIterator.next();
|
---|
| 1085 | return done ? { value, done } : {
|
---|
| 1086 | value: isPair ? [wrap(value[0]), wrap(value[1])] : wrap(value),
|
---|
| 1087 | done
|
---|
| 1088 | };
|
---|
| 1089 | },
|
---|
| 1090 | // iterable protocol
|
---|
| 1091 | [Symbol.iterator]() {
|
---|
| 1092 | return this;
|
---|
| 1093 | }
|
---|
| 1094 | };
|
---|
| 1095 | };
|
---|
| 1096 | }
|
---|
| 1097 | function createReadonlyMethod(type) {
|
---|
| 1098 | return function(...args) {
|
---|
| 1099 | if (!!(process.env.NODE_ENV !== "production")) {
|
---|
| 1100 | const key = args[0] ? `on key "${args[0]}" ` : ``;
|
---|
| 1101 | warn(
|
---|
| 1102 | `${capitalize(type)} operation ${key}failed: target is readonly.`,
|
---|
| 1103 | toRaw(this)
|
---|
| 1104 | );
|
---|
| 1105 | }
|
---|
| 1106 | return type === "delete" ? false : type === "clear" ? void 0 : this;
|
---|
| 1107 | };
|
---|
| 1108 | }
|
---|
| 1109 | function createInstrumentations(readonly, shallow) {
|
---|
| 1110 | const instrumentations = {
|
---|
| 1111 | get(key) {
|
---|
| 1112 | const target = this["__v_raw"];
|
---|
| 1113 | const rawTarget = toRaw(target);
|
---|
| 1114 | const rawKey = toRaw(key);
|
---|
| 1115 | if (!readonly) {
|
---|
| 1116 | if (hasChanged(key, rawKey)) {
|
---|
| 1117 | track(rawTarget, "get", key);
|
---|
| 1118 | }
|
---|
| 1119 | track(rawTarget, "get", rawKey);
|
---|
| 1120 | }
|
---|
| 1121 | const { has } = getProto(rawTarget);
|
---|
| 1122 | const wrap = shallow ? toShallow : readonly ? toReadonly : toReactive;
|
---|
| 1123 | if (has.call(rawTarget, key)) {
|
---|
| 1124 | return wrap(target.get(key));
|
---|
| 1125 | } else if (has.call(rawTarget, rawKey)) {
|
---|
| 1126 | return wrap(target.get(rawKey));
|
---|
| 1127 | } else if (target !== rawTarget) {
|
---|
| 1128 | target.get(key);
|
---|
| 1129 | }
|
---|
| 1130 | },
|
---|
| 1131 | get size() {
|
---|
| 1132 | const target = this["__v_raw"];
|
---|
| 1133 | !readonly && track(toRaw(target), "iterate", ITERATE_KEY);
|
---|
| 1134 | return Reflect.get(target, "size", target);
|
---|
| 1135 | },
|
---|
| 1136 | has(key) {
|
---|
| 1137 | const target = this["__v_raw"];
|
---|
| 1138 | const rawTarget = toRaw(target);
|
---|
| 1139 | const rawKey = toRaw(key);
|
---|
| 1140 | if (!readonly) {
|
---|
| 1141 | if (hasChanged(key, rawKey)) {
|
---|
| 1142 | track(rawTarget, "has", key);
|
---|
| 1143 | }
|
---|
| 1144 | track(rawTarget, "has", rawKey);
|
---|
| 1145 | }
|
---|
| 1146 | return key === rawKey ? target.has(key) : target.has(key) || target.has(rawKey);
|
---|
| 1147 | },
|
---|
| 1148 | forEach(callback, thisArg) {
|
---|
| 1149 | const observed = this;
|
---|
| 1150 | const target = observed["__v_raw"];
|
---|
| 1151 | const rawTarget = toRaw(target);
|
---|
| 1152 | const wrap = shallow ? toShallow : readonly ? toReadonly : toReactive;
|
---|
| 1153 | !readonly && track(rawTarget, "iterate", ITERATE_KEY);
|
---|
| 1154 | return target.forEach((value, key) => {
|
---|
| 1155 | return callback.call(thisArg, wrap(value), wrap(key), observed);
|
---|
| 1156 | });
|
---|
| 1157 | }
|
---|
| 1158 | };
|
---|
| 1159 | extend(
|
---|
| 1160 | instrumentations,
|
---|
| 1161 | readonly ? {
|
---|
| 1162 | add: createReadonlyMethod("add"),
|
---|
| 1163 | set: createReadonlyMethod("set"),
|
---|
| 1164 | delete: createReadonlyMethod("delete"),
|
---|
| 1165 | clear: createReadonlyMethod("clear")
|
---|
| 1166 | } : {
|
---|
| 1167 | add(value) {
|
---|
| 1168 | if (!shallow && !isShallow(value) && !isReadonly(value)) {
|
---|
| 1169 | value = toRaw(value);
|
---|
| 1170 | }
|
---|
| 1171 | const target = toRaw(this);
|
---|
| 1172 | const proto = getProto(target);
|
---|
| 1173 | const hadKey = proto.has.call(target, value);
|
---|
| 1174 | if (!hadKey) {
|
---|
| 1175 | target.add(value);
|
---|
| 1176 | trigger(target, "add", value, value);
|
---|
| 1177 | }
|
---|
| 1178 | return this;
|
---|
| 1179 | },
|
---|
| 1180 | set(key, value) {
|
---|
| 1181 | if (!shallow && !isShallow(value) && !isReadonly(value)) {
|
---|
| 1182 | value = toRaw(value);
|
---|
| 1183 | }
|
---|
| 1184 | const target = toRaw(this);
|
---|
| 1185 | const { has, get } = getProto(target);
|
---|
| 1186 | let hadKey = has.call(target, key);
|
---|
| 1187 | if (!hadKey) {
|
---|
| 1188 | key = toRaw(key);
|
---|
| 1189 | hadKey = has.call(target, key);
|
---|
| 1190 | } else if (!!(process.env.NODE_ENV !== "production")) {
|
---|
| 1191 | checkIdentityKeys(target, has, key);
|
---|
| 1192 | }
|
---|
| 1193 | const oldValue = get.call(target, key);
|
---|
| 1194 | target.set(key, value);
|
---|
| 1195 | if (!hadKey) {
|
---|
| 1196 | trigger(target, "add", key, value);
|
---|
| 1197 | } else if (hasChanged(value, oldValue)) {
|
---|
| 1198 | trigger(target, "set", key, value, oldValue);
|
---|
| 1199 | }
|
---|
| 1200 | return this;
|
---|
| 1201 | },
|
---|
| 1202 | delete(key) {
|
---|
| 1203 | const target = toRaw(this);
|
---|
| 1204 | const { has, get } = getProto(target);
|
---|
| 1205 | let hadKey = has.call(target, key);
|
---|
| 1206 | if (!hadKey) {
|
---|
| 1207 | key = toRaw(key);
|
---|
| 1208 | hadKey = has.call(target, key);
|
---|
| 1209 | } else if (!!(process.env.NODE_ENV !== "production")) {
|
---|
| 1210 | checkIdentityKeys(target, has, key);
|
---|
| 1211 | }
|
---|
| 1212 | const oldValue = get ? get.call(target, key) : void 0;
|
---|
| 1213 | const result = target.delete(key);
|
---|
| 1214 | if (hadKey) {
|
---|
| 1215 | trigger(target, "delete", key, void 0, oldValue);
|
---|
| 1216 | }
|
---|
| 1217 | return result;
|
---|
| 1218 | },
|
---|
| 1219 | clear() {
|
---|
| 1220 | const target = toRaw(this);
|
---|
| 1221 | const hadItems = target.size !== 0;
|
---|
| 1222 | const oldTarget = !!(process.env.NODE_ENV !== "production") ? isMap(target) ? new Map(target) : new Set(target) : void 0;
|
---|
| 1223 | const result = target.clear();
|
---|
| 1224 | if (hadItems) {
|
---|
| 1225 | trigger(
|
---|
| 1226 | target,
|
---|
| 1227 | "clear",
|
---|
| 1228 | void 0,
|
---|
| 1229 | void 0,
|
---|
| 1230 | oldTarget
|
---|
| 1231 | );
|
---|
| 1232 | }
|
---|
| 1233 | return result;
|
---|
| 1234 | }
|
---|
| 1235 | }
|
---|
| 1236 | );
|
---|
| 1237 | const iteratorMethods = [
|
---|
| 1238 | "keys",
|
---|
| 1239 | "values",
|
---|
| 1240 | "entries",
|
---|
| 1241 | Symbol.iterator
|
---|
| 1242 | ];
|
---|
| 1243 | iteratorMethods.forEach((method) => {
|
---|
| 1244 | instrumentations[method] = createIterableMethod(method, readonly, shallow);
|
---|
| 1245 | });
|
---|
| 1246 | return instrumentations;
|
---|
| 1247 | }
|
---|
| 1248 | function createInstrumentationGetter(isReadonly2, shallow) {
|
---|
| 1249 | const instrumentations = createInstrumentations(isReadonly2, shallow);
|
---|
| 1250 | return (target, key, receiver) => {
|
---|
| 1251 | if (key === "__v_isReactive") {
|
---|
| 1252 | return !isReadonly2;
|
---|
| 1253 | } else if (key === "__v_isReadonly") {
|
---|
| 1254 | return isReadonly2;
|
---|
| 1255 | } else if (key === "__v_raw") {
|
---|
| 1256 | return target;
|
---|
| 1257 | }
|
---|
| 1258 | return Reflect.get(
|
---|
| 1259 | hasOwn(instrumentations, key) && key in target ? instrumentations : target,
|
---|
| 1260 | key,
|
---|
| 1261 | receiver
|
---|
| 1262 | );
|
---|
| 1263 | };
|
---|
| 1264 | }
|
---|
| 1265 | const mutableCollectionHandlers = {
|
---|
| 1266 | get: /* @__PURE__ */ createInstrumentationGetter(false, false)
|
---|
| 1267 | };
|
---|
| 1268 | const shallowCollectionHandlers = {
|
---|
| 1269 | get: /* @__PURE__ */ createInstrumentationGetter(false, true)
|
---|
| 1270 | };
|
---|
| 1271 | const readonlyCollectionHandlers = {
|
---|
| 1272 | get: /* @__PURE__ */ createInstrumentationGetter(true, false)
|
---|
| 1273 | };
|
---|
| 1274 | const shallowReadonlyCollectionHandlers = {
|
---|
| 1275 | get: /* @__PURE__ */ createInstrumentationGetter(true, true)
|
---|
| 1276 | };
|
---|
| 1277 | function checkIdentityKeys(target, has, key) {
|
---|
| 1278 | const rawKey = toRaw(key);
|
---|
| 1279 | if (rawKey !== key && has.call(target, rawKey)) {
|
---|
| 1280 | const type = toRawType(target);
|
---|
| 1281 | warn(
|
---|
| 1282 | `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.`
|
---|
| 1283 | );
|
---|
| 1284 | }
|
---|
| 1285 | }
|
---|
| 1286 |
|
---|
| 1287 | const reactiveMap = /* @__PURE__ */ new WeakMap();
|
---|
| 1288 | const shallowReactiveMap = /* @__PURE__ */ new WeakMap();
|
---|
| 1289 | const readonlyMap = /* @__PURE__ */ new WeakMap();
|
---|
| 1290 | const shallowReadonlyMap = /* @__PURE__ */ new WeakMap();
|
---|
| 1291 | function targetTypeMap(rawType) {
|
---|
| 1292 | switch (rawType) {
|
---|
| 1293 | case "Object":
|
---|
| 1294 | case "Array":
|
---|
| 1295 | return 1 /* COMMON */;
|
---|
| 1296 | case "Map":
|
---|
| 1297 | case "Set":
|
---|
| 1298 | case "WeakMap":
|
---|
| 1299 | case "WeakSet":
|
---|
| 1300 | return 2 /* COLLECTION */;
|
---|
| 1301 | default:
|
---|
| 1302 | return 0 /* INVALID */;
|
---|
| 1303 | }
|
---|
| 1304 | }
|
---|
| 1305 | function getTargetType(value) {
|
---|
| 1306 | return value["__v_skip"] || !Object.isExtensible(value) ? 0 /* INVALID */ : targetTypeMap(toRawType(value));
|
---|
| 1307 | }
|
---|
| 1308 | function reactive(target) {
|
---|
| 1309 | if (isReadonly(target)) {
|
---|
| 1310 | return target;
|
---|
| 1311 | }
|
---|
| 1312 | return createReactiveObject(
|
---|
| 1313 | target,
|
---|
| 1314 | false,
|
---|
| 1315 | mutableHandlers,
|
---|
| 1316 | mutableCollectionHandlers,
|
---|
| 1317 | reactiveMap
|
---|
| 1318 | );
|
---|
| 1319 | }
|
---|
| 1320 | function shallowReactive(target) {
|
---|
| 1321 | return createReactiveObject(
|
---|
| 1322 | target,
|
---|
| 1323 | false,
|
---|
| 1324 | shallowReactiveHandlers,
|
---|
| 1325 | shallowCollectionHandlers,
|
---|
| 1326 | shallowReactiveMap
|
---|
| 1327 | );
|
---|
| 1328 | }
|
---|
| 1329 | function readonly(target) {
|
---|
| 1330 | return createReactiveObject(
|
---|
| 1331 | target,
|
---|
| 1332 | true,
|
---|
| 1333 | readonlyHandlers,
|
---|
| 1334 | readonlyCollectionHandlers,
|
---|
| 1335 | readonlyMap
|
---|
| 1336 | );
|
---|
| 1337 | }
|
---|
| 1338 | function shallowReadonly(target) {
|
---|
| 1339 | return createReactiveObject(
|
---|
| 1340 | target,
|
---|
| 1341 | true,
|
---|
| 1342 | shallowReadonlyHandlers,
|
---|
| 1343 | shallowReadonlyCollectionHandlers,
|
---|
| 1344 | shallowReadonlyMap
|
---|
| 1345 | );
|
---|
| 1346 | }
|
---|
| 1347 | function createReactiveObject(target, isReadonly2, baseHandlers, collectionHandlers, proxyMap) {
|
---|
| 1348 | if (!isObject(target)) {
|
---|
| 1349 | if (!!(process.env.NODE_ENV !== "production")) {
|
---|
| 1350 | warn(
|
---|
| 1351 | `value cannot be made ${isReadonly2 ? "readonly" : "reactive"}: ${String(
|
---|
| 1352 | target
|
---|
| 1353 | )}`
|
---|
| 1354 | );
|
---|
| 1355 | }
|
---|
| 1356 | return target;
|
---|
| 1357 | }
|
---|
| 1358 | if (target["__v_raw"] && !(isReadonly2 && target["__v_isReactive"])) {
|
---|
| 1359 | return target;
|
---|
| 1360 | }
|
---|
| 1361 | const existingProxy = proxyMap.get(target);
|
---|
| 1362 | if (existingProxy) {
|
---|
| 1363 | return existingProxy;
|
---|
| 1364 | }
|
---|
| 1365 | const targetType = getTargetType(target);
|
---|
| 1366 | if (targetType === 0 /* INVALID */) {
|
---|
| 1367 | return target;
|
---|
| 1368 | }
|
---|
| 1369 | const proxy = new Proxy(
|
---|
| 1370 | target,
|
---|
| 1371 | targetType === 2 /* COLLECTION */ ? collectionHandlers : baseHandlers
|
---|
| 1372 | );
|
---|
| 1373 | proxyMap.set(target, proxy);
|
---|
| 1374 | return proxy;
|
---|
| 1375 | }
|
---|
| 1376 | function isReactive(value) {
|
---|
| 1377 | if (isReadonly(value)) {
|
---|
| 1378 | return isReactive(value["__v_raw"]);
|
---|
| 1379 | }
|
---|
| 1380 | return !!(value && value["__v_isReactive"]);
|
---|
| 1381 | }
|
---|
| 1382 | function isReadonly(value) {
|
---|
| 1383 | return !!(value && value["__v_isReadonly"]);
|
---|
| 1384 | }
|
---|
| 1385 | function isShallow(value) {
|
---|
| 1386 | return !!(value && value["__v_isShallow"]);
|
---|
| 1387 | }
|
---|
| 1388 | function isProxy(value) {
|
---|
| 1389 | return value ? !!value["__v_raw"] : false;
|
---|
| 1390 | }
|
---|
| 1391 | function toRaw(observed) {
|
---|
| 1392 | const raw = observed && observed["__v_raw"];
|
---|
| 1393 | return raw ? toRaw(raw) : observed;
|
---|
| 1394 | }
|
---|
| 1395 | function markRaw(value) {
|
---|
| 1396 | if (!hasOwn(value, "__v_skip") && Object.isExtensible(value)) {
|
---|
| 1397 | def(value, "__v_skip", true);
|
---|
| 1398 | }
|
---|
| 1399 | return value;
|
---|
| 1400 | }
|
---|
| 1401 | const toReactive = (value) => isObject(value) ? reactive(value) : value;
|
---|
| 1402 | const toReadonly = (value) => isObject(value) ? readonly(value) : value;
|
---|
| 1403 |
|
---|
| 1404 | function isRef(r) {
|
---|
| 1405 | return r ? r["__v_isRef"] === true : false;
|
---|
| 1406 | }
|
---|
| 1407 | function ref(value) {
|
---|
| 1408 | return createRef(value, false);
|
---|
| 1409 | }
|
---|
| 1410 | function shallowRef(value) {
|
---|
| 1411 | return createRef(value, true);
|
---|
| 1412 | }
|
---|
| 1413 | function createRef(rawValue, shallow) {
|
---|
| 1414 | if (isRef(rawValue)) {
|
---|
| 1415 | return rawValue;
|
---|
| 1416 | }
|
---|
| 1417 | return new RefImpl(rawValue, shallow);
|
---|
| 1418 | }
|
---|
| 1419 | class RefImpl {
|
---|
| 1420 | constructor(value, isShallow2) {
|
---|
| 1421 | this.dep = new Dep();
|
---|
| 1422 | this["__v_isRef"] = true;
|
---|
| 1423 | this["__v_isShallow"] = false;
|
---|
| 1424 | this._rawValue = isShallow2 ? value : toRaw(value);
|
---|
| 1425 | this._value = isShallow2 ? value : toReactive(value);
|
---|
| 1426 | this["__v_isShallow"] = isShallow2;
|
---|
| 1427 | }
|
---|
| 1428 | get value() {
|
---|
| 1429 | if (!!(process.env.NODE_ENV !== "production")) {
|
---|
| 1430 | this.dep.track({
|
---|
| 1431 | target: this,
|
---|
| 1432 | type: "get",
|
---|
| 1433 | key: "value"
|
---|
| 1434 | });
|
---|
| 1435 | } else {
|
---|
| 1436 | this.dep.track();
|
---|
| 1437 | }
|
---|
| 1438 | return this._value;
|
---|
| 1439 | }
|
---|
| 1440 | set value(newValue) {
|
---|
| 1441 | const oldValue = this._rawValue;
|
---|
| 1442 | const useDirectValue = this["__v_isShallow"] || isShallow(newValue) || isReadonly(newValue);
|
---|
| 1443 | newValue = useDirectValue ? newValue : toRaw(newValue);
|
---|
| 1444 | if (hasChanged(newValue, oldValue)) {
|
---|
| 1445 | this._rawValue = newValue;
|
---|
| 1446 | this._value = useDirectValue ? newValue : toReactive(newValue);
|
---|
| 1447 | if (!!(process.env.NODE_ENV !== "production")) {
|
---|
| 1448 | this.dep.trigger({
|
---|
| 1449 | target: this,
|
---|
| 1450 | type: "set",
|
---|
| 1451 | key: "value",
|
---|
| 1452 | newValue,
|
---|
| 1453 | oldValue
|
---|
| 1454 | });
|
---|
| 1455 | } else {
|
---|
| 1456 | this.dep.trigger();
|
---|
| 1457 | }
|
---|
| 1458 | }
|
---|
| 1459 | }
|
---|
| 1460 | }
|
---|
| 1461 | function triggerRef(ref2) {
|
---|
| 1462 | if (ref2.dep) {
|
---|
| 1463 | if (!!(process.env.NODE_ENV !== "production")) {
|
---|
| 1464 | ref2.dep.trigger({
|
---|
| 1465 | target: ref2,
|
---|
| 1466 | type: "set",
|
---|
| 1467 | key: "value",
|
---|
| 1468 | newValue: ref2._value
|
---|
| 1469 | });
|
---|
| 1470 | } else {
|
---|
| 1471 | ref2.dep.trigger();
|
---|
| 1472 | }
|
---|
| 1473 | }
|
---|
| 1474 | }
|
---|
| 1475 | function unref(ref2) {
|
---|
| 1476 | return isRef(ref2) ? ref2.value : ref2;
|
---|
| 1477 | }
|
---|
| 1478 | function toValue(source) {
|
---|
| 1479 | return isFunction(source) ? source() : unref(source);
|
---|
| 1480 | }
|
---|
| 1481 | const shallowUnwrapHandlers = {
|
---|
| 1482 | get: (target, key, receiver) => key === "__v_raw" ? target : unref(Reflect.get(target, key, receiver)),
|
---|
| 1483 | set: (target, key, value, receiver) => {
|
---|
| 1484 | const oldValue = target[key];
|
---|
| 1485 | if (isRef(oldValue) && !isRef(value)) {
|
---|
| 1486 | oldValue.value = value;
|
---|
| 1487 | return true;
|
---|
| 1488 | } else {
|
---|
| 1489 | return Reflect.set(target, key, value, receiver);
|
---|
| 1490 | }
|
---|
| 1491 | }
|
---|
| 1492 | };
|
---|
| 1493 | function proxyRefs(objectWithRefs) {
|
---|
| 1494 | return isReactive(objectWithRefs) ? objectWithRefs : new Proxy(objectWithRefs, shallowUnwrapHandlers);
|
---|
| 1495 | }
|
---|
| 1496 | class CustomRefImpl {
|
---|
| 1497 | constructor(factory) {
|
---|
| 1498 | this["__v_isRef"] = true;
|
---|
| 1499 | this._value = void 0;
|
---|
| 1500 | const dep = this.dep = new Dep();
|
---|
| 1501 | const { get, set } = factory(dep.track.bind(dep), dep.trigger.bind(dep));
|
---|
| 1502 | this._get = get;
|
---|
| 1503 | this._set = set;
|
---|
| 1504 | }
|
---|
| 1505 | get value() {
|
---|
| 1506 | return this._value = this._get();
|
---|
| 1507 | }
|
---|
| 1508 | set value(newVal) {
|
---|
| 1509 | this._set(newVal);
|
---|
| 1510 | }
|
---|
| 1511 | }
|
---|
| 1512 | function customRef(factory) {
|
---|
| 1513 | return new CustomRefImpl(factory);
|
---|
| 1514 | }
|
---|
| 1515 | function toRefs(object) {
|
---|
| 1516 | if (!!(process.env.NODE_ENV !== "production") && !isProxy(object)) {
|
---|
| 1517 | warn(`toRefs() expects a reactive object but received a plain one.`);
|
---|
| 1518 | }
|
---|
| 1519 | const ret = isArray(object) ? new Array(object.length) : {};
|
---|
| 1520 | for (const key in object) {
|
---|
| 1521 | ret[key] = propertyToRef(object, key);
|
---|
| 1522 | }
|
---|
| 1523 | return ret;
|
---|
| 1524 | }
|
---|
| 1525 | class ObjectRefImpl {
|
---|
| 1526 | constructor(_object, _key, _defaultValue) {
|
---|
| 1527 | this._object = _object;
|
---|
| 1528 | this._key = _key;
|
---|
| 1529 | this._defaultValue = _defaultValue;
|
---|
| 1530 | this["__v_isRef"] = true;
|
---|
| 1531 | this._value = void 0;
|
---|
| 1532 | }
|
---|
| 1533 | get value() {
|
---|
| 1534 | const val = this._object[this._key];
|
---|
| 1535 | return this._value = val === void 0 ? this._defaultValue : val;
|
---|
| 1536 | }
|
---|
| 1537 | set value(newVal) {
|
---|
| 1538 | this._object[this._key] = newVal;
|
---|
| 1539 | }
|
---|
| 1540 | get dep() {
|
---|
| 1541 | return getDepFromReactive(toRaw(this._object), this._key);
|
---|
| 1542 | }
|
---|
| 1543 | }
|
---|
| 1544 | class GetterRefImpl {
|
---|
| 1545 | constructor(_getter) {
|
---|
| 1546 | this._getter = _getter;
|
---|
| 1547 | this["__v_isRef"] = true;
|
---|
| 1548 | this["__v_isReadonly"] = true;
|
---|
| 1549 | this._value = void 0;
|
---|
| 1550 | }
|
---|
| 1551 | get value() {
|
---|
| 1552 | return this._value = this._getter();
|
---|
| 1553 | }
|
---|
| 1554 | }
|
---|
| 1555 | function toRef(source, key, defaultValue) {
|
---|
| 1556 | if (isRef(source)) {
|
---|
| 1557 | return source;
|
---|
| 1558 | } else if (isFunction(source)) {
|
---|
| 1559 | return new GetterRefImpl(source);
|
---|
| 1560 | } else if (isObject(source) && arguments.length > 1) {
|
---|
| 1561 | return propertyToRef(source, key, defaultValue);
|
---|
| 1562 | } else {
|
---|
| 1563 | return ref(source);
|
---|
| 1564 | }
|
---|
| 1565 | }
|
---|
| 1566 | function propertyToRef(source, key, defaultValue) {
|
---|
| 1567 | const val = source[key];
|
---|
| 1568 | return isRef(val) ? val : new ObjectRefImpl(source, key, defaultValue);
|
---|
| 1569 | }
|
---|
| 1570 |
|
---|
| 1571 | class ComputedRefImpl {
|
---|
| 1572 | constructor(fn, setter, isSSR) {
|
---|
| 1573 | this.fn = fn;
|
---|
| 1574 | this.setter = setter;
|
---|
| 1575 | /**
|
---|
| 1576 | * @internal
|
---|
| 1577 | */
|
---|
| 1578 | this._value = void 0;
|
---|
| 1579 | /**
|
---|
| 1580 | * @internal
|
---|
| 1581 | */
|
---|
| 1582 | this.dep = new Dep(this);
|
---|
| 1583 | /**
|
---|
| 1584 | * @internal
|
---|
| 1585 | */
|
---|
| 1586 | this.__v_isRef = true;
|
---|
| 1587 | // TODO isolatedDeclarations "__v_isReadonly"
|
---|
| 1588 | // A computed is also a subscriber that tracks other deps
|
---|
| 1589 | /**
|
---|
| 1590 | * @internal
|
---|
| 1591 | */
|
---|
| 1592 | this.deps = void 0;
|
---|
| 1593 | /**
|
---|
| 1594 | * @internal
|
---|
| 1595 | */
|
---|
| 1596 | this.depsTail = void 0;
|
---|
| 1597 | /**
|
---|
| 1598 | * @internal
|
---|
| 1599 | */
|
---|
| 1600 | this.flags = 16;
|
---|
| 1601 | /**
|
---|
| 1602 | * @internal
|
---|
| 1603 | */
|
---|
| 1604 | this.globalVersion = globalVersion - 1;
|
---|
| 1605 | /**
|
---|
| 1606 | * @internal
|
---|
| 1607 | */
|
---|
| 1608 | this.next = void 0;
|
---|
| 1609 | // for backwards compat
|
---|
| 1610 | this.effect = this;
|
---|
| 1611 | this["__v_isReadonly"] = !setter;
|
---|
| 1612 | this.isSSR = isSSR;
|
---|
| 1613 | }
|
---|
| 1614 | /**
|
---|
| 1615 | * @internal
|
---|
| 1616 | */
|
---|
| 1617 | notify() {
|
---|
| 1618 | this.flags |= 16;
|
---|
| 1619 | if (!(this.flags & 8) && // avoid infinite self recursion
|
---|
| 1620 | activeSub !== this) {
|
---|
| 1621 | batch(this, true);
|
---|
| 1622 | return true;
|
---|
| 1623 | } else if (!!(process.env.NODE_ENV !== "production")) ;
|
---|
| 1624 | }
|
---|
| 1625 | get value() {
|
---|
| 1626 | const link = !!(process.env.NODE_ENV !== "production") ? this.dep.track({
|
---|
| 1627 | target: this,
|
---|
| 1628 | type: "get",
|
---|
| 1629 | key: "value"
|
---|
| 1630 | }) : this.dep.track();
|
---|
| 1631 | refreshComputed(this);
|
---|
| 1632 | if (link) {
|
---|
| 1633 | link.version = this.dep.version;
|
---|
| 1634 | }
|
---|
| 1635 | return this._value;
|
---|
| 1636 | }
|
---|
| 1637 | set value(newValue) {
|
---|
| 1638 | if (this.setter) {
|
---|
| 1639 | this.setter(newValue);
|
---|
| 1640 | } else if (!!(process.env.NODE_ENV !== "production")) {
|
---|
| 1641 | warn("Write operation failed: computed value is readonly");
|
---|
| 1642 | }
|
---|
| 1643 | }
|
---|
| 1644 | }
|
---|
| 1645 | function computed(getterOrOptions, debugOptions, isSSR = false) {
|
---|
| 1646 | let getter;
|
---|
| 1647 | let setter;
|
---|
| 1648 | if (isFunction(getterOrOptions)) {
|
---|
| 1649 | getter = getterOrOptions;
|
---|
| 1650 | } else {
|
---|
| 1651 | getter = getterOrOptions.get;
|
---|
| 1652 | setter = getterOrOptions.set;
|
---|
| 1653 | }
|
---|
| 1654 | const cRef = new ComputedRefImpl(getter, setter, isSSR);
|
---|
| 1655 | if (!!(process.env.NODE_ENV !== "production") && debugOptions && !isSSR) {
|
---|
| 1656 | cRef.onTrack = debugOptions.onTrack;
|
---|
| 1657 | cRef.onTrigger = debugOptions.onTrigger;
|
---|
| 1658 | }
|
---|
| 1659 | return cRef;
|
---|
| 1660 | }
|
---|
| 1661 |
|
---|
| 1662 | const TrackOpTypes = {
|
---|
| 1663 | "GET": "get",
|
---|
| 1664 | "HAS": "has",
|
---|
| 1665 | "ITERATE": "iterate"
|
---|
| 1666 | };
|
---|
| 1667 | const TriggerOpTypes = {
|
---|
| 1668 | "SET": "set",
|
---|
| 1669 | "ADD": "add",
|
---|
| 1670 | "DELETE": "delete",
|
---|
| 1671 | "CLEAR": "clear"
|
---|
| 1672 | };
|
---|
| 1673 | const ReactiveFlags = {
|
---|
| 1674 | "SKIP": "__v_skip",
|
---|
| 1675 | "IS_REACTIVE": "__v_isReactive",
|
---|
| 1676 | "IS_READONLY": "__v_isReadonly",
|
---|
| 1677 | "IS_SHALLOW": "__v_isShallow",
|
---|
| 1678 | "RAW": "__v_raw",
|
---|
| 1679 | "IS_REF": "__v_isRef"
|
---|
| 1680 | };
|
---|
| 1681 |
|
---|
| 1682 | const WatchErrorCodes = {
|
---|
| 1683 | "WATCH_GETTER": 2,
|
---|
| 1684 | "2": "WATCH_GETTER",
|
---|
| 1685 | "WATCH_CALLBACK": 3,
|
---|
| 1686 | "3": "WATCH_CALLBACK",
|
---|
| 1687 | "WATCH_CLEANUP": 4,
|
---|
| 1688 | "4": "WATCH_CLEANUP"
|
---|
| 1689 | };
|
---|
| 1690 | const INITIAL_WATCHER_VALUE = {};
|
---|
| 1691 | const cleanupMap = /* @__PURE__ */ new WeakMap();
|
---|
| 1692 | let activeWatcher = void 0;
|
---|
| 1693 | function getCurrentWatcher() {
|
---|
| 1694 | return activeWatcher;
|
---|
| 1695 | }
|
---|
| 1696 | function onWatcherCleanup(cleanupFn, failSilently = false, owner = activeWatcher) {
|
---|
| 1697 | if (owner) {
|
---|
| 1698 | let cleanups = cleanupMap.get(owner);
|
---|
| 1699 | if (!cleanups) cleanupMap.set(owner, cleanups = []);
|
---|
| 1700 | cleanups.push(cleanupFn);
|
---|
| 1701 | } else if (!!(process.env.NODE_ENV !== "production") && !failSilently) {
|
---|
| 1702 | warn(
|
---|
| 1703 | `onWatcherCleanup() was called when there was no active watcher to associate with.`
|
---|
| 1704 | );
|
---|
| 1705 | }
|
---|
| 1706 | }
|
---|
| 1707 | function watch(source, cb, options = EMPTY_OBJ) {
|
---|
| 1708 | const { immediate, deep, once, scheduler, augmentJob, call } = options;
|
---|
| 1709 | const warnInvalidSource = (s) => {
|
---|
| 1710 | (options.onWarn || warn)(
|
---|
| 1711 | `Invalid watch source: `,
|
---|
| 1712 | s,
|
---|
| 1713 | `A watch source can only be a getter/effect function, a ref, a reactive object, or an array of these types.`
|
---|
| 1714 | );
|
---|
| 1715 | };
|
---|
| 1716 | const reactiveGetter = (source2) => {
|
---|
| 1717 | if (deep) return source2;
|
---|
| 1718 | if (isShallow(source2) || deep === false || deep === 0)
|
---|
| 1719 | return traverse(source2, 1);
|
---|
| 1720 | return traverse(source2);
|
---|
| 1721 | };
|
---|
| 1722 | let effect;
|
---|
| 1723 | let getter;
|
---|
| 1724 | let cleanup;
|
---|
| 1725 | let boundCleanup;
|
---|
| 1726 | let forceTrigger = false;
|
---|
| 1727 | let isMultiSource = false;
|
---|
| 1728 | if (isRef(source)) {
|
---|
| 1729 | getter = () => source.value;
|
---|
| 1730 | forceTrigger = isShallow(source);
|
---|
| 1731 | } else if (isReactive(source)) {
|
---|
| 1732 | getter = () => reactiveGetter(source);
|
---|
| 1733 | forceTrigger = true;
|
---|
| 1734 | } else if (isArray(source)) {
|
---|
| 1735 | isMultiSource = true;
|
---|
| 1736 | forceTrigger = source.some((s) => isReactive(s) || isShallow(s));
|
---|
| 1737 | getter = () => source.map((s) => {
|
---|
| 1738 | if (isRef(s)) {
|
---|
| 1739 | return s.value;
|
---|
| 1740 | } else if (isReactive(s)) {
|
---|
| 1741 | return reactiveGetter(s);
|
---|
| 1742 | } else if (isFunction(s)) {
|
---|
| 1743 | return call ? call(s, 2) : s();
|
---|
| 1744 | } else {
|
---|
| 1745 | !!(process.env.NODE_ENV !== "production") && warnInvalidSource(s);
|
---|
| 1746 | }
|
---|
| 1747 | });
|
---|
| 1748 | } else if (isFunction(source)) {
|
---|
| 1749 | if (cb) {
|
---|
| 1750 | getter = call ? () => call(source, 2) : source;
|
---|
| 1751 | } else {
|
---|
| 1752 | getter = () => {
|
---|
| 1753 | if (cleanup) {
|
---|
| 1754 | pauseTracking();
|
---|
| 1755 | try {
|
---|
| 1756 | cleanup();
|
---|
| 1757 | } finally {
|
---|
| 1758 | resetTracking();
|
---|
| 1759 | }
|
---|
| 1760 | }
|
---|
| 1761 | const currentEffect = activeWatcher;
|
---|
| 1762 | activeWatcher = effect;
|
---|
| 1763 | try {
|
---|
| 1764 | return call ? call(source, 3, [boundCleanup]) : source(boundCleanup);
|
---|
| 1765 | } finally {
|
---|
| 1766 | activeWatcher = currentEffect;
|
---|
| 1767 | }
|
---|
| 1768 | };
|
---|
| 1769 | }
|
---|
| 1770 | } else {
|
---|
| 1771 | getter = NOOP;
|
---|
| 1772 | !!(process.env.NODE_ENV !== "production") && warnInvalidSource(source);
|
---|
| 1773 | }
|
---|
| 1774 | if (cb && deep) {
|
---|
| 1775 | const baseGetter = getter;
|
---|
| 1776 | const depth = deep === true ? Infinity : deep;
|
---|
| 1777 | getter = () => traverse(baseGetter(), depth);
|
---|
| 1778 | }
|
---|
| 1779 | const scope = getCurrentScope();
|
---|
| 1780 | const watchHandle = () => {
|
---|
| 1781 | effect.stop();
|
---|
| 1782 | if (scope && scope.active) {
|
---|
| 1783 | remove(scope.effects, effect);
|
---|
| 1784 | }
|
---|
| 1785 | };
|
---|
| 1786 | if (once && cb) {
|
---|
| 1787 | const _cb = cb;
|
---|
| 1788 | cb = (...args) => {
|
---|
| 1789 | _cb(...args);
|
---|
| 1790 | watchHandle();
|
---|
| 1791 | };
|
---|
| 1792 | }
|
---|
| 1793 | let oldValue = isMultiSource ? new Array(source.length).fill(INITIAL_WATCHER_VALUE) : INITIAL_WATCHER_VALUE;
|
---|
| 1794 | const job = (immediateFirstRun) => {
|
---|
| 1795 | if (!(effect.flags & 1) || !effect.dirty && !immediateFirstRun) {
|
---|
| 1796 | return;
|
---|
| 1797 | }
|
---|
| 1798 | if (cb) {
|
---|
| 1799 | const newValue = effect.run();
|
---|
| 1800 | if (deep || forceTrigger || (isMultiSource ? newValue.some((v, i) => hasChanged(v, oldValue[i])) : hasChanged(newValue, oldValue))) {
|
---|
| 1801 | if (cleanup) {
|
---|
| 1802 | cleanup();
|
---|
| 1803 | }
|
---|
| 1804 | const currentWatcher = activeWatcher;
|
---|
| 1805 | activeWatcher = effect;
|
---|
| 1806 | try {
|
---|
| 1807 | const args = [
|
---|
| 1808 | newValue,
|
---|
| 1809 | // pass undefined as the old value when it's changed for the first time
|
---|
| 1810 | oldValue === INITIAL_WATCHER_VALUE ? void 0 : isMultiSource && oldValue[0] === INITIAL_WATCHER_VALUE ? [] : oldValue,
|
---|
| 1811 | boundCleanup
|
---|
| 1812 | ];
|
---|
| 1813 | call ? call(cb, 3, args) : (
|
---|
| 1814 | // @ts-expect-error
|
---|
| 1815 | cb(...args)
|
---|
| 1816 | );
|
---|
| 1817 | oldValue = newValue;
|
---|
| 1818 | } finally {
|
---|
| 1819 | activeWatcher = currentWatcher;
|
---|
| 1820 | }
|
---|
| 1821 | }
|
---|
| 1822 | } else {
|
---|
| 1823 | effect.run();
|
---|
| 1824 | }
|
---|
| 1825 | };
|
---|
| 1826 | if (augmentJob) {
|
---|
| 1827 | augmentJob(job);
|
---|
| 1828 | }
|
---|
| 1829 | effect = new ReactiveEffect(getter);
|
---|
| 1830 | effect.scheduler = scheduler ? () => scheduler(job, false) : job;
|
---|
| 1831 | boundCleanup = (fn) => onWatcherCleanup(fn, false, effect);
|
---|
| 1832 | cleanup = effect.onStop = () => {
|
---|
| 1833 | const cleanups = cleanupMap.get(effect);
|
---|
| 1834 | if (cleanups) {
|
---|
| 1835 | if (call) {
|
---|
| 1836 | call(cleanups, 4);
|
---|
| 1837 | } else {
|
---|
| 1838 | for (const cleanup2 of cleanups) cleanup2();
|
---|
| 1839 | }
|
---|
| 1840 | cleanupMap.delete(effect);
|
---|
| 1841 | }
|
---|
| 1842 | };
|
---|
| 1843 | if (!!(process.env.NODE_ENV !== "production")) {
|
---|
| 1844 | effect.onTrack = options.onTrack;
|
---|
| 1845 | effect.onTrigger = options.onTrigger;
|
---|
| 1846 | }
|
---|
| 1847 | if (cb) {
|
---|
| 1848 | if (immediate) {
|
---|
| 1849 | job(true);
|
---|
| 1850 | } else {
|
---|
| 1851 | oldValue = effect.run();
|
---|
| 1852 | }
|
---|
| 1853 | } else if (scheduler) {
|
---|
| 1854 | scheduler(job.bind(null, true), true);
|
---|
| 1855 | } else {
|
---|
| 1856 | effect.run();
|
---|
| 1857 | }
|
---|
| 1858 | watchHandle.pause = effect.pause.bind(effect);
|
---|
| 1859 | watchHandle.resume = effect.resume.bind(effect);
|
---|
| 1860 | watchHandle.stop = watchHandle;
|
---|
| 1861 | return watchHandle;
|
---|
| 1862 | }
|
---|
| 1863 | function traverse(value, depth = Infinity, seen) {
|
---|
| 1864 | if (depth <= 0 || !isObject(value) || value["__v_skip"]) {
|
---|
| 1865 | return value;
|
---|
| 1866 | }
|
---|
| 1867 | seen = seen || /* @__PURE__ */ new Set();
|
---|
| 1868 | if (seen.has(value)) {
|
---|
| 1869 | return value;
|
---|
| 1870 | }
|
---|
| 1871 | seen.add(value);
|
---|
| 1872 | depth--;
|
---|
| 1873 | if (isRef(value)) {
|
---|
| 1874 | traverse(value.value, depth, seen);
|
---|
| 1875 | } else if (isArray(value)) {
|
---|
| 1876 | for (let i = 0; i < value.length; i++) {
|
---|
| 1877 | traverse(value[i], depth, seen);
|
---|
| 1878 | }
|
---|
| 1879 | } else if (isSet(value) || isMap(value)) {
|
---|
| 1880 | value.forEach((v) => {
|
---|
| 1881 | traverse(v, depth, seen);
|
---|
| 1882 | });
|
---|
| 1883 | } else if (isPlainObject(value)) {
|
---|
| 1884 | for (const key in value) {
|
---|
| 1885 | traverse(value[key], depth, seen);
|
---|
| 1886 | }
|
---|
| 1887 | for (const key of Object.getOwnPropertySymbols(value)) {
|
---|
| 1888 | if (Object.prototype.propertyIsEnumerable.call(value, key)) {
|
---|
| 1889 | traverse(value[key], depth, seen);
|
---|
| 1890 | }
|
---|
| 1891 | }
|
---|
| 1892 | }
|
---|
| 1893 | return value;
|
---|
| 1894 | }
|
---|
| 1895 |
|
---|
| 1896 | export { ARRAY_ITERATE_KEY, EffectFlags, EffectScope, ITERATE_KEY, MAP_KEY_ITERATE_KEY, ReactiveEffect, ReactiveFlags, TrackOpTypes, TriggerOpTypes, WatchErrorCodes, computed, customRef, effect, effectScope, enableTracking, getCurrentScope, getCurrentWatcher, isProxy, isReactive, isReadonly, isRef, isShallow, markRaw, onEffectCleanup, onScopeDispose, onWatcherCleanup, pauseTracking, proxyRefs, reactive, reactiveReadArray, readonly, ref, resetTracking, shallowReactive, shallowReadArray, shallowReadonly, shallowRef, stop, toRaw, toReactive, toReadonly, toRef, toRefs, toValue, track, traverse, trigger, triggerRef, unref, watch };
|
---|