@aweebit/react-essentials 0.8.0 → 0.9.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.
Files changed (36) hide show
  1. package/LICENSE +0 -2
  2. package/README.md +81 -185
  3. package/dist/hooks/index.d.ts +0 -1
  4. package/dist/hooks/index.d.ts.map +1 -1
  5. package/dist/hooks/index.js +0 -1
  6. package/dist/hooks/index.js.map +1 -1
  7. package/dist/hooks/useEventListener.d.ts +12 -11
  8. package/dist/hooks/useEventListener.d.ts.map +1 -1
  9. package/dist/hooks/useEventListener.js +3 -7
  10. package/dist/hooks/useEventListener.js.map +1 -1
  11. package/dist/hooks/useReducerWithDeps.d.ts +7 -0
  12. package/dist/hooks/useReducerWithDeps.d.ts.map +1 -1
  13. package/dist/hooks/useReducerWithDeps.js +10 -3
  14. package/dist/hooks/useReducerWithDeps.js.map +1 -1
  15. package/dist/hooks/useStateWithDeps.d.ts +6 -7
  16. package/dist/hooks/useStateWithDeps.d.ts.map +1 -1
  17. package/dist/hooks/useStateWithDeps.js +14 -45
  18. package/dist/hooks/useStateWithDeps.js.map +1 -1
  19. package/dist/misc/createSafeContext.d.ts +12 -12
  20. package/dist/misc/createSafeContext.d.ts.map +1 -1
  21. package/dist/utils.d.ts +0 -3
  22. package/dist/utils.d.ts.map +1 -1
  23. package/dist/utils.js +0 -4
  24. package/dist/utils.js.map +1 -1
  25. package/package.json +1 -1
  26. package/src/hooks/index.ts +0 -1
  27. package/src/hooks/useEventListener.ts +21 -17
  28. package/src/hooks/useReducerWithDeps.ts +10 -3
  29. package/src/hooks/useStateWithDeps.ts +14 -48
  30. package/src/misc/createSafeContext.ts +13 -13
  31. package/src/utils.ts +0 -8
  32. package/dist/hooks/useForceUpdate.d.ts +0 -75
  33. package/dist/hooks/useForceUpdate.d.ts.map +0 -1
  34. package/dist/hooks/useForceUpdate.js +0 -87
  35. package/dist/hooks/useForceUpdate.js.map +0 -1
  36. package/src/hooks/useForceUpdate.ts +0 -91
@@ -1,91 +0,0 @@
1
- import { useReducer, useRef } from 'react';
2
-
3
- /* eslint-disable */
4
- import type { useStateWithDeps } from './useStateWithDeps.js';
5
- import type { useReducerWithDeps } from './useReducerWithDeps.js';
6
- /* eslint-enable */
7
-
8
- /**
9
- * Enables you to imperatively trigger re-rendering of components
10
- *
11
- * This hook is designed in the most general way possible in order to cover all
12
- * imaginable use cases.
13
- *
14
- * @example
15
- * Sometimes, React's immutability constraints mean too much unnecessary copying
16
- * of data when new data arrives at a high frequency. In such cases, it might be
17
- * desirable to ignore the constraints by embracing imperative patterns.
18
- * Here is an example of a scenario where that can make sense:
19
- *
20
- * ```tsx
21
- * type SensorData = { timestamp: number; value: number };
22
- * const sensorDataRef = useRef<SensorData[]>([]);
23
- * const mostRecentSensorDataTimestampRef = useRef<number>(0);
24
- *
25
- * const [forceUpdate, updateCount] = useForceUpdate();
26
- * // Limiting the frequency of forced re-renders with some throttle function:
27
- * const throttledForceUpdateRef = useRef(throttle(forceUpdate));
28
- *
29
- * useEffect(() => {
30
- * return sensorDataObservable.subscribe((data: SensorData) => {
31
- * // Imagine new sensor data arrives every 1 millisecond. If we were following
32
- * // React's immutability rules by creating a new array every time, the data
33
- * // that's already there would have to be copied many times before the new
34
- * // data would even get a chance to be reflected in the UI for the first time
35
- * // because it typically takes much longer than 1 millisecond for a new frame
36
- * // to be displayed. To prevent the waste of computational resources, we just
37
- * // mutate the existing array every time instead:
38
- * sensorDataRef.current.push(data);
39
- * if (data.timestamp > mostRecentSensorDataTimestampRef.current) {
40
- * mostRecentSensorDataTimestampRef.current = data.timestamp;
41
- * }
42
- * throttledForceUpdateRef.current();
43
- * });
44
- * }, []);
45
- *
46
- * const [timeWindow, setTimeWindow] = useState(1000);
47
- * const selectedSensorData = useMemo(
48
- * () => {
49
- * // Keep this line if you don't want to disable the
50
- * // react-hooks/exhaustive-deps ESLint rule:
51
- * updateCount;
52
- * const threshold = mostRecentSensorDataTimestampRef.current - timeWindow;
53
- * return sensorDataRef.current.filter(
54
- * ({ timestamp }) => timestamp >= threshold,
55
- * );
56
- * },
57
- * // sensorDataRef.current always references the same array, so listing it as a
58
- * // dependency is pointless. Instead, updateCount should be used:
59
- * [updateCount, timeWindow],
60
- * );
61
- * ```
62
- *
63
- * @param callback An optional callback function to call during renders that
64
- * were triggered with `forceUpdate()`
65
- *
66
- * Can be used for conditionally calling state setters when state needs to be
67
- * reset. That is legal and better than using effects (see
68
- * {@link https://react.dev/learn/-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes You Might Not Need an Effect > Adjusting some state when a prop changes}),
69
- * but can often be avoided by using {@linkcode useStateWithDeps} or
70
- * {@linkcode useReducerWithDeps}.
71
- *
72
- * Important: the callback function is called once per render, not once per
73
- * `forceUpdate` call! If React batches `forceUpdate` calls, then it will only
74
- * be called once.
75
- *
76
- * @returns An array with the following two elements:
77
- *
78
- * 1. A `forceUpdate` function that triggers a re-render
79
- * 2. The number of times `forceUpdate` has been called so far
80
- */
81
- export function useForceUpdate(callback?: () => void): [() => void, bigint] {
82
- // It is very unlikely that the number of updates will exceed
83
- // Number.MAX_SAFE_INTEGER, but not impossible. That is why we use bigints.
84
- const [updateCount, forceUpdate] = useReducer((prev) => prev + 1n, 0n);
85
- const updateCountRef = useRef(updateCount);
86
- if (updateCount !== updateCountRef.current) {
87
- updateCountRef.current = updateCount;
88
- callback?.();
89
- }
90
- return [forceUpdate, updateCount];
91
- }