r-state-tree 0.3.1 → 0.4.0

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,4 +1,64 @@
1
- import { batch, Signal, signal, computed, untracked, effect } from "@preact/signals-core";
1
+ import { batch, Signal, signal, computed as computed$1, untracked, effect } from "@preact/signals-core";
2
+ import { ReadonlySignal, Signal as Signal2, batch as batch2, effect as effect2, signal as signal2, untracked as untracked2 } from "@preact/signals-core";
3
+ var lib = {};
4
+ var hasRequiredLib;
5
+ function requireLib() {
6
+ if (hasRequiredLib) return lib;
7
+ hasRequiredLib = 1;
8
+ Object.defineProperty(lib, "__esModule", { value: true });
9
+ Symbol.metadata ??= Symbol("Symbol.metadata");
10
+ return lib;
11
+ }
12
+ requireLib();
13
+ var CommonCfgTypes = /* @__PURE__ */ ((CommonCfgTypes2) => {
14
+ CommonCfgTypes2["child"] = "child";
15
+ return CommonCfgTypes2;
16
+ })(CommonCfgTypes || {});
17
+ var ModelCfgTypes = /* @__PURE__ */ ((ModelCfgTypes2) => {
18
+ ModelCfgTypes2["state"] = "state";
19
+ ModelCfgTypes2["id"] = "id";
20
+ ModelCfgTypes2["modelRef"] = "modelRef";
21
+ return ModelCfgTypes2;
22
+ })(ModelCfgTypes || {});
23
+ var StoreCfgTypes = /* @__PURE__ */ ((StoreCfgTypes2) => {
24
+ StoreCfgTypes2["model"] = "model";
25
+ return StoreCfgTypes2;
26
+ })(StoreCfgTypes || {});
27
+ var ObservableCfgTypes = /* @__PURE__ */ ((ObservableCfgTypes2) => {
28
+ ObservableCfgTypes2["observable"] = "observable";
29
+ ObservableCfgTypes2["computed"] = "computed";
30
+ return ObservableCfgTypes2;
31
+ })(ObservableCfgTypes || {});
32
+ const childType = Object.assign(
33
+ function(childType2) {
34
+ return { type: "child", childType: childType2 };
35
+ },
36
+ {
37
+ type: "child"
38
+ /* child */
39
+ }
40
+ );
41
+ const stateType = {
42
+ type: "state"
43
+ /* state */
44
+ };
45
+ const modelRefType = Object.assign(
46
+ function(childType2) {
47
+ return { type: "modelRef", childType: childType2 };
48
+ },
49
+ {
50
+ type: "modelRef"
51
+ /* modelRef */
52
+ }
53
+ );
54
+ const idType = {
55
+ type: "id"
56
+ /* id */
57
+ };
58
+ const modelType = {
59
+ type: "model"
60
+ /* model */
61
+ };
2
62
  function isNonPrimitive(val) {
3
63
  return val != null && (typeof val === "object" || typeof val === "function");
4
64
  }
@@ -6,15 +66,34 @@ function isPropertyKey(val) {
6
66
  return typeof val === "string" || typeof val === "number" || typeof val === "symbol";
7
67
  }
8
68
  function getPropertyType(key, obj) {
69
+ const hasMetadata = obj.constructor[Symbol.metadata] !== void 0;
9
70
  const descriptor = getPropertyDescriptor$1(obj, key);
10
- if (descriptor) {
11
- if (typeof descriptor.get === "function" || typeof descriptor.set === "function") {
71
+ if (descriptor?.value && typeof descriptor.value === "function") {
72
+ return "action";
73
+ }
74
+ const isGetter = descriptor && (typeof descriptor.get === "function" || typeof descriptor.set === "function");
75
+ if (!hasMetadata) {
76
+ if (isGetter) {
12
77
  return "computed";
13
- } else if (typeof descriptor.value === "function") {
14
- return "action";
78
+ }
79
+ return "observable";
80
+ }
81
+ const metadata = obj.constructor[Symbol.metadata];
82
+ if (metadata && metadata[key]) {
83
+ const config = metadata[key];
84
+ switch (config.type) {
85
+ case ObservableCfgTypes.computed:
86
+ return "computed";
87
+ case ModelCfgTypes.state:
88
+ case ObservableCfgTypes.observable:
89
+ case ModelCfgTypes.id:
90
+ case ModelCfgTypes.modelRef:
91
+ case CommonCfgTypes.child:
92
+ case StoreCfgTypes.model:
93
+ return "observable";
15
94
  }
16
95
  }
17
- return "observable";
96
+ return null;
18
97
  }
19
98
  function getPropertyDescriptor$1(obj, key) {
20
99
  let node = obj;
@@ -36,9 +115,15 @@ function resolveNode(node) {
36
115
  return node.node ?? node;
37
116
  }
38
117
  let circularRefSet = null;
39
- const _Administration = class _Administration {
118
+ class Administration {
119
+ static proxyTraps = {};
120
+ proxy;
121
+ source;
122
+ atom;
123
+ valuesMap;
124
+ isObserved = false;
125
+ forceObservedAtoms;
40
126
  constructor(source2) {
41
- this.isObserved = false;
42
127
  this.atom = createObservedAtom();
43
128
  this.source = source2;
44
129
  this.proxy = new Proxy(
@@ -85,12 +170,13 @@ const _Administration = class _Administration {
85
170
  circularRefSet = null;
86
171
  }
87
172
  }
88
- };
89
- _Administration.proxyTraps = {};
90
- let Administration = _Administration;
173
+ }
91
174
  class NodeMap {
175
+ map;
176
+ weakMap;
177
+ observedAtom;
178
+ cleanUpRegistered = false;
92
179
  constructor(observedAtom) {
93
- this.cleanUpRegistered = false;
94
180
  this.observedAtom = observedAtom;
95
181
  }
96
182
  registerCleanup() {
@@ -153,7 +239,47 @@ class SignalMap extends NodeMap {
153
239
  return createSignal(initialValue);
154
240
  }
155
241
  }
156
- const _ObjectAdministration = class _ObjectAdministration extends Administration {
242
+ class ObjectAdministration extends Administration {
243
+ keysAtom;
244
+ hasMap;
245
+ valuesMap;
246
+ computedMap;
247
+ types;
248
+ static proxyTraps = {
249
+ has(target, name) {
250
+ const adm = getAdministration(target);
251
+ if (!(name in Object.prototype) && isPropertyKey(name))
252
+ return adm.has(name);
253
+ return Reflect.has(adm.source, name);
254
+ },
255
+ get(target, name) {
256
+ const adm = getAdministration(target);
257
+ if (!(name in Object.prototype) && isPropertyKey(name) && (typeof adm.source !== "function" || name !== "prototype")) {
258
+ return adm.read(name);
259
+ }
260
+ return Reflect.get(adm.source, name, adm.proxy);
261
+ },
262
+ set(target, name, value) {
263
+ if (!isPropertyKey(name)) return false;
264
+ const adm = getAdministration(target);
265
+ adm.write(name, value);
266
+ return true;
267
+ },
268
+ deleteProperty(target, name) {
269
+ if (!isPropertyKey(name)) return false;
270
+ const adm = getAdministration(target);
271
+ adm.remove(name);
272
+ return true;
273
+ },
274
+ ownKeys(target) {
275
+ const adm = getAdministration(target);
276
+ batch(() => {
277
+ adm.keysAtom.reportObserved();
278
+ adm.atom.reportObserved();
279
+ });
280
+ return Reflect.ownKeys(adm.source);
281
+ }
282
+ };
157
283
  constructor(source2 = {}) {
158
284
  super(source2);
159
285
  this.keysAtom = createAtom();
@@ -165,7 +291,7 @@ const _ObjectAdministration = class _ObjectAdministration extends Administration
165
291
  return Reflect.get(this.source, key, this.proxy);
166
292
  }
167
293
  set(key, value) {
168
- runInBatch(() => {
294
+ batch(() => {
169
295
  Reflect.set(this.source, key, value, this.proxy);
170
296
  });
171
297
  }
@@ -188,7 +314,7 @@ const _ObjectAdministration = class _ObjectAdministration extends Administration
188
314
  }
189
315
  getType(key) {
190
316
  let type = this.types.get(key);
191
- if (!type) {
317
+ if (type === void 0) {
192
318
  type = getPropertyType(key, this.source);
193
319
  this.types.set(key, type);
194
320
  }
@@ -221,6 +347,9 @@ const _ObjectAdministration = class _ObjectAdministration extends Administration
221
347
  }
222
348
  read(key) {
223
349
  const type = this.getType(key);
350
+ if (type === null) {
351
+ return this.get(key);
352
+ }
224
353
  switch (type) {
225
354
  case "observable":
226
355
  case "action": {
@@ -245,8 +374,12 @@ const _ObjectAdministration = class _ObjectAdministration extends Administration
245
374
  }
246
375
  write(key, newValue) {
247
376
  const type = this.getType(key);
377
+ if (type === null) {
378
+ this.set(key, newValue);
379
+ return;
380
+ }
248
381
  if (type === "computed") {
249
- runInBatch(() => this.set(key, newValue));
382
+ batch(() => this.set(key, newValue));
250
383
  return;
251
384
  }
252
385
  const had = key in this.source;
@@ -257,7 +390,7 @@ const _ObjectAdministration = class _ObjectAdministration extends Administration
257
390
  }
258
391
  if (!had || (isObservable(oldValue) ? oldValue !== newValue : oldValue !== targetValue)) {
259
392
  this.set(key, targetValue);
260
- runInBatch(() => {
393
+ batch(() => {
261
394
  this.flushChange();
262
395
  if (!had) {
263
396
  this.keysAtom.reportChanged();
@@ -277,7 +410,7 @@ const _ObjectAdministration = class _ObjectAdministration extends Administration
277
410
  remove(key) {
278
411
  if (!(key in this.source)) return;
279
412
  delete this.source[key];
280
- runInBatch(() => {
413
+ batch(() => {
281
414
  this.flushChange();
282
415
  this.valuesMap.reportChanged(key, void 0);
283
416
  this.keysAtom.reportChanged();
@@ -285,66 +418,29 @@ const _ObjectAdministration = class _ObjectAdministration extends Administration
285
418
  this.valuesMap.delete(key);
286
419
  });
287
420
  }
288
- };
289
- _ObjectAdministration.proxyTraps = {
290
- has(target, name) {
291
- const adm = getAdministration(target);
292
- if (!(name in Object.prototype) && isPropertyKey(name))
293
- return adm.has(name);
294
- return Reflect.has(adm.source, name);
295
- },
296
- get(target, name) {
297
- const adm = getAdministration(target);
298
- if (!(name in Object.prototype) && isPropertyKey(name) && (typeof adm.source !== "function" || name !== "prototype")) {
299
- return adm.read(name);
300
- }
301
- return Reflect.get(adm.source, name, adm.proxy);
302
- },
303
- set(target, name, value) {
304
- if (!isPropertyKey(name)) return false;
305
- const adm = getAdministration(target);
306
- adm.write(name, value);
307
- return true;
308
- },
309
- deleteProperty(target, name) {
310
- if (!isPropertyKey(name)) return false;
311
- const adm = getAdministration(target);
312
- adm.remove(name);
313
- return true;
314
- },
315
- ownKeys(target) {
316
- const adm = getAdministration(target);
317
- runInBatch(() => {
318
- adm.keysAtom.reportObserved();
319
- adm.atom.reportObserved();
320
- });
321
- return Reflect.ownKeys(adm.source);
322
- }
323
- };
324
- let ObjectAdministration = _ObjectAdministration;
325
- const _PreactObjectAdministration = class _PreactObjectAdministration extends ObjectAdministration {
326
- };
327
- _PreactObjectAdministration.proxyTraps = Object.assign(
328
- {},
329
- ObjectAdministration.proxyTraps,
330
- {
331
- get(target, prop, proxy) {
332
- if (!(prop in target) && (typeof prop === "string" || typeof prop === "number") && String(prop)[0] === "$") {
333
- return getSignal(proxy, prop.substring(1));
421
+ }
422
+ class PreactObjectAdministration extends ObjectAdministration {
423
+ static proxyTraps = Object.assign(
424
+ {},
425
+ ObjectAdministration.proxyTraps,
426
+ {
427
+ get(target, prop, proxy) {
428
+ if (!(prop in target) && (typeof prop === "string" || typeof prop === "number") && String(prop)[0] === "$") {
429
+ return getSignal(proxy, prop.substring(1));
430
+ }
431
+ return ObjectAdministration.proxyTraps.get?.apply(
432
+ null,
433
+ arguments
434
+ );
334
435
  }
335
- return ObjectAdministration.proxyTraps.get?.apply(
336
- null,
337
- arguments
338
- );
339
436
  }
340
- }
341
- );
342
- let PreactObjectAdministration = _PreactObjectAdministration;
437
+ );
438
+ }
343
439
  function createObservedAtom() {
344
440
  let value = 0;
345
441
  const callbacks = /* @__PURE__ */ new Set();
346
442
  let observing = false;
347
- const signal2 = new Signal(value, {
443
+ const signal22 = new Signal(value, {
348
444
  watched() {
349
445
  callbacks.forEach((callback) => callback(true));
350
446
  observing = true;
@@ -355,12 +451,12 @@ function createObservedAtom() {
355
451
  }
356
452
  });
357
453
  return {
358
- node: signal2,
454
+ node: signal22,
359
455
  reportObserved() {
360
- return signal2.value;
456
+ return signal22.value;
361
457
  },
362
458
  reportChanged() {
363
- return signal2.value = ++value;
459
+ return signal22.value = ++value;
364
460
  },
365
461
  get observing() {
366
462
  return observing;
@@ -404,13 +500,7 @@ function createAtom() {
404
500
  }
405
501
  };
406
502
  }
407
- function createEffect(fn) {
408
- return effect(fn);
409
- }
410
- function runInUntracked(fn) {
411
- return untracked(() => fn());
412
- }
413
- function createReaction(fn, callback) {
503
+ function reaction(fn, callback) {
414
504
  let initialized = false;
415
505
  let currentValue;
416
506
  return effect(() => {
@@ -469,7 +559,7 @@ function createListener(callback) {
469
559
  return listener;
470
560
  }
471
561
  function createComputed(fn, context = null) {
472
- const c = computed(context ? fn.bind(context) : fn);
562
+ const c = computed$1(context ? fn.bind(context) : fn);
473
563
  return {
474
564
  node: c,
475
565
  get() {
@@ -482,15 +572,23 @@ function createComputed(fn, context = null) {
482
572
  }
483
573
  };
484
574
  }
485
- function observable(obj) {
486
- return getObservable(obj);
575
+ function observable(value, context) {
576
+ if (context && typeof context === "object" && "kind" in context) {
577
+ context.metadata[context.name] = { type: "observable" };
578
+ return value;
579
+ }
580
+ return getObservable(value);
581
+ }
582
+ function computed(value, context) {
583
+ if (context && typeof context === "object" && "kind" in context) {
584
+ context.metadata[context.name] = { type: "computed" };
585
+ return value;
586
+ }
587
+ return computed$1(value);
487
588
  }
488
589
  function source(obj) {
489
590
  return getSource(obj);
490
591
  }
491
- function runInBatch(fn) {
492
- return batch(() => fn());
493
- }
494
592
  class Observable {
495
593
  constructor() {
496
594
  return getObservableClassInstance(this);
@@ -510,10 +608,10 @@ const signalMap = /* @__PURE__ */ new WeakMap();
510
608
  function getSignal(obj, key) {
511
609
  const node = getInternalNode(obj, key);
512
610
  if (node instanceof Signal) {
513
- let signal2 = signalMap.get(node);
514
- if (!signal2) {
515
- signal2 = new Signal();
516
- Object.defineProperties(signal2, {
611
+ let signal22 = signalMap.get(node);
612
+ if (!signal22) {
613
+ signal22 = new Signal();
614
+ Object.defineProperties(signal22, {
517
615
  value: {
518
616
  get() {
519
617
  return obj[key];
@@ -528,17 +626,46 @@ function getSignal(obj, key) {
528
626
  }
529
627
  }
530
628
  });
531
- signalMap.set(node, signal2);
629
+ signalMap.set(node, signal22);
532
630
  }
533
- return signal2;
631
+ return signal22;
534
632
  }
535
633
  return node;
536
634
  }
537
- var _a, _b;
538
- const _CollectionAdministration = class _CollectionAdministration extends Administration {
635
+ class CollectionAdministration extends Administration {
636
+ isMap;
637
+ hasMap;
638
+ valuesMap;
639
+ keysAtom;
640
+ static proxyTraps = {
641
+ get(target, name) {
642
+ const adm = getAdministration(target);
643
+ if (name === "size" && "size" in adm.source) {
644
+ return adm.size;
645
+ }
646
+ const val = adm.source[name];
647
+ const collectionMethods = adm.constructor.methods;
648
+ if (collectionMethods.hasOwnProperty(name) && typeof val === "function") {
649
+ return collectionMethods[name];
650
+ }
651
+ return val;
652
+ }
653
+ };
654
+ static methods = {
655
+ clear: createMethod$1("clear"),
656
+ forEach: createMethod$1("forEach"),
657
+ has: createMethod$1("has"),
658
+ add: createMethod$1("add"),
659
+ set: createMethod$1("set"),
660
+ get: createMethod$1("get"),
661
+ delete: createMethod$1("delete"),
662
+ entries: createMethod$1("entries"),
663
+ keys: createMethod$1("keys"),
664
+ values: createMethod$1("values"),
665
+ [Symbol.iterator]: createMethod$1(Symbol.iterator)
666
+ };
539
667
  constructor(source2) {
540
668
  super(source2);
541
- this[_a] = "Set";
542
669
  this.hasMap = new AtomMap(this.atom);
543
670
  this.valuesMap = new SignalMap();
544
671
  this.keysAtom = createAtom();
@@ -548,7 +675,7 @@ const _CollectionAdministration = class _CollectionAdministration extends Admini
548
675
  return this.source.has(getSource(key)) || this.source.has(key);
549
676
  }
550
677
  onCollectionChange(key) {
551
- runInBatch(() => {
678
+ batch(() => {
552
679
  this.keysAtom.reportChanged();
553
680
  this.hasMap.reportChanged(key);
554
681
  this.flushChange();
@@ -573,7 +700,7 @@ const _CollectionAdministration = class _CollectionAdministration extends Admini
573
700
  );
574
701
  }
575
702
  clear() {
576
- runInBatch(() => {
703
+ batch(() => {
577
704
  this.source.forEach((_, key) => this.delete(key));
578
705
  });
579
706
  }
@@ -674,7 +801,7 @@ const _CollectionAdministration = class _CollectionAdministration extends Admini
674
801
  const hasKey = this.hasEntry(key);
675
802
  const oldValue = sourceMap.get(targetKey) ?? sourceMap.get(key);
676
803
  if (!hasKey || isObservable(oldValue) ? oldValue !== value : oldValue !== targetValue) {
677
- runInBatch(() => {
804
+ batch(() => {
678
805
  this.flushChange();
679
806
  if (sourceMap.has(key)) {
680
807
  sourceMap.set(key, targetValue);
@@ -709,45 +836,126 @@ const _CollectionAdministration = class _CollectionAdministration extends Admini
709
836
  }
710
837
  };
711
838
  }
712
- [(_b = Symbol.iterator, _a = Symbol.toStringTag, _b)]() {
839
+ [Symbol.iterator]() {
713
840
  return this.isMap ? this.entries() : this.values();
714
841
  }
715
- };
716
- _CollectionAdministration.proxyTraps = {
717
- get(target, name) {
718
- const adm = getAdministration(target);
719
- if (name === "size" && "size" in adm.source) {
720
- return adm.size;
721
- }
722
- const val = adm.source[name];
723
- const collectionMethods = adm.constructor.methods;
724
- if (collectionMethods.hasOwnProperty(name) && typeof val === "function") {
725
- return collectionMethods[name];
726
- }
727
- return val;
728
- }
729
- };
730
- _CollectionAdministration.methods = {
731
- clear: createMethod$1("clear"),
732
- forEach: createMethod$1("forEach"),
733
- has: createMethod$1("has"),
734
- add: createMethod$1("add"),
735
- set: createMethod$1("set"),
736
- get: createMethod$1("get"),
737
- delete: createMethod$1("delete"),
738
- entries: createMethod$1("entries"),
739
- keys: createMethod$1("keys"),
740
- values: createMethod$1("values"),
741
- [Symbol.iterator]: createMethod$1(Symbol.iterator)
742
- };
743
- let CollectionAdministration = _CollectionAdministration;
842
+ [Symbol.toStringTag] = "Set";
843
+ }
744
844
  function createMethod$1(method) {
745
845
  return function() {
746
846
  const adm = getAdministration(this);
747
847
  return adm[method].apply(adm, arguments);
748
848
  };
749
849
  }
750
- const _ArrayAdministration = class _ArrayAdministration extends Administration {
850
+ class ArrayAdministration extends Administration {
851
+ valuesMap;
852
+ keysAtom;
853
+ static proxyTraps = {
854
+ get(target, name) {
855
+ const adm = getAdministration(target);
856
+ if (name === "length") {
857
+ return adm.getArrayLength();
858
+ }
859
+ if (typeof name === "number") {
860
+ return adm.get(name);
861
+ }
862
+ if (typeof name === "string" && String(parseInt(name)) === name) {
863
+ return adm.get(parseInt(name));
864
+ }
865
+ const arrayMethods = adm.constructor.methods;
866
+ if (arrayMethods.hasOwnProperty(name)) {
867
+ return arrayMethods[name];
868
+ }
869
+ return adm.source[name];
870
+ },
871
+ set(target, name, value) {
872
+ const adm = getAdministration(target);
873
+ if (name === "length") {
874
+ adm.setArrayLength(value);
875
+ } else if (typeof name === "number") {
876
+ adm.set(name, value);
877
+ } else if (typeof name === "string" && String(parseInt(name)) === name) {
878
+ adm.set(parseInt(name), value);
879
+ } else {
880
+ adm.source[name] = value;
881
+ }
882
+ return true;
883
+ }
884
+ };
885
+ static methods = {
886
+ fill(value, start, end) {
887
+ const adm = getAdministration(this);
888
+ const oldLength = adm.source.length;
889
+ adm.source.fill(value, start, end);
890
+ adm.onArrayChanged(oldLength !== adm.source.length, start, end);
891
+ return this;
892
+ },
893
+ splice(index, deleteCount, ...newItems) {
894
+ const adm = getAdministration(this);
895
+ switch (arguments.length) {
896
+ case 0:
897
+ return [];
898
+ case 1:
899
+ return adm.spliceWithArray(index);
900
+ case 2:
901
+ return adm.spliceWithArray(index, deleteCount);
902
+ }
903
+ return adm.spliceWithArray(index, deleteCount, newItems);
904
+ },
905
+ push(...items) {
906
+ const adm = getAdministration(this);
907
+ adm.spliceWithArray(adm.source.length, 0, items);
908
+ return adm.source.length;
909
+ },
910
+ pop() {
911
+ return this.splice(
912
+ Math.max(getAdministration(this).source.length - 1, 0),
913
+ 1
914
+ )[0];
915
+ },
916
+ shift() {
917
+ return this.splice(0, 1)[0];
918
+ },
919
+ unshift(...items) {
920
+ const adm = getAdministration(this);
921
+ adm.spliceWithArray(0, 0, items);
922
+ return adm.source.length;
923
+ },
924
+ reverse() {
925
+ const adm = getAdministration(this);
926
+ adm.source.reverse();
927
+ adm.onArrayChanged(false, 0, adm.source.length);
928
+ return this;
929
+ },
930
+ sort(compareFn) {
931
+ const adm = getAdministration(this);
932
+ adm.onArrayChanged();
933
+ adm.source.sort(
934
+ compareFn && ((a, b) => compareFn(getObservable(a), getObservable(b)))
935
+ );
936
+ return this;
937
+ },
938
+ join: createStringMethod("join"),
939
+ toString: createStringMethod("toString"),
940
+ toLocaleString: createStringMethod("toLocaleString"),
941
+ indexOf: createSearchMethod("indexOf"),
942
+ lastIndexOf: createSearchMethod("lastIndexOf"),
943
+ includes: createSearchMethod("includes"),
944
+ slice: createCopyMethod("slice"),
945
+ concat: createCopyMethod("concat"),
946
+ flat: createCopyMethod("flat"),
947
+ copyWithin: createCopyMethod("copyWithin"),
948
+ every: createMapMethod("every"),
949
+ forEach: createMapMethod("forEach"),
950
+ map: createMapMethod("map"),
951
+ flatMap: createMapMethod("flatMap"),
952
+ findIndex: createMapMethod("findIndex"),
953
+ some: createMapMethod("some"),
954
+ filter: createFilterMethod("filter"),
955
+ find: createFilterMethod("find"),
956
+ reduce: createReduceMethod("reduce"),
957
+ reduceRight: createReduceMethod("reduceRight")
958
+ };
751
959
  constructor(source2 = []) {
752
960
  super(source2);
753
961
  this.valuesMap = new SignalMap();
@@ -838,7 +1046,7 @@ const _ArrayAdministration = class _ArrayAdministration extends Administration {
838
1046
  );
839
1047
  }
840
1048
  onArrayChanged(lengthChanged = false, index, count) {
841
- runInBatch(() => {
1049
+ batch(() => {
842
1050
  if (lengthChanged) {
843
1051
  this.keysAtom.reportChanged();
844
1052
  }
@@ -852,114 +1060,7 @@ const _ArrayAdministration = class _ArrayAdministration extends Administration {
852
1060
  this.flushChange();
853
1061
  });
854
1062
  }
855
- };
856
- _ArrayAdministration.proxyTraps = {
857
- get(target, name) {
858
- const adm = getAdministration(target);
859
- if (name === "length") {
860
- return adm.getArrayLength();
861
- }
862
- if (typeof name === "number") {
863
- return adm.get(name);
864
- }
865
- if (typeof name === "string" && String(parseInt(name)) === name) {
866
- return adm.get(parseInt(name));
867
- }
868
- const arrayMethods = adm.constructor.methods;
869
- if (arrayMethods.hasOwnProperty(name)) {
870
- return arrayMethods[name];
871
- }
872
- return adm.source[name];
873
- },
874
- set(target, name, value) {
875
- const adm = getAdministration(target);
876
- if (name === "length") {
877
- adm.setArrayLength(value);
878
- } else if (typeof name === "number") {
879
- adm.set(name, value);
880
- } else if (typeof name === "string" && String(parseInt(name)) === name) {
881
- adm.set(parseInt(name), value);
882
- } else {
883
- adm.source[name] = value;
884
- }
885
- return true;
886
- }
887
- };
888
- _ArrayAdministration.methods = {
889
- fill(value, start, end) {
890
- const adm = getAdministration(this);
891
- const oldLength = adm.source.length;
892
- adm.source.fill(value, start, end);
893
- adm.onArrayChanged(oldLength !== adm.source.length, start, end);
894
- return this;
895
- },
896
- splice(index, deleteCount, ...newItems) {
897
- const adm = getAdministration(this);
898
- switch (arguments.length) {
899
- case 0:
900
- return [];
901
- case 1:
902
- return adm.spliceWithArray(index);
903
- case 2:
904
- return adm.spliceWithArray(index, deleteCount);
905
- }
906
- return adm.spliceWithArray(index, deleteCount, newItems);
907
- },
908
- push(...items) {
909
- const adm = getAdministration(this);
910
- adm.spliceWithArray(adm.source.length, 0, items);
911
- return adm.source.length;
912
- },
913
- pop() {
914
- return this.splice(
915
- Math.max(getAdministration(this).source.length - 1, 0),
916
- 1
917
- )[0];
918
- },
919
- shift() {
920
- return this.splice(0, 1)[0];
921
- },
922
- unshift(...items) {
923
- const adm = getAdministration(this);
924
- adm.spliceWithArray(0, 0, items);
925
- return adm.source.length;
926
- },
927
- reverse() {
928
- const adm = getAdministration(this);
929
- adm.source.reverse();
930
- adm.onArrayChanged(false, 0, adm.source.length);
931
- return this;
932
- },
933
- sort(compareFn) {
934
- const adm = getAdministration(this);
935
- adm.onArrayChanged();
936
- adm.source.sort(
937
- compareFn && ((a, b) => compareFn(getObservable(a), getObservable(b)))
938
- );
939
- return this;
940
- },
941
- join: createStringMethod("join"),
942
- toString: createStringMethod("toString"),
943
- toLocaleString: createStringMethod("toLocaleString"),
944
- indexOf: createSearchMethod("indexOf"),
945
- lastIndexOf: createSearchMethod("lastIndexOf"),
946
- includes: createSearchMethod("includes"),
947
- slice: createCopyMethod("slice"),
948
- concat: createCopyMethod("concat"),
949
- flat: createCopyMethod("flat"),
950
- copyWithin: createCopyMethod("copyWithin"),
951
- every: createMapMethod("every"),
952
- forEach: createMapMethod("forEach"),
953
- map: createMapMethod("map"),
954
- flatMap: createMapMethod("flatMap"),
955
- findIndex: createMapMethod("findIndex"),
956
- some: createMapMethod("some"),
957
- filter: createFilterMethod("filter"),
958
- find: createFilterMethod("find"),
959
- reduce: createReduceMethod("reduce"),
960
- reduceRight: createReduceMethod("reduceRight")
961
- };
962
- let ArrayAdministration = _ArrayAdministration;
1063
+ }
963
1064
  function createMethod(method, func) {
964
1065
  if (Array.prototype.hasOwnProperty(method)) {
965
1066
  return func;
@@ -1046,23 +1147,22 @@ function createReduceMethod(method) {
1046
1147
  return adm.source[method].apply(adm.source, arguments);
1047
1148
  });
1048
1149
  }
1049
- const _DateAdministration = class _DateAdministration extends Administration {
1050
- };
1051
- _DateAdministration.proxyTraps = {
1052
- get(target, name) {
1053
- const adm = getAdministration(target);
1054
- if (typeof adm.source[name] === "function") {
1055
- if (typeof name === "string" && name.startsWith("set")) {
1056
- addDateSetMethod(name);
1057
- } else {
1058
- addDateGetMethod(name);
1150
+ class DateAdministration extends Administration {
1151
+ static proxyTraps = {
1152
+ get(target, name) {
1153
+ const adm = getAdministration(target);
1154
+ if (typeof adm.source[name] === "function") {
1155
+ if (typeof name === "string" && name.startsWith("set")) {
1156
+ addDateSetMethod(name);
1157
+ } else {
1158
+ addDateGetMethod(name);
1159
+ }
1160
+ return dateMethods[name];
1059
1161
  }
1060
- return dateMethods[name];
1162
+ return adm.source[name];
1061
1163
  }
1062
- return adm.source[name];
1063
- }
1064
- };
1065
- let DateAdministration = _DateAdministration;
1164
+ };
1165
+ }
1066
1166
  const dateMethods = /* @__PURE__ */ Object.create(null);
1067
1167
  function addDateSetMethod(method) {
1068
1168
  if (!dateMethods[method])
@@ -1157,60 +1257,6 @@ function getInternalNode(obj, key) {
1157
1257
  }
1158
1258
  return adm.getNode(key);
1159
1259
  }
1160
- var CommonCfgTypes = /* @__PURE__ */ ((CommonCfgTypes2) => {
1161
- CommonCfgTypes2["child"] = "child";
1162
- CommonCfgTypes2["children"] = "children";
1163
- return CommonCfgTypes2;
1164
- })(CommonCfgTypes || {});
1165
- var ModelCfgTypes = /* @__PURE__ */ ((ModelCfgTypes2) => {
1166
- ModelCfgTypes2["state"] = "state";
1167
- ModelCfgTypes2["id"] = "id";
1168
- ModelCfgTypes2["modelRef"] = "modelRef";
1169
- ModelCfgTypes2["modelRefs"] = "modelRefs";
1170
- return ModelCfgTypes2;
1171
- })(ModelCfgTypes || {});
1172
- var StoreCfgTypes = /* @__PURE__ */ ((StoreCfgTypes2) => {
1173
- StoreCfgTypes2["model"] = "model";
1174
- return StoreCfgTypes2;
1175
- })(StoreCfgTypes || {});
1176
- const childType = Object.assign(
1177
- function(childType2) {
1178
- return { type: "child", childType: childType2 };
1179
- },
1180
- {
1181
- type: "child"
1182
- /* child */
1183
- }
1184
- );
1185
- const childrenType = Object.assign(
1186
- function(childType2) {
1187
- return { type: "children", childType: childType2 };
1188
- },
1189
- {
1190
- type: "children"
1191
- /* children */
1192
- }
1193
- );
1194
- const stateType = {
1195
- type: "state"
1196
- /* state */
1197
- };
1198
- const modelRefType = {
1199
- type: "modelRef"
1200
- /* modelRef */
1201
- };
1202
- const modelRefsType = {
1203
- type: "modelRefs"
1204
- /* modelRefs */
1205
- };
1206
- const idType = {
1207
- type: "id"
1208
- /* id */
1209
- };
1210
- const modelType = {
1211
- type: "model"
1212
- /* model */
1213
- };
1214
1260
  function getPropertyDescriptor(obj, key) {
1215
1261
  let node = obj;
1216
1262
  while (node) {
@@ -1249,17 +1295,20 @@ function getDiff(o1, o2, getConfig) {
1249
1295
  const key = keys[i];
1250
1296
  if (obj1[key] !== obj2[key]) {
1251
1297
  if (config?.[key]?.type === CommonCfgTypes.child) {
1252
- const childDiff = getDiff(obj1[key], obj2[key], getConfig);
1253
- if (childDiff) {
1254
- diff[key] = childDiff;
1255
- }
1256
- } else if (config?.[key]?.type === CommonCfgTypes.children) {
1257
- diff[key] = obj2[key].map((model2, index) => {
1258
- if (obj1[key][index]) {
1259
- return getDiff(obj1[key][index], model2, getConfig);
1298
+ const value = obj2[key];
1299
+ if (Array.isArray(value)) {
1300
+ diff[key] = value.map((model2, index) => {
1301
+ if (obj1[key][index]) {
1302
+ return getDiff(obj1[key][index], model2, getConfig);
1303
+ }
1304
+ return model2;
1305
+ });
1306
+ } else {
1307
+ const childDiff = getDiff(obj1[key], obj2[key], getConfig);
1308
+ if (childDiff) {
1309
+ diff[key] = childDiff;
1260
1310
  }
1261
- return model2;
1262
- });
1311
+ }
1263
1312
  } else {
1264
1313
  diff[key] = obj2[key];
1265
1314
  }
@@ -1267,25 +1316,9 @@ function getDiff(o1, o2, getConfig) {
1267
1316
  }
1268
1317
  return Object.keys(diff).length > 0 ? diff : null;
1269
1318
  }
1270
- function computedProxy(computed2) {
1271
- const initial = {};
1272
- Object.keys(computed2.get()).forEach((k) => initial[k] = void 0);
1273
- const proxy = new Proxy(initial, {
1274
- get(target, key) {
1275
- if (!target[key]) {
1276
- target[key] = createComputed(
1277
- () => computed2.get()[key],
1278
- null
1279
- );
1280
- }
1281
- return target[key].get();
1282
- }
1283
- });
1284
- return proxy;
1285
- }
1286
1319
  function updateProps(props, newProps) {
1287
- runInUntracked(() => {
1288
- runInBatch(() => {
1320
+ untracked(() => {
1321
+ batch(() => {
1289
1322
  const propKeys = Object.keys(newProps);
1290
1323
  propKeys.forEach((k) => {
1291
1324
  if (k !== "models") {
@@ -1302,26 +1335,63 @@ function updateProps(props, newProps) {
1302
1335
  function getStoreAdm(store) {
1303
1336
  return getAdministration(store);
1304
1337
  }
1305
- const _StoreAdministration = class _StoreAdministration extends PreactObjectAdministration {
1306
- constructor() {
1307
- super(...arguments);
1308
- this.parent = null;
1309
- this.mounted = false;
1310
- this.contextReactionUnsub = null;
1311
- this.childStoreDataMap = /* @__PURE__ */ new Map();
1312
- this.reactionsUnsub = [];
1313
- }
1314
- setConfiguration(configuration) {
1315
- this.configuration = configuration;
1338
+ class StoreAdministration extends PreactObjectAdministration {
1339
+ static proxyTraps = Object.assign(
1340
+ {},
1341
+ PreactObjectAdministration.proxyTraps,
1342
+ {
1343
+ get(target, name) {
1344
+ if (name === "key") {
1345
+ return target.key;
1346
+ }
1347
+ const adm = getAdministration(target);
1348
+ switch (adm.configuration[name]?.type) {
1349
+ case CommonCfgTypes.child:
1350
+ return adm.getStore(name);
1351
+ case StoreCfgTypes.model:
1352
+ return adm.getModelRef(name);
1353
+ default:
1354
+ return PreactObjectAdministration.proxyTraps.get?.apply(
1355
+ null,
1356
+ arguments
1357
+ );
1358
+ }
1359
+ },
1360
+ set(target, name, value) {
1361
+ const adm = getAdministration(target);
1362
+ if (name === "props") {
1363
+ throw new Error(`r-state-tree: ${name} is read-only`);
1364
+ }
1365
+ if (adm.configuration[name]?.type === StoreCfgTypes.model) {
1366
+ if (value !== void 0) {
1367
+ throw new Error(`r-state-tree: model ${String(name)} is read-only`);
1368
+ }
1369
+ }
1370
+ return PreactObjectAdministration.proxyTraps.set?.apply(
1371
+ null,
1372
+ arguments
1373
+ );
1374
+ }
1375
+ }
1376
+ );
1377
+ parent = null;
1378
+ mounted = false;
1379
+ contextCache = /* @__PURE__ */ new Map();
1380
+ childStoreDataMap = /* @__PURE__ */ new Map();
1381
+ reactionsUnsub = [];
1382
+ configurationGetter;
1383
+ setConfiguration(configurationGetter) {
1384
+ this.configurationGetter = configurationGetter;
1385
+ }
1386
+ get configuration() {
1387
+ return this.configurationGetter?.() ?? {};
1316
1388
  }
1317
1389
  createChildStore(element) {
1318
1390
  return allowNewStore(() => new element.Type(element.props));
1319
1391
  }
1320
1392
  setStoreList(name, elements) {
1321
1393
  const childStoreData = this.childStoreDataMap.get(name);
1322
- const oldStores = runInUntracked(
1323
- () => childStoreData.value.get()
1324
- );
1394
+ const oldStores = untracked(() => childStoreData.value.get());
1325
1395
  const stores = [];
1326
1396
  let keyedIndexChanged = false;
1327
1397
  if (!oldStores) {
@@ -1380,7 +1450,7 @@ const _StoreAdministration = class _StoreAdministration extends PreactObjectAdmi
1380
1450
  }
1381
1451
  });
1382
1452
  if (newStores.size || removedStores.size || keyedIndexChanged) {
1383
- runInBatch(() => childStoreData.value.set(stores));
1453
+ batch(() => childStoreData.value.set(stores));
1384
1454
  }
1385
1455
  removedStores.forEach((s) => getStoreAdm(s).unmount());
1386
1456
  newStores.forEach((s) => getStoreAdm(s).mount(this));
@@ -1388,24 +1458,24 @@ const _StoreAdministration = class _StoreAdministration extends PreactObjectAdmi
1388
1458
  }
1389
1459
  setSingleStore(name, element) {
1390
1460
  const childStoreData = this.childStoreDataMap.get(name);
1391
- const oldStore = runInUntracked(
1461
+ const oldStore = untracked(
1392
1462
  () => childStoreData.value.get()
1393
1463
  );
1394
1464
  const { key, Type, props } = element || {};
1395
1465
  if (!element) {
1396
1466
  oldStore && getStoreAdm(oldStore).unmount();
1397
- runInBatch(() => childStoreData.value.set(null));
1467
+ batch(() => childStoreData.value.set(null));
1398
1468
  return null;
1399
1469
  } else if (!oldStore || oldStore.props.key !== key || !(oldStore instanceof Type)) {
1400
1470
  if (oldStore) {
1401
1471
  getStoreAdm(oldStore).unmount();
1402
1472
  }
1403
1473
  const childStore = this.createChildStore(element);
1404
- runInBatch(() => childStoreData.value.set(childStore));
1474
+ batch(() => childStoreData.value.set(childStore));
1405
1475
  getStoreAdm(childStore).mount(this);
1406
1476
  return childStore;
1407
1477
  } else {
1408
- runInBatch(() => updateProps(oldStore.props, props));
1478
+ batch(() => updateProps(oldStore.props, props));
1409
1479
  return oldStore;
1410
1480
  }
1411
1481
  }
@@ -1414,14 +1484,7 @@ const _StoreAdministration = class _StoreAdministration extends PreactObjectAdmi
1414
1484
  const storeElement = childStoreData.listener.track(
1415
1485
  () => childStoreData.computed.get()
1416
1486
  );
1417
- this.setSingleStore(name, storeElement);
1418
- }
1419
- updateStores(name) {
1420
- const childStoreData = this.childStoreDataMap.get(name);
1421
- const storeElement = childStoreData.listener.track(
1422
- () => childStoreData.computed.get()
1423
- );
1424
- this.setStoreList(name, storeElement);
1487
+ Array.isArray(storeElement) ? this.setStoreList(name, storeElement) : this.setSingleStore(name, storeElement);
1425
1488
  }
1426
1489
  getComputedGetter(name) {
1427
1490
  const descriptor = getPropertyDescriptor(this.source, name);
@@ -1441,21 +1504,7 @@ const _StoreAdministration = class _StoreAdministration extends PreactObjectAdmi
1441
1504
  const storeElement = childStoreData.listener.track(
1442
1505
  () => childStoreData.computed.get()
1443
1506
  );
1444
- this.setSingleStore(name, storeElement);
1445
- return childStoreData.value.get();
1446
- }
1447
- initializeStores(name) {
1448
- const value = createSignal([]);
1449
- const childStoreData = this.childStoreDataMap.get(name) ?? {
1450
- computed: this.getComputedGetter(name),
1451
- listener: createListener(() => this.updateStores(name)),
1452
- value
1453
- };
1454
- this.childStoreDataMap.set(name, childStoreData);
1455
- const storeElement = childStoreData.listener.track(
1456
- () => childStoreData.computed.get()
1457
- );
1458
- this.setStoreList(name, storeElement);
1507
+ Array.isArray(storeElement) ? this.setStoreList(name, storeElement) : this.setSingleStore(name, storeElement);
1459
1508
  return childStoreData.value.get();
1460
1509
  }
1461
1510
  getStore(name) {
@@ -1463,18 +1512,8 @@ const _StoreAdministration = class _StoreAdministration extends PreactObjectAdmi
1463
1512
  if (!childStoreData) {
1464
1513
  return this.initializeStore(name);
1465
1514
  } else {
1466
- const storeElement = runInUntracked(() => childStoreData.computed.get());
1467
- this.setSingleStore(name, storeElement);
1468
- return childStoreData.value.get();
1469
- }
1470
- }
1471
- getStores(name) {
1472
- const childStoreData = this.childStoreDataMap.get(name);
1473
- if (!childStoreData) {
1474
- return this.initializeStores(name);
1475
- } else {
1476
- const storeElement = runInUntracked(() => childStoreData.computed.get());
1477
- this.setStoreList(name, storeElement);
1515
+ const storeElement = untracked(() => childStoreData.computed.get());
1516
+ Array.isArray(storeElement) ? this.setStoreList(name, storeElement) : this.setSingleStore(name, storeElement);
1478
1517
  return childStoreData.value.get();
1479
1518
  }
1480
1519
  }
@@ -1484,22 +1523,46 @@ const _StoreAdministration = class _StoreAdministration extends PreactObjectAdmi
1484
1523
  isRoot() {
1485
1524
  return !this.parent;
1486
1525
  }
1487
- createReaction(track, callback) {
1488
- const unsub = createReaction(track, callback);
1526
+ getContextValue(contextId, provideSymbol, defaultValue, hasDefault) {
1527
+ let computed2 = this.contextCache.get(contextId);
1528
+ if (!computed2) {
1529
+ computed2 = createComputed(() => {
1530
+ return this.lookupContextValue(
1531
+ contextId,
1532
+ provideSymbol,
1533
+ defaultValue,
1534
+ hasDefault
1535
+ );
1536
+ });
1537
+ this.contextCache.set(contextId, computed2);
1538
+ }
1539
+ return computed2.get();
1540
+ }
1541
+ lookupContextValue(contextId, provideSymbol, defaultValue, hasDefault) {
1542
+ const provideMethod = this.source[provideSymbol];
1543
+ if (typeof provideMethod === "function") {
1544
+ return provideMethod.call(this.proxy);
1545
+ }
1546
+ if (this.parent) {
1547
+ return this.parent.getContextValue(
1548
+ contextId,
1549
+ provideSymbol,
1550
+ defaultValue,
1551
+ hasDefault
1552
+ );
1553
+ }
1554
+ if (hasDefault) {
1555
+ return defaultValue;
1556
+ }
1557
+ return void 0;
1558
+ }
1559
+ reaction(track, callback) {
1560
+ const unsub = reaction(track, callback);
1489
1561
  this.reactionsUnsub.push(unsub);
1490
1562
  return unsub;
1491
1563
  }
1492
1564
  mount(parent = null) {
1493
1565
  this.parent = parent || null;
1494
- if (this.parent) {
1495
- const parentSource = this.parent.proxy;
1496
- this.computedContext = computedProxy(
1497
- createComputed(() => ({
1498
- ...parentSource.context,
1499
- ...parentSource.provideContext()
1500
- }))
1501
- );
1502
- }
1503
1566
  this.childStoreDataMap.forEach(({ value }) => {
1504
1567
  const stores = value.get();
1505
1568
  if (Array.isArray(stores)) {
@@ -1509,7 +1572,7 @@ const _StoreAdministration = class _StoreAdministration extends PreactObjectAdmi
1509
1572
  }
1510
1573
  });
1511
1574
  this.mounted = true;
1512
- runInBatch(() => this.proxy.storeDidMount?.());
1575
+ batch(() => this.proxy.storeDidMount?.());
1513
1576
  }
1514
1577
  unmount() {
1515
1578
  this.proxy.storeWillUnmount?.();
@@ -1526,52 +1589,12 @@ const _StoreAdministration = class _StoreAdministration extends PreactObjectAdmi
1526
1589
  listener.dispose();
1527
1590
  });
1528
1591
  this.childStoreDataMap.clear();
1529
- this.contextReactionUnsub?.();
1592
+ this.contextCache.forEach((computed2) => computed2.clear());
1593
+ this.contextCache.clear();
1530
1594
  this.reactionsUnsub.forEach((u) => u());
1531
1595
  this.parent = null;
1532
1596
  }
1533
- };
1534
- _StoreAdministration.proxyTraps = Object.assign(
1535
- {},
1536
- PreactObjectAdministration.proxyTraps,
1537
- {
1538
- get(target, name) {
1539
- if (name === "key") {
1540
- return target.key;
1541
- } else if (name === "context") {
1542
- return target.context;
1543
- }
1544
- const adm = getAdministration(target);
1545
- switch (adm.configuration[name]?.type) {
1546
- case CommonCfgTypes.child:
1547
- return adm.getStore(name);
1548
- case CommonCfgTypes.children:
1549
- return adm.getStores(name);
1550
- case StoreCfgTypes.model:
1551
- return adm.getModelRef(name);
1552
- default:
1553
- return PreactObjectAdministration.proxyTraps.get?.apply(
1554
- null,
1555
- arguments
1556
- );
1557
- }
1558
- },
1559
- set(target, name, value) {
1560
- const adm = getAdministration(target);
1561
- if (name === "props" || name === "context") {
1562
- throw new Error(`r-state-tree: ${name} is read-only`);
1563
- }
1564
- if (adm.configuration[name]?.type === StoreCfgTypes.model) {
1565
- throw new Error(`r-state-tree: model ${String(name)} is read-only`);
1566
- }
1567
- return PreactObjectAdministration.proxyTraps.set?.apply(
1568
- null,
1569
- arguments
1570
- );
1571
- }
1572
- }
1573
- );
1574
- let StoreAdministration = _StoreAdministration;
1597
+ }
1575
1598
  let initEnabled$1 = false;
1576
1599
  function allowNewStore(fn) {
1577
1600
  initEnabled$1 = true;
@@ -1592,18 +1615,23 @@ function updateStore(store, props) {
1592
1615
  updateProps(store.props, props);
1593
1616
  return store;
1594
1617
  }
1595
- const _Store = class _Store {
1618
+ class Store {
1619
+ static get types() {
1620
+ return this[Symbol.metadata];
1621
+ }
1622
+ props;
1596
1623
  constructor(props) {
1597
1624
  if (!initEnabled$1) {
1598
1625
  throw new Error("r-state-tree: Can't initialize store directly");
1599
1626
  }
1600
- const config = this.constructor.types;
1601
1627
  const observable2 = createObservableWithCustomAdministration(
1602
1628
  this,
1603
1629
  StoreAdministration
1604
1630
  );
1605
1631
  const adm = getStoreAdm(observable2);
1606
- adm.setConfiguration(config ?? {});
1632
+ adm.setConfiguration(
1633
+ () => this.constructor.types ?? {}
1634
+ );
1607
1635
  adm.write("props", getObservable({}));
1608
1636
  updateProps(observable2.props, props);
1609
1637
  return observable2;
@@ -1611,14 +1639,8 @@ const _Store = class _Store {
1611
1639
  get key() {
1612
1640
  return this.props.key;
1613
1641
  }
1614
- get context() {
1615
- return getStoreAdm(this).computedContext;
1616
- }
1617
- createReaction(track, callback) {
1618
- return getStoreAdm(this).createReaction(track, callback);
1619
- }
1620
- provideContext() {
1621
- return null;
1642
+ reaction(track, callback) {
1643
+ return getStoreAdm(this).reaction(track, callback);
1622
1644
  }
1623
1645
  // eslint-disable-next-line @typescript-eslint/no-empty-function
1624
1646
  storeDidMount() {
@@ -1626,9 +1648,7 @@ const _Store = class _Store {
1626
1648
  // eslint-disable-next-line @typescript-eslint/no-empty-function
1627
1649
  storeWillUnmount() {
1628
1650
  }
1629
- };
1630
- _Store.types = {};
1631
- let Store = _Store;
1651
+ }
1632
1652
  const attachedIdMap = /* @__PURE__ */ new WeakMap();
1633
1653
  const idMap = /* @__PURE__ */ new WeakMap();
1634
1654
  let loadingSnapshot = false;
@@ -1784,9 +1804,8 @@ function observe(obj, method) {
1784
1804
  return listener.subscribe(method);
1785
1805
  }
1786
1806
  class ObservableListener {
1787
- constructor() {
1788
- this.notifying = false;
1789
- }
1807
+ listeners;
1808
+ notifying = false;
1790
1809
  subscribe(l) {
1791
1810
  let unsubed = false;
1792
1811
  this.listeners = this.listeners || [];
@@ -1818,7 +1837,7 @@ class ObservableListener {
1818
1837
  }
1819
1838
  class ChildModelsAdministration extends ArrayAdministration {
1820
1839
  set(index, newValue) {
1821
- return runInBatch(() => {
1840
+ return batch(() => {
1822
1841
  super.set(index, newValue);
1823
1842
  const sourceValue = getSource(newValue);
1824
1843
  if (this.source[index] !== sourceValue) {
@@ -1827,7 +1846,7 @@ class ChildModelsAdministration extends ArrayAdministration {
1827
1846
  });
1828
1847
  }
1829
1848
  spliceWithArray(index, deleteCount, newItems) {
1830
- return runInBatch(() => {
1849
+ return batch(() => {
1831
1850
  const deleted = super.spliceWithArray(index, deleteCount, newItems);
1832
1851
  if (deleteCount || newItems?.length) {
1833
1852
  notifySpliceArray(this.proxy, index, newItems ?? [], deleted);
@@ -1871,19 +1890,108 @@ function getSnapshotRefId(snapshot) {
1871
1890
  }
1872
1891
  return snapshot[keys[0]];
1873
1892
  }
1874
- const _ModelAdministration = class _ModelAdministration extends PreactObjectAdministration {
1875
- constructor() {
1876
- super(...arguments);
1877
- this.parent = null;
1878
- this.activeModels = /* @__PURE__ */ new Set();
1879
- this.root = this;
1880
- this.modelsTraceUnsub = /* @__PURE__ */ new Map();
1881
- this.writeInProgress = /* @__PURE__ */ new Set();
1882
- this.snapshotMap = /* @__PURE__ */ new Map();
1883
- this.parentName = null;
1893
+ class ModelAdministration extends PreactObjectAdministration {
1894
+ static proxyTraps = Object.assign(
1895
+ {},
1896
+ PreactObjectAdministration.proxyTraps,
1897
+ {
1898
+ get(target, prop, proxy) {
1899
+ const adm = getAdministration(target);
1900
+ if (prop === "parent") {
1901
+ return target.parent;
1902
+ }
1903
+ switch (adm.configuration[prop]?.type) {
1904
+ case ModelCfgTypes.modelRef:
1905
+ if (Array.isArray(adm.source[prop])) {
1906
+ return adm.getModelRefs(prop);
1907
+ }
1908
+ return adm.getModelRef(prop);
1909
+ default:
1910
+ return PreactObjectAdministration.proxyTraps.get?.apply(
1911
+ null,
1912
+ arguments
1913
+ );
1914
+ }
1915
+ },
1916
+ set(target, name, value) {
1917
+ const adm = getAdministration(target);
1918
+ adm.writeInProgress.add(name);
1919
+ try {
1920
+ switch (adm.configuration[name]?.type) {
1921
+ case ModelCfgTypes.modelRef: {
1922
+ Array.isArray(value) ? adm.setModelRefs(name, value) : adm.setModelRef(name, value);
1923
+ return true;
1924
+ }
1925
+ case CommonCfgTypes.child: {
1926
+ if (Array.isArray(value)) {
1927
+ adm.setModels(name, value);
1928
+ return true;
1929
+ } else {
1930
+ adm.setModel(name, value ?? null);
1931
+ break;
1932
+ }
1933
+ }
1934
+ case ModelCfgTypes.id: {
1935
+ adm.setId(name, value);
1936
+ break;
1937
+ }
1938
+ case ModelCfgTypes.state: {
1939
+ adm.setState(name, value);
1940
+ break;
1941
+ }
1942
+ }
1943
+ return PreactObjectAdministration.proxyTraps.set?.apply(
1944
+ null,
1945
+ arguments
1946
+ );
1947
+ } finally {
1948
+ adm.writeInProgress.delete(name);
1949
+ }
1950
+ },
1951
+ defineProperty(target, name, desc) {
1952
+ const adm = getAdministration(target);
1953
+ if (desc && "value" in desc && !adm.writeInProgress.has(name)) {
1954
+ switch (adm.configuration[name]?.type) {
1955
+ case ModelCfgTypes.modelRef:
1956
+ case CommonCfgTypes.child:
1957
+ case ModelCfgTypes.id: {
1958
+ adm.proxy[name] = desc.value;
1959
+ return true;
1960
+ }
1961
+ }
1962
+ }
1963
+ return Reflect.defineProperty(target, name, desc);
1964
+ }
1965
+ }
1966
+ );
1967
+ configurationGetter;
1968
+ _parent = null;
1969
+ parentAtom = createAtom();
1970
+ referencedAtoms;
1971
+ referencedModels;
1972
+ activeModels = /* @__PURE__ */ new Set();
1973
+ root = this;
1974
+ modelsTraceUnsub = /* @__PURE__ */ new Map();
1975
+ writeInProgress = /* @__PURE__ */ new Set();
1976
+ computedSnapshot;
1977
+ snapshotMap = /* @__PURE__ */ new Map();
1978
+ contextCache = /* @__PURE__ */ new Map();
1979
+ parentName = null;
1980
+ get parent() {
1981
+ this.parentAtom.reportObserved();
1982
+ return this._parent;
1983
+ }
1984
+ set parent(value) {
1985
+ if (this._parent !== value) {
1986
+ this._parent = value;
1987
+ this.parentAtom.reportChanged();
1988
+ }
1884
1989
  }
1885
- setConfiguration(configuration) {
1886
- this.configuration = configuration;
1990
+ setConfiguration(configurationGetter) {
1991
+ this.configurationGetter = configurationGetter;
1992
+ }
1993
+ get configuration() {
1994
+ return this.configurationGetter?.() ?? {};
1887
1995
  }
1888
1996
  getReferencedAtom(name) {
1889
1997
  let a = this.referencedAtoms?.get(name);
@@ -1921,13 +2029,18 @@ const _ModelAdministration = class _ModelAdministration extends PreactObjectAdmi
1921
2029
  }
1922
2030
  }
1923
2031
  setModel(name, newModel) {
1924
- const oldModel = this.getModel(name);
1925
- if (oldModel === newModel) {
2032
+ const currentValue = this.proxy[name];
2033
+ if (currentValue === newModel) {
1926
2034
  return;
1927
2035
  }
1928
2036
  this.activeModels.add(name);
1929
- if (oldModel) {
1930
- getModelAdm(oldModel).detach();
2037
+ if (Array.isArray(currentValue)) {
2038
+ const oldModels = currentValue;
2039
+ oldModels.forEach((child2) => getModelAdm(child2).detach());
2040
+ this.modelsTraceUnsub.get(name)?.();
2041
+ this.modelsTraceUnsub.delete(name);
2042
+ } else if (currentValue) {
2043
+ getModelAdm(currentValue).detach();
1931
2044
  }
1932
2045
  if (newModel) {
1933
2046
  const adm = getModelAdm(newModel);
@@ -1940,17 +2053,21 @@ const _ModelAdministration = class _ModelAdministration extends PreactObjectAdmi
1940
2053
  ChildModelsAdministration
1941
2054
  );
1942
2055
  newModels.push(...newModelsSource);
1943
- const oldModels = this.getModels(name) ?? [];
1944
- if (oldModels === newModels) {
2056
+ const currentValue = this.proxy[name];
2057
+ if (currentValue === newModels) {
1945
2058
  return;
1946
2059
  }
1947
- const oldModelSet = new Set(oldModels);
1948
- const newModelSet = new Set(newModels);
1949
2060
  this.activeModels.add(name);
1950
- oldModels.forEach(
1951
- (child2) => newModelSet.has(child2) || getModelAdm(child2).detach()
1952
- );
1953
- this.modelsTraceUnsub.get(name)?.();
2061
+ if (currentValue && !Array.isArray(currentValue)) {
2062
+ getModelAdm(currentValue).detach();
2063
+ } else if (Array.isArray(currentValue)) {
2064
+ const oldModels = currentValue;
2065
+ const newModelSet = new Set(newModels);
2066
+ oldModels.forEach(
2067
+ (child2) => newModelSet.has(child2) || getModelAdm(child2).detach()
2068
+ );
2069
+ this.modelsTraceUnsub.get(name)?.();
2070
+ }
1954
2071
  PreactObjectAdministration.proxyTraps.set(
1955
2072
  this.source,
1956
2073
  name,
@@ -1970,20 +2087,13 @@ const _ModelAdministration = class _ModelAdministration extends PreactObjectAdmi
1970
2087
  })
1971
2088
  );
1972
2089
  newModels.forEach((child2) => {
2090
+ const oldModelSet = Array.isArray(currentValue) ? new Set(currentValue) : /* @__PURE__ */ new Set();
1973
2091
  if (!oldModelSet.has(child2)) {
1974
2092
  const internalModel = getModelAdm(child2);
1975
2093
  internalModel.attach(this, name);
1976
2094
  }
1977
2095
  });
1978
2096
  }
1979
- getModel(name) {
1980
- const model2 = this.proxy[name];
1981
- return model2 ?? null;
1982
- }
1983
- getModels(name) {
1984
- const model2 = this.proxy[name];
1985
- return model2 ?? [];
1986
- }
1987
2097
  getModelRef(name) {
1988
2098
  const a = this.getReferencedAtom(name);
1989
2099
  a.reportObserved();
@@ -2040,19 +2150,55 @@ const _ModelAdministration = class _ModelAdministration extends PreactObjectAdmi
2040
2150
  this.root = parent.root;
2041
2151
  this.parentName = parentName;
2042
2152
  }
2043
- runInBatch(() => {
2153
+ batch(() => {
2044
2154
  onModelAttached(this.proxy);
2045
2155
  this.proxy.modelDidAttach();
2046
2156
  });
2047
2157
  }
2048
2158
  detach() {
2049
- runInBatch(() => {
2159
+ batch(() => {
2050
2160
  this.proxy.modelWillDetach();
2051
2161
  onModelDetached(this.proxy);
2052
2162
  });
2163
+ this.contextCache.forEach((computed2) => computed2.clear());
2164
+ this.contextCache.clear();
2053
2165
  this.parent = null;
2054
2166
  this.root = this;
2055
2167
  }
2168
+ getContextValue(contextId, provideSymbol, defaultValue, hasDefault) {
2169
+ let computed2 = this.contextCache.get(contextId);
2170
+ if (!computed2) {
2171
+ computed2 = createComputed(() => {
2172
+ return this.lookupContextValue(
2173
+ contextId,
2174
+ provideSymbol,
2175
+ defaultValue,
2176
+ hasDefault
2177
+ );
2178
+ });
2179
+ this.contextCache.set(contextId, computed2);
2180
+ }
2181
+ return computed2.get();
2182
+ }
2183
+ lookupContextValue(contextId, provideSymbol, defaultValue, hasDefault) {
2184
+ const provideMethod = this.source[provideSymbol];
2185
+ if (typeof provideMethod === "function") {
2186
+ return provideMethod.call(this.proxy);
2187
+ }
2188
+ const parent = this.parent;
2189
+ if (parent) {
2190
+ return parent.getContextValue(
2191
+ contextId,
2192
+ provideSymbol,
2193
+ defaultValue,
2194
+ hasDefault
2195
+ );
2196
+ }
2197
+ if (hasDefault) {
2198
+ return defaultValue;
2199
+ }
2200
+ return void 0;
2201
+ }
2056
2202
  toJSON() {
2057
2203
  return Object.keys(this.configuration).reduce((json, key) => {
2058
2204
  switch (this.configuration[key].type) {
@@ -2062,44 +2208,47 @@ const _ModelAdministration = class _ModelAdministration extends PreactObjectAdmi
2062
2208
  break;
2063
2209
  case ModelCfgTypes.modelRef:
2064
2210
  const model2 = this.proxy[key];
2065
- json[key] = model2 && getModelRefSnapshot(model2);
2066
- break;
2067
- case ModelCfgTypes.modelRefs:
2068
- if (!this.snapshotMap.has(key)) {
2069
- this.snapshotMap.set(
2070
- key,
2071
- createComputed(() => {
2072
- const models = this.proxy[key] ?? [];
2073
- return models.map((m) => getModelRefSnapshot(m));
2074
- })
2075
- );
2211
+ if (Array.isArray(model2)) {
2212
+ if (!this.snapshotMap.has(key)) {
2213
+ this.snapshotMap.set(
2214
+ key,
2215
+ createComputed(() => {
2216
+ const models = this.proxy[key] ?? [];
2217
+ return models.map((m) => getModelRefSnapshot(m));
2218
+ })
2219
+ );
2220
+ }
2221
+ json[key] = this.snapshotMap.get(key).get();
2222
+ break;
2076
2223
  }
2077
- json[key] = this.snapshotMap.get(key).get();
2224
+ json[key] = model2 && getModelRefSnapshot(model2);
2078
2225
  break;
2079
2226
  case CommonCfgTypes.child:
2080
- json[key] = getModelAdm(this.proxy[key])?.getSnapshot();
2081
- break;
2082
- case CommonCfgTypes.children:
2083
- if (!this.snapshotMap.has(key)) {
2084
- this.snapshotMap.set(
2085
- key,
2086
- createComputed(() => {
2087
- return getSource(
2088
- (this.proxy[key] ?? []).map(
2089
- (model22) => getModelAdm(model22).getSnapshot()
2090
- )
2091
- );
2092
- })
2093
- );
2227
+ const child2 = this.proxy[key];
2228
+ if (Array.isArray(child2)) {
2229
+ if (!this.snapshotMap.has(key)) {
2230
+ this.snapshotMap.set(
2231
+ key,
2232
+ createComputed(() => {
2233
+ return getSource(
2234
+ (this.proxy[key] ?? []).map(
2235
+ (model22) => getModelAdm(model22).getSnapshot()
2236
+ )
2237
+ );
2238
+ })
2239
+ );
2240
+ }
2241
+ json[key] = this.snapshotMap.get(key).get();
2242
+ break;
2094
2243
  }
2095
- json[key] = this.snapshotMap.get(key).get();
2244
+ json[key] = getModelAdm(child2)?.getSnapshot();
2096
2245
  break;
2097
2246
  }
2098
2247
  return json;
2099
2248
  }, {});
2100
2249
  }
2101
2250
  onSnapshotChange(onChange) {
2102
- return createReaction(
2251
+ return reaction(
2103
2252
  () => this.getSnapshot(),
2104
2253
  (snapshot) => onChange(snapshot, this.proxy)
2105
2254
  );
@@ -2122,29 +2271,53 @@ const _ModelAdministration = class _ModelAdministration extends PreactObjectAdmi
2122
2271
  this.proxy[key] = value;
2123
2272
  break;
2124
2273
  case ModelCfgTypes.modelRef:
2125
- if (value instanceof Model) {
2274
+ if (Array.isArray(value)) {
2275
+ if (value?.[0] instanceof Model) {
2276
+ this.proxy[key] = value;
2277
+ } else {
2278
+ this.source[key] = value.map(
2279
+ (snapshot2) => getSnapshotRefId(snapshot2)
2280
+ );
2281
+ this.referencedAtoms?.get(key)?.reportChanged();
2282
+ }
2283
+ break;
2284
+ } else if (value instanceof Model) {
2126
2285
  this.proxy[key] = value;
2127
2286
  } else {
2128
2287
  this.source[key] = getSnapshotRefId(value);
2129
2288
  this.referencedAtoms?.get(key)?.reportChanged();
2130
2289
  }
2131
2290
  break;
2132
- case ModelCfgTypes.modelRefs:
2133
- if (value?.[0] instanceof Model) {
2134
- this.proxy[key] = value;
2135
- } else {
2136
- this.source[key] = value.map(
2137
- (snapshot2) => getSnapshotRefId(snapshot2)
2138
- );
2139
- this.referencedAtoms?.get(key)?.reportChanged();
2140
- }
2141
- break;
2142
2291
  case ModelCfgTypes.id:
2143
2292
  this.setId(key, value);
2144
2293
  break;
2145
2294
  case CommonCfgTypes.child:
2146
2295
  let model2;
2147
- if (value instanceof Model) {
2296
+ if (Array.isArray(value)) {
2297
+ const Ctor = childType2;
2298
+ this.proxy[key] = value?.map(
2299
+ (snapshot2, index) => {
2300
+ snapshot2 = snapshot2 ?? {};
2301
+ let model22;
2302
+ if (snapshot2 instanceof Model) {
2303
+ model22 = snapshot2;
2304
+ } else {
2305
+ ensureChildTypes(key);
2306
+ const id = childType2 && getSnapshotId(snapshot2, Ctor);
2307
+ const foundModel = id != null ? getModelById(this.root.proxy, id) : this.proxy[key][index];
2308
+ const adm = foundModel && getModelAdm(foundModel);
2309
+ if (adm && foundModel.parent === this.proxy && adm?.parentName === key) {
2310
+ adm.loadSnapshot(snapshot2);
2311
+ model22 = foundModel;
2312
+ } else {
2313
+ model22 = Ctor.create(snapshot2);
2314
+ }
2315
+ }
2316
+ return model22;
2317
+ }
2318
+ );
2319
+ break;
2320
+ } else if (value instanceof Model) {
2148
2321
  model2 = value;
2149
2322
  } else {
2150
2323
  ensureChildTypes(key);
@@ -2159,28 +2332,6 @@ const _ModelAdministration = class _ModelAdministration extends PreactObjectAdmi
2159
2332
  }
2160
2333
  this.proxy[key] = model2;
2161
2334
  break;
2162
- case CommonCfgTypes.children:
2163
- const Ctor = childType2;
2164
- this.proxy[key] = value?.map((snapshot2, index) => {
2165
- snapshot2 = snapshot2 ?? {};
2166
- let model22;
2167
- if (snapshot2 instanceof Model) {
2168
- model22 = snapshot2;
2169
- } else {
2170
- ensureChildTypes(key);
2171
- const id = childType2 && getSnapshotId(snapshot2, Ctor);
2172
- const foundModel = id != null ? getModelById(this.root.proxy, id) : this.proxy[key][index];
2173
- const adm = foundModel && getModelAdm(foundModel);
2174
- if (adm && foundModel.parent === this.proxy && adm?.parentName === key) {
2175
- adm.loadSnapshot(snapshot2);
2176
- model22 = foundModel;
2177
- } else {
2178
- model22 = Ctor.create(snapshot2);
2179
- }
2180
- }
2181
- return model22;
2182
- });
2183
- break;
2184
2335
  default:
2185
2336
  console.warn(
2186
2337
  `r-state-tree: invalid key '${key}' found in snapshot, ignored.`
@@ -2199,87 +2350,13 @@ const _ModelAdministration = class _ModelAdministration extends PreactObjectAdmi
2199
2350
  }
2200
2351
  return this.computedSnapshot.get();
2201
2352
  }
2202
- };
2203
- _ModelAdministration.proxyTraps = Object.assign(
2204
- {},
2205
- PreactObjectAdministration.proxyTraps,
2206
- {
2207
- get(target, prop, proxy) {
2208
- const adm = getAdministration(target);
2209
- if (prop === "parent") {
2210
- return target.parent;
2211
- }
2212
- switch (adm.configuration[prop]?.type) {
2213
- case ModelCfgTypes.modelRef:
2214
- return adm.getModelRef(prop);
2215
- case ModelCfgTypes.modelRefs:
2216
- return adm.getModelRefs(prop);
2217
- default:
2218
- return PreactObjectAdministration.proxyTraps.get?.apply(
2219
- null,
2220
- arguments
2221
- );
2222
- }
2223
- },
2224
- set(target, name, value) {
2225
- const adm = getAdministration(target);
2226
- adm.writeInProgress.add(name);
2227
- try {
2228
- switch (adm.configuration[name]?.type) {
2229
- case ModelCfgTypes.modelRef: {
2230
- adm.setModelRef(name, value);
2231
- return true;
2232
- }
2233
- case ModelCfgTypes.modelRefs: {
2234
- adm.setModelRefs(name, value);
2235
- return true;
2236
- }
2237
- case CommonCfgTypes.children: {
2238
- adm.setModels(name, value);
2239
- return true;
2240
- }
2241
- case CommonCfgTypes.child: {
2242
- adm.setModel(name, value);
2243
- break;
2244
- }
2245
- case ModelCfgTypes.id: {
2246
- adm.setId(name, value);
2247
- break;
2248
- }
2249
- case ModelCfgTypes.state: {
2250
- adm.setState(name, value);
2251
- break;
2252
- }
2253
- }
2254
- return PreactObjectAdministration.proxyTraps.set?.apply(
2255
- null,
2256
- arguments
2257
- );
2258
- } finally {
2259
- adm.writeInProgress.delete(name);
2260
- }
2261
- },
2262
- defineProperty(target, name, desc) {
2263
- const adm = getAdministration(target);
2264
- if (desc && "value" in desc && !adm.writeInProgress.has(name)) {
2265
- switch (adm.configuration[name]?.type) {
2266
- case ModelCfgTypes.modelRef:
2267
- case ModelCfgTypes.modelRefs:
2268
- case CommonCfgTypes.children:
2269
- case CommonCfgTypes.child:
2270
- case ModelCfgTypes.id: {
2271
- adm.proxy[name] = desc.value;
2272
- return true;
2273
- }
2274
- }
2275
- }
2276
- return Reflect.defineProperty(target, name, desc);
2277
- }
2278
- }
2279
- );
2280
- let ModelAdministration = _ModelAdministration;
2353
+ }
2281
2354
  let initEnabled = false;
2282
- const _Model = class _Model {
2355
+ class Model {
2356
+ static get types() {
2357
+ return this[Symbol.metadata];
2358
+ }
2359
+ static childTypes = {};
2283
2360
  static create(snapshot, ...args) {
2284
2361
  let instance;
2285
2362
  try {
@@ -2304,8 +2381,9 @@ const _Model = class _Model {
2304
2381
  ModelAdministration
2305
2382
  );
2306
2383
  const adm = getModelAdm(observable2);
2307
- const config = this.constructor.types;
2308
- adm.setConfiguration(config ?? {});
2384
+ adm.setConfiguration(
2385
+ () => this.constructor.types ?? {}
2386
+ );
2309
2387
  return observable2;
2310
2388
  }
2311
2389
  get parent() {
@@ -2320,10 +2398,7 @@ const _Model = class _Model {
2320
2398
  // eslint-disable-next-line @typescript-eslint/no-empty-function
2321
2399
  modelWillDetach() {
2322
2400
  }
2323
- };
2324
- _Model.types = {};
2325
- _Model.childTypes = {};
2326
- let Model = _Model;
2401
+ }
2327
2402
  function mount(container) {
2328
2403
  return allowNewStore(() => {
2329
2404
  const element = container;
@@ -2361,59 +2436,84 @@ function applySnapshot(model2, snapshot) {
2361
2436
  adm.loadSnapshot(snapshot);
2362
2437
  return model2;
2363
2438
  }
2364
- function makeDecorator(type, asMethod = (val) => makeDecorator(type(val))) {
2365
- return function(...args) {
2366
- if (args.length === 1) {
2367
- return asMethod(args[0]);
2368
- } else {
2369
- const [target, propertyKey, descriptor] = args;
2370
- const Ctor = target.constructor;
2371
- if (Ctor.types === Store.types || Ctor.types === Model.types) {
2372
- Ctor.types = {};
2439
+ const CONTEXT_SYMBOL = Symbol("context");
2440
+ const HAS_DEFAULT = Symbol("hasDefault");
2441
+ function createContext(defaultValue) {
2442
+ const provideSymbol = Symbol("provide");
2443
+ const contextId = Symbol("contextId");
2444
+ const hasDefault = arguments.length > 0;
2445
+ return {
2446
+ [CONTEXT_SYMBOL]: contextId,
2447
+ [HAS_DEFAULT]: hasDefault,
2448
+ defaultValue,
2449
+ provide: provideSymbol,
2450
+ consume(instance) {
2451
+ const adm = getAdministration(instance);
2452
+ if (!adm || typeof adm.getContextValue !== "function") {
2453
+ throw new Error("Context can only be consumed in Models or Stores");
2373
2454
  }
2374
- Ctor.types[propertyKey] = type;
2375
- return descriptor;
2455
+ return adm.getContextValue(
2456
+ contextId,
2457
+ provideSymbol,
2458
+ defaultValue,
2459
+ hasDefault
2460
+ );
2376
2461
  }
2377
2462
  };
2378
2463
  }
2379
- const child = makeDecorator(childType);
2380
- const children = makeDecorator(childrenType);
2464
+ function makeDecorator(type) {
2465
+ return function(value, context) {
2466
+ context.metadata[context.name] = type;
2467
+ return value;
2468
+ };
2469
+ }
2470
+ function makeChildDecorator(typeObj) {
2471
+ return function(valueOrChildType, context) {
2472
+ if (context !== void 0) {
2473
+ return makeDecorator(typeObj)(valueOrChildType, context);
2474
+ }
2475
+ const childCtor = valueOrChildType;
2476
+ return function(value, context2) {
2477
+ const typeWithCtor = typeObj(childCtor);
2478
+ return makeDecorator(typeWithCtor)(value, context2);
2479
+ };
2480
+ };
2481
+ }
2482
+ const child = makeChildDecorator(childType);
2483
+ const modelRef = makeChildDecorator(modelRefType);
2381
2484
  const model = makeDecorator(modelType);
2382
- const modelRef = makeDecorator(modelRefType);
2383
- const modelRefs = makeDecorator(modelRefsType);
2384
2485
  const identifier = makeDecorator(idType);
2385
2486
  const state = makeDecorator(stateType);
2386
2487
  export {
2387
2488
  Model,
2388
2489
  Observable,
2490
+ ReadonlySignal,
2491
+ Signal2 as Signal,
2389
2492
  Store,
2390
2493
  applySnapshot,
2494
+ batch2 as batch,
2391
2495
  child,
2392
- children,
2393
- createAtom,
2394
- createComputed,
2395
- createEffect,
2396
- createListener,
2397
- createReaction,
2398
- createSignal,
2496
+ computed,
2497
+ createContext,
2399
2498
  createStore,
2499
+ effect2 as effect,
2400
2500
  getSignal,
2401
2501
  identifier,
2402
2502
  isObservable,
2403
2503
  model,
2404
2504
  modelRef,
2405
- modelRefs,
2406
2505
  mount,
2407
2506
  observable,
2408
2507
  onSnapshot,
2409
2508
  onSnapshotDiff,
2509
+ reaction,
2410
2510
  reportChanged,
2411
2511
  reportObserved,
2412
- runInBatch,
2413
- runInUntracked,
2512
+ signal2 as signal,
2414
2513
  source,
2415
2514
  state,
2416
2515
  toSnapshot,
2417
2516
  unmount,
2517
+ untracked2 as untracked,
2418
2518
  updateStore
2419
2519
  };