@ptolemy2002/react-proxy-context 2.0.1 → 2.1.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.
package/README.md CHANGED
@@ -24,6 +24,8 @@ type Dependency<T> = keyof T | (
24
24
  <K extends keyof T>(prop: K, current: T[K], prev: T[K], obj: T) => boolean
25
25
  ) | null | undefined | false;
26
26
 
27
+ type ProxyContext<T> = ContextWithName<ProxyContextValue<T> | undefined>;
28
+
27
29
  type ProxyContextValue<T> = {
28
30
  obj: T;
29
31
  set: (newObj: T) => T;
@@ -60,14 +62,14 @@ Creates a new instance of the ProxyContext, essentially to be used as the contex
60
62
  - `name` (String): The name of the context. This is used for debugging purposes.
61
63
 
62
64
  #### Returns
63
- `ContextWithName<ProxyContextValue<T> | undefined>` - The context object that can be used in a provider.
65
+ `ProxyContext<T>` - The context object that can be used in a provider.
64
66
 
65
67
  ### createProxyContextProvider<T extends object>
66
68
  #### Description
67
69
  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.
68
70
 
69
71
  #### Parameters
70
- - `contextClass` (`ContextWithName<ProxyContextValue<T> | undefined>`): The context class that was created using `createProxyContext`.
72
+ - `contextClass` (`ProxyContext<T>`): The context class that was created using `createProxyContext`.
71
73
 
72
74
  #### Returns
73
75
  `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).
@@ -85,7 +87,7 @@ The following hooks are available in the library:
85
87
  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.
86
88
 
87
89
  #### Parameters
88
- - `contextClass` (`ContextWithName<ProxyContextValue<T> | undefined>`): The context class that was created using `createProxyContext`.
90
+ - `contextClass` (`ProxyContext<T>`): The context class that was created using `createProxyContext`.
89
91
  - `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
92
  - `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
93
  - `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.
package/dist/main.d.ts CHANGED
@@ -11,8 +11,9 @@ export type ProxyContextValue<T> = {
11
11
  subscribe: (propCallback: OnChangePropCallback<T>, reinitCallback: OnChangeReinitCallback<T>, deps: Dependency<T>[] | null) => string;
12
12
  unsubscribe: (id: string) => void;
13
13
  };
14
- export declare function createProxyContext<T>(name: string): ContextWithName<ProxyContextValue<T> | undefined>;
14
+ export declare function createProxyContext<T>(name: string): ProxyContext<T>;
15
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 ProxyContext<T> = ContextWithName<ProxyContextValue<T> | undefined>;
16
17
  export type ProxyContextProviderProps<T> = {
17
18
  children: ReactNode;
18
19
  value: T;
@@ -20,11 +21,11 @@ export type ProxyContextProviderProps<T> = {
20
21
  onChangeReinit?: OnChangeReinitCallback<T>;
21
22
  proxyRef?: React.MutableRefObject<T>;
22
23
  };
23
- export declare function createProxyContextProvider<T extends object>(contextClass: ContextWithName<ProxyContextValue<T> | undefined>): import('react').MemoExoticComponent<import('react').FunctionComponent<ProxyContextProviderProps<T> & {
24
+ export declare function createProxyContextProvider<T extends object>(contextClass: ProxyContext<T>): import('react').MemoExoticComponent<import('react').FunctionComponent<ProxyContextProviderProps<T> & {
24
25
  renderDeps?: any[];
25
26
  }>>;
26
27
  export type UseProxyContextResult<T> = HookResultData<{
27
28
  value: T;
28
29
  set: (newObj: T) => T;
29
30
  }, 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>;
31
+ export declare function useProxyContext<T>(contextClass: ProxyContext<T>, deps?: Dependency<T>[] | null, onChangeProp?: OnChangePropCallback<T>, onChangeReinit?: OnChangeReinitCallback<T>, listenReinit?: boolean): UseProxyContextResult<T>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ptolemy2002/react-proxy-context",
3
3
  "private": false,
4
- "version": "2.0.1",
4
+ "version": "2.1.0",
5
5
  "type": "module",
6
6
  "repository": {
7
7
  "type": "git",