@zeus-js/signal 0.1.0 → 0.1.1-beta.1

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