@faasjs/react 2.0.0 → 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
@@ -55,6 +55,7 @@ const client = FaasReactClient({
55
55
 
56
56
  - [FaasDataWrapper](functions/FaasDataWrapper.md)
57
57
  - [FaasReactClient](functions/FaasReactClient.md)
58
+ - [createSplitedContext](functions/createSplitedContext.md)
58
59
  - [faas](functions/faas.md)
59
60
  - [getClient](functions/getClient.md)
60
61
  - [useConstant](functions/useConstant.md)
package/dist/index.d.mts CHANGED
@@ -9,6 +9,73 @@ import { ReactNode, ReactElement, Component } from 'react';
9
9
  * Returns a constant value that is created by the given function.
10
10
  */
11
11
  declare function useConstant<T>(fn: () => T): T;
12
+ declare namespace useConstant {
13
+ var whyDidYouRender: boolean;
14
+ }
15
+
16
+ /**
17
+ * Creates a splited context with the given default value.
18
+ *
19
+ * @template T - The type of the default value.
20
+ * @param {T} defaultValue - The default value for the split context.
21
+ * @returns {Object} - An object containing the Provider and use functions.
22
+ *
23
+ * @example
24
+ * ```tsx
25
+ * const { Provider, use } = createSplitedContext({
26
+ * value: 0,
27
+ * setValue: (_: any) => {},
28
+ * })
29
+ *
30
+ * function ReaderComponent() {
31
+ * const { value } = use()
32
+ *
33
+ * return <div>{value}</div>
34
+ * }
35
+ *
36
+ * function WriterComponent() {
37
+ * const { setValue } = use()
38
+ *
39
+ * return (
40
+ * <button type='button' onClick={() => setValue((p: number) => p + 1)}>
41
+ * Change
42
+ * </button>
43
+ * )
44
+ * }
45
+ *
46
+ * const App = memo(() => {
47
+ * return (
48
+ * <>
49
+ * <ReaderComponent />
50
+ * <WriterComponent />
51
+ * </>
52
+ * )
53
+ * })
54
+ *
55
+ * function Container() {
56
+ * const [value, setValue] = useState(0)
57
+ *
58
+ * return (
59
+ * <Provider value={{ value, setValue }}>
60
+ * <App />
61
+ * </Provider>
62
+ * )
63
+ * }
64
+ * ```
65
+ */
66
+ declare function createSplitedContext<T extends Record<string, any>>(defaultValue: T): {
67
+ Provider: {
68
+ (props: {
69
+ value: T;
70
+ children: React.ReactNode;
71
+ }): react_jsx_runtime.JSX.Element;
72
+ whyDidYouRender: boolean;
73
+ };
74
+ use: {
75
+ (): Readonly<T>;
76
+ whyDidYouRender: boolean;
77
+ };
78
+ };
12
79
 
13
80
  type FaasReactClientInstance = {
14
81
  id: string
@@ -178,4 +245,4 @@ declare class ErrorBoundary extends Component<ErrorBoundaryProps, {
178
245
  render(): string | number | boolean | Iterable<ReactNode> | react_jsx_runtime.JSX.Element;
179
246
  }
180
247
 
181
- export { ErrorBoundary, type ErrorBoundaryProps, type ErrorChildrenProps, type FaasDataInjection, FaasDataWrapper, type FaasDataWrapperProps, FaasReactClient, type FaasReactClientInstance, type FaasReactClientOptions, faas, getClient, useConstant, useFaas, type useFaasOptions };
248
+ export { ErrorBoundary, type ErrorBoundaryProps, type ErrorChildrenProps, type FaasDataInjection, FaasDataWrapper, type FaasDataWrapperProps, FaasReactClient, type FaasReactClientInstance, type FaasReactClientOptions, createSplitedContext, faas, getClient, useConstant, useFaas, type useFaasOptions };
package/dist/index.d.ts CHANGED
@@ -9,6 +9,73 @@ import { ReactNode, ReactElement, Component } from 'react';
9
9
  * Returns a constant value that is created by the given function.
10
10
  */
11
11
  declare function useConstant<T>(fn: () => T): T;
12
+ declare namespace useConstant {
13
+ var whyDidYouRender: boolean;
14
+ }
15
+
16
+ /**
17
+ * Creates a splited context with the given default value.
18
+ *
19
+ * @template T - The type of the default value.
20
+ * @param {T} defaultValue - The default value for the split context.
21
+ * @returns {Object} - An object containing the Provider and use functions.
22
+ *
23
+ * @example
24
+ * ```tsx
25
+ * const { Provider, use } = createSplitedContext({
26
+ * value: 0,
27
+ * setValue: (_: any) => {},
28
+ * })
29
+ *
30
+ * function ReaderComponent() {
31
+ * const { value } = use()
32
+ *
33
+ * return <div>{value}</div>
34
+ * }
35
+ *
36
+ * function WriterComponent() {
37
+ * const { setValue } = use()
38
+ *
39
+ * return (
40
+ * <button type='button' onClick={() => setValue((p: number) => p + 1)}>
41
+ * Change
42
+ * </button>
43
+ * )
44
+ * }
45
+ *
46
+ * const App = memo(() => {
47
+ * return (
48
+ * <>
49
+ * <ReaderComponent />
50
+ * <WriterComponent />
51
+ * </>
52
+ * )
53
+ * })
54
+ *
55
+ * function Container() {
56
+ * const [value, setValue] = useState(0)
57
+ *
58
+ * return (
59
+ * <Provider value={{ value, setValue }}>
60
+ * <App />
61
+ * </Provider>
62
+ * )
63
+ * }
64
+ * ```
65
+ */
66
+ declare function createSplitedContext<T extends Record<string, any>>(defaultValue: T): {
67
+ Provider: {
68
+ (props: {
69
+ value: T;
70
+ children: React.ReactNode;
71
+ }): react_jsx_runtime.JSX.Element;
72
+ whyDidYouRender: boolean;
73
+ };
74
+ use: {
75
+ (): Readonly<T>;
76
+ whyDidYouRender: boolean;
77
+ };
78
+ };
12
79
 
13
80
  type FaasReactClientInstance = {
14
81
  id: string
@@ -178,4 +245,4 @@ declare class ErrorBoundary extends Component<ErrorBoundaryProps, {
178
245
  render(): string | number | boolean | Iterable<ReactNode> | react_jsx_runtime.JSX.Element;
179
246
  }
180
247
 
181
- export { ErrorBoundary, type ErrorBoundaryProps, type ErrorChildrenProps, type FaasDataInjection, FaasDataWrapper, type FaasDataWrapperProps, FaasReactClient, type FaasReactClientInstance, type FaasReactClientOptions, faas, getClient, useConstant, useFaas, type useFaasOptions };
248
+ export { ErrorBoundary, type ErrorBoundaryProps, type ErrorChildrenProps, type FaasDataInjection, FaasDataWrapper, type FaasDataWrapperProps, FaasReactClient, type FaasReactClientInstance, type FaasReactClientOptions, createSplitedContext, faas, getClient, useConstant, useFaas, type useFaasOptions };
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  'use strict';
2
2
 
3
3
  var react = require('react');
4
- var browser = require('@faasjs/browser');
5
4
  var jsxRuntime = require('react/jsx-runtime');
5
+ var browser = require('@faasjs/browser');
6
6
 
7
7
  // src/constant.ts
8
8
  function useConstant(fn) {
@@ -12,6 +12,38 @@ function useConstant(fn) {
12
12
  }
13
13
  return ref.current.v;
14
14
  }
15
+ useConstant.whyDidYouRender = true;
16
+ function createSplitedContext(defaultValue) {
17
+ const contexts = {};
18
+ for (const key of Object.keys(defaultValue))
19
+ contexts[key] = react.createContext(defaultValue[key]);
20
+ function Provider(props) {
21
+ let children = props.children;
22
+ for (const key of Object.keys(props.value)) {
23
+ const Context2 = contexts[key];
24
+ Context2.Provider.whyDidYouRender = true;
25
+ children = /* @__PURE__ */ jsxRuntime.jsx(Context2.Provider, { value: props.value[key], children });
26
+ }
27
+ return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children });
28
+ }
29
+ Provider.whyDidYouRender = true;
30
+ function use() {
31
+ return useConstant(() => {
32
+ const obj = /* @__PURE__ */ Object.create(null);
33
+ for (const key of Object.keys(contexts)) {
34
+ Object.defineProperty(obj, key, {
35
+ get: () => react.useContext(contexts[key])
36
+ });
37
+ }
38
+ return Object.freeze(obj);
39
+ });
40
+ }
41
+ use.whyDidYouRender = true;
42
+ return {
43
+ Provider,
44
+ use
45
+ };
46
+ }
15
47
  var clients = {};
16
48
  function FaasReactClient({
17
49
  domain,
@@ -240,10 +272,12 @@ var ErrorBoundary = class extends react.Component {
240
272
  }
241
273
  };
242
274
  ErrorBoundary.whyDidYouRender = true;
275
+ ErrorBoundary.whyDidYouRender = true;
243
276
 
244
277
  exports.ErrorBoundary = ErrorBoundary;
245
278
  exports.FaasDataWrapper = FaasDataWrapper;
246
279
  exports.FaasReactClient = FaasReactClient;
280
+ exports.createSplitedContext = createSplitedContext;
247
281
  exports.faas = faas;
248
282
  exports.getClient = getClient;
249
283
  exports.useConstant = useConstant;
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
- import { useRef, useState, useEffect, Component, cloneElement, useCallback, useMemo } from 'react';
1
+ import { useRef, createContext, useState, useEffect, Component, cloneElement, useContext, useCallback, useMemo } from 'react';
2
+ import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
2
3
  import { FaasBrowserClient } from '@faasjs/browser';
3
- import { jsx, jsxs } from 'react/jsx-runtime';
4
4
 
5
5
  // src/constant.ts
6
6
  function useConstant(fn) {
@@ -10,6 +10,38 @@ function useConstant(fn) {
10
10
  }
11
11
  return ref.current.v;
12
12
  }
13
+ useConstant.whyDidYouRender = true;
14
+ function createSplitedContext(defaultValue) {
15
+ const contexts = {};
16
+ for (const key of Object.keys(defaultValue))
17
+ contexts[key] = createContext(defaultValue[key]);
18
+ function Provider(props) {
19
+ let children = props.children;
20
+ for (const key of Object.keys(props.value)) {
21
+ const Context2 = contexts[key];
22
+ Context2.Provider.whyDidYouRender = true;
23
+ children = /* @__PURE__ */ jsx(Context2.Provider, { value: props.value[key], children });
24
+ }
25
+ return /* @__PURE__ */ jsx(Fragment, { children });
26
+ }
27
+ Provider.whyDidYouRender = true;
28
+ function use() {
29
+ return useConstant(() => {
30
+ const obj = /* @__PURE__ */ Object.create(null);
31
+ for (const key of Object.keys(contexts)) {
32
+ Object.defineProperty(obj, key, {
33
+ get: () => useContext(contexts[key])
34
+ });
35
+ }
36
+ return Object.freeze(obj);
37
+ });
38
+ }
39
+ use.whyDidYouRender = true;
40
+ return {
41
+ Provider,
42
+ use
43
+ };
44
+ }
13
45
  var clients = {};
14
46
  function FaasReactClient({
15
47
  domain,
@@ -238,5 +270,6 @@ var ErrorBoundary = class extends Component {
238
270
  }
239
271
  };
240
272
  ErrorBoundary.whyDidYouRender = true;
273
+ ErrorBoundary.whyDidYouRender = true;
241
274
 
242
- export { ErrorBoundary, FaasDataWrapper, FaasReactClient, faas, getClient, useConstant, useFaas };
275
+ export { ErrorBoundary, FaasDataWrapper, FaasReactClient, createSplitedContext, faas, getClient, useConstant, useFaas };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/react",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -22,7 +22,7 @@
22
22
  "dist"
23
23
  ],
24
24
  "dependencies": {
25
- "@faasjs/browser": "2.0.0"
25
+ "@faasjs/browser": "2.1.0"
26
26
  },
27
27
  "peerDependencies": {
28
28
  "react": "*"