@aweebit/react-essentials 0.7.0 → 0.8.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.
- package/LICENSE +0 -2
- package/README.md +392 -237
- package/dist/hooks/useEventListener.d.ts +63 -43
- package/dist/hooks/useEventListener.d.ts.map +1 -1
- package/dist/hooks/useEventListener.js +26 -26
- package/dist/hooks/useEventListener.js.map +1 -1
- package/dist/hooks/useForceUpdate.d.ts +4 -48
- package/dist/hooks/useForceUpdate.d.ts.map +1 -1
- package/dist/hooks/useForceUpdate.js +4 -48
- package/dist/hooks/useForceUpdate.js.map +1 -1
- package/dist/hooks/useReducerWithDeps.d.ts +7 -0
- package/dist/hooks/useReducerWithDeps.d.ts.map +1 -1
- package/dist/hooks/useReducerWithDeps.js +11 -4
- package/dist/hooks/useReducerWithDeps.js.map +1 -1
- package/dist/hooks/useStateWithDeps.d.ts +6 -7
- package/dist/hooks/useStateWithDeps.d.ts.map +1 -1
- package/dist/hooks/useStateWithDeps.js +14 -45
- package/dist/hooks/useStateWithDeps.js.map +1 -1
- package/dist/misc/createSafeContext.d.ts +8 -2
- package/dist/misc/createSafeContext.d.ts.map +1 -1
- package/dist/misc/createSafeContext.js +3 -0
- package/dist/misc/createSafeContext.js.map +1 -1
- package/dist/utils.d.ts +0 -3
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +0 -4
- package/dist/utils.js.map +1 -1
- package/package.json +1 -1
- package/src/hooks/useEventListener.ts +110 -124
- package/src/hooks/useForceUpdate.ts +4 -48
- package/src/hooks/useReducerWithDeps.ts +11 -4
- package/src/hooks/useStateWithDeps.ts +14 -49
- package/src/misc/createSafeContext.ts +8 -2
- package/src/utils.ts +0 -8
|
@@ -1,19 +1,72 @@
|
|
|
1
|
+
import { type RefObject } from 'react';
|
|
1
2
|
/**
|
|
2
|
-
*
|
|
3
|
+
* The type of {@linkcode useEventListener}
|
|
3
4
|
*
|
|
4
|
-
* @
|
|
5
|
-
* @
|
|
5
|
+
* @see
|
|
6
|
+
* {@linkcode useEventListener},
|
|
7
|
+
* {@linkcode UseEventListenerWithImplicitWindowTarget},
|
|
8
|
+
* {@linkcode UseEventListenerWithExplicitGlobalTarget},
|
|
9
|
+
* {@linkcode UseEventListenerWithAnyExplicitTarget}
|
|
6
10
|
*/
|
|
7
|
-
type
|
|
8
|
-
|
|
11
|
+
export type UseEventListener = UseEventListenerWithImplicitWindowTarget & UseEventListenerWithExplicitGlobalTarget & UseEventListenerWithAnyExplicitTarget;
|
|
12
|
+
/**
|
|
13
|
+
* @see
|
|
14
|
+
* {@linkcode useEventListener},
|
|
15
|
+
* {@linkcode UseEventListenerWithImplicitWindowTargetArgs}
|
|
16
|
+
*/
|
|
17
|
+
export type UseEventListenerWithImplicitWindowTarget = <K extends keyof WindowEventMap>(...args: UseEventListenerWithImplicitWindowTargetArgs<K>) => void;
|
|
18
|
+
/**
|
|
19
|
+
* @see
|
|
20
|
+
* {@linkcode useEventListener},
|
|
21
|
+
* {@linkcode UseEventListenerWithExplicitTarget}
|
|
22
|
+
*/
|
|
23
|
+
export type UseEventListenerWithExplicitGlobalTarget = UseEventListenerWithExplicitTarget<Window, WindowEventMap> & UseEventListenerWithExplicitTarget<Document, DocumentEventMap> & UseEventListenerWithExplicitTarget<HTMLElement, HTMLElementEventMap> & UseEventListenerWithExplicitTarget<SVGElement, SVGElementEventMap> & UseEventListenerWithExplicitTarget<MathMLElement, MathMLElementEventMap>;
|
|
24
|
+
/**
|
|
25
|
+
* @see
|
|
26
|
+
* {@linkcode useEventListener},
|
|
27
|
+
* {@linkcode UseEventListenerWithExplicitTargetArgs}
|
|
28
|
+
*/
|
|
29
|
+
export type UseEventListenerWithExplicitTarget<Target extends EventTarget, EventMap = Record<string, Event>> = <T extends Target, K extends keyof EventMap>(...args: UseEventListenerWithExplicitTargetArgs<EventMap, T, K>) => void;
|
|
30
|
+
/**
|
|
31
|
+
* @see
|
|
32
|
+
* {@linkcode useEventListener},
|
|
33
|
+
* {@linkcode UseEventListenerWithExplicitTarget}
|
|
34
|
+
*/
|
|
35
|
+
export type UseEventListenerWithAnyExplicitTarget = UseEventListenerWithExplicitTarget<EventTarget>;
|
|
36
|
+
/**
|
|
37
|
+
* @see
|
|
38
|
+
* {@linkcode useEventListener},
|
|
39
|
+
* {@linkcode UseEventListenerWithExplicitTargetArgs}
|
|
40
|
+
*/
|
|
41
|
+
export type UseEventListenerWithImplicitWindowTargetArgs<K extends keyof WindowEventMap> = UseEventListenerWithExplicitTargetArgs<WindowEventMap, Window, K> extends [
|
|
42
|
+
unknown,
|
|
43
|
+
...infer Args
|
|
44
|
+
] ? Args : never;
|
|
45
|
+
/**
|
|
46
|
+
* @see
|
|
47
|
+
* {@linkcode useEventListener}
|
|
48
|
+
*/
|
|
49
|
+
export type UseEventListenerWithExplicitTargetArgs<EventMap, T extends EventTarget, K extends keyof EventMap> = [
|
|
50
|
+
target: T | (RefObject<T> & {
|
|
51
|
+
addEventListener?: never;
|
|
52
|
+
}) | null,
|
|
9
53
|
eventName: K,
|
|
10
54
|
handler: (this: NoInfer<T>, event: EventMap[K]) => void,
|
|
11
|
-
options?: AddEventListenerOptions | boolean
|
|
55
|
+
options?: AddEventListenerOptions | boolean | undefined
|
|
12
56
|
];
|
|
13
57
|
/**
|
|
14
58
|
* Adds `handler` as a listener for the event `eventName` of `target` with the
|
|
15
59
|
* provided `options` applied
|
|
16
60
|
*
|
|
61
|
+
* The following call signatures are available:
|
|
62
|
+
*
|
|
63
|
+
* ```ts
|
|
64
|
+
* function useEventListener(eventName, handler, options?): void;
|
|
65
|
+
* function useEventListener(target, eventName, handler, options?): void;
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* For the full definition of the hook's type, see {@linkcode UseEventListener}.
|
|
69
|
+
*
|
|
17
70
|
* If `target` is not provided, `window` is used instead.
|
|
18
71
|
*
|
|
19
72
|
* If `target` is `null`, no event listener is added. This is useful when
|
|
@@ -31,44 +84,11 @@ type UseEventListenerOverloadArgs<EventMap, K extends keyof EventMap, T extends
|
|
|
31
84
|
* });
|
|
32
85
|
*
|
|
33
86
|
* const buttonRef = useRef<HTMLButtonElement>(null);
|
|
34
|
-
* useEventListener(buttonRef
|
|
87
|
+
* useEventListener(buttonRef, 'click', () => console.log('click'));
|
|
35
88
|
* ```
|
|
36
89
|
*
|
|
37
|
-
* @
|
|
38
|
-
|
|
39
|
-
export declare function useEventListener<K extends keyof WindowEventMap>(eventName: K, handler: (this: Window, event: WindowEventMap[K]) => void, options?: AddEventListenerOptions | boolean): void;
|
|
40
|
-
/**
|
|
41
|
-
* @see {@linkcode useEventListener}
|
|
42
|
-
* @ignore
|
|
43
|
-
*/
|
|
44
|
-
export declare function useEventListener<K extends keyof WindowEventMap>(...args: UseEventListenerOverloadArgs<WindowEventMap, K, Window>): void;
|
|
45
|
-
/**
|
|
46
|
-
* @see {@linkcode useEventListener}
|
|
47
|
-
* @ignore
|
|
48
|
-
*/
|
|
49
|
-
export declare function useEventListener<K extends keyof DocumentEventMap>(...args: UseEventListenerOverloadArgs<DocumentEventMap, K, Document>): void;
|
|
50
|
-
/**
|
|
51
|
-
* @see {@linkcode useEventListener}
|
|
52
|
-
* @ignore
|
|
53
|
-
*/
|
|
54
|
-
export declare function useEventListener<K extends keyof HTMLElementEventMap, T extends HTMLElement>(...args: UseEventListenerOverloadArgs<HTMLElementEventMap, K, T>): void;
|
|
55
|
-
/**
|
|
56
|
-
* @see {@linkcode useEventListener}
|
|
57
|
-
* @ignore
|
|
58
|
-
*/
|
|
59
|
-
export declare function useEventListener<K extends keyof SVGElementEventMap, T extends SVGElement>(...args: UseEventListenerOverloadArgs<SVGElementEventMap, K, T>): void;
|
|
60
|
-
/**
|
|
61
|
-
* @see {@linkcode useEventListener}
|
|
62
|
-
* @ignore
|
|
63
|
-
*/
|
|
64
|
-
export declare function useEventListener<K extends keyof MathMLElementEventMap, T extends MathMLElement>(...args: UseEventListenerOverloadArgs<MathMLElementEventMap, K, T>): void;
|
|
65
|
-
/**
|
|
66
|
-
* @see {@linkcode useEventListener}
|
|
67
|
-
*/
|
|
68
|
-
export declare function useEventListener<K extends keyof WindowEventMap>(eventName: K, handler: (this: Window, event: WindowEventMap[K]) => void, options?: AddEventListenerOptions | boolean): void;
|
|
69
|
-
/**
|
|
70
|
-
* @see {@linkcode useEventListener}
|
|
90
|
+
* @see
|
|
91
|
+
* {@linkcode UseEventListener}
|
|
71
92
|
*/
|
|
72
|
-
export declare
|
|
73
|
-
export {};
|
|
93
|
+
export declare const useEventListener: UseEventListener;
|
|
74
94
|
//# sourceMappingURL=useEventListener.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useEventListener.d.ts","sourceRoot":"","sources":["../../src/hooks/useEventListener.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"useEventListener.d.ts","sourceRoot":"","sources":["../../src/hooks/useEventListener.ts"],"names":[],"mappings":"AAAA,OAAO,EAA8B,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAEnE;;;;;;;;GAQG;AACH,MAAM,MAAM,gBAAgB,GAAG,wCAAwC,GACrE,wCAAwC,GACxC,qCAAqC,CAAC;AAExC;;;;GAIG;AACH,MAAM,MAAM,wCAAwC,GAAG,CACrD,CAAC,SAAS,MAAM,cAAc,EAE9B,GAAG,IAAI,EAAE,4CAA4C,CAAC,CAAC,CAAC,KACrD,IAAI,CAAC;AAEV;;;;GAIG;AACH,MAAM,MAAM,wCAAwC,GAClD,kCAAkC,CAAC,MAAM,EAAE,cAAc,CAAC,GACxD,kCAAkC,CAAC,QAAQ,EAAE,gBAAgB,CAAC,GAC9D,kCAAkC,CAAC,WAAW,EAAE,mBAAmB,CAAC,GACpE,kCAAkC,CAAC,UAAU,EAAE,kBAAkB,CAAC,GAClE,kCAAkC,CAAC,aAAa,EAAE,qBAAqB,CAAC,CAAC;AAE7E;;;;GAIG;AACH,MAAM,MAAM,kCAAkC,CAC5C,MAAM,SAAS,WAAW,EAC1B,QAAQ,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,IAC9B,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,QAAQ,EAC7C,GAAG,IAAI,EAAE,sCAAsC,CAAC,QAAQ,EAAE,CAAC,EAAE,CAAC,CAAC,KAC5D,IAAI,CAAC;AAEV;;;;GAIG;AACH,MAAM,MAAM,qCAAqC,GAC/C,kCAAkC,CAAC,WAAW,CAAC,CAAC;AAElD;;;;GAIG;AACH,MAAM,MAAM,4CAA4C,CACtD,CAAC,SAAS,MAAM,cAAc,IAE9B,sCAAsC,CAAC,cAAc,EAAE,MAAM,EAAE,CAAC,CAAC,SAAS;IACxE,OAAO;IACP,GAAG,MAAM,IAAI;CACd,GACG,IAAI,GACJ,KAAK,CAAC;AAEZ;;;GAGG;AACH,MAAM,MAAM,sCAAsC,CAChD,QAAQ,EACR,CAAC,SAAS,WAAW,EACrB,CAAC,SAAS,MAAM,QAAQ,IACtB;IACF,MAAM,EAAE,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG;QAAE,gBAAgB,CAAC,EAAE,KAAK,CAAA;KAAE,CAAC,GAAG,IAAI;IAChE,SAAS,EAAE,CAAC;IACZ,OAAO,EAAE,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,KAAK,IAAI;IACvD,OAAO,CAAC,EAAE,uBAAuB,GAAG,OAAO,GAAG,SAAS;CACxD,CAAC;AAYF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,eAAO,MAAM,gBAAgB,EAAE,gBAoDV,CAAC"}
|
|
@@ -1,14 +1,17 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file Based on {@link https://github.com/juliencrn/usehooks-ts}
|
|
3
|
-
*
|
|
4
|
-
* @license MIT
|
|
5
|
-
* @copyright 2020 Julien CARON
|
|
6
|
-
*/
|
|
7
1
|
import { useEffect, useMemo, useRef } from 'react';
|
|
8
2
|
/**
|
|
9
3
|
* Adds `handler` as a listener for the event `eventName` of `target` with the
|
|
10
4
|
* provided `options` applied
|
|
11
5
|
*
|
|
6
|
+
* The following call signatures are available:
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* function useEventListener(eventName, handler, options?): void;
|
|
10
|
+
* function useEventListener(target, eventName, handler, options?): void;
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* For the full definition of the hook's type, see {@linkcode UseEventListener}.
|
|
14
|
+
*
|
|
12
15
|
* If `target` is not provided, `window` is used instead.
|
|
13
16
|
*
|
|
14
17
|
* If `target` is `null`, no event listener is added. This is useful when
|
|
@@ -26,40 +29,37 @@ import { useEffect, useMemo, useRef } from 'react';
|
|
|
26
29
|
* });
|
|
27
30
|
*
|
|
28
31
|
* const buttonRef = useRef<HTMLButtonElement>(null);
|
|
29
|
-
* useEventListener(buttonRef
|
|
32
|
+
* useEventListener(buttonRef, 'click', () => console.log('click'));
|
|
30
33
|
* ```
|
|
34
|
+
*
|
|
35
|
+
* @see
|
|
36
|
+
* {@linkcode UseEventListener}
|
|
31
37
|
*/
|
|
32
|
-
export function useEventListener(...args) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (typeof args[0] === 'string') {
|
|
38
|
-
[eventName, handler, options] = args;
|
|
39
|
-
}
|
|
40
|
-
else {
|
|
41
|
-
[target, eventName, handler, options] =
|
|
42
|
-
args;
|
|
43
|
-
}
|
|
38
|
+
export const useEventListener = function useEventListener(...args) {
|
|
39
|
+
const [target, eventName, handler, options] = typeof args[0] === 'string'
|
|
40
|
+
? [window, ...args]
|
|
41
|
+
: args;
|
|
42
|
+
const unwrappedTarget = target && !('addEventListener' in target) ? target.current : target;
|
|
44
43
|
const handlerRef = useRef(handler);
|
|
45
|
-
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
handlerRef.current = handler;
|
|
46
|
+
}, [handler]);
|
|
46
47
|
const { capture = false, once = false, passive, signal, } = typeof options === 'boolean' ? { capture: options } : (options ?? {});
|
|
47
48
|
const memoizedOptions = useMemo(() => options,
|
|
48
49
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
49
50
|
[capture, once, passive, signal]);
|
|
50
51
|
useEffect(() => {
|
|
51
|
-
if (
|
|
52
|
+
if (unwrappedTarget === null) {
|
|
52
53
|
// No element has been attached to the ref yet
|
|
53
54
|
return;
|
|
54
55
|
}
|
|
55
|
-
const definedTarget = target ?? window;
|
|
56
56
|
const listener = function (event) {
|
|
57
57
|
handlerRef.current.call(this, event);
|
|
58
58
|
};
|
|
59
|
-
|
|
59
|
+
unwrappedTarget.addEventListener(eventName, listener, memoizedOptions);
|
|
60
60
|
return () => {
|
|
61
|
-
|
|
61
|
+
unwrappedTarget.removeEventListener(eventName, listener, memoizedOptions);
|
|
62
62
|
};
|
|
63
|
-
}, [
|
|
64
|
-
}
|
|
63
|
+
}, [unwrappedTarget, eventName, memoizedOptions]);
|
|
64
|
+
};
|
|
65
65
|
//# sourceMappingURL=useEventListener.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useEventListener.js","sourceRoot":"","sources":["../../src/hooks/useEventListener.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"useEventListener.js","sourceRoot":"","sources":["../../src/hooks/useEventListener.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAkB,MAAM,OAAO,CAAC;AAkGnE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAqB,SAAS,gBAAgB,CACzE,GAAG,IAE0C;IAE7C,MAAM,CAAC,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,CAAC,GAMzC,OAAO,IAAI,CAAC,CAAC,CAAC,KAAK,QAAQ;QACzB,CAAC,CAAC,CAAC,MAAM,EAAE,GAAI,IAAwD,CAAC;QACxE,CAAC,CAAE,IAAkD,CAAC;IAE1D,MAAM,eAAe,GACnB,MAAM,IAAI,CAAC,CAAC,kBAAkB,IAAI,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC;IAEtE,MAAM,UAAU,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC;IACnC,SAAS,CAAC,GAAG,EAAE;QACb,UAAU,CAAC,OAAO,GAAG,OAAO,CAAC;IAC/B,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;IAEd,MAAM,EACJ,OAAO,GAAG,KAAK,EACf,IAAI,GAAG,KAAK,EACZ,OAAO,EACP,MAAM,GACP,GAAG,OAAO,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO,IAAI,EAAE,CAAC,CAAC;IAE1E,MAAM,eAAe,GAAG,OAAO,CAC7B,GAAG,EAAE,CAAC,OAAO;IACb,uDAAuD;IACvD,CAAC,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,CAAC,CACjC,CAAC;IAEF,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,eAAe,KAAK,IAAI,EAAE,CAAC;YAC7B,8CAA8C;YAC9C,OAAO;QACT,CAAC;QAED,MAAM,QAAQ,GAAmB,UAAU,KAAK;YAC9C,UAAU,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;QACvC,CAAC,CAAC;QAEF,eAAe,CAAC,gBAAgB,CAAC,SAAS,EAAE,QAAQ,EAAE,eAAe,CAAC,CAAC;QAEvE,OAAO,GAAG,EAAE;YACV,eAAe,CAAC,mBAAmB,CAAC,SAAS,EAAE,QAAQ,EAAE,eAAe,CAAC,CAAC;QAC5E,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,eAAe,EAAE,SAAS,EAAE,eAAe,CAAC,CAAC,CAAC;AACpD,CAAqB,CAAC"}
|
|
@@ -4,54 +4,10 @@
|
|
|
4
4
|
* This hook is designed in the most general way possible in order to cover all
|
|
5
5
|
* imaginable use cases.
|
|
6
6
|
*
|
|
7
|
-
* @
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* Here is an example of a scenario where that can make sense:
|
|
12
|
-
*
|
|
13
|
-
* ```tsx
|
|
14
|
-
* type SensorData = { timestamp: number; value: number };
|
|
15
|
-
* const sensorDataRef = useRef<SensorData[]>([]);
|
|
16
|
-
* const mostRecentSensorDataTimestampRef = useRef<number>(0);
|
|
17
|
-
*
|
|
18
|
-
* const [forceUpdate, updateCount] = useForceUpdate();
|
|
19
|
-
* // Limiting the frequency of forced re-renders with some throttle function:
|
|
20
|
-
* const throttledForceUpdateRef = useRef(throttle(forceUpdate));
|
|
21
|
-
*
|
|
22
|
-
* useEffect(() => {
|
|
23
|
-
* return sensorDataObservable.subscribe((data: SensorData) => {
|
|
24
|
-
* // Imagine new sensor data arrives every 1 millisecond. If we were following
|
|
25
|
-
* // React's immutability rules by creating a new array every time, the data
|
|
26
|
-
* // that's already there would have to be copied many times before the new
|
|
27
|
-
* // data would even get a chance to be reflected in the UI for the first time
|
|
28
|
-
* // because it typically takes much longer than 1 millisecond for a new frame
|
|
29
|
-
* // to be displayed. To prevent the waste of computational resources, we just
|
|
30
|
-
* // mutate the existing array every time instead:
|
|
31
|
-
* sensorDataRef.current.push(data);
|
|
32
|
-
* if (data.timestamp > mostRecentSensorDataTimestampRef.current) {
|
|
33
|
-
* mostRecentSensorDataTimestampRef.current = data.timestamp;
|
|
34
|
-
* }
|
|
35
|
-
* throttledForceUpdateRef.current();
|
|
36
|
-
* });
|
|
37
|
-
* }, []);
|
|
38
|
-
*
|
|
39
|
-
* const [timeWindow, setTimeWindow] = useState(1000);
|
|
40
|
-
* const selectedSensorData = useMemo(
|
|
41
|
-
* () => {
|
|
42
|
-
* // Keep this line if you don't want to disable the
|
|
43
|
-
* // react-hooks/exhaustive-deps ESLint rule:
|
|
44
|
-
* updateCount;
|
|
45
|
-
* const threshold = mostRecentSensorDataTimestampRef.current - timeWindow;
|
|
46
|
-
* return sensorDataRef.current.filter(
|
|
47
|
-
* ({ timestamp }) => timestamp >= threshold,
|
|
48
|
-
* );
|
|
49
|
-
* },
|
|
50
|
-
* // sensorDataRef.current always references the same array, so listing it as a
|
|
51
|
-
* // dependency is pointless. Instead, updateCount should be used:
|
|
52
|
-
* [updateCount, timeWindow],
|
|
53
|
-
* );
|
|
54
|
-
* ```
|
|
7
|
+
* @deprecated
|
|
8
|
+
* This hook encourages patterns that are unsafe in Concurrent React.
|
|
9
|
+
* For details and ideas on how to get rid of it, please check the discussion at
|
|
10
|
+
* https://www.reddit.com/r/react/comments/1nqcsri/comment/ng76cv5/.
|
|
55
11
|
*
|
|
56
12
|
* @param callback An optional callback function to call during renders that
|
|
57
13
|
* were triggered with `forceUpdate()`
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useForceUpdate.d.ts","sourceRoot":"","sources":["../../src/hooks/useForceUpdate.ts"],"names":[],"mappings":"AAOA
|
|
1
|
+
{"version":3,"file":"useForceUpdate.d.ts","sourceRoot":"","sources":["../../src/hooks/useForceUpdate.ts"],"names":[],"mappings":"AAOA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,cAAc,CAAC,QAAQ,CAAC,EAAE,MAAM,IAAI,GAAG,CAAC,MAAM,IAAI,EAAE,MAAM,CAAC,CAU1E"}
|
|
@@ -6,54 +6,10 @@ import { useReducer, useRef } from 'react';
|
|
|
6
6
|
* This hook is designed in the most general way possible in order to cover all
|
|
7
7
|
* imaginable use cases.
|
|
8
8
|
*
|
|
9
|
-
* @
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* Here is an example of a scenario where that can make sense:
|
|
14
|
-
*
|
|
15
|
-
* ```tsx
|
|
16
|
-
* type SensorData = { timestamp: number; value: number };
|
|
17
|
-
* const sensorDataRef = useRef<SensorData[]>([]);
|
|
18
|
-
* const mostRecentSensorDataTimestampRef = useRef<number>(0);
|
|
19
|
-
*
|
|
20
|
-
* const [forceUpdate, updateCount] = useForceUpdate();
|
|
21
|
-
* // Limiting the frequency of forced re-renders with some throttle function:
|
|
22
|
-
* const throttledForceUpdateRef = useRef(throttle(forceUpdate));
|
|
23
|
-
*
|
|
24
|
-
* useEffect(() => {
|
|
25
|
-
* return sensorDataObservable.subscribe((data: SensorData) => {
|
|
26
|
-
* // Imagine new sensor data arrives every 1 millisecond. If we were following
|
|
27
|
-
* // React's immutability rules by creating a new array every time, the data
|
|
28
|
-
* // that's already there would have to be copied many times before the new
|
|
29
|
-
* // data would even get a chance to be reflected in the UI for the first time
|
|
30
|
-
* // because it typically takes much longer than 1 millisecond for a new frame
|
|
31
|
-
* // to be displayed. To prevent the waste of computational resources, we just
|
|
32
|
-
* // mutate the existing array every time instead:
|
|
33
|
-
* sensorDataRef.current.push(data);
|
|
34
|
-
* if (data.timestamp > mostRecentSensorDataTimestampRef.current) {
|
|
35
|
-
* mostRecentSensorDataTimestampRef.current = data.timestamp;
|
|
36
|
-
* }
|
|
37
|
-
* throttledForceUpdateRef.current();
|
|
38
|
-
* });
|
|
39
|
-
* }, []);
|
|
40
|
-
*
|
|
41
|
-
* const [timeWindow, setTimeWindow] = useState(1000);
|
|
42
|
-
* const selectedSensorData = useMemo(
|
|
43
|
-
* () => {
|
|
44
|
-
* // Keep this line if you don't want to disable the
|
|
45
|
-
* // react-hooks/exhaustive-deps ESLint rule:
|
|
46
|
-
* updateCount;
|
|
47
|
-
* const threshold = mostRecentSensorDataTimestampRef.current - timeWindow;
|
|
48
|
-
* return sensorDataRef.current.filter(
|
|
49
|
-
* ({ timestamp }) => timestamp >= threshold,
|
|
50
|
-
* );
|
|
51
|
-
* },
|
|
52
|
-
* // sensorDataRef.current always references the same array, so listing it as a
|
|
53
|
-
* // dependency is pointless. Instead, updateCount should be used:
|
|
54
|
-
* [updateCount, timeWindow],
|
|
55
|
-
* );
|
|
56
|
-
* ```
|
|
9
|
+
* @deprecated
|
|
10
|
+
* This hook encourages patterns that are unsafe in Concurrent React.
|
|
11
|
+
* For details and ideas on how to get rid of it, please check the discussion at
|
|
12
|
+
* https://www.reddit.com/r/react/comments/1nqcsri/comment/ng76cv5/.
|
|
57
13
|
*
|
|
58
14
|
* @param callback An optional callback function to call during renders that
|
|
59
15
|
* were triggered with `forceUpdate()`
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useForceUpdate.js","sourceRoot":"","sources":["../../src/hooks/useForceUpdate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAK3C,mBAAmB;AAEnB
|
|
1
|
+
{"version":3,"file":"useForceUpdate.js","sourceRoot":"","sources":["../../src/hooks/useForceUpdate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAK3C,mBAAmB;AAEnB;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,UAAU,cAAc,CAAC,QAAqB;IAClD,6DAA6D;IAC7D,2EAA2E;IAC3E,MAAM,CAAC,WAAW,EAAE,WAAW,CAAC,GAAG,UAAU,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,GAAG,EAAE,EAAE,EAAE,CAAC,CAAC;IACvE,MAAM,cAAc,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC;IAC3C,IAAI,WAAW,KAAK,cAAc,CAAC,OAAO,EAAE,CAAC;QAC3C,cAAc,CAAC,OAAO,GAAG,WAAW,CAAC;QACrC,QAAQ,EAAE,EAAE,CAAC;IACf,CAAC;IACD,OAAO,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC;AACpC,CAAC"}
|
|
@@ -7,6 +7,13 @@ export type ActionDispatch<ActionArg extends AnyActionArg> = (...args: ActionArg
|
|
|
7
7
|
* `useReducer` hook with an additional dependency array `deps` that resets the
|
|
8
8
|
* state to `initialState` when dependencies change
|
|
9
9
|
*
|
|
10
|
+
* This hook is the reducer pattern counterpart of {@linkcode useStateWithDeps}.
|
|
11
|
+
*
|
|
12
|
+
* Due to React's limitations, a change in dependencies always causes two
|
|
13
|
+
* renders when using this hook. The result of the first render is thrown away
|
|
14
|
+
* as described in
|
|
15
|
+
* [useState > Storing information from previous renders](https://react.dev/reference/react/useState#storing-information-from-previous-renders).
|
|
16
|
+
*
|
|
10
17
|
* For motivation and examples, see
|
|
11
18
|
* https://github.com/facebook/react/issues/33041.
|
|
12
19
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useReducerWithDeps.d.ts","sourceRoot":"","sources":["../../src/hooks/useReducerWithDeps.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,
|
|
1
|
+
{"version":3,"file":"useReducerWithDeps.d.ts","sourceRoot":"","sources":["../../src/hooks/useReducerWithDeps.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAU,MAAM,OAAO,CAAC;AAOpD,cAAc;AAEd,MAAM,MAAM,YAAY,GAAG,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;AAEtC,cAAc;AACd,MAAM,MAAM,cAAc,CAAC,SAAS,SAAS,YAAY,IAAI,CAC3D,GAAG,IAAI,EAAE,SAAS,KACf,IAAI,CAAC;AAEV;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,CAAC,SAAS,YAAY,EAC1D,OAAO,EAAE,CAAC,SAAS,EAAE,CAAC,EAAE,GAAG,IAAI,EAAE,CAAC,KAAK,CAAC,EACxC,YAAY,EAAE,CAAC,GAAG,CAAC,CAAC,aAAa,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,EAC5C,IAAI,EAAE,cAAc,GACnB,CAAC,CAAC,EAAE,cAAc,CAAC,CAAC,CAAC,CAAC,CAYxB"}
|
|
@@ -1,9 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useRef } from 'react';
|
|
2
2
|
import { useStateWithDeps } from './useStateWithDeps.js';
|
|
3
3
|
/**
|
|
4
4
|
* `useReducer` hook with an additional dependency array `deps` that resets the
|
|
5
5
|
* state to `initialState` when dependencies change
|
|
6
6
|
*
|
|
7
|
+
* This hook is the reducer pattern counterpart of {@linkcode useStateWithDeps}.
|
|
8
|
+
*
|
|
9
|
+
* Due to React's limitations, a change in dependencies always causes two
|
|
10
|
+
* renders when using this hook. The result of the first render is thrown away
|
|
11
|
+
* as described in
|
|
12
|
+
* [useState > Storing information from previous renders](https://react.dev/reference/react/useState#storing-information-from-previous-renders).
|
|
13
|
+
*
|
|
7
14
|
* For motivation and examples, see
|
|
8
15
|
* https://github.com/facebook/react/issues/33041.
|
|
9
16
|
*
|
|
@@ -40,9 +47,9 @@ export function useReducerWithDeps(reducer, initialState, deps) {
|
|
|
40
47
|
const [state, setState] = useStateWithDeps(initialState, deps);
|
|
41
48
|
// Only the initially provided reducer is used
|
|
42
49
|
const reducerRef = useRef(reducer);
|
|
43
|
-
|
|
50
|
+
function dispatch(...args) {
|
|
44
51
|
setState((previousState) => reducerRef.current(previousState, ...args));
|
|
45
|
-
}
|
|
46
|
-
return [state, dispatch];
|
|
52
|
+
}
|
|
53
|
+
return [state, useRef(dispatch).current];
|
|
47
54
|
}
|
|
48
55
|
//# sourceMappingURL=useReducerWithDeps.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useReducerWithDeps.js","sourceRoot":"","sources":["../../src/hooks/useReducerWithDeps.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,
|
|
1
|
+
{"version":3,"file":"useReducerWithDeps.js","sourceRoot":"","sources":["../../src/hooks/useReducerWithDeps.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,MAAM,EAAE,MAAM,OAAO,CAAC;AACpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAezD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,MAAM,UAAU,kBAAkB,CAChC,OAAwC,EACxC,YAA4C,EAC5C,IAAoB;IAEpB,uDAAuD;IACvD,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,gBAAgB,CAAC,YAAY,EAAE,IAAI,CAAC,CAAC;IAE/D,8CAA8C;IAC9C,MAAM,UAAU,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC;IAEnC,SAAS,QAAQ,CAAC,GAAG,IAAO;QAC1B,QAAQ,CAAC,CAAC,aAAa,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC,aAAa,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC;IAC1E,CAAC;IAED,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,CAAC;AAC3C,CAAC"}
|
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file Based on {@link https://github.com/peterjuras/use-state-with-deps}
|
|
3
|
-
*
|
|
4
|
-
* @license MIT
|
|
5
|
-
* @copyright 2020 Peter Juras
|
|
6
|
-
*/
|
|
7
1
|
import { type DependencyList, type Dispatch, type SetStateAction } from 'react';
|
|
8
2
|
/**
|
|
9
3
|
* `useState` hook with an additional dependency array `deps` that resets the
|
|
10
4
|
* state to `initialState` when dependencies change
|
|
11
5
|
*
|
|
6
|
+
* Due to React's limitations, a change in dependencies always causes two
|
|
7
|
+
* renders when using this hook. The result of the first render is thrown away
|
|
8
|
+
* as described in
|
|
9
|
+
* [useState > Storing information from previous renders](https://react.dev/reference/react/useState#storing-information-from-previous-renders).
|
|
10
|
+
*
|
|
12
11
|
* For motivation and more examples, see
|
|
13
12
|
* https://github.com/facebook/react/issues/33041.
|
|
14
13
|
*
|
|
@@ -27,7 +26,7 @@ import { type DependencyList, type Dispatch, type SetStateAction } from 'react';
|
|
|
27
26
|
* evening: ['board games', 'dinner'],
|
|
28
27
|
* };
|
|
29
28
|
*
|
|
30
|
-
*
|
|
29
|
+
* function Example() {
|
|
31
30
|
* const [timeOfDay, setTimeOfDay] = useState<TimeOfDay>('morning');
|
|
32
31
|
*
|
|
33
32
|
* const activityOptions = activityOptionsByTimeOfDay[timeOfDay];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useStateWithDeps.d.ts","sourceRoot":"","sources":["../../src/hooks/useStateWithDeps.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"useStateWithDeps.d.ts","sourceRoot":"","sources":["../../src/hooks/useStateWithDeps.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,cAAc,EACnB,KAAK,QAAQ,EACb,KAAK,cAAc,EACpB,MAAM,OAAO,CAAC;AAGf;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoDG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAChC,YAAY,EAAE,CAAC,GAAG,CAAC,CAAC,aAAa,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,EAC5C,IAAI,EAAE,cAAc,GACnB,CAAC,CAAC,EAAE,QAAQ,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,CAWlC"}
|
|
@@ -1,16 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
* @license MIT
|
|
5
|
-
* @copyright 2020 Peter Juras
|
|
6
|
-
*/
|
|
7
|
-
import { useCallback, useRef, } from 'react';
|
|
8
|
-
import { depsAreEqual, isFunction } from '../utils.js';
|
|
9
|
-
import { useForceUpdate } from './useForceUpdate.js';
|
|
1
|
+
import { useState, } from 'react';
|
|
2
|
+
import { depsAreEqual } from '../utils.js';
|
|
10
3
|
/**
|
|
11
4
|
* `useState` hook with an additional dependency array `deps` that resets the
|
|
12
5
|
* state to `initialState` when dependencies change
|
|
13
6
|
*
|
|
7
|
+
* Due to React's limitations, a change in dependencies always causes two
|
|
8
|
+
* renders when using this hook. The result of the first render is thrown away
|
|
9
|
+
* as described in
|
|
10
|
+
* [useState > Storing information from previous renders](https://react.dev/reference/react/useState#storing-information-from-previous-renders).
|
|
11
|
+
*
|
|
14
12
|
* For motivation and more examples, see
|
|
15
13
|
* https://github.com/facebook/react/issues/33041.
|
|
16
14
|
*
|
|
@@ -29,7 +27,7 @@ import { useForceUpdate } from './useForceUpdate.js';
|
|
|
29
27
|
* evening: ['board games', 'dinner'],
|
|
30
28
|
* };
|
|
31
29
|
*
|
|
32
|
-
*
|
|
30
|
+
* function Example() {
|
|
33
31
|
* const [timeOfDay, setTimeOfDay] = useState<TimeOfDay>('morning');
|
|
34
32
|
*
|
|
35
33
|
* const activityOptions = activityOptionsByTimeOfDay[timeOfDay];
|
|
@@ -56,41 +54,12 @@ import { useForceUpdate } from './useForceUpdate.js';
|
|
|
56
54
|
* @param deps Dependencies that reset the state to `initialState`
|
|
57
55
|
*/
|
|
58
56
|
export function useStateWithDeps(initialState, deps) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const prevDeps = useRef(deps);
|
|
65
|
-
const isMounted = useRef(false);
|
|
66
|
-
// If first render, or if dependencies have changed since last time
|
|
67
|
-
if (!isMounted.current || !depsAreEqual(prevDeps.current, deps)) {
|
|
68
|
-
// Update state and deps
|
|
69
|
-
let nextState;
|
|
70
|
-
if (isFunction(initialState)) {
|
|
71
|
-
nextState = initialState(state.current);
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
nextState = initialState;
|
|
75
|
-
}
|
|
76
|
-
state.current = nextState;
|
|
77
|
-
prevDeps.current = deps;
|
|
78
|
-
isMounted.current = true;
|
|
57
|
+
const [state, setState] = useState(initialState);
|
|
58
|
+
const [prevDeps, setPrevDeps] = useState(deps);
|
|
59
|
+
if (!depsAreEqual(deps, prevDeps)) {
|
|
60
|
+
setPrevDeps(deps);
|
|
61
|
+
setState(initialState);
|
|
79
62
|
}
|
|
80
|
-
|
|
81
|
-
const updateState = useCallback(function updateState(newState) {
|
|
82
|
-
let nextState;
|
|
83
|
-
if (isFunction(newState)) {
|
|
84
|
-
nextState = newState(state.current);
|
|
85
|
-
}
|
|
86
|
-
else {
|
|
87
|
-
nextState = newState;
|
|
88
|
-
}
|
|
89
|
-
if (!Object.is(state.current, nextState)) {
|
|
90
|
-
state.current = nextState;
|
|
91
|
-
forceUpdate();
|
|
92
|
-
}
|
|
93
|
-
}, []); // eslint-disable-line react-hooks/exhaustive-deps
|
|
94
|
-
return [state.current, updateState];
|
|
63
|
+
return [state, setState];
|
|
95
64
|
}
|
|
96
65
|
//# sourceMappingURL=useStateWithDeps.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useStateWithDeps.js","sourceRoot":"","sources":["../../src/hooks/useStateWithDeps.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"useStateWithDeps.js","sourceRoot":"","sources":["../../src/hooks/useStateWithDeps.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,GAIT,MAAM,OAAO,CAAC;AACf,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoDG;AACH,MAAM,UAAU,gBAAgB,CAC9B,YAA4C,EAC5C,IAAoB;IAEpB,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAC,YAAY,CAAC,CAAC;IAEjD,MAAM,CAAC,QAAQ,EAAE,WAAW,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC;IAE/C,IAAI,CAAC,YAAY,CAAC,IAAI,EAAE,QAAQ,CAAC,EAAE,CAAC;QAClC,WAAW,CAAC,IAAI,CAAC,CAAC;QAClB,QAAQ,CAAC,YAAY,CAAC,CAAC;IACzB,CAAC;IAED,OAAO,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;AAC3B,CAAC"}
|
|
@@ -6,7 +6,8 @@ import type { ArgumentFallback } from '../utils.js';
|
|
|
6
6
|
* as an argument to `useContext` or `use` (the hook produced with
|
|
7
7
|
* {@linkcode createSafeContext} should be used instead)
|
|
8
8
|
*
|
|
9
|
-
* @see
|
|
9
|
+
* @see
|
|
10
|
+
* {@linkcode createSafeContext}
|
|
10
11
|
*/
|
|
11
12
|
export type RestrictedContext<T> = Context<T> extends Provider<T> ? {
|
|
12
13
|
Provider: Provider<T>;
|
|
@@ -18,7 +19,9 @@ export type RestrictedContext<T> = Context<T> extends Provider<T> ? {
|
|
|
18
19
|
/**
|
|
19
20
|
* The return type of {@linkcode createSafeContext}
|
|
20
21
|
*
|
|
21
|
-
* @see
|
|
22
|
+
* @see
|
|
23
|
+
* {@linkcode createSafeContext},
|
|
24
|
+
* {@linkcode RestrictedContext}
|
|
22
25
|
*/
|
|
23
26
|
export type SafeContext<DisplayName extends string, T> = {
|
|
24
27
|
[K in `${DisplayName}Context`]: RestrictedContext<T>;
|
|
@@ -80,6 +83,9 @@ export type SafeContext<DisplayName extends string, T> = {
|
|
|
80
83
|
* - ``` `${displayName}Context` ``` (e.g. `DirectionContext`): the context
|
|
81
84
|
* - ``` `use${displayName}` ``` (e.g. `useDirection`): a hook that returns the
|
|
82
85
|
* current context value if one was provided, or throws an error otherwise
|
|
86
|
+
*
|
|
87
|
+
* @see
|
|
88
|
+
* {@linkcode SafeContext}
|
|
83
89
|
*/
|
|
84
90
|
export declare function createSafeContext<T = never>(): <DisplayName extends string>(displayName: [T] extends [never] ? never : ArgumentFallback<DisplayName, never, string>) => SafeContext<DisplayName, T>;
|
|
85
91
|
//# sourceMappingURL=createSafeContext.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createSafeContext.d.ts","sourceRoot":"","sources":["../../src/misc/createSafeContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,OAAO,EAAE,KAAK,QAAQ,EAA6B,MAAM,OAAO,CAAC;AAC/E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAIpD
|
|
1
|
+
{"version":3,"file":"createSafeContext.d.ts","sourceRoot":"","sources":["../../src/misc/createSafeContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,OAAO,EAAE,KAAK,QAAQ,EAA6B,MAAM,OAAO,CAAC;AAC/E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAIpD;;;;;;;;GAQG;AAIH,MAAM,MAAM,iBAAiB,CAAC,CAAC,IAC7B,OAAO,CAAC,CAAC,CAAC,SAAS,QAAQ,CAAC,CAAC,CAAC,GAC1B;IAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC;IAAC,WAAW,EAAE,MAAM,CAAA;CAAE,GAAG,QAAQ,CAAC,CAAC,CAAC,GAC5D;IAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC;IAAC,WAAW,EAAE,MAAM,CAAA;CAAE,CAAC;AAErD;;;;;;GAMG;AACH,MAAM,MAAM,WAAW,CAAC,WAAW,SAAS,MAAM,EAAE,CAAC,IAAI;KACtD,CAAC,IAAI,GAAG,WAAW,SAAS,GAAG,iBAAiB,CAAC,CAAC,CAAC;CACrD,GAAG;KACD,CAAC,IAAI,MAAM,WAAW,EAAE,GAAG,MAAM,CAAC;CACpC,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0DG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,GAAG,KAAK,MACjC,WAAW,SAAS,MAAM,EAChC,aAAa,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,GAC5B,KAAK,GACL,gBAAgB,CAAC,WAAW,EAAE,KAAK,EAAE,MAAM,CAAC,KAC/C,WAAW,CAAC,WAAW,EAAE,CAAC,CAAC,CAsB/B"}
|
|
@@ -55,6 +55,9 @@ const moValueSymbol = Symbol('noValue');
|
|
|
55
55
|
* - ``` `${displayName}Context` ``` (e.g. `DirectionContext`): the context
|
|
56
56
|
* - ``` `use${displayName}` ``` (e.g. `useDirection`): a hook that returns the
|
|
57
57
|
* current context value if one was provided, or throws an error otherwise
|
|
58
|
+
*
|
|
59
|
+
* @see
|
|
60
|
+
* {@linkcode SafeContext}
|
|
58
61
|
*/
|
|
59
62
|
export function createSafeContext() {
|
|
60
63
|
return (displayName) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createSafeContext.js","sourceRoot":"","sources":["../../src/misc/createSafeContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAA+B,aAAa,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAG/E,MAAM,aAAa,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"createSafeContext.js","sourceRoot":"","sources":["../../src/misc/createSafeContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAA+B,aAAa,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAG/E,MAAM,aAAa,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC;AAgCxC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0DG;AACH,MAAM,UAAU,iBAAiB;IAC/B,OAAO,CACL,WAEgD,EACnB,EAAE;QAC/B,MAAM,WAAW,GAAG,GAAG,WAA0B,SAAkB,CAAC;QACpE,MAAM,QAAQ,GAAG,MAAM,WAA0B,EAAW,CAAC;QAE7D,MAAM,OAAO,GAAG,aAAa,CAA2B,aAAa,CAAC,CAAC;QACvE,OAAO,CAAC,WAAW,GAAG,WAAW,CAAC;QAElC,OAAO;YACL,CAAC,WAAW,CAAC,EAAE,OAA+B;YAC9C,CAAC,QAAQ,CAAC,EAAE,GAAG,EAAE;gBACf,MAAM,KAAK,GAAG,UAAU,CAAC,OAAO,CAAC,CAAC;gBAClC,IAAI,KAAK,KAAK,aAAa,EAAE,CAAC;oBAC5B,MAAM,IAAI,KAAK,CAAC,MAAM,WAAW,qBAAqB,CAAC,CAAC;gBAC1D,CAAC;gBACD,OAAO,KAAK,CAAC;YACf,CAAC;SAKF,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
1
|
import type { DependencyList } from 'react';
|
|
2
|
-
export type Callable = (...args: never) => unknown;
|
|
3
2
|
export type ArgumentFallback<T extends Base, Default extends Base, Base = unknown> = [T] extends [never] ? Default : [Base] extends [T] ? Default : T;
|
|
4
|
-
export declare function noop(): void;
|
|
5
|
-
export declare function isFunction(input: unknown): input is Callable;
|
|
6
3
|
export declare function depsAreEqual(prevDeps: DependencyList, deps: DependencyList): boolean;
|
|
7
4
|
//# sourceMappingURL=utils.d.ts.map
|
package/dist/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,OAAO,CAAC;AAE5C,MAAM,MAAM,
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,OAAO,CAAC;AAE5C,MAAM,MAAM,gBAAgB,CAC1B,CAAC,SAAS,IAAI,EACd,OAAO,SAAS,IAAI,EACpB,IAAI,GAAG,OAAO,IACZ,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,OAAO,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,OAAO,GAAG,CAAC,CAAC;AAErE,wBAAgB,YAAY,CAC1B,QAAQ,EAAE,cAAc,EACxB,IAAI,EAAE,cAAc,GACnB,OAAO,CAKT"}
|