@ptolemy2002/react-proxy-context 1.2.1 → 2.0.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/README.md CHANGED
@@ -2,65 +2,107 @@
2
2
  This library is a solution for React that uses Javascript Proxies to allow React context consumers to listen to mutations as well as reassignments. Another benefit is that you can listen to the mutation of only specific properties. I find that many consider this style more intuitive to work with, though it is considered bad practice by others. A limitation is that it is only able to track direct mutations, not mutations that occur in nested arrays or objects, so those will still require recreation and reassignment.
3
3
 
4
4
  The functions are not exported as default, so you can import them in one of the following ways:
5
- ```
5
+ ```javascript
6
6
  // ES6
7
7
  import { functionName } from '@ptolemy2002/react-proxy-context';
8
8
  // CommonJS
9
9
  const { functionName } = require('@ptolemy2002/react-proxy-context');
10
10
  ```
11
11
 
12
+ ## Type Reference
13
+ ```typescript
14
+ type ContextWithName<T> = React.Context<T> & { name: string };
15
+
16
+ type OnChangePropCallback<T> =
17
+ <K extends keyof T>(prop: K, current: T[K], prev?: T[K]) => void
18
+ ;
19
+ type OnChangeReinitCallback<T> =
20
+ (current: T, prev?: T) => void
21
+ ;
22
+
23
+ type Dependency<T> = keyof T | (
24
+ <K extends keyof T>(prop: K, current: T[K], prev: T[K], obj: T) => boolean
25
+ ) | null | undefined | false;
26
+
27
+ type ProxyContextValue<T> = {
28
+ obj: T;
29
+ set: (newObj: T) => T;
30
+ subscribe: (
31
+ propCallback: OnChangePropCallback<T>,
32
+ reinitCallback: OnChangeReinitCallback<T>,
33
+ deps: Dependency<T>[]
34
+ ) => string;
35
+ unsubscribe: (id: string) => void;
36
+ };
37
+
38
+ type ProxyContextProviderProps<T> = {
39
+ children: ReactNode;
40
+ value: T;
41
+ onChangeProp?: OnChangePropCallback<T>;
42
+ onChangeReinit?: OnChangeReinitCallback<T>;
43
+ proxyRef?: React.MutableRefObject<T>;
44
+ };
45
+
46
+ type UseProxyContextResult<T> = HookResultData<{
47
+ value: T;
48
+ set: (newObj: T) => T;
49
+ }, readonly [T, (newObj: T) => T]>;
50
+ ```
51
+
12
52
  ## Functions
13
53
  The following functions are available in the library:
14
54
 
15
- ### createProxyContext
55
+ ### createProxyContext<T>
16
56
  #### Description
17
- Creates a new instance of the ProxyContext, essentially to be used as the context type, with the specified default value and name. This is effectively just the normal React createContext function, but with a check to ensure that the browser supports Proxies.
57
+ Creates a new instance of the ProxyContext, essentially to be used as the context type, with the specified name. This is effectively just the normal React createContext function, but with a check to ensure that the browser supports Proxies. `T` represents the type of the object that will be stored in the context.
18
58
 
19
59
  #### Parameters
20
- - `defaultValue` (Object): The default value of the context. This is what is reported when the context is not provided.
21
60
  - `name` (String): The name of the context. This is used for debugging purposes.
22
61
 
23
62
  #### Returns
24
- Object - The ProxyContext object, which is a React context object.
63
+ `ContextWithName<ProxyContextValue<T> | undefined>` - The context object that can be used in a provider.
25
64
 
26
- ## Components
27
- The following components are available in the library:
28
-
29
- ### ProxyContextProvider
65
+ ### createProxyContextProvider<T extends object>
30
66
  #### Description
31
- A component that provides context of the specified class to its children using proxies. `useProxyContext` can only be used to access the context provided by this component.
67
+ Creates a new proxy context provider component with the specified type. `ProxyContextProvider` is no longer used due to a TypeScript limitation that prevents the context type from being inferred.
32
68
 
33
- #### Props
34
- - `contextClass` (Object): The class of the context to provide. This is the class that was created using `createProxyContext`.
35
- - `value` (Object): The value of the context. This is what is reported when the context is not provided.
36
- - `onChange` (Function): A function that is called whenever the context is changed. The first parameter is the property that was changed (null if it was reassignment), the second parameter is the current value of the property, and the third parameter is the previous value of the property. This is useful for listening to changes in the provider's parent component.
37
- - `proxyRef` (Object): A ref object that is assigned the proxy object of the context. This is useful for accessing the proxy object directly by the provider's parent component.
69
+ #### Parameters
70
+ - `contextClass` (`ContextWithName<ProxyContextValue<T> | undefined>`): The context class that was created using `createProxyContext`.
71
+
72
+ #### Returns
73
+ `React.MemoExoticComponent<FunctionComponent<ProxyContextProviderProps<T> & { renderDeps?: any[] }>>` - The provider component that can be used in the React tree. The resulting component is memoized to prevent unnecessary re-renders, but the `renderDeps` prop can be used to force a re-render when the specified dependencies change (necessary when working with the children prop).
74
+
75
+ The component has the following other props:
76
+ - `value` (T): The value of the context. This is what is reported when the context is not provided.
77
+ - `onChangeProp` (`OnChangePropCallback<T>`): A function that is called whenever a property of the context is changed. The first parameter is the property that was changed, the second parameter is the current value of the property, and the third parameter is the previous value of the property. This is useful for listening to changes in the provider's parent component.
78
+ - `onChangeReinit` (`OnChangeReinitCallback<T>`): A function that is called whenever the context is reinitialized. The first parameter is the current value of the context, and the second parameter is the previous value of the context. This is useful for listening to changes in the provider's parent component.
79
+ - `proxyRef` (`React.MutableRefObject<T>`): A ref object that is assigned the proxy object of the context. This is useful for accessing the proxy object directly by the provider's parent component.
38
80
 
39
81
  ## Hooks
40
82
  The following hooks are available in the library:
41
83
 
42
- ### useProxyContext
43
- A hook that uses the context provided by the `ProxyContextProvider` component. This hook also provides options to choose which properties to listen to and whether to listen to full reassignments.
84
+ ### useProxyContext<T>
85
+ A hook that uses the context provided by the `ProxyContextProvider` component. This hook also provides options to choose which properties to listen to and whether to listen to full reassignments. `T` represents the type of the object that is stored in the context.
44
86
 
45
87
  #### Parameters
46
- - `contextClass` (Object): The class of the context to use. This is the class that was created using `createProxyContext`.
47
- - `deps` (Array): An array of dependencies to listen to. If any of these properties on the context change, the hook will re-render. If this is falsy, any mutation will trigger a re-render. You can also specify a function that returns a boolean to determine whether to re-render (provided with the same arguments as `onChange` would be and a 4th argument that is the current value of the context).
48
- - `onChange` (Function): A function that is called whenever the context is changed. The first parameter is the property that was changed (null if it was reassignment), the second parameter is the current value of the property, and the third parameter is the previous value of the property. This is useful for listening to changes in the provider's parent component.
49
- - `listenReinit` (Boolean): Whether to listen to full reassignments of the context and re-render when they occur. Default is `true`.
88
+ - `contextClass` (`ContextWithName<ProxyContextValue<T> | undefined>`): The context class that was created using `createProxyContext`.
89
+ - `deps` (`Dependency<T>[] | null`): An array of dependencies to listen to. If any of these properties on the context change, the hook will re-render. If this is falsy, any mutation will trigger a re-render. You can also specify a function that returns a boolean to determine whether to re-render. If this is null, the hook will re-render on any mutation. By default, this is an empty array.
90
+ - `onChangeProp` (`OnChangePropCallback<T> | undefined`): A function that is called whenever a property of the context is changed. The first parameter is the property that was changed, the second parameter is the current value of the property, and the third parameter is the previous value of the property. This is useful for listening to changes in the provider's parent component.
91
+ - `onChangeReinit` (`OnChangeReinitCallback<T> | undefined`): A function that is called whenever the context is reinitialized. The first parameter is the current value of the context, and the second parameter is the previous value of the context. This is useful for listening to changes in the provider's parent component.
92
+ - `listenReinit` (`boolean`): Whether to listen to full reassignments of the context. If this is true, the hook will re-render whenever the context is reinitialized. By default, this is true.
50
93
 
51
94
  #### Returns
52
- Array - An array with the first element being the current value of the context and the second element being a setter function to reassign the context.
53
-
54
- ## Meta
55
- This is a React Library Created by Ptolemy2002's [cra-template-react-library](https://www.npmjs.com/package/@ptolemy2002/cra-template-react-library) template in combination with [create-react-app](https://www.npmjs.com/package/create-react-app). It contains methods of building and publishing your library to npm.
56
- For now, the library makes use of React 18 and does not use TypeScript.
95
+ `UseProxyContextResult<T>` - An object containing the current value of the context and a function to set the context. The function returns the new value of the context wrapped in a `Proxy`.
57
96
 
58
97
  ## Peer Dependencies
59
98
  These should be installed in order to use the library, as npm does not automatically add peer dependencies to your project.
60
- - @types/react: ^18.3.3
61
- - @types/react-dom: ^18.3.0
62
- - react: ^18.3.1
63
- - react-dom: ^18.3.1
99
+ - `react^18.3.1`
100
+ - `react-dom^18.3.1`
101
+ - `@ptolemy2002/js-utils^3.0.2`
102
+ - `@ptolemy2002/react-utils^3.0.0`
103
+ - `@ptolemy2002/react-force-rerender^2.0.0`
104
+ - `@ptolemy2002/react-hook-result^2.1.1`
105
+ - `@ptolemy2002/react-mount-effects^2.0.0`
64
106
 
65
107
  ## Commands
66
108
  The following commands exist in the project:
package/dist/main.d.ts ADDED
@@ -0,0 +1,30 @@
1
+ import { Context, ReactNode } from 'react';
2
+ import { HookResultData } from '@ptolemy2002/react-hook-result';
3
+ export type ContextWithName<T> = Context<T> & {
4
+ name: string;
5
+ };
6
+ export type OnChangePropCallback<T> = <K extends keyof T>(prop: K, current: T[K], prev?: T[K]) => void;
7
+ export type OnChangeReinitCallback<T> = (current: T, prev?: T) => void;
8
+ export type ProxyContextValue<T> = {
9
+ obj: T;
10
+ set: (newObj: T) => T;
11
+ subscribe: (propCallback: OnChangePropCallback<T>, reinitCallback: OnChangeReinitCallback<T>, deps: Dependency<T>[] | null) => string;
12
+ unsubscribe: (id: string) => void;
13
+ };
14
+ export declare function createProxyContext<T>(name: string): ContextWithName<ProxyContextValue<T> | undefined>;
15
+ export type Dependency<T> = keyof T | (<K extends keyof T>(prop: K, current: T[K], prev: T[K], obj: T) => boolean) | null | undefined | false;
16
+ export type ProxyContextProviderProps<T> = {
17
+ children: ReactNode;
18
+ value: T;
19
+ onChangeProp?: OnChangePropCallback<T>;
20
+ onChangeReinit?: OnChangeReinitCallback<T>;
21
+ proxyRef?: React.MutableRefObject<T>;
22
+ };
23
+ export declare function createProxyContextProvider<T extends object>(contextClass: ContextWithName<ProxyContextValue<T> | undefined>): import('react').MemoExoticComponent<import('react').FunctionComponent<ProxyContextProviderProps<T> & {
24
+ renderDeps?: any[];
25
+ }>>;
26
+ export type UseProxyContextResult<T> = HookResultData<{
27
+ value: T;
28
+ set: (newObj: T) => T;
29
+ }, readonly [T, (newObj: T) => T]>;
30
+ export declare function useProxyContext<T>(contextClass: ContextWithName<ProxyContextValue<T> | undefined>, deps?: Dependency<T>[] | null, onChangeProp?: OnChangePropCallback<T>, onChangeReinit?: OnChangeReinitCallback<T>, listenReinit?: boolean): UseProxyContextResult<T>;