@faasjs/react 2.3.1 → 2.5.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
@@ -63,6 +63,7 @@ const client = FaasReactClient({
63
63
  - [FaasDataWrapper](functions/FaasDataWrapper.md)
64
64
  - [FaasReactClient](functions/FaasReactClient.md)
65
65
  - [createSplitedContext](functions/createSplitedContext.md)
66
+ - [createSplittingContext](functions/createSplittingContext.md)
66
67
  - [faas](functions/faas.md)
67
68
  - [getClient](functions/getClient.md)
68
69
  - [useConstant](functions/useConstant.md)
package/dist/index.d.mts CHANGED
@@ -15,11 +15,14 @@ declare namespace useConstant {
15
15
  }
16
16
 
17
17
  /**
18
- * Creates a splited context with the given default value.
18
+ * Creates a splitting context with the given default value.
19
+ *
20
+ * @param defaultValue The default value of the splitting context.
21
+ * @returns The provider component and the hook to use the splitting context.
19
22
  *
20
23
  * @example
21
24
  * ```tsx
22
- * const { Provider, use } = createSplitedContext<{
25
+ * const { Provider, use } = createSplittingContext<{
23
26
  * value: number
24
27
  * setValue: React.Dispatch<React.SetStateAction<number>>
25
28
  * }>({
@@ -55,7 +58,7 @@ declare namespace useConstant {
55
58
  * }
56
59
  * ```
57
60
  */
58
- declare function createSplitedContext<T extends Record<string, any>>(defaultValue: {
61
+ declare function createSplittingContext<T extends Record<string, any>>(defaultValue: {
59
62
  [K in keyof T]: Partial<T[K]> | null;
60
63
  }): {
61
64
  Provider: (props: {
@@ -64,6 +67,10 @@ declare function createSplitedContext<T extends Record<string, any>>(defaultValu
64
67
  }) => react.ReactNode;
65
68
  use: () => Readonly<T>;
66
69
  };
70
+ /**
71
+ * @deprecated Use `createSplittingContext` instead.
72
+ */
73
+ declare const createSplitedContext: typeof createSplittingContext;
67
74
 
68
75
  type FaasReactClientInstance = {
69
76
  id: string
@@ -233,4 +240,4 @@ declare class ErrorBoundary extends Component<ErrorBoundaryProps, {
233
240
  render(): string | number | boolean | Iterable<ReactNode> | react_jsx_runtime.JSX.Element;
234
241
  }
235
242
 
236
- export { ErrorBoundary, type ErrorBoundaryProps, type ErrorChildrenProps, type FaasDataInjection, FaasDataWrapper, type FaasDataWrapperProps, FaasReactClient, type FaasReactClientInstance, type FaasReactClientOptions, createSplitedContext, faas, getClient, useConstant, useFaas, type useFaasOptions };
243
+ export { ErrorBoundary, type ErrorBoundaryProps, type ErrorChildrenProps, type FaasDataInjection, FaasDataWrapper, type FaasDataWrapperProps, FaasReactClient, type FaasReactClientInstance, type FaasReactClientOptions, createSplitedContext, createSplittingContext, faas, getClient, useConstant, useFaas, type useFaasOptions };
package/dist/index.d.ts CHANGED
@@ -15,11 +15,14 @@ declare namespace useConstant {
15
15
  }
16
16
 
17
17
  /**
18
- * Creates a splited context with the given default value.
18
+ * Creates a splitting context with the given default value.
19
+ *
20
+ * @param defaultValue The default value of the splitting context.
21
+ * @returns The provider component and the hook to use the splitting context.
19
22
  *
20
23
  * @example
21
24
  * ```tsx
22
- * const { Provider, use } = createSplitedContext<{
25
+ * const { Provider, use } = createSplittingContext<{
23
26
  * value: number
24
27
  * setValue: React.Dispatch<React.SetStateAction<number>>
25
28
  * }>({
@@ -55,7 +58,7 @@ declare namespace useConstant {
55
58
  * }
56
59
  * ```
57
60
  */
58
- declare function createSplitedContext<T extends Record<string, any>>(defaultValue: {
61
+ declare function createSplittingContext<T extends Record<string, any>>(defaultValue: {
59
62
  [K in keyof T]: Partial<T[K]> | null;
60
63
  }): {
61
64
  Provider: (props: {
@@ -64,6 +67,10 @@ declare function createSplitedContext<T extends Record<string, any>>(defaultValu
64
67
  }) => react.ReactNode;
65
68
  use: () => Readonly<T>;
66
69
  };
70
+ /**
71
+ * @deprecated Use `createSplittingContext` instead.
72
+ */
73
+ declare const createSplitedContext: typeof createSplittingContext;
67
74
 
68
75
  type FaasReactClientInstance = {
69
76
  id: string
@@ -233,4 +240,4 @@ declare class ErrorBoundary extends Component<ErrorBoundaryProps, {
233
240
  render(): string | number | boolean | Iterable<ReactNode> | react_jsx_runtime.JSX.Element;
234
241
  }
235
242
 
236
- export { ErrorBoundary, type ErrorBoundaryProps, type ErrorChildrenProps, type FaasDataInjection, FaasDataWrapper, type FaasDataWrapperProps, FaasReactClient, type FaasReactClientInstance, type FaasReactClientOptions, createSplitedContext, faas, getClient, useConstant, useFaas, type useFaasOptions };
243
+ export { ErrorBoundary, type ErrorBoundaryProps, type ErrorChildrenProps, type FaasDataInjection, FaasDataWrapper, type FaasDataWrapperProps, FaasReactClient, type FaasReactClientInstance, type FaasReactClientOptions, createSplitedContext, createSplittingContext, faas, getClient, useConstant, useFaas, type useFaasOptions };
package/dist/index.js CHANGED
@@ -13,11 +13,10 @@ function useConstant(fn) {
13
13
  return ref.current.v;
14
14
  }
15
15
  useConstant.whyDidYouRender = true;
16
- function createSplitedContext(defaultValue) {
16
+ function createSplittingContext(defaultValue) {
17
17
  const contexts = {};
18
18
  const keys = Object.keys(defaultValue);
19
- for (const key of keys)
20
- contexts[key] = react.createContext(defaultValue[key]);
19
+ for (const key of keys) contexts[key] = react.createContext(defaultValue[key]);
21
20
  function Provider(props) {
22
21
  var _a, _b;
23
22
  let children = props.children;
@@ -44,6 +43,7 @@ function createSplitedContext(defaultValue) {
44
43
  use
45
44
  };
46
45
  }
46
+ var createSplitedContext = createSplittingContext;
47
47
  var clients = {};
48
48
  function FaasReactClient({
49
49
  domain,
@@ -60,8 +60,7 @@ function FaasReactClient({
60
60
  return client.action(action, params);
61
61
  }
62
62
  function useFaas2(action, defaultParams, options2) {
63
- if (!options2)
64
- options2 = {};
63
+ if (!options2) options2 = {};
65
64
  const [loading, setLoading] = react.useState(true);
66
65
  const [data, setData] = react.useState();
67
66
  const [error, setError] = react.useState();
@@ -115,8 +114,7 @@ function FaasReactClient({
115
114
  } catch (error2) {
116
115
  setError(error2);
117
116
  }
118
- else
119
- setError(e);
117
+ else setError(e);
120
118
  setLoading(false);
121
119
  return Promise.reject(e);
122
120
  });
@@ -137,8 +135,7 @@ function FaasReactClient({
137
135
  }, [action, JSON.stringify(options2.params || params), reloadTimes, skip]);
138
136
  const reload = react.useCallback(
139
137
  (params2) => {
140
- if (params2)
141
- setParams(params2);
138
+ if (params2) setParams(params2);
142
139
  setReloadTimes((prev) => prev + 1);
143
140
  return promise;
144
141
  },
@@ -176,19 +173,15 @@ function FaasReactClient({
176
173
  });
177
174
  const [loaded, setLoaded] = react.useState(false);
178
175
  react.useEffect(() => {
179
- if (!loaded && !request.loading)
180
- setLoaded(true);
176
+ if (!loaded && !request.loading) setLoaded(true);
181
177
  }, [request.loading]);
182
178
  react.useEffect(() => {
183
- if (onDataChange)
184
- onDataChange(request);
179
+ if (onDataChange) onDataChange(request);
185
180
  }, [JSON.stringify(request.data)]);
186
181
  const child = react.useMemo(() => {
187
182
  if (loaded) {
188
- if (children)
189
- return react.cloneElement(children, request);
190
- if (render)
191
- return render(request);
183
+ if (children) return react.cloneElement(children, request);
184
+ if (render) return render(request);
192
185
  }
193
186
  return fallback || null;
194
187
  }, [
@@ -213,8 +206,7 @@ function FaasReactClient({
213
206
  }
214
207
  function getClient(domain) {
215
208
  const client = clients[domain || Object.keys(clients)[0]];
216
- if (!client)
217
- throw Error("FaasReactClient is not initialized");
209
+ if (!client) throw Error("FaasReactClient is not initialized");
218
210
  return client;
219
211
  }
220
212
  async function faas(action, params) {
@@ -226,12 +218,10 @@ function useFaas(action, defaultParams, options) {
226
218
  function FaasDataWrapper(props) {
227
219
  const [client, setClient] = react.useState();
228
220
  react.useEffect(() => {
229
- if (client)
230
- return;
221
+ if (client) return;
231
222
  setClient(getClient());
232
223
  }, []);
233
- if (!client)
234
- return props.fallback || null;
224
+ if (!client) return props.fallback || null;
235
225
  return /* @__PURE__ */ jsxRuntime.jsx(client.FaasDataWrapper, { ...props });
236
226
  }
237
227
  FaasDataWrapper.whyDidYouRender = true;
@@ -278,6 +268,7 @@ exports.ErrorBoundary = ErrorBoundary;
278
268
  exports.FaasDataWrapper = FaasDataWrapper;
279
269
  exports.FaasReactClient = FaasReactClient;
280
270
  exports.createSplitedContext = createSplitedContext;
271
+ exports.createSplittingContext = createSplittingContext;
281
272
  exports.faas = faas;
282
273
  exports.getClient = getClient;
283
274
  exports.useConstant = useConstant;
package/dist/index.mjs CHANGED
@@ -11,11 +11,10 @@ function useConstant(fn) {
11
11
  return ref.current.v;
12
12
  }
13
13
  useConstant.whyDidYouRender = true;
14
- function createSplitedContext(defaultValue) {
14
+ function createSplittingContext(defaultValue) {
15
15
  const contexts = {};
16
16
  const keys = Object.keys(defaultValue);
17
- for (const key of keys)
18
- contexts[key] = createContext(defaultValue[key]);
17
+ for (const key of keys) contexts[key] = createContext(defaultValue[key]);
19
18
  function Provider(props) {
20
19
  var _a, _b;
21
20
  let children = props.children;
@@ -42,6 +41,7 @@ function createSplitedContext(defaultValue) {
42
41
  use
43
42
  };
44
43
  }
44
+ var createSplitedContext = createSplittingContext;
45
45
  var clients = {};
46
46
  function FaasReactClient({
47
47
  domain,
@@ -58,8 +58,7 @@ function FaasReactClient({
58
58
  return client.action(action, params);
59
59
  }
60
60
  function useFaas2(action, defaultParams, options2) {
61
- if (!options2)
62
- options2 = {};
61
+ if (!options2) options2 = {};
63
62
  const [loading, setLoading] = useState(true);
64
63
  const [data, setData] = useState();
65
64
  const [error, setError] = useState();
@@ -113,8 +112,7 @@ function FaasReactClient({
113
112
  } catch (error2) {
114
113
  setError(error2);
115
114
  }
116
- else
117
- setError(e);
115
+ else setError(e);
118
116
  setLoading(false);
119
117
  return Promise.reject(e);
120
118
  });
@@ -135,8 +133,7 @@ function FaasReactClient({
135
133
  }, [action, JSON.stringify(options2.params || params), reloadTimes, skip]);
136
134
  const reload = useCallback(
137
135
  (params2) => {
138
- if (params2)
139
- setParams(params2);
136
+ if (params2) setParams(params2);
140
137
  setReloadTimes((prev) => prev + 1);
141
138
  return promise;
142
139
  },
@@ -174,19 +171,15 @@ function FaasReactClient({
174
171
  });
175
172
  const [loaded, setLoaded] = useState(false);
176
173
  useEffect(() => {
177
- if (!loaded && !request.loading)
178
- setLoaded(true);
174
+ if (!loaded && !request.loading) setLoaded(true);
179
175
  }, [request.loading]);
180
176
  useEffect(() => {
181
- if (onDataChange)
182
- onDataChange(request);
177
+ if (onDataChange) onDataChange(request);
183
178
  }, [JSON.stringify(request.data)]);
184
179
  const child = useMemo(() => {
185
180
  if (loaded) {
186
- if (children)
187
- return cloneElement(children, request);
188
- if (render)
189
- return render(request);
181
+ if (children) return cloneElement(children, request);
182
+ if (render) return render(request);
190
183
  }
191
184
  return fallback || null;
192
185
  }, [
@@ -211,8 +204,7 @@ function FaasReactClient({
211
204
  }
212
205
  function getClient(domain) {
213
206
  const client = clients[domain || Object.keys(clients)[0]];
214
- if (!client)
215
- throw Error("FaasReactClient is not initialized");
207
+ if (!client) throw Error("FaasReactClient is not initialized");
216
208
  return client;
217
209
  }
218
210
  async function faas(action, params) {
@@ -224,12 +216,10 @@ function useFaas(action, defaultParams, options) {
224
216
  function FaasDataWrapper(props) {
225
217
  const [client, setClient] = useState();
226
218
  useEffect(() => {
227
- if (client)
228
- return;
219
+ if (client) return;
229
220
  setClient(getClient());
230
221
  }, []);
231
- if (!client)
232
- return props.fallback || null;
222
+ if (!client) return props.fallback || null;
233
223
  return /* @__PURE__ */ jsx(client.FaasDataWrapper, { ...props });
234
224
  }
235
225
  FaasDataWrapper.whyDidYouRender = true;
@@ -272,4 +262,4 @@ var ErrorBoundary = class extends Component {
272
262
  ErrorBoundary.whyDidYouRender = true;
273
263
  ErrorBoundary.whyDidYouRender = true;
274
264
 
275
- export { ErrorBoundary, FaasDataWrapper, FaasReactClient, createSplitedContext, faas, getClient, useConstant, useFaas };
265
+ export { ErrorBoundary, FaasDataWrapper, FaasReactClient, createSplitedContext, createSplittingContext, faas, getClient, useConstant, useFaas };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/react",
3
- "version": "2.3.1",
3
+ "version": "2.5.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.3.1"
25
+ "@faasjs/browser": "2.5.0"
26
26
  },
27
27
  "peerDependencies": {
28
28
  "react": "*"