react-simplikit 0.0.50 → 0.0.51

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.
@@ -30,10 +30,15 @@ var import_react = require("react");
30
30
  function useAsyncEffect(effect, deps) {
31
31
  (0, import_react.useEffect)(() => {
32
32
  let cleanup;
33
+ let isCleaned = false;
33
34
  effect().then((result) => {
34
35
  cleanup = result;
36
+ if (isCleaned) {
37
+ cleanup?.();
38
+ }
35
39
  });
36
40
  return () => {
41
+ isCleaned = true;
37
42
  cleanup?.();
38
43
  };
39
44
  }, deps);
@@ -49,11 +49,21 @@ function useInterval(callback, options) {
49
49
  const immediate = typeof options === "number" ? false : options.immediate;
50
50
  const enabled = typeof options === "number" ? true : options.enabled ?? true;
51
51
  const preservedCallback = usePreservedCallback(callback);
52
+ const immediateCalledRef = (0, import_react2.useRef)(false);
52
53
  (0, import_react2.useEffect)(
53
54
  function runImmediateCallback() {
54
- if (immediate === true && enabled) {
55
- preservedCallback();
55
+ if (immediate !== true) {
56
+ immediateCalledRef.current = false;
57
+ return;
58
+ }
59
+ if (!enabled) {
60
+ return;
61
+ }
62
+ if (immediateCalledRef.current) {
63
+ return;
56
64
  }
65
+ immediateCalledRef.current = true;
66
+ preservedCallback();
57
67
  },
58
68
  [immediate, preservedCallback, enabled]
59
69
  );
package/dist/index.cjs CHANGED
@@ -349,10 +349,15 @@ var import_react9 = require("react");
349
349
  function useAsyncEffect(effect, deps) {
350
350
  (0, import_react9.useEffect)(() => {
351
351
  let cleanup;
352
+ let isCleaned = false;
352
353
  effect().then((result) => {
353
354
  cleanup = result;
355
+ if (isCleaned) {
356
+ cleanup?.();
357
+ }
354
358
  });
355
359
  return () => {
360
+ isCleaned = true;
356
361
  cleanup?.();
357
362
  };
358
363
  }, deps);
@@ -687,11 +692,21 @@ function useInterval(callback, options) {
687
692
  const immediate = typeof options === "number" ? false : options.immediate;
688
693
  const enabled = typeof options === "number" ? true : options.enabled ?? true;
689
694
  const preservedCallback = usePreservedCallback(callback);
695
+ const immediateCalledRef = (0, import_react20.useRef)(false);
690
696
  (0, import_react20.useEffect)(
691
697
  function runImmediateCallback() {
692
- if (immediate === true && enabled) {
693
- preservedCallback();
698
+ if (immediate !== true) {
699
+ immediateCalledRef.current = false;
700
+ return;
701
+ }
702
+ if (!enabled) {
703
+ return;
704
+ }
705
+ if (immediateCalledRef.current) {
706
+ return;
694
707
  }
708
+ immediateCalledRef.current = true;
709
+ preservedCallback();
695
710
  },
696
711
  [immediate, preservedCallback, enabled]
697
712
  );
@@ -5,10 +5,15 @@ import { useEffect } from "react";
5
5
  function useAsyncEffect(effect, deps) {
6
6
  useEffect(() => {
7
7
  let cleanup;
8
+ let isCleaned = false;
8
9
  effect().then((result) => {
9
10
  cleanup = result;
11
+ if (isCleaned) {
12
+ cleanup?.();
13
+ }
10
14
  });
11
15
  return () => {
16
+ isCleaned = true;
12
17
  cleanup?.();
13
18
  };
14
19
  }, deps);
@@ -1,7 +1,7 @@
1
1
  "use client";
2
2
 
3
3
  // src/hooks/useInterval/useInterval.ts
4
- import { useEffect as useEffect2 } from "react";
4
+ import { useEffect as useEffect2, useRef as useRef2 } from "react";
5
5
 
6
6
  // src/hooks/usePreservedCallback/usePreservedCallback.ts
7
7
  import { useCallback, useEffect, useRef } from "react";
@@ -24,11 +24,21 @@ function useInterval(callback, options) {
24
24
  const immediate = typeof options === "number" ? false : options.immediate;
25
25
  const enabled = typeof options === "number" ? true : options.enabled ?? true;
26
26
  const preservedCallback = usePreservedCallback(callback);
27
+ const immediateCalledRef = useRef2(false);
27
28
  useEffect2(
28
29
  function runImmediateCallback() {
29
- if (immediate === true && enabled) {
30
- preservedCallback();
30
+ if (immediate !== true) {
31
+ immediateCalledRef.current = false;
32
+ return;
33
+ }
34
+ if (!enabled) {
35
+ return;
36
+ }
37
+ if (immediateCalledRef.current) {
38
+ return;
31
39
  }
40
+ immediateCalledRef.current = true;
41
+ preservedCallback();
32
42
  },
33
43
  [immediate, preservedCallback, enabled]
34
44
  );
package/esm/index.js CHANGED
@@ -287,10 +287,15 @@ import { useEffect as useEffect4 } from "react";
287
287
  function useAsyncEffect(effect, deps) {
288
288
  useEffect4(() => {
289
289
  let cleanup;
290
+ let isCleaned = false;
290
291
  effect().then((result) => {
291
292
  cleanup = result;
293
+ if (isCleaned) {
294
+ cleanup?.();
295
+ }
292
296
  });
293
297
  return () => {
298
+ isCleaned = true;
294
299
  cleanup?.();
295
300
  };
296
301
  }, deps);
@@ -619,17 +624,27 @@ function echo(v) {
619
624
  }
620
625
 
621
626
  // src/hooks/useInterval/useInterval.ts
622
- import { useEffect as useEffect10 } from "react";
627
+ import { useEffect as useEffect10, useRef as useRef9 } from "react";
623
628
  function useInterval(callback, options) {
624
629
  const delay = typeof options === "number" ? options : options.delay;
625
630
  const immediate = typeof options === "number" ? false : options.immediate;
626
631
  const enabled = typeof options === "number" ? true : options.enabled ?? true;
627
632
  const preservedCallback = usePreservedCallback(callback);
633
+ const immediateCalledRef = useRef9(false);
628
634
  useEffect10(
629
635
  function runImmediateCallback() {
630
- if (immediate === true && enabled) {
631
- preservedCallback();
636
+ if (immediate !== true) {
637
+ immediateCalledRef.current = false;
638
+ return;
639
+ }
640
+ if (!enabled) {
641
+ return;
642
+ }
643
+ if (immediateCalledRef.current) {
644
+ return;
632
645
  }
646
+ immediateCalledRef.current = true;
647
+ preservedCallback();
633
648
  },
634
649
  [immediate, preservedCallback, enabled]
635
650
  );
@@ -664,9 +679,9 @@ var useIsomorphicLayoutEffect = isServer ? useEffect12 : useLayoutEffect;
664
679
  import { useMemo as useMemo5, useState as useState7 } from "react";
665
680
 
666
681
  // src/hooks/usePreservedReference/usePreservedReference.ts
667
- import { useMemo as useMemo4, useRef as useRef9 } from "react";
682
+ import { useMemo as useMemo4, useRef as useRef10 } from "react";
668
683
  function usePreservedReference(value, areValuesEqual = areDeeplyEqual) {
669
- const ref = useRef9(value);
684
+ const ref = useRef10(value);
670
685
  return useMemo4(() => {
671
686
  if (!areValuesEqual(ref.current, value)) {
672
687
  ref.current = value;
@@ -720,7 +735,7 @@ function useList(initialState = []) {
720
735
  }
721
736
 
722
737
  // src/hooks/useLoading/useLoading.ts
723
- import { useCallback as useCallback12, useEffect as useEffect13, useMemo as useMemo6, useRef as useRef10, useState as useState8 } from "react";
738
+ import { useCallback as useCallback12, useEffect as useEffect13, useMemo as useMemo6, useRef as useRef11, useState as useState8 } from "react";
724
739
  function useLoading() {
725
740
  const [loading, setLoading] = useState8(false);
726
741
  const ref = useIsMountedRef();
@@ -741,7 +756,7 @@ function useLoading() {
741
756
  return useMemo6(() => [loading, startTransition], [loading, startTransition]);
742
757
  }
743
758
  function useIsMountedRef() {
744
- const ref = useRef10({ isMounted: true }).current;
759
+ const ref = useRef11({ isMounted: true }).current;
745
760
  useEffect13(
746
761
  function trackMountedState() {
747
762
  ref.isMounted = true;
@@ -755,11 +770,11 @@ function useIsMountedRef() {
755
770
  }
756
771
 
757
772
  // src/hooks/useLongPress/useLongPress.ts
758
- import { useCallback as useCallback13, useRef as useRef11 } from "react";
773
+ import { useCallback as useCallback13, useRef as useRef12 } from "react";
759
774
  function useLongPress(onLongPress, { delay = 500, moveThreshold, onClick, onLongPressEnd } = {}) {
760
- const timeoutRef = useRef11(null);
761
- const isLongPressActiveRef = useRef11(false);
762
- const initialPositionRef = useRef11({ x: 0, y: 0 });
775
+ const timeoutRef = useRef12(null);
776
+ const isLongPressActiveRef = useRef12(false);
777
+ const initialPositionRef = useRef12({ x: 0, y: 0 });
763
778
  const preservedOnLongPress = usePreservedCallback(onLongPress);
764
779
  const preservedOnClick = usePreservedCallback(onClick || (() => {
765
780
  }));
@@ -866,9 +881,9 @@ function useMap(initialState = /* @__PURE__ */ new Map()) {
866
881
  }
867
882
 
868
883
  // src/hooks/useOutsideClickEffect/useOutsideClickEffect.ts
869
- import { useEffect as useEffect14, useRef as useRef12 } from "react";
884
+ import { useEffect as useEffect14, useRef as useRef13 } from "react";
870
885
  function useOutsideClickEffect(container, callback) {
871
- const containers = useRef12([]);
886
+ const containers = useRef13([]);
872
887
  const handleDocumentClick = usePreservedCallback(({ target }) => {
873
888
  if (target === null) {
874
889
  return;
@@ -893,12 +908,12 @@ function useOutsideClickEffect(container, callback) {
893
908
  }
894
909
 
895
910
  // src/hooks/usePrevious/usePrevious.ts
896
- import { useRef as useRef13 } from "react";
911
+ import { useRef as useRef14 } from "react";
897
912
  var strictEquals = (prev, next) => prev === next;
898
913
  function usePrevious(state, compare = strictEquals) {
899
- const prevRef = useRef13(state);
900
- const currentRef = useRef13(state);
901
- const isFirstRender = useRef13(true);
914
+ const prevRef = useRef14(state);
915
+ const currentRef = useRef14(state);
916
+ const isFirstRender = useRef14(true);
902
917
  if (isFirstRender.current) {
903
918
  isFirstRender.current = false;
904
919
  return prevRef.current;
@@ -954,7 +969,7 @@ function useSet(initialState = /* @__PURE__ */ new Set()) {
954
969
  }
955
970
 
956
971
  // src/hooks/useStorageState/useStorageState.ts
957
- import { useCallback as useCallback15, useRef as useRef14, useSyncExternalStore } from "react";
972
+ import { useCallback as useCallback15, useRef as useRef15, useSyncExternalStore } from "react";
958
973
 
959
974
  // src/hooks/useStorageState/storage.ts
960
975
  var MemoStorage = class {
@@ -1066,7 +1081,7 @@ function useStorageState(key, {
1066
1081
  ...options
1067
1082
  } = {}) {
1068
1083
  const serializedDefaultValue = defaultValue;
1069
- const cache = useRef14({
1084
+ const cache = useRef15({
1070
1085
  data: null,
1071
1086
  parsed: serializedDefaultValue
1072
1087
  });
@@ -1158,14 +1173,14 @@ function useThrottle(callback, wait, options) {
1158
1173
  }
1159
1174
 
1160
1175
  // src/hooks/useThrottledCallback/useThrottledCallback.ts
1161
- import { useCallback as useCallback16, useEffect as useEffect16, useRef as useRef15 } from "react";
1176
+ import { useCallback as useCallback16, useEffect as useEffect16, useRef as useRef16 } from "react";
1162
1177
  function useThrottledCallback({
1163
1178
  onChange,
1164
1179
  timeThreshold,
1165
1180
  edges = ["leading", "trailing"]
1166
1181
  }) {
1167
1182
  const handleChange = usePreservedCallback(onChange);
1168
- const ref = useRef15({ value: false, clearPreviousThrottle: () => {
1183
+ const ref = useRef16({ value: false, clearPreviousThrottle: () => {
1169
1184
  } });
1170
1185
  useEffect16(function cleanupThrottleOnUnmount() {
1171
1186
  const current = ref.current;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-simplikit",
3
- "version": "0.0.50",
3
+ "version": "0.0.51",
4
4
  "main": "./dist/index.cjs",
5
5
  "module": "./esm/index.js",
6
6
  "types": "./dist/index.d.cts",