@mmstack/primitives 20.0.1 → 20.0.3

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.
@@ -101,7 +101,7 @@ function debounced(initial, opt) {
101
101
  */
102
102
  function debounce(source, opt) {
103
103
  const ms = opt?.ms ?? 0;
104
- const trigger = signal(false);
104
+ const trigger = signal(false, ...(ngDevMode ? [{ debugName: "trigger" }] : []));
105
105
  let timeout;
106
106
  try {
107
107
  const destroyRef = opt?.destroyRef ?? inject(DestroyRef, { optional: true });
@@ -349,7 +349,7 @@ function elementVisibility(target = inject(ElementRef), opt) {
349
349
  const base = computed(() => undefined, {
350
350
  debugName: opt?.debugName,
351
351
  });
352
- base.visible = computed(() => false);
352
+ base.visible = computed(() => false, ...(ngDevMode ? [{ debugName: "visible" }] : []));
353
353
  return base;
354
354
  }
355
355
  const state = signal(undefined, {
@@ -386,7 +386,7 @@ function elementVisibility(target = inject(ElementRef), opt) {
386
386
  if (!s)
387
387
  return false;
388
388
  return s.isIntersecting;
389
- });
389
+ }, ...(ngDevMode ? [{ debugName: "visible" }] : []));
390
390
  return base;
391
391
  }
392
392
 
@@ -451,7 +451,7 @@ function elementVisibility(target = inject(ElementRef), opt) {
451
451
  */
452
452
  function mapArray(source, map, opt) {
453
453
  const data = isSignal(source) ? source : computed(source);
454
- const len = computed(() => data().length);
454
+ const len = computed(() => data().length, ...(ngDevMode ? [{ debugName: "len" }] : []));
455
455
  return linkedSignal({
456
456
  source: () => len(),
457
457
  computation: (len, prev) => {
@@ -460,7 +460,13 @@ function mapArray(source, map, opt) {
460
460
  if (len === prev.value.length)
461
461
  return prev.value;
462
462
  if (len < prev.value.length) {
463
- return prev.value.slice(0, len);
463
+ const slice = prev.value.slice(0, len);
464
+ if (opt?.onDestroy) {
465
+ for (let i = len; i < prev.value.length; i++) {
466
+ opt.onDestroy?.(prev.value[i]);
467
+ }
468
+ }
469
+ return slice;
464
470
  }
465
471
  else {
466
472
  const next = [...prev.value];
@@ -518,11 +524,11 @@ function mapArray(source, map, opt) {
518
524
  * }
519
525
  * ```
520
526
  */
521
- function mediaQuery(query, debugName) {
527
+ function mediaQuery(query, debugName = 'mediaQuery') {
522
528
  if (isPlatformServer(inject(PLATFORM_ID)))
523
529
  return computed(() => false, { debugName });
524
530
  const mediaQueryList = window.matchMedia(query);
525
- const state = signal(mediaQueryList.matches, { debugName });
531
+ const state = signal(mediaQueryList.matches, { debugName: debugName });
526
532
  const handleChange = (event) => {
527
533
  state.set(event.matches);
528
534
  };
@@ -632,7 +638,7 @@ function throttled(initial, opt) {
632
638
  */
633
639
  function throttle(source, opt) {
634
640
  const ms = opt?.ms ?? 0;
635
- const trigger = signal(false);
641
+ const trigger = signal(false, ...(ngDevMode ? [{ debugName: "trigger" }] : []));
636
642
  let timeout;
637
643
  try {
638
644
  const destroyRef = opt?.destroyRef ?? inject(DestroyRef, { optional: true });
@@ -706,12 +712,12 @@ function mousePosition(opt) {
706
712
  x: 0,
707
713
  y: 0,
708
714
  }), {
709
- debugName: opt?.debugName,
715
+ debugName: opt?.debugName ?? 'mousePosition',
710
716
  });
711
717
  base.unthrottled = base;
712
718
  return base;
713
719
  }
714
- const { target = window, coordinateSpace = 'client', touch = false, debugName, throttle = 100, } = opt ?? {};
720
+ const { target = window, coordinateSpace = 'client', touch = false, debugName = 'mousePosition', throttle = 100, } = opt ?? {};
715
721
  const eventTarget = target instanceof ElementRef ? target.nativeElement : target;
716
722
  if (!eventTarget) {
717
723
  if (isDevMode())
@@ -772,18 +778,18 @@ const serverDate = new Date();
772
778
  * @param debugName Optional debug name for the signal.
773
779
  * @returns A `NetworkStatusSignal` instance.
774
780
  */
775
- function networkStatus(debugName) {
781
+ function networkStatus(debugName = 'networkStatus') {
776
782
  if (isPlatformServer(inject(PLATFORM_ID))) {
777
783
  const sig = computed(() => true, {
778
784
  debugName,
779
785
  });
780
- sig.since = computed(() => serverDate);
786
+ sig.since = computed(() => serverDate, ...(ngDevMode ? [{ debugName: "since" }] : []));
781
787
  return sig;
782
788
  }
783
- const state = signal(navigator.onLine, {
784
- debugName,
785
- });
786
- const since = signal(new Date());
789
+ const state = signal(navigator.onLine, ...(ngDevMode ? [{ debugName: "state", debugName }] : [{
790
+ debugName,
791
+ }]));
792
+ const since = signal(new Date(), ...(ngDevMode ? [{ debugName: "since" }] : []));
787
793
  const goOnline = () => {
788
794
  state.set(true);
789
795
  since.set(new Date());
@@ -839,11 +845,11 @@ function networkStatus(debugName) {
839
845
  * }
840
846
  * ```
841
847
  */
842
- function pageVisibility(debugName) {
848
+ function pageVisibility(debugName = 'pageVisibility') {
843
849
  if (isPlatformServer(inject(PLATFORM_ID))) {
844
850
  return computed(() => 'visible', { debugName });
845
851
  }
846
- const visibility = signal(document.visibilityState, { debugName });
852
+ const visibility = signal(document.visibilityState, ...(ngDevMode ? [{ debugName: "visibility", debugName }] : [{ debugName }]));
847
853
  const onVisibilityChange = () => visibility.set(document.visibilityState);
848
854
  document.addEventListener('visibilitychange', onVisibilityChange);
849
855
  inject(DestroyRef).onDestroy(() => document.removeEventListener('visibilitychange', onVisibilityChange));
@@ -901,12 +907,12 @@ function scrollPosition(opt) {
901
907
  x: 0,
902
908
  y: 0,
903
909
  }), {
904
- debugName: opt?.debugName,
910
+ debugName: opt?.debugName ?? 'scrollPosition',
905
911
  });
906
912
  base.unthrottled = base;
907
913
  return base;
908
914
  }
909
- const { target = window, throttle = 100, debugName } = opt || {};
915
+ const { target = window, throttle = 100, debugName = 'scrollPosition', } = opt || {};
910
916
  let element;
911
917
  let getScrollPosition;
912
918
  if (target instanceof Window) {
@@ -992,12 +998,12 @@ function windowSize(opt) {
992
998
  const base = computed(() => ({
993
999
  width: 1024,
994
1000
  height: 768,
995
- }), { debugName: opt?.debugName });
1001
+ }), { debugName: opt?.debugName ?? 'windowSize' });
996
1002
  base.unthrottled = base;
997
1003
  return base;
998
1004
  }
999
1005
  const sizeSignal = throttled({ width: window.innerWidth, height: window.innerHeight }, {
1000
- debugName: opt?.debugName,
1006
+ debugName: opt?.debugName ?? 'windowSize',
1001
1007
  equal: (a, b) => a.width === b.width && a.height === b.height,
1002
1008
  ms: opt?.throttle ?? 100,
1003
1009
  });
@@ -1139,16 +1145,23 @@ function stored(fallback, { key, store: providedStore, serialize = JSON.stringif
1139
1145
  equal,
1140
1146
  };
1141
1147
  const initialKey = untracked(keySig);
1142
- const internal = signal(getValue(initialKey), {
1143
- ...opt,
1144
- equal: (a, b) => {
1145
- if (a === null && b === null)
1146
- return true;
1147
- if (a === null || b === null)
1148
- return false;
1149
- return equal(a, b);
1150
- },
1151
- });
1148
+ const internal = signal(getValue(initialKey), ...(ngDevMode ? [{ debugName: "internal", ...opt,
1149
+ equal: (a, b) => {
1150
+ if (a === null && b === null)
1151
+ return true;
1152
+ if (a === null || b === null)
1153
+ return false;
1154
+ return equal(a, b);
1155
+ } }] : [{
1156
+ ...opt,
1157
+ equal: (a, b) => {
1158
+ if (a === null && b === null)
1159
+ return true;
1160
+ if (a === null || b === null)
1161
+ return false;
1162
+ return equal(a, b);
1163
+ },
1164
+ }]));
1152
1165
  let prevKey = initialKey;
1153
1166
  if (onKeyChange === 'store') {
1154
1167
  effect(() => {
@@ -1417,9 +1430,9 @@ function withHistory(source, opt) {
1417
1430
  history.set([]);
1418
1431
  redoArray.set([]);
1419
1432
  };
1420
- internal.canUndo = computed(() => history().length > 0);
1421
- internal.canRedo = computed(() => redoArray().length > 0);
1422
- internal.canClear = computed(() => internal.canUndo() || internal.canRedo());
1433
+ internal.canUndo = computed(() => history().length > 0, ...(ngDevMode ? [{ debugName: "canUndo" }] : []));
1434
+ internal.canRedo = computed(() => redoArray().length > 0, ...(ngDevMode ? [{ debugName: "canRedo" }] : []));
1435
+ internal.canClear = computed(() => internal.canUndo() || internal.canRedo(), ...(ngDevMode ? [{ debugName: "canClear" }] : []));
1423
1436
  return internal;
1424
1437
  }
1425
1438