@ng-org/alien-deepsignals 0.1.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.
package/dist/index.js ADDED
@@ -0,0 +1,975 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/core.ts
2
+
3
+
4
+
5
+
6
+
7
+
8
+
9
+
10
+ var _aliensignals = require('alien-signals');
11
+
12
+
13
+
14
+
15
+
16
+
17
+ function tagSignal(fn) {
18
+ Object.defineProperty(fn, "__v_isSignal" /* IS_SIGNAL */, { value: true });
19
+ Object.defineProperty(fn, "value", {
20
+ get: () => fn(),
21
+ set: (v) => fn(v)
22
+ });
23
+ if (!fn.peek) Object.defineProperty(fn, "peek", { value: () => fn() });
24
+ if (!fn.get) Object.defineProperty(fn, "get", { value: () => fn() });
25
+ if (!fn.set) Object.defineProperty(fn, "set", { value: (v) => fn(v) });
26
+ return fn;
27
+ }
28
+ function tagComputed(fn) {
29
+ Object.defineProperty(fn, "__v_isSignal" /* IS_SIGNAL */, { value: true });
30
+ Object.defineProperty(fn, "value", { get: () => fn() });
31
+ if (!fn.peek) Object.defineProperty(fn, "peek", { value: () => fn() });
32
+ if (!fn.get) Object.defineProperty(fn, "get", { value: () => fn() });
33
+ return fn;
34
+ }
35
+ var signal2 = (v) => tagSignal(_aliensignals.signal.call(void 0, v));
36
+ var computed2 = (getter) => tagComputed(_aliensignals.computed.call(void 0, getter));
37
+ var isSignal = (s) => typeof s === "function" && !!s && !!s["__v_isSignal" /* IS_SIGNAL */];
38
+ function batch(fn) {
39
+ _aliensignals.startBatch.call(void 0, );
40
+ try {
41
+ return fn();
42
+ } finally {
43
+ _aliensignals.endBatch.call(void 0, );
44
+ }
45
+ }
46
+
47
+ // src/iteratorHelpers.ts
48
+ function hasNativeIteratorHelpers() {
49
+ return typeof Iterator !== "undefined" && typeof Iterator.from === "function";
50
+ }
51
+ var iteratorHelperKeys = /* @__PURE__ */ new Set([
52
+ "map",
53
+ "filter",
54
+ "take",
55
+ "drop",
56
+ "flatMap",
57
+ "reduce",
58
+ "toArray",
59
+ "forEach",
60
+ "some",
61
+ "every",
62
+ "find"
63
+ ]);
64
+ function createIteratorWithHelpers(nextImpl, sourceIterator) {
65
+ const base = {
66
+ next: nextImpl,
67
+ [Symbol.iterator]() {
68
+ return this;
69
+ },
70
+ return() {
71
+ if (sourceIterator && typeof sourceIterator.return === "function") {
72
+ sourceIterator.return();
73
+ }
74
+ return { value: void 0, done: true };
75
+ }
76
+ };
77
+ if (hasNativeIteratorHelpers()) {
78
+ return Iterator.from(base);
79
+ }
80
+ return base;
81
+ }
82
+
83
+ // src/deepSignal.ts
84
+ var rawToProxy = /* @__PURE__ */ new WeakMap();
85
+ var proxyToMeta = /* @__PURE__ */ new WeakMap();
86
+ var proxySignals = /* @__PURE__ */ new WeakMap();
87
+ var iterableSignals = /* @__PURE__ */ new WeakMap();
88
+ var ignored = /* @__PURE__ */ new WeakSet();
89
+ var rootStates = /* @__PURE__ */ new Map();
90
+ var pendingRoots = /* @__PURE__ */ new Set();
91
+ var supported = /* @__PURE__ */ new Set([Object, Array, Set]);
92
+ var descriptor = Object.getOwnPropertyDescriptor;
93
+ var blankNodeCounter = 0;
94
+ var wellKnownSymbols = /* @__PURE__ */ new Set([
95
+ Symbol.asyncDispose,
96
+ Symbol.asyncIterator,
97
+ Symbol.dispose,
98
+ Symbol.hasInstance,
99
+ Symbol.iterator,
100
+ Symbol.isConcatSpreadable,
101
+ Symbol.match,
102
+ Symbol.matchAll,
103
+ Symbol.metadata,
104
+ Symbol.replace,
105
+ Symbol.search,
106
+ Symbol.species,
107
+ Symbol.split,
108
+ Symbol.toPrimitive,
109
+ Symbol.toStringTag,
110
+ Symbol.unscopables
111
+ ]);
112
+ var forcedSyntheticIds = /* @__PURE__ */ new WeakMap();
113
+ var META_KEY = "__meta__";
114
+ var RAW_KEY = "__raw__";
115
+ var DEFAULT_SYNTHETIC_ID_PROPERTY_NAME = "@id";
116
+ function getMeta(target) {
117
+ if (!target || typeof target !== "object") return void 0;
118
+ return proxyToMeta.get(target);
119
+ }
120
+ function getRaw(value) {
121
+ const meta = getMeta(value);
122
+ return _nullishCoalesce(_optionalChain([meta, 'optionalAccess', _ => _.raw]), () => ( value));
123
+ }
124
+ function shouldProxy(value) {
125
+ return !!value && typeof value === "object" && supported.has(value.constructor) && !ignored.has(value);
126
+ }
127
+ function ensureSignalMap(proxy) {
128
+ if (!proxySignals.has(proxy)) proxySignals.set(proxy, /* @__PURE__ */ new Map());
129
+ return proxySignals.get(proxy);
130
+ }
131
+ function setSignalValue(signals, key, value) {
132
+ if (!signals.has(key)) {
133
+ signals.set(key, signal2(value));
134
+ return;
135
+ }
136
+ const existing = signals.get(key);
137
+ if (typeof existing.set === "function") {
138
+ existing.set(value);
139
+ }
140
+ }
141
+ function ensureIterableSignal(target) {
142
+ if (!iterableSignals.has(target)) iterableSignals.set(target, signal2(0));
143
+ return iterableSignals.get(target);
144
+ }
145
+ function touchIterable(target) {
146
+ if (!iterableSignals.has(target)) return;
147
+ const sig = iterableSignals.get(target);
148
+ sig.set(sig() + 1);
149
+ }
150
+ function hasGetter(target, key) {
151
+ return typeof _optionalChain([descriptor, 'call', _2 => _2(target, key), 'optionalAccess', _3 => _3.get]) === "function";
152
+ }
153
+ function ensureComputed(signals, target, key, receiver) {
154
+ if (!signals.has(key) && hasGetter(target, key)) {
155
+ signals.set(
156
+ key,
157
+ computed2(() => Reflect.get(target, key, receiver))
158
+ );
159
+ }
160
+ }
161
+ function escapePathSegment(segment) {
162
+ return segment.replace(/~/g, "~0").replace(/\//g, "~1").replace(/\|/g, "~2");
163
+ }
164
+ function isReactiveSymbol(key) {
165
+ return typeof key === "symbol" && !wellKnownSymbols.has(key);
166
+ }
167
+ function buildPath(meta, key, skipEscape = false) {
168
+ const path = [];
169
+ const format = (segment) => {
170
+ if (typeof segment === "symbol") {
171
+ return _nullishCoalesce(segment.description, () => ( segment.toString()));
172
+ }
173
+ return segment;
174
+ };
175
+ const push = (segment, synthetic = false) => {
176
+ if (typeof segment === "number") {
177
+ path.unshift(segment);
178
+ return;
179
+ }
180
+ const normalized = format(segment);
181
+ path.unshift(
182
+ synthetic || skipEscape ? normalized : escapePathSegment(String(normalized))
183
+ );
184
+ };
185
+ push(key, skipEscape);
186
+ let cursor = meta;
187
+ while (cursor && cursor.parent && cursor.key !== void 0) {
188
+ push(cursor.key, !!cursor.isSyntheticId);
189
+ cursor = getMeta(cursor.parent);
190
+ }
191
+ return path;
192
+ }
193
+ function resolveContainerPath(meta) {
194
+ if (!meta || !meta.parent || meta.key === void 0) return [];
195
+ const parentMeta = getMeta(meta.parent);
196
+ return buildPath(parentMeta, meta.key, !!meta.isSyntheticId);
197
+ }
198
+ function schedulePatch(meta, build) {
199
+ if (!meta) return;
200
+ const state = rootStates.get(meta.root);
201
+ const hasListeners = state && (state.listeners.size > 0 || state.justInTimeListeners.size > 0);
202
+ if (!hasListeners) return;
203
+ const result = build();
204
+ if (!result) return;
205
+ const patches = Array.isArray(result) ? result : [result];
206
+ if (!patches.length) return;
207
+ state.pendingPatches.push(...patches);
208
+ state.justInTimeListeners.forEach((cb) => cb({ patches }));
209
+ if (state.listeners.size > 0 && !pendingRoots.has(meta.root)) {
210
+ pendingRoots.add(meta.root);
211
+ queueMicrotask(() => {
212
+ pendingRoots.delete(meta.root);
213
+ const state2 = rootStates.get(meta.root);
214
+ if (!state2) return;
215
+ if (!state2.pendingPatches.length || state2.listeners.size === 0) {
216
+ state2.pendingPatches.length = 0;
217
+ return;
218
+ }
219
+ state2.version += 1;
220
+ const batch2 = {
221
+ version: state2.version,
222
+ patches: state2.pendingPatches.slice()
223
+ };
224
+ state2.pendingPatches.length = 0;
225
+ state2.listeners.forEach((cb) => cb(batch2));
226
+ });
227
+ }
228
+ }
229
+ function applyPropGeneratorResult(meta, value, basePath, inSet) {
230
+ if (!value || typeof value !== "object" || value.constructor !== Object || !_optionalChain([meta, 'access', _4 => _4.options, 'optionalAccess', _5 => _5.propGenerator])) {
231
+ return;
232
+ }
233
+ const result = meta.options.propGenerator({
234
+ path: basePath,
235
+ inSet,
236
+ object: value
237
+ });
238
+ if (result.extraProps) {
239
+ Object.entries(result.extraProps).forEach(([k, v]) => {
240
+ value[k] = v;
241
+ });
242
+ }
243
+ if (result.syntheticId !== void 0 && meta.options.syntheticIdPropertyName && !(meta.options.syntheticIdPropertyName in value)) {
244
+ Object.defineProperty(value, meta.options.syntheticIdPropertyName, {
245
+ value: result.syntheticId,
246
+ enumerable: true,
247
+ configurable: false,
248
+ writable: false
249
+ });
250
+ }
251
+ }
252
+ function initializeObjectTree(meta, value, basePath, inSet) {
253
+ if (!_optionalChain([meta, 'access', _6 => _6.options, 'optionalAccess', _7 => _7.propGenerator])) return;
254
+ if (!value || typeof value !== "object") return;
255
+ if (Array.isArray(value)) {
256
+ value.forEach((entry, idx) => {
257
+ if (entry && typeof entry === "object") {
258
+ initializeObjectTree(meta, entry, [...basePath, idx], false);
259
+ }
260
+ });
261
+ return;
262
+ }
263
+ if (value instanceof Set) {
264
+ for (const entry of value) {
265
+ if (entry && typeof entry === "object") {
266
+ const synthetic = assignSyntheticId(
267
+ meta,
268
+ entry,
269
+ basePath,
270
+ true
271
+ );
272
+ initializeObjectTree(
273
+ meta,
274
+ entry,
275
+ [...basePath, synthetic],
276
+ true
277
+ );
278
+ }
279
+ }
280
+ return;
281
+ }
282
+ if (value.constructor !== Object) return;
283
+ applyPropGeneratorResult(meta, value, basePath, inSet);
284
+ Object.keys(value).forEach((childKey) => {
285
+ if (childKey === meta.options.syntheticIdPropertyName) return;
286
+ const child = value[childKey];
287
+ if (child && typeof child === "object") {
288
+ initializeObjectTree(meta, child, [...basePath, childKey], false);
289
+ }
290
+ });
291
+ }
292
+ function initializeObjectTreeIfNoListeners(meta, basePath, value, inSet) {
293
+ if (!meta || !_optionalChain([meta, 'access', _8 => _8.options, 'optionalAccess', _9 => _9.propGenerator])) return;
294
+ if (!value || typeof value !== "object") return;
295
+ const state = rootStates.get(meta.root);
296
+ if (state && (state.listeners.size > 0 || state.justInTimeListeners.size > 0))
297
+ return;
298
+ initializeObjectTree(meta, value, _nullishCoalesce(basePath, () => ( [])), inSet);
299
+ }
300
+ function ensureChildProxy(value, parentProxy, key, isSyntheticId = false) {
301
+ if (!shouldProxy(value)) return value;
302
+ if (rawToProxy.has(value)) {
303
+ const proxied = rawToProxy.get(value);
304
+ const proxiedMeta = getMeta(proxied);
305
+ if (proxiedMeta) {
306
+ proxiedMeta.parent = parentProxy;
307
+ proxiedMeta.key = key;
308
+ proxiedMeta.isSyntheticId = isSyntheticId;
309
+ }
310
+ return proxied;
311
+ }
312
+ const parentMeta = getMeta(parentProxy);
313
+ if (!parentMeta) return value;
314
+ const proxy = createProxy(
315
+ value,
316
+ parentMeta.root,
317
+ parentMeta.options,
318
+ parentProxy,
319
+ key,
320
+ isSyntheticId
321
+ );
322
+ return proxy;
323
+ }
324
+ function ensureSetInfo(meta) {
325
+ if (!meta.setInfo) {
326
+ meta.setInfo = {
327
+ idForObject: /* @__PURE__ */ new WeakMap(),
328
+ objectForId: /* @__PURE__ */ new Map()
329
+ };
330
+ }
331
+ return meta.setInfo;
332
+ }
333
+ function assignSyntheticId(meta, entry, path, inSet) {
334
+ const rawEntry = getRaw(entry);
335
+ if (!rawEntry || typeof rawEntry !== "object") {
336
+ return rawEntry;
337
+ }
338
+ const info = ensureSetInfo(meta);
339
+ if (info.idForObject.has(rawEntry)) return info.idForObject.get(rawEntry);
340
+ let synthetic = forcedSyntheticIds.get(rawEntry);
341
+ const generatorValue = _optionalChain([meta, 'access', _10 => _10.options, 'optionalAccess', _11 => _11.propGenerator, 'optionalCall', _12 => _12({
342
+ path,
343
+ inSet,
344
+ object: rawEntry
345
+ })]);
346
+ if (synthetic === void 0 && _optionalChain([generatorValue, 'optionalAccess', _13 => _13.syntheticId]) !== void 0) {
347
+ synthetic = generatorValue.syntheticId;
348
+ }
349
+ if (_optionalChain([generatorValue, 'optionalAccess', _14 => _14.extraProps]) && rawEntry && typeof rawEntry === "object") {
350
+ Object.entries(generatorValue.extraProps).forEach(([k, v]) => {
351
+ rawEntry[k] = v;
352
+ });
353
+ }
354
+ const idPropName = _optionalChain([meta, 'access', _15 => _15.options, 'optionalAccess', _16 => _16.syntheticIdPropertyName]);
355
+ if (synthetic === void 0 && idPropName && rawEntry && typeof rawEntry === "object" && rawEntry[idPropName] !== void 0) {
356
+ synthetic = rawEntry[idPropName];
357
+ }
358
+ if (synthetic === void 0) {
359
+ synthetic = `_s${++blankNodeCounter}`;
360
+ }
361
+ const idString = String(synthetic);
362
+ info.idForObject.set(rawEntry, idString);
363
+ info.objectForId.set(idString, rawEntry);
364
+ if (idPropName && rawEntry && typeof rawEntry === "object" && !(idPropName in rawEntry)) {
365
+ Object.defineProperty(rawEntry, idPropName, {
366
+ value: idString,
367
+ enumerable: true,
368
+ configurable: false,
369
+ writable: false
370
+ });
371
+ }
372
+ return idString;
373
+ }
374
+ function createProxy(target, root, options, parent, key, isSyntheticId) {
375
+ const handlers = target instanceof Set ? setHandlers : objectHandlers;
376
+ const proxy = new Proxy(target, handlers);
377
+ const meta = {
378
+ raw: target,
379
+ parent,
380
+ key,
381
+ isSyntheticId,
382
+ root,
383
+ options
384
+ };
385
+ proxyToMeta.set(proxy, meta);
386
+ proxySignals.set(proxy, /* @__PURE__ */ new Map());
387
+ rawToProxy.set(target, proxy);
388
+ return proxy;
389
+ }
390
+ function ensureValueForWrite(value, receiver, key) {
391
+ const rawValue = getRaw(value);
392
+ const proxied = shouldProxy(rawValue) ? ensureChildProxy(rawValue, receiver, key) : rawValue;
393
+ return { raw: rawValue, proxied };
394
+ }
395
+ function snapshotLiteral(value) {
396
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
397
+ return value;
398
+ }
399
+ return void 0;
400
+ }
401
+ function emitPatchesForNew(value, meta, basePath, inSet = false) {
402
+ applyPropGeneratorResult(meta, value, basePath, inSet);
403
+ if (value === null || value === void 0 || typeof value !== "object") {
404
+ const literal = snapshotLiteral(value);
405
+ if (literal === void 0) return [];
406
+ return [
407
+ {
408
+ path: basePath,
409
+ op: "add",
410
+ value: literal
411
+ }
412
+ ];
413
+ }
414
+ const patches = [
415
+ {
416
+ path: basePath,
417
+ op: "add",
418
+ type: value instanceof Set ? "set" : "object",
419
+ value: value instanceof Set ? [] : void 0
420
+ }
421
+ ];
422
+ const idPropName = meta.options.syntheticIdPropertyName;
423
+ if (idPropName in value) {
424
+ const literal = snapshotLiteral(value[idPropName]);
425
+ if (literal !== void 0) {
426
+ patches.push({
427
+ path: [...basePath, idPropName],
428
+ op: "add",
429
+ value: literal
430
+ });
431
+ }
432
+ }
433
+ if (Array.isArray(value)) {
434
+ value.forEach((entry, idx) => {
435
+ patches.push(...emitPatchesForNew(entry, meta, [...basePath, idx]));
436
+ });
437
+ } else if (value instanceof Set) {
438
+ const setMeta = ensureSetInfo(meta);
439
+ for (const entry of value) {
440
+ if (entry && typeof entry === "object") {
441
+ const synthetic = assignSyntheticId(
442
+ meta,
443
+ entry,
444
+ basePath,
445
+ true
446
+ );
447
+ setMeta.objectForId.set(String(synthetic), entry);
448
+ patches.push(
449
+ ...emitPatchesForNew(
450
+ entry,
451
+ meta,
452
+ [...basePath, synthetic],
453
+ true
454
+ )
455
+ );
456
+ } else {
457
+ const literal = snapshotLiteral(entry);
458
+ if (literal !== void 0) {
459
+ patches.push({
460
+ path: basePath,
461
+ op: "add",
462
+ type: "set",
463
+ value: [literal]
464
+ });
465
+ }
466
+ }
467
+ }
468
+ } else {
469
+ Object.keys(value).forEach((childKey) => {
470
+ if (childKey === idPropName) return;
471
+ patches.push(
472
+ ...emitPatchesForNew(value[childKey], meta, [
473
+ ...basePath,
474
+ childKey
475
+ ])
476
+ );
477
+ });
478
+ }
479
+ return patches;
480
+ }
481
+ var objectHandlers = {
482
+ get(target, key, receiver) {
483
+ if (key === RAW_KEY) return _nullishCoalesce(_optionalChain([getMeta, 'call', _17 => _17(receiver), 'optionalAccess', _18 => _18.raw]), () => ( target));
484
+ if (key === META_KEY) return getMeta(receiver);
485
+ if (typeof key === "symbol") {
486
+ if (key === Symbol.iterator) {
487
+ const iterableSig = ensureIterableSignal(target);
488
+ iterableSig();
489
+ }
490
+ if (!isReactiveSymbol(key))
491
+ return Reflect.get(target, key, receiver);
492
+ }
493
+ const signals = ensureSignalMap(receiver);
494
+ ensureComputed(signals, target, key, receiver);
495
+ if (!signals.has(key)) {
496
+ let rawValue = Reflect.get(target, key, receiver);
497
+ if (typeof rawValue === "function")
498
+ return rawValue.bind(_nullishCoalesce(receiver, () => ( target)));
499
+ rawValue = shouldProxy(rawValue) ? ensureChildProxy(rawValue, receiver, key) : rawValue;
500
+ signals.set(key, signal2(rawValue));
501
+ }
502
+ const sig = signals.get(key);
503
+ return sig();
504
+ },
505
+ set(target, key, value, receiver) {
506
+ if (typeof key === "symbol" && !isReactiveSymbol(key))
507
+ return Reflect.set(target, key, value, receiver);
508
+ const meta = getMeta(receiver);
509
+ if (_optionalChain([meta, 'optionalAccess', _19 => _19.options, 'optionalAccess', _20 => _20.readOnlyProps, 'optionalAccess', _21 => _21.includes, 'call', _22 => _22(String(key))])) {
510
+ throw new Error(`Cannot modify readonly property '${String(key)}'`);
511
+ }
512
+ const path = meta ? buildPath(meta, key) : void 0;
513
+ const desc = descriptor(target, key);
514
+ const hasAccessor = !!desc && (typeof desc.get === "function" || typeof desc.set === "function");
515
+ const { raw, proxied } = ensureValueForWrite(value, receiver, key);
516
+ const hadKey = Object.prototype.hasOwnProperty.call(target, key);
517
+ const previous = hadKey ? target[key] : void 0;
518
+ const result = Reflect.set(target, key, raw, receiver);
519
+ if (!hasAccessor) {
520
+ const signals = ensureSignalMap(receiver);
521
+ setSignalValue(signals, key, proxied);
522
+ }
523
+ if (!hadKey) touchIterable(target);
524
+ if (meta && path && typeof raw === "object") {
525
+ initializeObjectTreeIfNoListeners(meta, path, raw, false);
526
+ }
527
+ schedulePatch(meta, () => {
528
+ const resolvedPath = _nullishCoalesce(path, () => ( buildPath(meta, key)));
529
+ if (!hadKey || typeof raw === "object") {
530
+ return emitPatchesForNew(raw, meta, resolvedPath);
531
+ }
532
+ if (snapshotLiteral(raw) === void 0) return void 0;
533
+ return {
534
+ path: resolvedPath,
535
+ op: "add",
536
+ value: raw
537
+ };
538
+ });
539
+ return result;
540
+ },
541
+ deleteProperty(target, key) {
542
+ if (typeof key === "symbol" && !isReactiveSymbol(key))
543
+ return Reflect.deleteProperty(target, key);
544
+ const receiver = rawToProxy.get(target);
545
+ const meta = receiver ? getMeta(receiver) : void 0;
546
+ const hadKey = Object.prototype.hasOwnProperty.call(target, key);
547
+ const result = Reflect.deleteProperty(target, key);
548
+ if (hadKey) {
549
+ if (receiver && proxySignals.has(receiver)) {
550
+ const signals = proxySignals.get(receiver);
551
+ const existing = signals.get(key);
552
+ if (existing && typeof existing.set === "function") {
553
+ existing.set(void 0);
554
+ }
555
+ signals.delete(key);
556
+ }
557
+ touchIterable(target);
558
+ schedulePatch(meta, () => ({
559
+ path: buildPath(meta, key),
560
+ op: "remove"
561
+ }));
562
+ }
563
+ return result;
564
+ },
565
+ ownKeys(target) {
566
+ const sig = ensureIterableSignal(target);
567
+ sig();
568
+ return Reflect.ownKeys(target);
569
+ }
570
+ };
571
+ function ensureEntryProxy(receiver, entry, syntheticKey, meta) {
572
+ return shouldProxy(entry) ? ensureChildProxy(entry, receiver, syntheticKey, true) : entry;
573
+ }
574
+ function createSetIterator(target, receiver, mapValue) {
575
+ const iterator = target.values();
576
+ const iterableSignal = ensureIterableSignal(target);
577
+ iterableSignal();
578
+ return createIteratorWithHelpers(() => {
579
+ const next = iterator.next();
580
+ if (next.done) return next;
581
+ const meta = getMeta(receiver);
582
+ const proxied = ensureEntryProxy(
583
+ receiver,
584
+ next.value,
585
+ assignSyntheticId(meta, next.value, [], true),
586
+ meta
587
+ );
588
+ return {
589
+ value: mapValue(proxied),
590
+ done: false
591
+ };
592
+ });
593
+ }
594
+ var setHandlers = {
595
+ get(target, key, receiver) {
596
+ if (key === RAW_KEY) return _nullishCoalesce(_optionalChain([getMeta, 'call', _23 => _23(receiver), 'optionalAccess', _24 => _24.raw]), () => ( target));
597
+ if (key === META_KEY) return getMeta(receiver);
598
+ if (key === "size") {
599
+ const sig = ensureIterableSignal(target);
600
+ sig();
601
+ return target.size;
602
+ }
603
+ if (key === "first") {
604
+ return function first() {
605
+ const iterableSig = ensureIterableSignal(target);
606
+ iterableSig();
607
+ const iterator = target.values().next();
608
+ if (iterator.done) return void 0;
609
+ const meta = getMeta(receiver);
610
+ return ensureEntryProxy(
611
+ receiver,
612
+ iterator.value,
613
+ assignSyntheticId(meta, iterator.value, [], true),
614
+ meta
615
+ );
616
+ };
617
+ }
618
+ if (key === "getById") {
619
+ return function getById(id) {
620
+ const iterableSig = ensureIterableSignal(target);
621
+ iterableSig();
622
+ const meta = getMeta(receiver);
623
+ if (!_optionalChain([meta, 'optionalAccess', _25 => _25.setInfo])) return void 0;
624
+ const entry = meta.setInfo.objectForId.get(String(id));
625
+ if (!entry) return void 0;
626
+ return ensureEntryProxy(receiver, entry, String(id), meta);
627
+ };
628
+ }
629
+ if (key === "getBy") {
630
+ return function getBy(graphIri, subjectIri) {
631
+ const iterableSig = ensureIterableSignal(target);
632
+ iterableSig();
633
+ return this.getById(`${graphIri}|${subjectIri}`);
634
+ };
635
+ }
636
+ if (key === "add") {
637
+ return function add(value) {
638
+ const meta = getMeta(receiver);
639
+ const containerPath = resolveContainerPath(meta);
640
+ const rawValue = getRaw(value);
641
+ const sizeBefore = target.size;
642
+ const result = target.add(rawValue);
643
+ if (target.size !== sizeBefore) {
644
+ touchIterable(target);
645
+ if (rawValue && typeof rawValue === "object") {
646
+ const synthetic = assignSyntheticId(
647
+ meta,
648
+ rawValue,
649
+ containerPath,
650
+ true
651
+ );
652
+ initializeObjectTreeIfNoListeners(
653
+ meta,
654
+ [...containerPath, synthetic],
655
+ rawValue,
656
+ true
657
+ );
658
+ ensureEntryProxy(receiver, rawValue, synthetic, meta);
659
+ schedulePatch(
660
+ meta,
661
+ () => emitPatchesForNew(
662
+ rawValue,
663
+ meta,
664
+ [...containerPath, synthetic],
665
+ true
666
+ )
667
+ );
668
+ } else {
669
+ const literal = snapshotLiteral(rawValue);
670
+ if (literal !== void 0) {
671
+ schedulePatch(meta, () => ({
672
+ path: containerPath,
673
+ op: "add",
674
+ type: "set",
675
+ value: [literal]
676
+ }));
677
+ }
678
+ }
679
+ }
680
+ return receiver;
681
+ };
682
+ }
683
+ if (key === "delete") {
684
+ return function deleteEntry(value) {
685
+ const meta = getMeta(receiver);
686
+ const containerPath = resolveContainerPath(meta);
687
+ const rawValue = getRaw(value);
688
+ const synthetic = rawValue && typeof rawValue === "object" ? ensureSetInfo(meta).idForObject.get(rawValue) : rawValue;
689
+ const existed = target.delete(rawValue);
690
+ if (existed && synthetic !== void 0) {
691
+ touchIterable(target);
692
+ if (rawValue && typeof rawValue === "object") {
693
+ schedulePatch(meta, () => ({
694
+ path: [...containerPath, synthetic],
695
+ op: "remove"
696
+ }));
697
+ if (meta.setInfo) {
698
+ meta.setInfo.objectForId.delete(String(synthetic));
699
+ meta.setInfo.idForObject.delete(rawValue);
700
+ }
701
+ } else {
702
+ schedulePatch(meta, () => ({
703
+ path: containerPath,
704
+ op: "remove",
705
+ type: "set",
706
+ value: rawValue
707
+ }));
708
+ }
709
+ }
710
+ return existed;
711
+ };
712
+ }
713
+ if (key === "clear") {
714
+ return function clear() {
715
+ const meta = getMeta(receiver);
716
+ const containerPath = resolveContainerPath(meta);
717
+ if (meta.setInfo) {
718
+ meta.setInfo.objectForId.clear();
719
+ meta.setInfo.idForObject = /* @__PURE__ */ new WeakMap();
720
+ }
721
+ target.clear();
722
+ touchIterable(target);
723
+ schedulePatch(meta, () => ({
724
+ path: containerPath,
725
+ op: "add",
726
+ type: "set",
727
+ value: []
728
+ }));
729
+ };
730
+ }
731
+ if (key === Symbol.iterator) {
732
+ return function iterator() {
733
+ return createSetIterator(target, receiver, (value) => value);
734
+ };
735
+ }
736
+ if (key === "values" || key === "keys") {
737
+ return function values() {
738
+ return createSetIterator(target, receiver, (value) => value);
739
+ };
740
+ }
741
+ if (key === "entries") {
742
+ return function entries() {
743
+ return createSetIterator(target, receiver, (value) => [
744
+ value,
745
+ value
746
+ ]);
747
+ };
748
+ }
749
+ if (typeof key === "string" && iteratorHelperKeys.has(key)) {
750
+ return function iteratorHelper(...args) {
751
+ const iterator = createSetIterator(
752
+ target,
753
+ receiver,
754
+ (value) => value
755
+ );
756
+ const helper = iterator[key];
757
+ if (typeof helper !== "function") {
758
+ throw new TypeError(
759
+ `Iterator helper '${String(key)}' is not available`
760
+ );
761
+ }
762
+ return helper.apply(iterator, args);
763
+ };
764
+ }
765
+ if (key === "forEach") {
766
+ return function forEach(callback, thisArg) {
767
+ const iterableSig = ensureIterableSignal(target);
768
+ iterableSig();
769
+ const meta = getMeta(receiver);
770
+ let index = 0;
771
+ const expectsIteratorSignature = callback.length <= 2;
772
+ const iteratorCallback = callback;
773
+ target.forEach((entry) => {
774
+ const proxied = ensureEntryProxy(
775
+ receiver,
776
+ entry,
777
+ assignSyntheticId(meta, entry, [], true),
778
+ meta
779
+ );
780
+ if (expectsIteratorSignature) {
781
+ iteratorCallback.call(thisArg, proxied, index++);
782
+ } else {
783
+ callback.call(thisArg, proxied, proxied, receiver);
784
+ }
785
+ });
786
+ };
787
+ }
788
+ if (key === "has") {
789
+ return function has(value) {
790
+ return target.has(getRaw(value));
791
+ };
792
+ }
793
+ return Reflect.get(target, key, receiver);
794
+ }
795
+ };
796
+ function isDeepSignal(value) {
797
+ return !!getMeta(value);
798
+ }
799
+ function deepSignal(input, options) {
800
+ if (isDeepSignal(input)) return input;
801
+ if (!shouldProxy(input))
802
+ throw new Error("deepSignal() expects an object, array, or Set");
803
+ if (rawToProxy.has(input)) return rawToProxy.get(input);
804
+ const root = Symbol("deepSignalRoot");
805
+ const rootState = {
806
+ options: {
807
+ syntheticIdPropertyName: DEFAULT_SYNTHETIC_ID_PROPERTY_NAME,
808
+ ...options
809
+ },
810
+ version: 0,
811
+ listeners: /* @__PURE__ */ new Set(),
812
+ justInTimeListeners: /* @__PURE__ */ new Set(),
813
+ pendingPatches: []
814
+ };
815
+ rootStates.set(root, rootState);
816
+ const proxy = createProxy(input, root, rootState.options);
817
+ return proxy;
818
+ }
819
+ function subscribeDeepMutations(root, cb, triggerInstantly = false) {
820
+ const rootId = typeof root === "symbol" ? root : getDeepSignalRootId(root);
821
+ if (!rootId)
822
+ throw new Error("subscribeDeepMutations() expects a deepSignal root");
823
+ const state = rootStates.get(rootId);
824
+ if (!state) throw new Error("Unknown deepSignal root");
825
+ if (triggerInstantly) {
826
+ state.justInTimeListeners.add(cb);
827
+ return () => {
828
+ state.justInTimeListeners.delete(cb);
829
+ };
830
+ } else {
831
+ state.listeners.add(cb);
832
+ return () => {
833
+ state.listeners.delete(cb);
834
+ };
835
+ }
836
+ }
837
+ function getDeepSignalRootId(value) {
838
+ return _optionalChain([getMeta, 'call', _26 => _26(value), 'optionalAccess', _27 => _27.root]);
839
+ }
840
+ function getDeepSignalVersion(root) {
841
+ const rootId = typeof root === "symbol" ? root : getDeepSignalRootId(root);
842
+ if (!rootId) return void 0;
843
+ return _optionalChain([rootStates, 'access', _28 => _28.get, 'call', _29 => _29(rootId), 'optionalAccess', _30 => _30.version]);
844
+ }
845
+ function shallow(obj) {
846
+ ignored.add(obj);
847
+ return obj;
848
+ }
849
+ function setSetEntrySyntheticId(obj, id) {
850
+ if (!obj || typeof obj !== "object") return;
851
+ forcedSyntheticIds.set(getRaw(obj), String(id));
852
+ }
853
+ function addWithId(set, entry, id) {
854
+ if (entry && typeof entry === "object") {
855
+ setSetEntrySyntheticId(entry, id);
856
+ }
857
+ set.add(entry);
858
+ if (entry && typeof entry === "object") {
859
+ const getter = _optionalChain([set, 'optionalAccess', _31 => _31.getById]);
860
+ if (typeof getter === "function") {
861
+ const proxied = getter.call(set, String(id));
862
+ if (proxied) return proxied;
863
+ }
864
+ }
865
+ return entry;
866
+ }
867
+
868
+ // src/watch.ts
869
+ function watch(source, callback, options = {}) {
870
+ if (!isDeepSignal(source)) {
871
+ throw new Error("watch() expects a deepSignal root proxy");
872
+ }
873
+ const rootId = getDeepSignalRootId(source);
874
+ if (!rootId) throw new Error("Unable to resolve deepSignal root id");
875
+ let active = true;
876
+ let cleanup;
877
+ const { immediate, once } = options;
878
+ const registerCleanup = (fn) => {
879
+ cleanup = fn;
880
+ };
881
+ const runCleanup = () => {
882
+ if (!cleanup) return;
883
+ try {
884
+ cleanup();
885
+ } finally {
886
+ cleanup = void 0;
887
+ }
888
+ };
889
+ const stopListening = () => {
890
+ if (!active) return;
891
+ active = false;
892
+ runCleanup();
893
+ unsubscribe();
894
+ };
895
+ const deliver = (batch2) => {
896
+ if (!active) return;
897
+ runCleanup();
898
+ const next = source;
899
+ callback({
900
+ patches: batch2.patches,
901
+ version: batch2.version,
902
+ newValue: next
903
+ });
904
+ if (once) stopListening();
905
+ };
906
+ const unsubscribe = subscribeDeepMutations(
907
+ rootId,
908
+ (batch2) => {
909
+ if (!batch2.patches.length) return;
910
+ deliver(batch2);
911
+ },
912
+ options.triggerInstantly
913
+ );
914
+ if (immediate) {
915
+ deliver({
916
+ patches: [],
917
+ version: _nullishCoalesce(getDeepSignalVersion(rootId), () => ( 0))
918
+ });
919
+ }
920
+ return {
921
+ stopListening,
922
+ registerCleanup
923
+ };
924
+ }
925
+
926
+ // src/effect.ts
927
+ function effect2(run) {
928
+ let cleanup;
929
+ const registerCleanup = (fn) => {
930
+ cleanup = fn;
931
+ };
932
+ const stop = _aliensignals.effect.call(void 0, () => {
933
+ if (cleanup) {
934
+ try {
935
+ cleanup();
936
+ } finally {
937
+ cleanup = void 0;
938
+ }
939
+ }
940
+ run(registerCleanup);
941
+ });
942
+ return () => {
943
+ if (cleanup) {
944
+ try {
945
+ cleanup();
946
+ } finally {
947
+ cleanup = void 0;
948
+ }
949
+ }
950
+ stop();
951
+ };
952
+ }
953
+
954
+
955
+
956
+
957
+
958
+
959
+
960
+
961
+
962
+
963
+
964
+
965
+
966
+
967
+
968
+
969
+
970
+
971
+
972
+
973
+
974
+
975
+ exports._rawComputed = _aliensignals.computed; exports._rawEffect = _aliensignals.effect; exports._rawEndBatch = _aliensignals.endBatch; exports._rawGetCurrentSub = _aliensignals.getCurrentSub; exports._rawSetCurrentSub = _aliensignals.setCurrentSub; exports._rawSignal = _aliensignals.signal; exports._rawStartBatch = _aliensignals.startBatch; exports.addWithId = addWithId; exports.batch = batch; exports.computed = computed2; exports.deepSignal = deepSignal; exports.effect = effect2; exports.getDeepSignalRootId = getDeepSignalRootId; exports.getDeepSignalVersion = getDeepSignalVersion; exports.isDeepSignal = isDeepSignal; exports.isSignal = isSignal; exports.setSetEntrySyntheticId = setSetEntrySyntheticId; exports.shallow = shallow; exports.signal = signal2; exports.subscribeDeepMutations = subscribeDeepMutations; exports.watch = watch;