@ptolemy2002/react-proxy-context 2.0.1 → 2.1.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
@@ -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.
@@ -98,7 +100,6 @@ A hook that uses the context provided by the `ProxyContextProvider` component. T
98
100
  These should be installed in order to use the library, as npm does not automatically add peer dependencies to your project.
99
101
  - `react^18.3.1`
100
102
  - `react-dom^18.3.1`
101
- - `@ptolemy2002/js-utils^3.0.2`
102
103
  - `@ptolemy2002/react-utils^3.0.0`
103
104
  - `@ptolemy2002/react-force-rerender^2.0.0`
104
105
  - `@ptolemy2002/react-hook-result^2.1.1`
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 | null>(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/dist/main.js CHANGED
@@ -2,9 +2,8 @@ import Ie, { useEffect as $e, createContext as br, useRef as X, useImperativeHan
2
2
  import { nanoid as Rr } from "nanoid";
3
3
  import M from "is-callable";
4
4
  import We from "@ptolemy2002/react-force-rerender";
5
- import { isNullOrUndefined as gr } from "@ptolemy2002/js-utils";
6
- import hr from "@ptolemy2002/react-hook-result";
7
- import { partialMemo as Er } from "@ptolemy2002/react-utils";
5
+ import gr from "@ptolemy2002/react-hook-result";
6
+ import { partialMemo as hr } from "@ptolemy2002/react-utils";
8
7
  var se = { exports: {} }, B = {};
9
8
  /**
10
9
  * @license React
@@ -16,7 +15,7 @@ var se = { exports: {} }, B = {};
16
15
  * LICENSE file in the root directory of this source tree.
17
16
  */
18
17
  var Fe;
19
- function _r() {
18
+ function Er() {
20
19
  if (Fe) return B;
21
20
  Fe = 1;
22
21
  var b = Ie, _ = Symbol.for("react.element"), k = Symbol.for("react.fragment"), T = Object.prototype.hasOwnProperty, C = b.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner, p = { key: !0, ref: !0, __self: !0, __source: !0 };
@@ -40,7 +39,7 @@ var J = {};
40
39
  * LICENSE file in the root directory of this source tree.
41
40
  */
42
41
  var Ae;
43
- function Tr() {
42
+ function _r() {
44
43
  return Ae || (Ae = 1, process.env.NODE_ENV !== "production" && function() {
45
44
  var b = Ie, _ = Symbol.for("react.element"), k = Symbol.for("react.portal"), T = Symbol.for("react.fragment"), C = Symbol.for("react.strict_mode"), p = Symbol.for("react.profiler"), S = Symbol.for("react.provider"), y = Symbol.for("react.context"), o = Symbol.for("react.forward_ref"), d = Symbol.for("react.suspense"), v = Symbol.for("react.suspense_list"), P = Symbol.for("react.memo"), w = Symbol.for("react.lazy"), D = Symbol.for("react.offscreen"), R = Symbol.iterator, I = "@@iterator";
46
45
  function x(e) {
@@ -633,25 +632,25 @@ React keys must be passed directly to JSX without using spread:
633
632
  J.Fragment = T, J.jsx = lr, J.jsxs = dr;
634
633
  }()), J;
635
634
  }
636
- process.env.NODE_ENV === "production" ? se.exports = _r() : se.exports = Tr();
637
- var Pr = se.exports;
638
- function xr(b) {
635
+ process.env.NODE_ENV === "production" ? se.exports = Er() : se.exports = _r();
636
+ var Tr = se.exports;
637
+ function Pr(b) {
639
638
  $e(() => {
640
639
  b();
641
640
  }, []);
642
641
  }
643
- function Cr(b) {
642
+ function xr(b) {
644
643
  $e(() => b, []);
645
644
  }
646
- function Ar(b) {
645
+ function Dr(b) {
647
646
  if (typeof Proxy > "u") throw new Error("Proxy is not supported in this environment.");
648
647
  const _ = br(
649
648
  void 0
650
649
  );
651
650
  return _.name = b, _;
652
651
  }
653
- function Ir(b) {
654
- return Er(function({
652
+ function Fr(b) {
653
+ return hr(function({
655
654
  children: k,
656
655
  value: T,
657
656
  onChangeProp: C,
@@ -673,7 +672,7 @@ function Ir(b) {
673
672
  }, []), D = ue((R) => {
674
673
  if (R !== o.current) {
675
674
  const I = o.current;
676
- gr(R) ? o.current = R : o.current = new Proxy(R, {
675
+ R !== null ? o.current = new Proxy(R, {
677
676
  get: function(x, g) {
678
677
  const s = Reflect.get(x, g, R);
679
678
  return M(s) ? s.bind(o.current) : s;
@@ -684,20 +683,20 @@ function Ir(b) {
684
683
  (!W.deps || W.deps.includes(j) && $ !== s || W.deps.some((q) => M(q) && q(j, s, $, R))) && W.propCallback(j, s, $);
685
684
  }), M(C) && C(j, s, $), !0;
686
685
  }
687
- }), Object.values(y.current).forEach((x) => {
686
+ }) : o.current = R, Object.values(y.current).forEach((x) => {
688
687
  x.reinitCallback(o.current, I);
689
688
  }), M(p) && p(o.current, I), v();
690
689
  }
691
690
  return d.current.obj = o.current, o.current;
692
691
  }, [C, p]);
693
- return o.current === void 0 && (o.current = D(T)), d.current.obj = o.current, d.current.set = D, d.current.subscribe = P, d.current.unsubscribe = w, /* @__PURE__ */ Pr.jsx(b.Provider, { value: d.current, children: k });
692
+ return o.current === void 0 && (o.current = D(T)), d.current.obj = o.current, d.current.set = D, d.current.subscribe = P, d.current.unsubscribe = w, /* @__PURE__ */ Tr.jsx(b.Provider, { value: d.current, children: k });
694
693
  }, ["children", "onChangeProp", "onChangeReinit", "proxyRef"], `${b.name}Provider (Memo)`);
695
694
  }
696
- function $r(b, _ = [], k, T, C = !0) {
695
+ function Ar(b, _ = [], k, T, C = !0) {
697
696
  const p = mr(b), S = We(), y = X(null);
698
697
  if (p === void 0)
699
698
  throw new Error(`No ${b.name} provider found.`);
700
- return xr(() => {
699
+ return Pr(() => {
701
700
  y.current = p == null ? void 0 : p.subscribe(
702
701
  (o, d, v) => {
703
702
  S(), M(k) && k(o, d, v);
@@ -707,15 +706,15 @@ function $r(b, _ = [], k, T, C = !0) {
707
706
  },
708
707
  _
709
708
  );
710
- }), Cr(() => {
709
+ }), xr(() => {
711
710
  p.unsubscribe(y.current);
712
- }), new hr(
711
+ }), new gr(
713
712
  { value: p.obj, set: p.set },
714
713
  ["value", "set"]
715
714
  );
716
715
  }
717
716
  export {
718
- Ar as createProxyContext,
719
- Ir as createProxyContextProvider,
720
- $r as useProxyContext
717
+ Dr as createProxyContext,
718
+ Fr as createProxyContextProvider,
719
+ Ar as useProxyContext
721
720
  };
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.1",
5
5
  "type": "module",
6
6
  "repository": {
7
7
  "type": "git",
@@ -21,7 +21,6 @@
21
21
  "release-major": "bash ./scripts/release.sh major"
22
22
  },
23
23
  "peerDependencies": {
24
- "@ptolemy2002/js-utils": "^3.0.2",
25
24
  "@ptolemy2002/react-force-rerender": "^2.0.0",
26
25
  "@ptolemy2002/react-hook-result": "^2.1.1",
27
26
  "@ptolemy2002/react-mount-effects": "^2.0.0",
@@ -33,7 +32,6 @@
33
32
  },
34
33
  "devDependencies": {
35
34
  "@eslint/js": "^9.11.1",
36
- "@ptolemy2002/js-utils": "^3.0.2",
37
35
  "@ptolemy2002/react-force-rerender": "^2.0.0",
38
36
  "@ptolemy2002/react-hook-result": "^2.1.1",
39
37
  "@ptolemy2002/react-mount-effects": "^2.0.0",