@zeus-js/signal 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1684 @@
1
+ /**
2
+ * signal v0.0.2
3
+ * (c) 2026 baicie
4
+ * Released under the MIT License.
5
+ **/
6
+ import { EMPTY_OBJ, NOOP, capitalize, extend, hasChanged, hasOwn, isArray, isFunction, isIntegerKey, isMap, isObject, isPlainObject, isSet, isSymbol, makeMap, remove, toRawType } from "@zeus-js/shared";
7
+ //#region packages/signal/src/constants.ts
8
+ let TrackOpTypes = /* @__PURE__ */ function(TrackOpTypes) {
9
+ TrackOpTypes["GET"] = "get";
10
+ TrackOpTypes["HAS"] = "has";
11
+ TrackOpTypes["ITERATE"] = "iterate";
12
+ return TrackOpTypes;
13
+ }({});
14
+ let TriggerOpTypes = /* @__PURE__ */ function(TriggerOpTypes) {
15
+ TriggerOpTypes["SET"] = "set";
16
+ TriggerOpTypes["ADD"] = "add";
17
+ TriggerOpTypes["DELETE"] = "delete";
18
+ TriggerOpTypes["CLEAR"] = "clear";
19
+ return TriggerOpTypes;
20
+ }({});
21
+ let ReactiveFlags = /* @__PURE__ */ function(ReactiveFlags) {
22
+ ReactiveFlags["SKIP"] = "__v_skip";
23
+ ReactiveFlags["IS_REACTIVE"] = "__v_isReactive";
24
+ ReactiveFlags["IS_READONLY"] = "__v_isReadonly";
25
+ ReactiveFlags["IS_SHALLOW"] = "__v_isShallow";
26
+ ReactiveFlags["RAW"] = "__v_raw";
27
+ ReactiveFlags["IS_REF"] = "__v_isRef";
28
+ return ReactiveFlags;
29
+ }({});
30
+ //#endregion
31
+ //#region packages/signal/src/warning.ts
32
+ function warn(msg, ...args) {
33
+ console.warn(`[Zeus warn] ${msg}`, ...args);
34
+ }
35
+ //#endregion
36
+ //#region packages/signal/src/effectScope.ts
37
+ let activeEffectScope;
38
+ var EffectScope = class {
39
+ constructor(detached = false) {
40
+ this.detached = detached;
41
+ this._active = true;
42
+ this._on = 0;
43
+ this.effects = [];
44
+ this.cleanups = [];
45
+ this._isPaused = false;
46
+ this._warnOnRun = true;
47
+ this.__v_skip = true;
48
+ if (!detached && activeEffectScope) if (activeEffectScope.active) {
49
+ this.parent = activeEffectScope;
50
+ this.index = (activeEffectScope.scopes || (activeEffectScope.scopes = [])).push(this) - 1;
51
+ } else {
52
+ this._active = false;
53
+ this._warnOnRun = false;
54
+ }
55
+ }
56
+ get active() {
57
+ return this._active;
58
+ }
59
+ pause() {
60
+ if (this._active) {
61
+ this._isPaused = true;
62
+ let i, l;
63
+ if (this.scopes) for (i = 0, l = this.scopes.length; i < l; i++) this.scopes[i].pause();
64
+ for (i = 0, l = this.effects.length; i < l; i++) this.effects[i].pause();
65
+ }
66
+ }
67
+ /**
68
+ * Resumes the effect scope, including all child scopes and effects.
69
+ */
70
+ resume() {
71
+ if (this._active) {
72
+ if (this._isPaused) {
73
+ this._isPaused = false;
74
+ let i, l;
75
+ if (this.scopes) for (i = 0, l = this.scopes.length; i < l; i++) this.scopes[i].resume();
76
+ for (i = 0, l = this.effects.length; i < l; i++) this.effects[i].resume();
77
+ }
78
+ }
79
+ }
80
+ run(fn) {
81
+ if (this._active) {
82
+ const currentEffectScope = activeEffectScope;
83
+ try {
84
+ activeEffectScope = this;
85
+ return fn();
86
+ } finally {
87
+ activeEffectScope = currentEffectScope;
88
+ }
89
+ } else if (this._warnOnRun) warn(`cannot run an inactive effect scope.`);
90
+ }
91
+ /**
92
+ * This should only be called on non-detached scopes
93
+ * @internal
94
+ */
95
+ on() {
96
+ if (++this._on === 1) {
97
+ this.prevScope = activeEffectScope;
98
+ activeEffectScope = this;
99
+ }
100
+ }
101
+ /**
102
+ * This should only be called on non-detached scopes
103
+ * @internal
104
+ */
105
+ off() {
106
+ if (this._on > 0 && --this._on === 0) {
107
+ if (activeEffectScope === this) activeEffectScope = this.prevScope;
108
+ else {
109
+ let current = activeEffectScope;
110
+ while (current) {
111
+ if (current.prevScope === this) {
112
+ current.prevScope = this.prevScope;
113
+ break;
114
+ }
115
+ current = current.prevScope;
116
+ }
117
+ }
118
+ this.prevScope = void 0;
119
+ }
120
+ }
121
+ stop(fromParent) {
122
+ if (this._active) {
123
+ this._active = false;
124
+ let i, l;
125
+ for (i = 0, l = this.effects.length; i < l; i++) this.effects[i].stop();
126
+ this.effects.length = 0;
127
+ for (i = 0, l = this.cleanups.length; i < l; i++) this.cleanups[i]();
128
+ this.cleanups.length = 0;
129
+ if (this.scopes) {
130
+ for (i = 0, l = this.scopes.length; i < l; i++) this.scopes[i].stop(true);
131
+ this.scopes.length = 0;
132
+ }
133
+ if (!this.detached && this.parent && !fromParent) {
134
+ const last = this.parent.scopes.pop();
135
+ if (last && last !== this) {
136
+ this.parent.scopes[this.index] = last;
137
+ last.index = this.index;
138
+ }
139
+ }
140
+ this.parent = void 0;
141
+ }
142
+ }
143
+ };
144
+ /**
145
+ * Creates an effect scope object which can capture the reactive effects (i.e.
146
+ * computed and watchers) created within it so that these effects can be
147
+ * disposed together. For detailed use cases of this API, please consult its
148
+ * corresponding {@link https://github.com/vuejs/rfcs/blob/master/active-rfcs/0041-reactivity-effect-scope.md | RFC}.
149
+ *
150
+ * @param detached - Can be used to create a "detached" effect scope.
151
+ * @see {@link https://vuejs.org/api/reactivity-advanced.html#effectscope}
152
+ */
153
+ function effectScope(detached) {
154
+ return new EffectScope(detached);
155
+ }
156
+ /**
157
+ * Returns the current active effect scope if there is one.
158
+ *
159
+ * @see {@link https://vuejs.org/api/reactivity-advanced.html#getcurrentscope}
160
+ */
161
+ function getCurrentScope() {
162
+ return activeEffectScope;
163
+ }
164
+ /**
165
+ * Registers a dispose callback on the current active effect scope. The
166
+ * callback will be invoked when the associated effect scope is stopped.
167
+ *
168
+ * @param fn - The callback function to attach to the scope's cleanup.
169
+ * @see {@link https://vuejs.org/api/reactivity-advanced.html#onscopedispose}
170
+ */
171
+ function onScopeDispose(fn, failSilently = false) {
172
+ if (activeEffectScope) activeEffectScope.cleanups.push(fn);
173
+ else if (!failSilently) warn("onScopeDispose() is called when there is no active effect scope to be associated with.");
174
+ }
175
+ //#endregion
176
+ //#region packages/signal/src/effect.ts
177
+ let activeSub;
178
+ let EffectFlags = /* @__PURE__ */ function(EffectFlags) {
179
+ /**
180
+ * ReactiveEffect only
181
+ */
182
+ EffectFlags[EffectFlags["ACTIVE"] = 1] = "ACTIVE";
183
+ EffectFlags[EffectFlags["RUNNING"] = 2] = "RUNNING";
184
+ EffectFlags[EffectFlags["TRACKING"] = 4] = "TRACKING";
185
+ EffectFlags[EffectFlags["NOTIFIED"] = 8] = "NOTIFIED";
186
+ EffectFlags[EffectFlags["DIRTY"] = 16] = "DIRTY";
187
+ EffectFlags[EffectFlags["ALLOW_RECURSE"] = 32] = "ALLOW_RECURSE";
188
+ EffectFlags[EffectFlags["PAUSED"] = 64] = "PAUSED";
189
+ EffectFlags[EffectFlags["EVALUATED"] = 128] = "EVALUATED";
190
+ return EffectFlags;
191
+ }({});
192
+ const pausedQueueEffects = /* @__PURE__ */ new WeakSet();
193
+ var ReactiveEffect = class {
194
+ constructor(fn) {
195
+ this.fn = fn;
196
+ this.deps = void 0;
197
+ this.depsTail = void 0;
198
+ this.flags = 5;
199
+ this.next = void 0;
200
+ this.cleanup = void 0;
201
+ this.scheduler = void 0;
202
+ if (activeEffectScope) if (activeEffectScope.active) activeEffectScope.effects.push(this);
203
+ else this.flags &= -2;
204
+ }
205
+ pause() {
206
+ this.flags |= 64;
207
+ }
208
+ resume() {
209
+ if (this.flags & 64) {
210
+ this.flags &= -65;
211
+ if (pausedQueueEffects.has(this)) {
212
+ pausedQueueEffects.delete(this);
213
+ this.trigger();
214
+ }
215
+ }
216
+ }
217
+ /**
218
+ * @internal
219
+ */
220
+ notify() {
221
+ if (this.flags & 2 && !(this.flags & 32)) return;
222
+ if (!(this.flags & 8)) queueSubscriber(this);
223
+ }
224
+ run() {
225
+ if (!(this.flags & 1)) return this.fn();
226
+ this.flags |= 2;
227
+ cleanupEffect(this);
228
+ prepareDeps(this);
229
+ const prevEffect = activeSub;
230
+ const prevShouldTrack = shouldTrack;
231
+ activeSub = this;
232
+ shouldTrack = true;
233
+ try {
234
+ return this.fn();
235
+ } finally {
236
+ if (activeSub !== this) warn("Active effect was not restored correctly - this is likely a Vue internal bug.");
237
+ cleanupDeps(this);
238
+ activeSub = prevEffect;
239
+ shouldTrack = prevShouldTrack;
240
+ this.flags &= -3;
241
+ }
242
+ }
243
+ stop() {
244
+ if (this.flags & 1) {
245
+ for (let link = this.deps; link; link = link.nextDep) removeSub(link);
246
+ this.deps = this.depsTail = void 0;
247
+ cleanupEffect(this);
248
+ this.onStop && this.onStop();
249
+ this.flags &= -2;
250
+ }
251
+ }
252
+ trigger() {
253
+ if (this.flags & 64) pausedQueueEffects.add(this);
254
+ else if (this.scheduler) this.scheduler();
255
+ else this.runIfDirty();
256
+ }
257
+ /**
258
+ * @internal
259
+ */
260
+ runIfDirty() {
261
+ if (isDirty(this)) this.run();
262
+ }
263
+ get dirty() {
264
+ return isDirty(this);
265
+ }
266
+ };
267
+ /**
268
+ * For debugging
269
+ */
270
+ let batchDepth = 0;
271
+ let batchedSub;
272
+ let batchedComputed;
273
+ /**
274
+ * @internal
275
+ */
276
+ function queueSubscriber(sub, isComputed = false) {
277
+ sub.flags |= 8;
278
+ if (isComputed) {
279
+ sub.next = batchedComputed;
280
+ batchedComputed = sub;
281
+ return;
282
+ }
283
+ sub.next = batchedSub;
284
+ batchedSub = sub;
285
+ }
286
+ /**
287
+ * @internal
288
+ */
289
+ function startBatch() {
290
+ batchDepth++;
291
+ }
292
+ /**
293
+ * Run batched effects when all batches have ended
294
+ * @internal
295
+ */
296
+ function endBatch() {
297
+ if (--batchDepth > 0) return;
298
+ if (batchedComputed) {
299
+ let e = batchedComputed;
300
+ batchedComputed = void 0;
301
+ while (e) {
302
+ const next = e.next;
303
+ e.next = void 0;
304
+ e.flags &= -9;
305
+ e = next;
306
+ }
307
+ }
308
+ let error;
309
+ while (batchedSub) {
310
+ let e = batchedSub;
311
+ batchedSub = void 0;
312
+ while (e) {
313
+ const next = e.next;
314
+ e.next = void 0;
315
+ e.flags &= -9;
316
+ if (e.flags & 1) try {
317
+ e.trigger();
318
+ } catch (err) {
319
+ if (!error) error = err;
320
+ }
321
+ e = next;
322
+ }
323
+ }
324
+ if (error) throw error;
325
+ }
326
+ function prepareDeps(sub) {
327
+ for (let link = sub.deps; link; link = link.nextDep) {
328
+ link.version = -1;
329
+ link.prevActiveLink = link.dep.activeLink;
330
+ link.dep.activeLink = link;
331
+ }
332
+ }
333
+ function cleanupDeps(sub) {
334
+ let head;
335
+ let tail = sub.depsTail;
336
+ let link = tail;
337
+ while (link) {
338
+ const prev = link.prevDep;
339
+ if (link.version === -1) {
340
+ if (link === tail) tail = prev;
341
+ removeSub(link);
342
+ removeDep(link);
343
+ } else head = link;
344
+ link.dep.activeLink = link.prevActiveLink;
345
+ link.prevActiveLink = void 0;
346
+ link = prev;
347
+ }
348
+ sub.deps = head;
349
+ sub.depsTail = tail;
350
+ }
351
+ function isDirty(sub) {
352
+ for (let link = sub.deps; link; link = link.nextDep) if (link.dep.version !== link.version || link.dep.computed && (refreshComputed(link.dep.computed) || link.dep.version !== link.version)) return true;
353
+ if (sub._dirty) return true;
354
+ return false;
355
+ }
356
+ /**
357
+ * Returning false indicates the refresh failed
358
+ * @internal
359
+ */
360
+ function refreshComputed(computed) {
361
+ if (computed.flags & 4 && !(computed.flags & 16)) return;
362
+ computed.flags &= -17;
363
+ if (computed.globalVersion === globalVersion) return;
364
+ computed.globalVersion = globalVersion;
365
+ if (!computed.isSSR && computed.flags & 128 && (!computed.deps && !computed._dirty || !isDirty(computed))) return;
366
+ computed.flags |= 2;
367
+ const dep = computed.dep;
368
+ const prevSub = activeSub;
369
+ const prevShouldTrack = shouldTrack;
370
+ activeSub = computed;
371
+ shouldTrack = true;
372
+ try {
373
+ prepareDeps(computed);
374
+ const value = computed.fn(computed._value);
375
+ if (dep.version === 0 || hasChanged(value, computed._value)) {
376
+ computed.flags |= 128;
377
+ computed._value = value;
378
+ dep.version++;
379
+ }
380
+ } catch (err) {
381
+ dep.version++;
382
+ throw err;
383
+ } finally {
384
+ activeSub = prevSub;
385
+ shouldTrack = prevShouldTrack;
386
+ cleanupDeps(computed);
387
+ computed.flags &= -3;
388
+ }
389
+ }
390
+ function removeSub(link, soft = false) {
391
+ const { dep, prevSub, nextSub } = link;
392
+ if (prevSub) {
393
+ prevSub.nextSub = nextSub;
394
+ link.prevSub = void 0;
395
+ }
396
+ if (nextSub) {
397
+ nextSub.prevSub = prevSub;
398
+ link.nextSub = void 0;
399
+ }
400
+ if (dep.subsHead === link) dep.subsHead = nextSub;
401
+ if (dep.subs === link) {
402
+ dep.subs = prevSub;
403
+ if (!prevSub && dep.computed) {
404
+ dep.computed.flags &= -5;
405
+ for (let l = dep.computed.deps; l; l = l.nextDep) removeSub(l, true);
406
+ }
407
+ }
408
+ if (!soft && !--dep.sc && dep.map) dep.map.delete(dep.key);
409
+ }
410
+ function removeDep(link) {
411
+ const { prevDep, nextDep } = link;
412
+ if (prevDep) {
413
+ prevDep.nextDep = nextDep;
414
+ link.prevDep = void 0;
415
+ }
416
+ if (nextDep) {
417
+ nextDep.prevDep = prevDep;
418
+ link.nextDep = void 0;
419
+ }
420
+ }
421
+ function effect(fn, options) {
422
+ if (fn.effect instanceof ReactiveEffect) fn = fn.effect.fn;
423
+ const e = new ReactiveEffect(fn);
424
+ if (options) extend(e, options);
425
+ try {
426
+ e.run();
427
+ } catch (err) {
428
+ e.stop();
429
+ throw err;
430
+ }
431
+ const runner = e.run.bind(e);
432
+ runner.effect = e;
433
+ return runner;
434
+ }
435
+ /**
436
+ * Stops the effect associated with the given runner.
437
+ *
438
+ * @param runner - Association with the effect to stop tracking.
439
+ */
440
+ function stop(runner) {
441
+ runner.effect.stop();
442
+ }
443
+ /**
444
+ * @internal
445
+ */
446
+ let shouldTrack = true;
447
+ const trackStack = [];
448
+ /**
449
+ * Temporarily pauses tracking.
450
+ */
451
+ function pauseTracking() {
452
+ trackStack.push(shouldTrack);
453
+ shouldTrack = false;
454
+ }
455
+ /**
456
+ * Re-enables effect tracking (if it was paused).
457
+ */
458
+ function enableTracking() {
459
+ trackStack.push(shouldTrack);
460
+ shouldTrack = true;
461
+ }
462
+ /**
463
+ * Resets the previous global effect tracking state.
464
+ */
465
+ function resetTracking() {
466
+ const last = trackStack.pop();
467
+ shouldTrack = last === void 0 ? true : last;
468
+ }
469
+ /**
470
+ * Registers a cleanup function for the current active effect.
471
+ * The cleanup function is called right before the next effect run, or when the
472
+ * effect is stopped.
473
+ *
474
+ * Throws a warning if there is no current active effect. The warning can be
475
+ * suppressed by passing `true` to the second argument.
476
+ *
477
+ * @param fn - the cleanup function to be registered
478
+ * @param failSilently - if `true`, will not throw warning when called without
479
+ * an active effect.
480
+ */
481
+ function onEffectCleanup(fn, failSilently = false) {
482
+ if (activeSub instanceof ReactiveEffect) activeSub.cleanup = fn;
483
+ else if (!failSilently) warn("onEffectCleanup() was called when there was no active effect to associate with.");
484
+ }
485
+ function cleanupEffect(e) {
486
+ const { cleanup } = e;
487
+ e.cleanup = void 0;
488
+ if (cleanup) {
489
+ const prevSub = activeSub;
490
+ activeSub = void 0;
491
+ try {
492
+ cleanup();
493
+ } finally {
494
+ activeSub = prevSub;
495
+ }
496
+ }
497
+ }
498
+ /**
499
+ * Batches reactive updates synchronously within the given function.
500
+ * All updates triggered inside `fn` are deferred until the function completes,
501
+ * then flushed together in a single batch.
502
+ */
503
+ function batch(fn) {
504
+ startBatch();
505
+ try {
506
+ return fn();
507
+ } finally {
508
+ endBatch();
509
+ }
510
+ }
511
+ /**
512
+ * Executes the given function without tracking reactive dependencies.
513
+ * Any reactive reads inside `fn` will not trigger effect re-runs.
514
+ */
515
+ function untrack(fn) {
516
+ pauseTracking();
517
+ try {
518
+ return fn();
519
+ } finally {
520
+ resetTracking();
521
+ }
522
+ }
523
+ /**
524
+ * Returns the currently executing reactive effect, if any.
525
+ */
526
+ function getCurrentEffect() {
527
+ return activeSub instanceof ReactiveEffect ? activeSub : void 0;
528
+ }
529
+ //#endregion
530
+ //#region packages/signal/src/dep.ts
531
+ /**
532
+ * Incremented every time a reactive change happens
533
+ * This is used to give computed a fast path to avoid re-compute when nothing
534
+ * has changed.
535
+ */
536
+ let globalVersion = 0;
537
+ /**
538
+ * Represents a link between a source (Dep) and a subscriber (Effect or Computed).
539
+ * Deps and subs have a many-to-many relationship - each link between a
540
+ * dep and a sub is represented by a Link instance.
541
+ *
542
+ * A Link is also a node in two doubly-linked lists - one for the associated
543
+ * sub to track all its deps, and one for the associated dep to track all its
544
+ * subs.
545
+ *
546
+ * @internal
547
+ */
548
+ var Link = class {
549
+ constructor(sub, dep) {
550
+ this.sub = sub;
551
+ this.dep = dep;
552
+ this.version = dep.version;
553
+ this.nextDep = this.prevDep = this.nextSub = this.prevSub = this.prevActiveLink = void 0;
554
+ }
555
+ };
556
+ /**
557
+ * @internal
558
+ */
559
+ var Dep = class {
560
+ constructor(computed) {
561
+ this.computed = computed;
562
+ this.version = 0;
563
+ this.activeLink = void 0;
564
+ this.subs = void 0;
565
+ this.map = void 0;
566
+ this.key = void 0;
567
+ this.sc = 0;
568
+ this.__v_skip = true;
569
+ this.subsHead = void 0;
570
+ }
571
+ track(debugInfo) {
572
+ if (!activeSub || !shouldTrack || activeSub === this.computed) return;
573
+ let link = this.activeLink;
574
+ if (link === void 0 || link.sub !== activeSub) {
575
+ link = this.activeLink = new Link(activeSub, this);
576
+ if (!activeSub.deps) activeSub.deps = activeSub.depsTail = link;
577
+ else {
578
+ link.prevDep = activeSub.depsTail;
579
+ activeSub.depsTail.nextDep = link;
580
+ activeSub.depsTail = link;
581
+ }
582
+ addSub(link);
583
+ } else if (link.version === -1) {
584
+ link.version = this.version;
585
+ if (link.nextDep) {
586
+ const next = link.nextDep;
587
+ next.prevDep = link.prevDep;
588
+ if (link.prevDep) link.prevDep.nextDep = next;
589
+ link.prevDep = activeSub.depsTail;
590
+ link.nextDep = void 0;
591
+ activeSub.depsTail.nextDep = link;
592
+ activeSub.depsTail = link;
593
+ if (activeSub.deps === link) activeSub.deps = next;
594
+ }
595
+ }
596
+ if (activeSub.onTrack) activeSub.onTrack(extend({ effect: activeSub }, debugInfo));
597
+ return link;
598
+ }
599
+ trigger(debugInfo) {
600
+ this.version++;
601
+ globalVersion++;
602
+ this.notify(debugInfo);
603
+ }
604
+ notify(debugInfo) {
605
+ startBatch();
606
+ try {
607
+ for (let head = this.subsHead; head; head = head.nextSub) if (head.sub.onTrigger && !(head.sub.flags & 8)) head.sub.onTrigger(extend({ effect: head.sub }, debugInfo));
608
+ for (let link = this.subs; link; link = link.prevSub) if (link.sub.notify()) link.sub.dep.notify();
609
+ } finally {
610
+ endBatch();
611
+ }
612
+ }
613
+ };
614
+ function addSub(link) {
615
+ link.dep.sc++;
616
+ if (link.sub.flags & 4) {
617
+ const computed = link.dep.computed;
618
+ if (computed && !link.dep.subs) {
619
+ computed.flags |= 20;
620
+ for (let l = computed.deps; l; l = l.nextDep) addSub(l);
621
+ }
622
+ const currentTail = link.dep.subs;
623
+ if (currentTail !== link) {
624
+ link.prevSub = currentTail;
625
+ if (currentTail) currentTail.nextSub = link;
626
+ }
627
+ if (link.dep.subsHead === void 0) link.dep.subsHead = link;
628
+ link.dep.subs = link;
629
+ }
630
+ }
631
+ const targetMap = /* @__PURE__ */ new WeakMap();
632
+ const ITERATE_KEY = Symbol("Object iterate");
633
+ const MAP_KEY_ITERATE_KEY = Symbol("Map keys iterate");
634
+ const ARRAY_ITERATE_KEY = Symbol("Array iterate");
635
+ /**
636
+ * Tracks access to a reactive property.
637
+ *
638
+ * This will check which effect is running at the moment and record it as dep
639
+ * which records all effects that depend on the reactive property.
640
+ *
641
+ * @param target - Object holding the reactive property.
642
+ * @param type - Defines the type of access to the reactive property.
643
+ * @param key - Identifier of the reactive property to track.
644
+ */
645
+ function track(target, type, key) {
646
+ if (shouldTrack && activeSub) {
647
+ let depsMap = targetMap.get(target);
648
+ if (!depsMap) targetMap.set(target, depsMap = /* @__PURE__ */ new Map());
649
+ let dep = depsMap.get(key);
650
+ if (!dep) {
651
+ depsMap.set(key, dep = new Dep());
652
+ dep.map = depsMap;
653
+ dep.key = key;
654
+ }
655
+ dep.track({
656
+ target,
657
+ type,
658
+ key
659
+ });
660
+ }
661
+ }
662
+ /**
663
+ * Finds all deps associated with the target (or a specific property) and
664
+ * triggers the effects stored within.
665
+ *
666
+ * @param target - The reactive object.
667
+ * @param type - Defines the type of the operation that needs to trigger effects.
668
+ * @param key - Can be used to target a specific reactive property in the target object.
669
+ */
670
+ function trigger(target, type, key, newValue, oldValue, oldTarget) {
671
+ const depsMap = targetMap.get(target);
672
+ if (!depsMap) {
673
+ globalVersion++;
674
+ return;
675
+ }
676
+ const run = (dep) => {
677
+ if (dep) dep.trigger({
678
+ target,
679
+ type,
680
+ key,
681
+ newValue,
682
+ oldValue,
683
+ oldTarget
684
+ });
685
+ };
686
+ startBatch();
687
+ if (type === "clear") depsMap.forEach(run);
688
+ else {
689
+ const targetIsArray = isArray(target);
690
+ const isArrayIndex = targetIsArray && isIntegerKey(key);
691
+ if (targetIsArray && key === "length") {
692
+ const newLength = Number(newValue);
693
+ depsMap.forEach((dep, key) => {
694
+ if (key === "length" || key === ARRAY_ITERATE_KEY || !isSymbol(key) && key >= newLength) run(dep);
695
+ });
696
+ } else {
697
+ if (key !== void 0 || depsMap.has(void 0)) run(depsMap.get(key));
698
+ if (isArrayIndex) run(depsMap.get(ARRAY_ITERATE_KEY));
699
+ switch (type) {
700
+ case "add":
701
+ if (!targetIsArray) {
702
+ run(depsMap.get(ITERATE_KEY));
703
+ if (isMap(target)) run(depsMap.get(MAP_KEY_ITERATE_KEY));
704
+ } else if (isArrayIndex) run(depsMap.get("length"));
705
+ break;
706
+ case "delete":
707
+ if (!targetIsArray) {
708
+ run(depsMap.get(ITERATE_KEY));
709
+ if (isMap(target)) run(depsMap.get(MAP_KEY_ITERATE_KEY));
710
+ }
711
+ break;
712
+ case "set":
713
+ if (isMap(target)) run(depsMap.get(ITERATE_KEY));
714
+ break;
715
+ }
716
+ }
717
+ }
718
+ endBatch();
719
+ }
720
+ //#endregion
721
+ //#region packages/signal/src/arrayInstrumentations.ts
722
+ /**
723
+ * Track array iteration and return:
724
+ * - if input is reactive: a cloned raw array with reactive values
725
+ * - if input is non-reactive or shallowReactive: the original raw array
726
+ */
727
+ function reactiveReadArray(array) {
728
+ const raw = /* @__PURE__ */ toRaw(array);
729
+ if (raw === array) return raw;
730
+ track(raw, "iterate", ARRAY_ITERATE_KEY);
731
+ return /* @__PURE__ */ isShallow(array) ? raw : raw.map(toReactive);
732
+ }
733
+ /**
734
+ * Track array iteration and return raw array
735
+ */
736
+ function shallowReadArray(arr) {
737
+ track(arr = /* @__PURE__ */ toRaw(arr), "iterate", ARRAY_ITERATE_KEY);
738
+ return arr;
739
+ }
740
+ function toWrapped(target, item) {
741
+ if (/* @__PURE__ */ isReadonly(target)) return /* @__PURE__ */ isReactive(target) ? toReadonly(toReactive(item)) : toReadonly(item);
742
+ return toReactive(item);
743
+ }
744
+ const arrayInstrumentations = {
745
+ __proto__: null,
746
+ [Symbol.iterator]() {
747
+ return iterator(this, Symbol.iterator, (item) => toWrapped(this, item));
748
+ },
749
+ concat(...args) {
750
+ return reactiveReadArray(this).concat(...args.map((x) => isArray(x) ? reactiveReadArray(x) : x));
751
+ },
752
+ entries() {
753
+ return iterator(this, "entries", (value) => {
754
+ value[1] = toWrapped(this, value[1]);
755
+ return value;
756
+ });
757
+ },
758
+ every(fn, thisArg) {
759
+ return apply(this, "every", fn, thisArg, void 0, arguments);
760
+ },
761
+ filter(fn, thisArg) {
762
+ return apply(this, "filter", fn, thisArg, (v) => v.map((item) => toWrapped(this, item)), arguments);
763
+ },
764
+ find(fn, thisArg) {
765
+ return apply(this, "find", fn, thisArg, (item) => toWrapped(this, item), arguments);
766
+ },
767
+ findIndex(fn, thisArg) {
768
+ return apply(this, "findIndex", fn, thisArg, void 0, arguments);
769
+ },
770
+ findLast(fn, thisArg) {
771
+ return apply(this, "findLast", fn, thisArg, (item) => toWrapped(this, item), arguments);
772
+ },
773
+ findLastIndex(fn, thisArg) {
774
+ return apply(this, "findLastIndex", fn, thisArg, void 0, arguments);
775
+ },
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
+ lastIndexOf(...args) {
789
+ return searchProxy(this, "lastIndexOf", args);
790
+ },
791
+ map(fn, thisArg) {
792
+ return apply(this, "map", fn, thisArg, void 0, arguments);
793
+ },
794
+ pop() {
795
+ return noTracking(this, "pop");
796
+ },
797
+ push(...args) {
798
+ return noTracking(this, "push", args);
799
+ },
800
+ reduce(fn, ...args) {
801
+ return reduce(this, "reduce", fn, args);
802
+ },
803
+ reduceRight(fn, ...args) {
804
+ return reduce(this, "reduceRight", fn, args);
805
+ },
806
+ shift() {
807
+ return noTracking(this, "shift");
808
+ },
809
+ some(fn, thisArg) {
810
+ return apply(this, "some", fn, thisArg, void 0, arguments);
811
+ },
812
+ splice(...args) {
813
+ return noTracking(this, "splice", args);
814
+ },
815
+ toReversed() {
816
+ return reactiveReadArray(this).toReversed();
817
+ },
818
+ toSorted(comparer) {
819
+ return reactiveReadArray(this).toSorted(comparer);
820
+ },
821
+ toSpliced(...args) {
822
+ return reactiveReadArray(this).toSpliced(...args);
823
+ },
824
+ unshift(...args) {
825
+ return noTracking(this, "unshift", args);
826
+ },
827
+ values() {
828
+ return iterator(this, "values", (item) => toWrapped(this, item));
829
+ }
830
+ };
831
+ function iterator(self, method, wrapValue) {
832
+ const arr = shallowReadArray(self);
833
+ const iter = arr[method]();
834
+ if (arr !== self && !/* @__PURE__ */ isShallow(self)) {
835
+ iter._next = iter.next;
836
+ iter.next = () => {
837
+ const result = iter._next();
838
+ if (!result.done) result.value = wrapValue(result.value);
839
+ return result;
840
+ };
841
+ }
842
+ return iter;
843
+ }
844
+ const arrayProto = Array.prototype;
845
+ function apply(self, method, fn, thisArg, wrappedRetFn, args) {
846
+ const arr = shallowReadArray(self);
847
+ const needsWrap = arr !== self && !/* @__PURE__ */ isShallow(self);
848
+ const methodFn = arr[method];
849
+ if (methodFn !== arrayProto[method]) {
850
+ const result = methodFn.apply(self, args);
851
+ return needsWrap ? toReactive(result) : result;
852
+ }
853
+ let wrappedFn = fn;
854
+ if (arr !== self) {
855
+ if (needsWrap) wrappedFn = function(item, index) {
856
+ return fn.call(this, toWrapped(self, item), index, self);
857
+ };
858
+ else if (fn.length > 2) wrappedFn = function(item, index) {
859
+ return fn.call(this, item, index, self);
860
+ };
861
+ }
862
+ const result = methodFn.call(arr, wrappedFn, thisArg);
863
+ return needsWrap && wrappedRetFn ? wrappedRetFn(result) : result;
864
+ }
865
+ function reduce(self, method, fn, args) {
866
+ const arr = shallowReadArray(self);
867
+ const needsWrap = arr !== self && !/* @__PURE__ */ isShallow(self);
868
+ let wrappedFn = fn;
869
+ let wrapInitialAccumulator = false;
870
+ if (arr !== self) {
871
+ if (needsWrap) {
872
+ wrapInitialAccumulator = args.length === 0;
873
+ wrappedFn = function(acc, item, index) {
874
+ if (wrapInitialAccumulator) {
875
+ wrapInitialAccumulator = false;
876
+ acc = toWrapped(self, acc);
877
+ }
878
+ return fn.call(this, acc, toWrapped(self, item), index, self);
879
+ };
880
+ } else if (fn.length > 3) wrappedFn = function(acc, item, index) {
881
+ return fn.call(this, acc, item, index, self);
882
+ };
883
+ }
884
+ const result = arr[method](wrappedFn, ...args);
885
+ return wrapInitialAccumulator ? toWrapped(self, result) : result;
886
+ }
887
+ function searchProxy(self, method, args) {
888
+ const arr = /* @__PURE__ */ toRaw(self);
889
+ track(arr, "iterate", ARRAY_ITERATE_KEY);
890
+ const res = arr[method](...args);
891
+ if ((res === -1 || res === false) && /* @__PURE__ */ isProxy(args[0])) {
892
+ args[0] = /* @__PURE__ */ toRaw(args[0]);
893
+ return arr[method](...args);
894
+ }
895
+ return res;
896
+ }
897
+ function noTracking(self, method, args = []) {
898
+ pauseTracking();
899
+ startBatch();
900
+ const res = (/* @__PURE__ */ toRaw(self))[method].apply(self, args);
901
+ endBatch();
902
+ resetTracking();
903
+ return res;
904
+ }
905
+ //#endregion
906
+ //#region packages/signal/src/ref.ts
907
+ let _ReactiveFlags$IS_REF, _ReactiveFlags$IS_SHA;
908
+ /*@__NO_SIDE_EFFECTS__*/
909
+ function isRef(r) {
910
+ return r ? r["__v_isRef"] === true : false;
911
+ }
912
+ /*@__NO_SIDE_EFFECTS__*/
913
+ function ref(value) {
914
+ return createRef(value, false);
915
+ }
916
+ function createRef(rawValue, shallow) {
917
+ if (/* @__PURE__ */ isRef(rawValue)) return rawValue;
918
+ return new RefImpl(rawValue, shallow);
919
+ }
920
+ _ReactiveFlags$IS_REF = "__v_isRef";
921
+ _ReactiveFlags$IS_SHA = "__v_isShallow";
922
+ /**
923
+ * @internal
924
+ */
925
+ var RefImpl = class {
926
+ constructor(value, isShallow) {
927
+ this.dep = new Dep();
928
+ this[_ReactiveFlags$IS_REF] = true;
929
+ this[_ReactiveFlags$IS_SHA] = false;
930
+ this._rawValue = isShallow ? value : /* @__PURE__ */ toRaw(value);
931
+ this._value = isShallow ? value : toReactive(value);
932
+ this["__v_isShallow"] = isShallow;
933
+ }
934
+ get value() {
935
+ this.dep.track({
936
+ target: this,
937
+ type: "get",
938
+ key: "value"
939
+ });
940
+ return this._value;
941
+ }
942
+ set value(newValue) {
943
+ const oldValue = this._rawValue;
944
+ const useDirectValue = this["__v_isShallow"] || /* @__PURE__ */ isShallow(newValue) || /* @__PURE__ */ isReadonly(newValue);
945
+ newValue = useDirectValue ? newValue : /* @__PURE__ */ toRaw(newValue);
946
+ if (hasChanged(newValue, oldValue)) {
947
+ this._rawValue = newValue;
948
+ this._value = useDirectValue ? newValue : toReactive(newValue);
949
+ this.dep.trigger({
950
+ target: this,
951
+ type: "set",
952
+ key: "value",
953
+ newValue,
954
+ oldValue
955
+ });
956
+ }
957
+ }
958
+ };
959
+ //#endregion
960
+ //#region packages/signal/src/baseHandlers.ts
961
+ const isNonTrackableKeys = /*@__PURE__*/ makeMap(`__proto__,__v_isRef,__isVue`);
962
+ const builtInSymbols = new Set(/*@__PURE__*/ Object.getOwnPropertyNames(Symbol).filter((key) => key !== "arguments" && key !== "caller").map((key) => Symbol[key]).filter(isSymbol));
963
+ function hasOwnProperty(key) {
964
+ if (!isSymbol(key)) key = String(key);
965
+ const obj = /* @__PURE__ */ toRaw(this);
966
+ track(obj, "has", key);
967
+ return obj.hasOwnProperty(key);
968
+ }
969
+ var BaseReactiveHandler = class {
970
+ constructor(_isReadonly = false, _isShallow = false) {
971
+ this._isReadonly = _isReadonly;
972
+ this._isShallow = _isShallow;
973
+ }
974
+ get(target, key, receiver) {
975
+ if (key === "__v_skip") return target["__v_skip"];
976
+ const isReadonly = this._isReadonly, isShallow = this._isShallow;
977
+ if (key === "__v_isReactive") return !isReadonly;
978
+ else if (key === "__v_isReadonly") return isReadonly;
979
+ else if (key === "__v_isShallow") return isShallow;
980
+ else if (key === "__v_raw") {
981
+ if (receiver === (isReadonly ? isShallow ? shallowReadonlyMap : readonlyMap : isShallow ? shallowReactiveMap : reactiveMap).get(target) || Object.getPrototypeOf(target) === Object.getPrototypeOf(receiver)) return target;
982
+ return;
983
+ }
984
+ const targetIsArray = isArray(target);
985
+ if (!isReadonly) {
986
+ let fn;
987
+ if (targetIsArray && (fn = arrayInstrumentations[key])) return fn;
988
+ if (key === "hasOwnProperty") return hasOwnProperty;
989
+ }
990
+ const res = Reflect.get(target, key, /* @__PURE__ */ isRef(target) ? target : receiver);
991
+ if (isSymbol(key) ? builtInSymbols.has(key) : isNonTrackableKeys(key)) return res;
992
+ if (!isReadonly) track(target, "get", key);
993
+ if (isShallow) return res;
994
+ if (/* @__PURE__ */ isRef(res)) {
995
+ const value = targetIsArray && isIntegerKey(key) ? res : res.value;
996
+ return isReadonly && isObject(value) ? /* @__PURE__ */ readonly(value) : value;
997
+ }
998
+ if (isObject(res)) return isReadonly ? /* @__PURE__ */ readonly(res) : /* @__PURE__ */ reactive(res);
999
+ return res;
1000
+ }
1001
+ };
1002
+ var MutableReactiveHandler = class extends BaseReactiveHandler {
1003
+ constructor(isShallow = false) {
1004
+ super(false, isShallow);
1005
+ }
1006
+ set(target, key, value, receiver) {
1007
+ let oldValue = target[key];
1008
+ const isArrayWithIntegerKey = isArray(target) && isIntegerKey(key);
1009
+ if (!this._isShallow) {
1010
+ const isOldValueReadonly = /* @__PURE__ */ isReadonly(oldValue);
1011
+ if (!/* @__PURE__ */ isShallow(value) && !/* @__PURE__ */ isReadonly(value)) {
1012
+ oldValue = /* @__PURE__ */ toRaw(oldValue);
1013
+ value = /* @__PURE__ */ toRaw(value);
1014
+ }
1015
+ if (!isArrayWithIntegerKey && /* @__PURE__ */ isRef(oldValue) && !/* @__PURE__ */ isRef(value)) if (isOldValueReadonly) {
1016
+ warn(`Set operation on key "${String(key)}" failed: target is readonly.`, target[key]);
1017
+ return true;
1018
+ } else {
1019
+ oldValue.value = value;
1020
+ return true;
1021
+ }
1022
+ }
1023
+ const hadKey = isArrayWithIntegerKey ? Number(key) < target.length : hasOwn(target, key);
1024
+ const result = Reflect.set(target, key, value, /* @__PURE__ */ isRef(target) ? target : receiver);
1025
+ if (target === /* @__PURE__ */ toRaw(receiver)) {
1026
+ if (!hadKey) trigger(target, "add", key, value);
1027
+ else if (hasChanged(value, oldValue)) trigger(target, "set", key, value, oldValue);
1028
+ }
1029
+ return result;
1030
+ }
1031
+ deleteProperty(target, key) {
1032
+ const hadKey = hasOwn(target, key);
1033
+ const oldValue = target[key];
1034
+ const result = Reflect.deleteProperty(target, key);
1035
+ if (result && hadKey) trigger(target, "delete", key, void 0, oldValue);
1036
+ return result;
1037
+ }
1038
+ has(target, key) {
1039
+ const result = Reflect.has(target, key);
1040
+ if (!isSymbol(key) || !builtInSymbols.has(key)) track(target, "has", key);
1041
+ return result;
1042
+ }
1043
+ ownKeys(target) {
1044
+ track(target, "iterate", isArray(target) ? "length" : ITERATE_KEY);
1045
+ return Reflect.ownKeys(target);
1046
+ }
1047
+ };
1048
+ var ReadonlyReactiveHandler = class extends BaseReactiveHandler {
1049
+ constructor(isShallow = false) {
1050
+ super(true, isShallow);
1051
+ }
1052
+ set(target, key) {
1053
+ warn(`Set operation on key "${String(key)}" failed: target is readonly.`, target);
1054
+ return true;
1055
+ }
1056
+ deleteProperty(target, key) {
1057
+ warn(`Delete operation on key "${String(key)}" failed: target is readonly.`, target);
1058
+ return true;
1059
+ }
1060
+ };
1061
+ const mutableHandlers = /*@__PURE__*/ new MutableReactiveHandler();
1062
+ const readonlyHandlers = /*@__PURE__*/ new ReadonlyReactiveHandler();
1063
+ //#endregion
1064
+ //#region packages/signal/src/collectionHandlers.ts
1065
+ const toShallow = (value) => value;
1066
+ const getProto = (v) => Reflect.getPrototypeOf(v);
1067
+ function createIterableMethod(method, isReadonly, isShallow) {
1068
+ return function(...args) {
1069
+ const target = this["__v_raw"];
1070
+ const rawTarget = /* @__PURE__ */ 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 = isShallow ? toShallow : isReadonly ? toReadonly : toReactive;
1076
+ !isReadonly && track(rawTarget, "iterate", isKeyOnly ? MAP_KEY_ITERATE_KEY : ITERATE_KEY);
1077
+ return extend(Object.create(innerIterator), { next() {
1078
+ const { value, done } = innerIterator.next();
1079
+ return done ? {
1080
+ value,
1081
+ done
1082
+ } : {
1083
+ value: isPair ? [wrap(value[0]), wrap(value[1])] : wrap(value),
1084
+ done
1085
+ };
1086
+ } });
1087
+ };
1088
+ }
1089
+ function createReadonlyMethod(type) {
1090
+ return function(...args) {
1091
+ {
1092
+ const key = args[0] ? `on key "${args[0]}" ` : ``;
1093
+ warn(`${capitalize(type)} operation ${key}failed: target is readonly.`, /* @__PURE__ */ toRaw(this));
1094
+ }
1095
+ return type === "delete" ? false : type === "clear" ? void 0 : this;
1096
+ };
1097
+ }
1098
+ function createInstrumentations(readonly, shallow) {
1099
+ const instrumentations = {
1100
+ get(key) {
1101
+ const target = this["__v_raw"];
1102
+ const rawTarget = /* @__PURE__ */ toRaw(target);
1103
+ const rawKey = /* @__PURE__ */ toRaw(key);
1104
+ if (!readonly) {
1105
+ if (hasChanged(key, rawKey)) track(rawTarget, "get", key);
1106
+ track(rawTarget, "get", rawKey);
1107
+ }
1108
+ const { has } = getProto(rawTarget);
1109
+ const wrap = shallow ? toShallow : readonly ? toReadonly : toReactive;
1110
+ if (has.call(rawTarget, key)) return wrap(target.get(key));
1111
+ else if (has.call(rawTarget, rawKey)) return wrap(target.get(rawKey));
1112
+ else if (target !== rawTarget) target.get(key);
1113
+ },
1114
+ get size() {
1115
+ const target = this["__v_raw"];
1116
+ !readonly && track(/* @__PURE__ */ toRaw(target), "iterate", ITERATE_KEY);
1117
+ return target.size;
1118
+ },
1119
+ has(key) {
1120
+ const target = this["__v_raw"];
1121
+ const rawTarget = /* @__PURE__ */ toRaw(target);
1122
+ const rawKey = /* @__PURE__ */ toRaw(key);
1123
+ if (!readonly) {
1124
+ if (hasChanged(key, rawKey)) track(rawTarget, "has", key);
1125
+ track(rawTarget, "has", rawKey);
1126
+ }
1127
+ return key === rawKey ? target.has(key) : target.has(key) || target.has(rawKey);
1128
+ },
1129
+ forEach(callback, thisArg) {
1130
+ const observed = this;
1131
+ const target = observed["__v_raw"];
1132
+ const rawTarget = /* @__PURE__ */ toRaw(target);
1133
+ const wrap = shallow ? toShallow : readonly ? toReadonly : toReactive;
1134
+ !readonly && track(rawTarget, "iterate", ITERATE_KEY);
1135
+ return target.forEach((value, key) => {
1136
+ return callback.call(thisArg, wrap(value), wrap(key), observed);
1137
+ });
1138
+ }
1139
+ };
1140
+ extend(instrumentations, readonly ? {
1141
+ add: createReadonlyMethod("add"),
1142
+ set: createReadonlyMethod("set"),
1143
+ delete: createReadonlyMethod("delete"),
1144
+ clear: createReadonlyMethod("clear")
1145
+ } : {
1146
+ add(value) {
1147
+ const target = /* @__PURE__ */ toRaw(this);
1148
+ const proto = getProto(target);
1149
+ const rawValue = /* @__PURE__ */ toRaw(value);
1150
+ const valueToAdd = !shallow && !/* @__PURE__ */ isShallow(value) && !/* @__PURE__ */ isReadonly(value) ? rawValue : value;
1151
+ if (!(proto.has.call(target, valueToAdd) || hasChanged(value, valueToAdd) && proto.has.call(target, value) || hasChanged(rawValue, valueToAdd) && proto.has.call(target, rawValue))) {
1152
+ target.add(valueToAdd);
1153
+ trigger(target, "add", valueToAdd, valueToAdd);
1154
+ }
1155
+ return this;
1156
+ },
1157
+ set(key, value) {
1158
+ if (!shallow && !/* @__PURE__ */ isShallow(value) && !/* @__PURE__ */ isReadonly(value)) value = /* @__PURE__ */ toRaw(value);
1159
+ const target = /* @__PURE__ */ toRaw(this);
1160
+ const { has, get } = getProto(target);
1161
+ let hadKey = has.call(target, key);
1162
+ if (!hadKey) {
1163
+ key = /* @__PURE__ */ toRaw(key);
1164
+ hadKey = has.call(target, key);
1165
+ } else checkIdentityKeys(target, has, key);
1166
+ const oldValue = get.call(target, key);
1167
+ target.set(key, value);
1168
+ if (!hadKey) trigger(target, "add", key, value);
1169
+ else if (hasChanged(value, oldValue)) trigger(target, "set", key, value, oldValue);
1170
+ return this;
1171
+ },
1172
+ delete(key) {
1173
+ const target = /* @__PURE__ */ toRaw(this);
1174
+ const { has, get } = getProto(target);
1175
+ let hadKey = has.call(target, key);
1176
+ if (!hadKey) {
1177
+ key = /* @__PURE__ */ toRaw(key);
1178
+ hadKey = has.call(target, key);
1179
+ } else checkIdentityKeys(target, has, key);
1180
+ const oldValue = get ? get.call(target, key) : void 0;
1181
+ const result = target.delete(key);
1182
+ if (hadKey) trigger(target, "delete", key, void 0, oldValue);
1183
+ return result;
1184
+ },
1185
+ clear() {
1186
+ const target = /* @__PURE__ */ toRaw(this);
1187
+ const hadItems = target.size !== 0;
1188
+ const oldTarget = isMap(target) ? new Map(target) : new Set(target);
1189
+ const result = target.clear();
1190
+ if (hadItems) trigger(target, "clear", void 0, void 0, oldTarget);
1191
+ return result;
1192
+ }
1193
+ });
1194
+ [
1195
+ "keys",
1196
+ "values",
1197
+ "entries",
1198
+ Symbol.iterator
1199
+ ].forEach((method) => {
1200
+ instrumentations[method] = createIterableMethod(method, readonly, shallow);
1201
+ });
1202
+ return instrumentations;
1203
+ }
1204
+ function createInstrumentationGetter(isReadonly, shallow) {
1205
+ const instrumentations = createInstrumentations(isReadonly, shallow);
1206
+ return (target, key, receiver) => {
1207
+ if (key === "__v_isReactive") return !isReadonly;
1208
+ else if (key === "__v_isReadonly") return isReadonly;
1209
+ else if (key === "__v_raw") return target;
1210
+ return Reflect.get(hasOwn(instrumentations, key) && key in target ? instrumentations : target, key, receiver);
1211
+ };
1212
+ }
1213
+ const mutableCollectionHandlers = { get: /*@__PURE__*/ createInstrumentationGetter(false, false) };
1214
+ const readonlyCollectionHandlers = { get: /*@__PURE__*/ createInstrumentationGetter(true, false) };
1215
+ function checkIdentityKeys(target, has, key) {
1216
+ const rawKey = /* @__PURE__ */ toRaw(key);
1217
+ if (rawKey !== key && has.call(target, rawKey)) {
1218
+ const type = toRawType(target);
1219
+ 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.`);
1220
+ }
1221
+ }
1222
+ //#endregion
1223
+ //#region packages/signal/src/reactive.ts
1224
+ const reactiveMap = /* @__PURE__ */ new WeakMap();
1225
+ const shallowReactiveMap = /* @__PURE__ */ new WeakMap();
1226
+ const readonlyMap = /* @__PURE__ */ new WeakMap();
1227
+ const shallowReadonlyMap = /* @__PURE__ */ new WeakMap();
1228
+ function targetTypeMap(rawType) {
1229
+ switch (rawType) {
1230
+ case "Object":
1231
+ case "Array": return 1;
1232
+ case "Map":
1233
+ case "Set":
1234
+ case "WeakMap":
1235
+ case "WeakSet": return 2;
1236
+ default: return 0;
1237
+ }
1238
+ }
1239
+ function getTargetType(value) {
1240
+ return value["__v_skip"] || !Object.isExtensible(value) ? 0 : targetTypeMap(toRawType(value));
1241
+ }
1242
+ /*@__NO_SIDE_EFFECTS__*/
1243
+ function reactive(target) {
1244
+ if (/* @__PURE__ */ isReadonly(target)) return target;
1245
+ return createReactiveObject(target, false, mutableHandlers, mutableCollectionHandlers, reactiveMap);
1246
+ }
1247
+ /**
1248
+ * Takes an object (reactive or plain) or a ref and returns a readonly proxy to
1249
+ * the original.
1250
+ *
1251
+ * A readonly proxy is deep: any nested property accessed will be readonly as
1252
+ * well. It also has the same ref-unwrapping behavior as {@link reactive},
1253
+ * except the unwrapped values will also be made readonly.
1254
+ *
1255
+ * @example
1256
+ * ```js
1257
+ * const original = reactive({ count: 0 })
1258
+ *
1259
+ * const copy = readonly(original)
1260
+ *
1261
+ * watchEffect(() => {
1262
+ * // works for reactivity tracking
1263
+ * console.log(copy.count)
1264
+ * })
1265
+ *
1266
+ * // mutating original will trigger watchers relying on the copy
1267
+ * original.count++
1268
+ *
1269
+ * // mutating the copy will fail and result in a warning
1270
+ * copy.count++ // warning!
1271
+ * ```
1272
+ *
1273
+ * @param target - The source object.
1274
+ * @see {@link https://vuejs.org/api/reactivity-core.html#readonly}
1275
+ */
1276
+ /*@__NO_SIDE_EFFECTS__*/
1277
+ function readonly(target) {
1278
+ return createReactiveObject(target, true, readonlyHandlers, readonlyCollectionHandlers, readonlyMap);
1279
+ }
1280
+ function createReactiveObject(target, isReadonly, baseHandlers, collectionHandlers, proxyMap) {
1281
+ if (!isObject(target)) {
1282
+ warn(`value cannot be made ${isReadonly ? "readonly" : "reactive"}: ${String(target)}`);
1283
+ return target;
1284
+ }
1285
+ if (target["__v_raw"] && !(isReadonly && target["__v_isReactive"])) return target;
1286
+ const targetType = getTargetType(target);
1287
+ if (targetType === 0) return target;
1288
+ const existingProxy = proxyMap.get(target);
1289
+ if (existingProxy) return existingProxy;
1290
+ const proxy = new Proxy(target, targetType === 2 ? collectionHandlers : baseHandlers);
1291
+ proxyMap.set(target, proxy);
1292
+ return proxy;
1293
+ }
1294
+ /**
1295
+ * Checks if an object is a proxy created by {@link reactive} or
1296
+ * {@link shallowReactive} (or {@link ref} in some cases).
1297
+ *
1298
+ * @example
1299
+ * ```js
1300
+ * isReactive(reactive({})) // => true
1301
+ * isReactive(readonly(reactive({}))) // => true
1302
+ * isReactive(ref({}).value) // => true
1303
+ * isReactive(readonly(ref({})).value) // => true
1304
+ * isReactive(ref(true)) // => false
1305
+ * isReactive(shallowRef({}).value) // => false
1306
+ * isReactive(shallowReactive({})) // => true
1307
+ * ```
1308
+ *
1309
+ * @param value - The value to check.
1310
+ * @see {@link https://vuejs.org/api/reactivity-utilities.html#isreactive}
1311
+ */
1312
+ /*@__NO_SIDE_EFFECTS__*/
1313
+ function isReactive(value) {
1314
+ if (/* @__PURE__ */ isReadonly(value)) return /* @__PURE__ */ isReactive(value["__v_raw"]);
1315
+ return !!(value && value["__v_isReactive"]);
1316
+ }
1317
+ /**
1318
+ * Checks whether the passed value is a readonly object. The properties of a
1319
+ * readonly object can change, but they can't be assigned directly via the
1320
+ * passed object.
1321
+ *
1322
+ * The proxies created by {@link readonly} and {@link shallowReadonly} are
1323
+ * both considered readonly, as is a computed ref without a set function.
1324
+ *
1325
+ * @param value - The value to check.
1326
+ * @see {@link https://vuejs.org/api/reactivity-utilities.html#isreadonly}
1327
+ */
1328
+ /*@__NO_SIDE_EFFECTS__*/
1329
+ function isReadonly(value) {
1330
+ return !!(value && value["__v_isReadonly"]);
1331
+ }
1332
+ /*@__NO_SIDE_EFFECTS__*/
1333
+ function isShallow(value) {
1334
+ return !!(value && value["__v_isShallow"]);
1335
+ }
1336
+ /**
1337
+ * Checks if an object is a proxy created by {@link reactive},
1338
+ * {@link readonly}, {@link shallowReactive} or {@link shallowReadonly}.
1339
+ *
1340
+ * @param value - The value to check.
1341
+ * @see {@link https://vuejs.org/api/reactivity-utilities.html#isproxy}
1342
+ */
1343
+ /*@__NO_SIDE_EFFECTS__*/
1344
+ function isProxy(value) {
1345
+ return value ? !!value["__v_raw"] : false;
1346
+ }
1347
+ /**
1348
+ * Returns the raw, original object of a Vue-created proxy.
1349
+ *
1350
+ * `toRaw()` can return the original object from proxies created by
1351
+ * {@link reactive}, {@link readonly}, {@link shallowReactive} or
1352
+ * {@link shallowReadonly}.
1353
+ *
1354
+ * This is an escape hatch that can be used to temporarily read without
1355
+ * incurring proxy access / tracking overhead or write without triggering
1356
+ * changes. It is **not** recommended to hold a persistent reference to the
1357
+ * original object. Use with caution.
1358
+ *
1359
+ * @example
1360
+ * ```js
1361
+ * const foo = {}
1362
+ * const reactiveFoo = reactive(foo)
1363
+ *
1364
+ * console.log(toRaw(reactiveFoo) === foo) // true
1365
+ * ```
1366
+ *
1367
+ * @param observed - The object for which the "raw" value is requested.
1368
+ * @see {@link https://vuejs.org/api/reactivity-advanced.html#toraw}
1369
+ */
1370
+ /*@__NO_SIDE_EFFECTS__*/
1371
+ function toRaw(observed) {
1372
+ const raw = observed && observed["__v_raw"];
1373
+ return raw ? /* @__PURE__ */ toRaw(raw) : observed;
1374
+ }
1375
+ /**
1376
+ * Returns a reactive proxy of the given value (if possible).
1377
+ *
1378
+ * If the given value is not an object, the original value itself is returned.
1379
+ *
1380
+ * @param value - The value for which a reactive proxy shall be created.
1381
+ */
1382
+ const toReactive = (value) => isObject(value) ? /* @__PURE__ */ reactive(value) : value;
1383
+ /**
1384
+ * Returns a readonly proxy of the given value (if possible).
1385
+ *
1386
+ * If the given value is not an object, the original value itself is returned.
1387
+ *
1388
+ * @param value - The value for which a readonly proxy shall be created.
1389
+ */
1390
+ const toReadonly = (value) => isObject(value) ? /* @__PURE__ */ readonly(value) : value;
1391
+ //#endregion
1392
+ //#region packages/signal/src/state.ts
1393
+ function state(value) {
1394
+ if (arguments.length === 0) return /* @__PURE__ */ ref();
1395
+ return isProxyable(value) ? /* @__PURE__ */ reactive(value) : /* @__PURE__ */ ref(value);
1396
+ }
1397
+ function isValueState(value) {
1398
+ if (!value || typeof value !== "object") return false;
1399
+ const obj = value;
1400
+ if (Object.getOwnPropertyDescriptor(obj, "get") || Object.getOwnPropertyDescriptor(obj, "has")) return false;
1401
+ return "value" in obj;
1402
+ }
1403
+ function isProxyable(value) {
1404
+ if (value === null || typeof value !== "object") return false;
1405
+ if (Array.isArray(value)) return true;
1406
+ if (value instanceof Map || value instanceof Set || value instanceof WeakMap || value instanceof WeakSet) return true;
1407
+ return isPlainObject$1(value);
1408
+ }
1409
+ function isPlainObject$1(value) {
1410
+ const proto = Object.getPrototypeOf(value);
1411
+ return proto === Object.prototype || proto === null;
1412
+ }
1413
+ //#endregion
1414
+ //#region packages/signal/src/computed.ts
1415
+ /**
1416
+ * @private exported by @vue/reactivity for Vue core use, but not exported from
1417
+ * the main vue package
1418
+ */
1419
+ var ComputedRefImpl = class {
1420
+ constructor(fn, setter, isSSR) {
1421
+ this.fn = fn;
1422
+ this.setter = setter;
1423
+ this._value = void 0;
1424
+ this.dep = new Dep(this);
1425
+ this.__v_isRef = true;
1426
+ this.deps = void 0;
1427
+ this.depsTail = void 0;
1428
+ this.flags = 16;
1429
+ this.globalVersion = globalVersion - 1;
1430
+ this.next = void 0;
1431
+ this.effect = this;
1432
+ this["__v_isReadonly"] = !setter;
1433
+ this.isSSR = isSSR;
1434
+ }
1435
+ /**
1436
+ * @internal
1437
+ */
1438
+ notify() {
1439
+ this.flags |= 16;
1440
+ if (!(this.flags & 8) && activeSub !== this) {
1441
+ queueSubscriber(this, true);
1442
+ return true;
1443
+ }
1444
+ }
1445
+ get value() {
1446
+ const link = this.dep.track({
1447
+ target: this,
1448
+ type: "get",
1449
+ key: "value"
1450
+ });
1451
+ refreshComputed(this);
1452
+ if (link) link.version = this.dep.version;
1453
+ return this._value;
1454
+ }
1455
+ set value(newValue) {
1456
+ if (this.setter) this.setter(newValue);
1457
+ else warn("Write operation failed: computed value is readonly");
1458
+ }
1459
+ };
1460
+ /*@__NO_SIDE_EFFECTS__*/
1461
+ function computed(getterOrOptions, debugOptions, isSSR = false) {
1462
+ let getter;
1463
+ let setter;
1464
+ if (isFunction(getterOrOptions)) getter = getterOrOptions;
1465
+ else {
1466
+ getter = getterOrOptions.get;
1467
+ setter = getterOrOptions.set;
1468
+ }
1469
+ const cRef = new ComputedRefImpl(getter, setter, isSSR);
1470
+ if (debugOptions && !isSSR) {
1471
+ cRef.onTrack = debugOptions.onTrack;
1472
+ cRef.onTrigger = debugOptions.onTrigger;
1473
+ }
1474
+ return cRef;
1475
+ }
1476
+ //#endregion
1477
+ //#region packages/signal/src/scheduler.ts
1478
+ const queue = /* @__PURE__ */ new Set();
1479
+ let flushing = false;
1480
+ let pending = false;
1481
+ function queueJob(job) {
1482
+ queue.add(job);
1483
+ if (!pending) {
1484
+ pending = true;
1485
+ queueMicrotask(flushJobs);
1486
+ }
1487
+ }
1488
+ function flushJobs() {
1489
+ if (flushing) return;
1490
+ pending = false;
1491
+ flushing = true;
1492
+ try {
1493
+ for (const job of queue) job();
1494
+ } finally {
1495
+ queue.clear();
1496
+ flushing = false;
1497
+ }
1498
+ }
1499
+ function nextTick() {
1500
+ return Promise.resolve();
1501
+ }
1502
+ //#endregion
1503
+ //#region packages/signal/src/watch.ts
1504
+ let WatchErrorCodes = /* @__PURE__ */ function(WatchErrorCodes) {
1505
+ WatchErrorCodes[WatchErrorCodes["WATCH_GETTER"] = 2] = "WATCH_GETTER";
1506
+ WatchErrorCodes[WatchErrorCodes["WATCH_CALLBACK"] = 3] = "WATCH_CALLBACK";
1507
+ WatchErrorCodes[WatchErrorCodes["WATCH_CLEANUP"] = 4] = "WATCH_CLEANUP";
1508
+ return WatchErrorCodes;
1509
+ }({});
1510
+ const INITIAL_WATCHER_VALUE = {};
1511
+ const cleanupMap = /* @__PURE__ */ new WeakMap();
1512
+ let activeWatcher = void 0;
1513
+ /**
1514
+ * Returns the current active effect if there is one.
1515
+ */
1516
+ function getCurrentWatcher() {
1517
+ return activeWatcher;
1518
+ }
1519
+ /**
1520
+ * Registers a cleanup callback on the current active effect. This
1521
+ * registered cleanup callback will be invoked right before the
1522
+ * associated effect re-runs.
1523
+ *
1524
+ * @param cleanupFn - The callback function to attach to the effect's cleanup.
1525
+ * @param failSilently - if `true`, will not throw warning when called without
1526
+ * an active effect.
1527
+ * @param owner - The effect that this cleanup function should be attached to.
1528
+ * By default, the current active effect.
1529
+ */
1530
+ function onWatcherCleanup(cleanupFn, failSilently = false, owner = activeWatcher) {
1531
+ if (owner) {
1532
+ let cleanups = cleanupMap.get(owner);
1533
+ if (!cleanups) cleanupMap.set(owner, cleanups = []);
1534
+ cleanups.push(cleanupFn);
1535
+ } else if (!failSilently) warn("onWatcherCleanup() was called when there was no active watcher to associate with.");
1536
+ }
1537
+ function watch(source, cb, options = EMPTY_OBJ) {
1538
+ const { immediate, deep, once, scheduler, augmentJob, call } = options;
1539
+ const warnInvalidSource = (s) => {
1540
+ (options.onWarn || warn)(`Invalid watch source: `, s, "A watch source can only be a getter/effect function, a ref, a reactive object, or an array of these types.");
1541
+ };
1542
+ const reactiveGetter = (source) => {
1543
+ if (deep) return source;
1544
+ if (/* @__PURE__ */ isShallow(source) || deep === false || deep === 0) return traverse(source, 1);
1545
+ return traverse(source);
1546
+ };
1547
+ let effect;
1548
+ let getter;
1549
+ let cleanup;
1550
+ let boundCleanup;
1551
+ let forceTrigger = false;
1552
+ let isMultiSource = false;
1553
+ if (/* @__PURE__ */ isRef(source)) {
1554
+ getter = () => source.value;
1555
+ forceTrigger = /* @__PURE__ */ isShallow(source);
1556
+ } else if (/* @__PURE__ */ isReactive(source)) {
1557
+ getter = () => reactiveGetter(source);
1558
+ forceTrigger = true;
1559
+ } else if (isArray(source)) {
1560
+ isMultiSource = true;
1561
+ forceTrigger = source.some((s) => /* @__PURE__ */ isReactive(s) || /* @__PURE__ */ isShallow(s));
1562
+ getter = () => source.map((s) => {
1563
+ if (/* @__PURE__ */ isRef(s)) return s.value;
1564
+ else if (/* @__PURE__ */ isReactive(s)) return reactiveGetter(s);
1565
+ else if (isFunction(s)) return call ? call(s, 2) : s();
1566
+ else warnInvalidSource(s);
1567
+ });
1568
+ } else if (isFunction(source)) if (cb) getter = call ? () => call(source, 2) : source;
1569
+ else getter = () => {
1570
+ if (cleanup) {
1571
+ pauseTracking();
1572
+ try {
1573
+ cleanup();
1574
+ } finally {
1575
+ resetTracking();
1576
+ }
1577
+ }
1578
+ const currentEffect = activeWatcher;
1579
+ activeWatcher = effect;
1580
+ try {
1581
+ return call ? call(source, 3, [boundCleanup]) : source(boundCleanup);
1582
+ } finally {
1583
+ activeWatcher = currentEffect;
1584
+ }
1585
+ };
1586
+ else {
1587
+ getter = NOOP;
1588
+ warnInvalidSource(source);
1589
+ }
1590
+ if (cb && deep) {
1591
+ const baseGetter = getter;
1592
+ const depth = deep === true ? Infinity : deep;
1593
+ getter = () => traverse(baseGetter(), depth);
1594
+ }
1595
+ const scope = getCurrentScope();
1596
+ const watchHandle = () => {
1597
+ effect.stop();
1598
+ if (scope && scope.active) remove(scope.effects, effect);
1599
+ };
1600
+ if (once && cb) {
1601
+ const _cb = cb;
1602
+ cb = (...args) => {
1603
+ _cb(...args);
1604
+ watchHandle();
1605
+ };
1606
+ }
1607
+ let oldValue = isMultiSource ? new Array(source.length).fill(INITIAL_WATCHER_VALUE) : INITIAL_WATCHER_VALUE;
1608
+ const job = (immediateFirstRun) => {
1609
+ if (!(effect.flags & 1) || !effect.dirty && !immediateFirstRun) return;
1610
+ if (cb) {
1611
+ const newValue = effect.run();
1612
+ if (deep || forceTrigger || (isMultiSource ? newValue.some((v, i) => hasChanged(v, oldValue[i])) : hasChanged(newValue, oldValue))) {
1613
+ if (cleanup) cleanup();
1614
+ const currentWatcher = activeWatcher;
1615
+ activeWatcher = effect;
1616
+ try {
1617
+ const args = [
1618
+ newValue,
1619
+ oldValue === INITIAL_WATCHER_VALUE ? void 0 : isMultiSource && oldValue[0] === INITIAL_WATCHER_VALUE ? [] : oldValue,
1620
+ boundCleanup
1621
+ ];
1622
+ oldValue = newValue;
1623
+ call ? call(cb, 3, args) : cb(...args);
1624
+ } finally {
1625
+ activeWatcher = currentWatcher;
1626
+ }
1627
+ }
1628
+ } else effect.run();
1629
+ };
1630
+ if (augmentJob) augmentJob(job);
1631
+ effect = new ReactiveEffect(getter);
1632
+ effect.scheduler = scheduler ? () => scheduler(job, false) : job;
1633
+ boundCleanup = (fn) => onWatcherCleanup(fn, false, effect);
1634
+ cleanup = effect.onStop = () => {
1635
+ const cleanups = cleanupMap.get(effect);
1636
+ if (cleanups) {
1637
+ if (call) call(cleanups, 4);
1638
+ else for (const cleanup of cleanups) cleanup();
1639
+ cleanupMap.delete(effect);
1640
+ }
1641
+ };
1642
+ effect.onTrack = options.onTrack;
1643
+ effect.onTrigger = options.onTrigger;
1644
+ if (cb) if (immediate) job(true);
1645
+ else oldValue = effect.run();
1646
+ else if (scheduler) scheduler(job.bind(null, true), true);
1647
+ else effect.run();
1648
+ watchHandle.pause = effect.pause.bind(effect);
1649
+ watchHandle.resume = effect.resume.bind(effect);
1650
+ watchHandle.stop = watchHandle;
1651
+ return watchHandle;
1652
+ }
1653
+ function traverse(value, depth = Infinity, seen) {
1654
+ if (depth <= 0 || !isObject(value) || value["__v_skip"]) return value;
1655
+ seen = seen || /* @__PURE__ */ new Map();
1656
+ if ((seen.get(value) || 0) >= depth) return value;
1657
+ seen.set(value, depth);
1658
+ depth--;
1659
+ if (/* @__PURE__ */ isRef(value)) traverse(value.value, depth, seen);
1660
+ else if (isArray(value)) for (let i = 0; i < value.length; i++) traverse(value[i], depth, seen);
1661
+ else if (isSet(value) || isMap(value)) value.forEach((v) => {
1662
+ traverse(v, depth, seen);
1663
+ });
1664
+ else if (isPlainObject(value)) {
1665
+ for (const key in value) traverse(value[key], depth, seen);
1666
+ for (const key of Object.getOwnPropertySymbols(value)) if (Object.prototype.propertyIsEnumerable.call(value, key)) traverse(value[key], depth, seen);
1667
+ }
1668
+ return value;
1669
+ }
1670
+ //#endregion
1671
+ //#region packages/signal/src/lifecycle.ts
1672
+ function onCleanup(fn) {
1673
+ if (getCurrentEffect()) {
1674
+ onEffectCleanup(fn, true);
1675
+ return;
1676
+ }
1677
+ if (getCurrentScope()) {
1678
+ onScopeDispose(fn, true);
1679
+ return;
1680
+ }
1681
+ warn("onCleanup() was called without active effect or scope.");
1682
+ }
1683
+ //#endregion
1684
+ export { ARRAY_ITERATE_KEY, EffectFlags, EffectScope, ITERATE_KEY, MAP_KEY_ITERATE_KEY, ReactiveEffect, ReactiveFlags, TrackOpTypes, TriggerOpTypes, WatchErrorCodes, batch, computed, effect, effectScope, effectScope as scope, enableTracking, flushJobs, getCurrentEffect, getCurrentScope, getCurrentWatcher, isValueState, nextTick, onCleanup, onEffectCleanup, onScopeDispose, onWatcherCleanup, pauseTracking, queueJob, reactiveReadArray, resetTracking, shallowReadArray, state, stop, track, traverse, trigger, untrack, watch };