@reactuses/core 6.1.5 → 6.1.7

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.cjs CHANGED
@@ -638,17 +638,20 @@ function getInitialState$3(key, defaultValue, storage, serializer, onError) {
638
638
  function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStorage : undefined, options = defaultOptions$1) {
639
639
  let storage;
640
640
  const { onError = defaultOnError, effectStorageValue, mountStorageValue, listenToStorageChanges = true } = options;
641
- const storageValue = mountStorageValue != null ? mountStorageValue : effectStorageValue;
641
+ const storageValueRef = useLatest(mountStorageValue != null ? mountStorageValue : effectStorageValue);
642
+ const onErrorRef = useLatest(onError);
642
643
  try {
643
644
  storage = getStorage();
644
645
  } catch (err) {
645
- onError(err);
646
+ onErrorRef.current(err);
646
647
  }
647
648
  const type = guessSerializerType(defaultValue);
648
649
  var _options_serializer;
649
- const serializer = (_options_serializer = options.serializer) != null ? _options_serializer : StorageSerializers[type];
650
- const [state, setState] = React.useState(getInitialState$3(key, defaultValue, storage, serializer, onError));
650
+ const serializerRef = useLatest((_options_serializer = options.serializer) != null ? _options_serializer : StorageSerializers[type]);
651
+ const [state, setState] = React.useState(getInitialState$3(key, defaultValue, storage, serializerRef.current, onError));
651
652
  useDeepCompareEffect(()=>{
653
+ const serializer = serializerRef.current;
654
+ const storageValue = storageValueRef.current;
652
655
  var _ref;
653
656
  const data = (_ref = storageValue ? isFunction(storageValue) ? storageValue() : storageValue : defaultValue) != null ? _ref : null;
654
657
  const getStoredValue = ()=>{
@@ -661,16 +664,13 @@ function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStora
661
664
  return data;
662
665
  }
663
666
  } catch (e) {
664
- onError(e);
667
+ onErrorRef.current(e);
665
668
  }
666
669
  };
667
670
  setState(getStoredValue());
668
671
  }, [
669
672
  key,
670
- serializer,
671
- storage,
672
- onError,
673
- storageValue
673
+ storage
674
674
  ]);
675
675
  const updateState = useEvent((valOrFunc)=>{
676
676
  const currentState = isFunction(valOrFunc) ? valOrFunc(state) : valOrFunc;
@@ -679,9 +679,9 @@ function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStora
679
679
  storage == null ? void 0 : storage.removeItem(key);
680
680
  } else {
681
681
  try {
682
- storage == null ? void 0 : storage.setItem(key, serializer.write(currentState));
682
+ storage == null ? void 0 : storage.setItem(key, serializerRef.current.write(currentState));
683
683
  } catch (e) {
684
- onError(e);
684
+ onErrorRef.current(e);
685
685
  }
686
686
  }
687
687
  });
@@ -689,12 +689,12 @@ function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStora
689
689
  try {
690
690
  const raw = storage == null ? void 0 : storage.getItem(key);
691
691
  if (raw !== undefined && raw !== null) {
692
- updateState(serializer.read(raw));
692
+ updateState(serializerRef.current.read(raw));
693
693
  } else {
694
694
  updateState(null);
695
695
  }
696
696
  } catch (e) {
697
- onError(e);
697
+ onErrorRef.current(e);
698
698
  }
699
699
  });
700
700
  React.useEffect(()=>{
@@ -2811,11 +2811,17 @@ const useScriptTag = (src, onLoaded = noop, options = defaultOptions$1)=>{
2811
2811
  // Event listeners
2812
2812
  el.addEventListener('error', (event)=>{
2813
2813
  setStatus(event.type === 'load' ? 'ready' : 'error');
2814
- return reject(event);
2814
+ const error = new Error(`Failed to load script: ${src}`);
2815
+ error.name = 'ScriptLoadError';
2816
+ error.event = event;
2817
+ return reject(error);
2815
2818
  });
2816
2819
  el.addEventListener('abort', (event)=>{
2817
2820
  setStatus(event.type === 'load' ? 'ready' : 'error');
2818
- return reject(event);
2821
+ const error = new Error(`Script load aborted: ${src}`);
2822
+ error.name = 'ScriptLoadAbortError';
2823
+ error.event = event;
2824
+ return reject(error);
2819
2825
  });
2820
2826
  el.addEventListener('load', (event)=>{
2821
2827
  setStatus(event.type === 'load' ? 'ready' : 'error');
package/dist/index.mjs CHANGED
@@ -630,17 +630,20 @@ function getInitialState$3(key, defaultValue, storage, serializer, onError) {
630
630
  function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStorage : undefined, options = defaultOptions$1) {
631
631
  let storage;
632
632
  const { onError = defaultOnError, effectStorageValue, mountStorageValue, listenToStorageChanges = true } = options;
633
- const storageValue = mountStorageValue != null ? mountStorageValue : effectStorageValue;
633
+ const storageValueRef = useLatest(mountStorageValue != null ? mountStorageValue : effectStorageValue);
634
+ const onErrorRef = useLatest(onError);
634
635
  try {
635
636
  storage = getStorage();
636
637
  } catch (err) {
637
- onError(err);
638
+ onErrorRef.current(err);
638
639
  }
639
640
  const type = guessSerializerType(defaultValue);
640
641
  var _options_serializer;
641
- const serializer = (_options_serializer = options.serializer) != null ? _options_serializer : StorageSerializers[type];
642
- const [state, setState] = useState(getInitialState$3(key, defaultValue, storage, serializer, onError));
642
+ const serializerRef = useLatest((_options_serializer = options.serializer) != null ? _options_serializer : StorageSerializers[type]);
643
+ const [state, setState] = useState(getInitialState$3(key, defaultValue, storage, serializerRef.current, onError));
643
644
  useDeepCompareEffect(()=>{
645
+ const serializer = serializerRef.current;
646
+ const storageValue = storageValueRef.current;
644
647
  var _ref;
645
648
  const data = (_ref = storageValue ? isFunction(storageValue) ? storageValue() : storageValue : defaultValue) != null ? _ref : null;
646
649
  const getStoredValue = ()=>{
@@ -653,16 +656,13 @@ function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStora
653
656
  return data;
654
657
  }
655
658
  } catch (e) {
656
- onError(e);
659
+ onErrorRef.current(e);
657
660
  }
658
661
  };
659
662
  setState(getStoredValue());
660
663
  }, [
661
664
  key,
662
- serializer,
663
- storage,
664
- onError,
665
- storageValue
665
+ storage
666
666
  ]);
667
667
  const updateState = useEvent((valOrFunc)=>{
668
668
  const currentState = isFunction(valOrFunc) ? valOrFunc(state) : valOrFunc;
@@ -671,9 +671,9 @@ function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStora
671
671
  storage == null ? void 0 : storage.removeItem(key);
672
672
  } else {
673
673
  try {
674
- storage == null ? void 0 : storage.setItem(key, serializer.write(currentState));
674
+ storage == null ? void 0 : storage.setItem(key, serializerRef.current.write(currentState));
675
675
  } catch (e) {
676
- onError(e);
676
+ onErrorRef.current(e);
677
677
  }
678
678
  }
679
679
  });
@@ -681,12 +681,12 @@ function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStora
681
681
  try {
682
682
  const raw = storage == null ? void 0 : storage.getItem(key);
683
683
  if (raw !== undefined && raw !== null) {
684
- updateState(serializer.read(raw));
684
+ updateState(serializerRef.current.read(raw));
685
685
  } else {
686
686
  updateState(null);
687
687
  }
688
688
  } catch (e) {
689
- onError(e);
689
+ onErrorRef.current(e);
690
690
  }
691
691
  });
692
692
  useEffect(()=>{
@@ -2803,11 +2803,17 @@ const useScriptTag = (src, onLoaded = noop, options = defaultOptions$1)=>{
2803
2803
  // Event listeners
2804
2804
  el.addEventListener('error', (event)=>{
2805
2805
  setStatus(event.type === 'load' ? 'ready' : 'error');
2806
- return reject(event);
2806
+ const error = new Error(`Failed to load script: ${src}`);
2807
+ error.name = 'ScriptLoadError';
2808
+ error.event = event;
2809
+ return reject(error);
2807
2810
  });
2808
2811
  el.addEventListener('abort', (event)=>{
2809
2812
  setStatus(event.type === 'load' ? 'ready' : 'error');
2810
- return reject(event);
2813
+ const error = new Error(`Script load aborted: ${src}`);
2814
+ error.name = 'ScriptLoadAbortError';
2815
+ error.event = event;
2816
+ return reject(error);
2811
2817
  });
2812
2818
  el.addEventListener('load', (event)=>{
2813
2819
  setStatus(event.type === 'load' ? 'ready' : 'error');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reactuses/core",
3
- "version": "6.1.5",
3
+ "version": "6.1.7",
4
4
  "license": "Unlicense",
5
5
  "homepage": "https://www.reactuse.com/",
6
6
  "repository": {