@lowentry/react-redux 1.2.4 → 1.4.1

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.
Files changed (2) hide show
  1. package/LeRed.js +70 -9
  2. package/package.json +1 -1
package/LeRed.js CHANGED
@@ -781,27 +781,54 @@ export const LeRed = (() =>
781
781
  {
782
782
  return LeRed.useEffect(() =>
783
783
  {
784
- let stop = false;
784
+ const run = () =>
785
+ {
786
+ callable();
787
+ };
788
+
789
+ if(typeof window !== 'undefined')
790
+ {
791
+ window.addEventListener('beforeunload', run, {capture:true});
792
+ }
793
+ return () =>
794
+ {
795
+ if(typeof window !== 'undefined')
796
+ {
797
+ window.removeEventListener('beforeunload', run, {capture:true});
798
+ }
799
+ run();
800
+ };
801
+ }, [comparingValues, equalsComparator]);
802
+ };
803
+
804
+ LeRed.useEffectPageFocusLost = (callable, comparingValues, equalsComparator) =>
805
+ {
806
+ const events = ['pagehide', 'freeze', 'blur', 'visibilitychange'];
807
+ return LeRed.useEffect(() =>
808
+ {
809
+ if((typeof window === 'undefined'))
810
+ {
811
+ return;
812
+ }
785
813
 
786
814
  const run = () =>
787
815
  {
788
- if(stop)
816
+ if(typeof document === 'undefined')
789
817
  {
790
818
  return;
791
819
  }
792
- stop = true;
793
- if(typeof window !== 'undefined')
820
+ if((document.visibilityState !== 'hidden') && document.hasFocus())
794
821
  {
795
- window.removeEventListener('beforeunload', run);
822
+ return;
796
823
  }
797
824
  callable();
798
825
  };
799
826
 
800
- if(typeof window !== 'undefined')
827
+ events.forEach(type => window.addEventListener(type, run, {capture:true}));
828
+ return () =>
801
829
  {
802
- window.addEventListener('beforeunload', run);
803
- }
804
- return run;
830
+ events.forEach(type => window.removeEventListener(type, run, {capture:true}));
831
+ };
805
832
  }, [comparingValues, equalsComparator]);
806
833
  };
807
834
 
@@ -951,6 +978,40 @@ export const LeRed = (() =>
951
978
  };
952
979
  };
953
980
 
981
+ LeRed.useTriggerable = (event) =>
982
+ {
983
+ const [[value, uniqueId], setValue] = LeRed.useState([null, LeUtils.uniqueId()]);
984
+
985
+ LeRed.useEffect(() =>
986
+ {
987
+ if(typeof window === 'undefined')
988
+ {
989
+ return;
990
+ }
991
+
992
+ const callback = (e) =>
993
+ {
994
+ setValue([e?.detail, LeUtils.uniqueId()]);
995
+ };
996
+
997
+ const eventName = 'lowentrytriggerable_' + event;
998
+ window.addEventListener(eventName, callback);
999
+ return () => window.removeEventListener(eventName, callback);
1000
+ }, []);
1001
+
1002
+ return value;
1003
+ };
1004
+
1005
+ LeRed.trigger = (event, value) =>
1006
+ {
1007
+ if(typeof window === 'undefined')
1008
+ {
1009
+ return;
1010
+ }
1011
+ const eventName = 'lowentrytriggerable_' + event;
1012
+ window.dispatchEvent(new CustomEvent(eventName, {detail:value}));
1013
+ };
1014
+
954
1015
 
955
1016
  LeRed.Root = LeRed.memo(({store, children}) =>
956
1017
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lowentry/react-redux",
3
- "version": "1.2.4",
3
+ "version": "1.4.1",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Provides utilities for React and Redux.",