@faasjs/react 1.7.2 → 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 +1 -14
- package/dist/index.d.mts +70 -46
- package/dist/index.d.ts +70 -46
- package/dist/index.js +36 -55
- package/dist/index.mjs +37 -25
- package/package.json +4 -7
package/README.md
CHANGED
|
@@ -27,17 +27,6 @@ const client = FaasReactClient({
|
|
|
27
27
|
|
|
28
28
|
2. Use [faas](#faas), [useFaas](#usefaas) or [FaasDataWrapper](#faasdatawrapper).
|
|
29
29
|
|
|
30
|
-
## Usage with [@preact/signal-react](https://github.com/preactjs/signals/blob/main/packages/react/README.md)
|
|
31
|
-
|
|
32
|
-
1. `npm i --save-dev @preact/signals-react-transform`
|
|
33
|
-
2. Add `@preact/signals-react-transform` to babel config:
|
|
34
|
-
```json
|
|
35
|
-
{
|
|
36
|
-
"plugins": [["module:@preact/signals-react-transform"]]
|
|
37
|
-
}
|
|
38
|
-
```
|
|
39
|
-
3. Add `import '@preact/signals-react/auto'` to your test files.
|
|
40
|
-
|
|
41
30
|
## Classes
|
|
42
31
|
|
|
43
32
|
- [ErrorBoundary](classes/ErrorBoundary.md)
|
|
@@ -60,16 +49,14 @@ const client = FaasReactClient({
|
|
|
60
49
|
- [FaasReactClientOptions](type-aliases/FaasReactClientOptions.md)
|
|
61
50
|
- [Options](type-aliases/Options.md)
|
|
62
51
|
- [ResponseHeaders](type-aliases/ResponseHeaders.md)
|
|
63
|
-
- [SignalOptions](type-aliases/SignalOptions.md)
|
|
64
52
|
- [useFaasOptions](type-aliases/useFaasOptions.md)
|
|
65
53
|
|
|
66
54
|
## Functions
|
|
67
55
|
|
|
68
56
|
- [FaasDataWrapper](functions/FaasDataWrapper.md)
|
|
69
57
|
- [FaasReactClient](functions/FaasReactClient.md)
|
|
58
|
+
- [createSplitedContext](functions/createSplitedContext.md)
|
|
70
59
|
- [faas](functions/faas.md)
|
|
71
60
|
- [getClient](functions/getClient.md)
|
|
72
|
-
- [signal](functions/signal.md)
|
|
73
61
|
- [useConstant](functions/useConstant.md)
|
|
74
62
|
- [useFaas](functions/useFaas.md)
|
|
75
|
-
- [useSignalState](functions/useSignalState.md)
|
package/dist/index.d.mts
CHANGED
|
@@ -3,15 +3,79 @@ export { FaasAction, FaasData, FaasParams } from '@faasjs/types';
|
|
|
3
3
|
import { Options, ResponseError, Response as Response$1 } from '@faasjs/browser';
|
|
4
4
|
export { Options, Response, ResponseError, ResponseHeaders } from '@faasjs/browser';
|
|
5
5
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
6
|
-
import { ReactNode, ReactElement, Component
|
|
7
|
-
import * as _preact_signals_core from '@preact/signals-core';
|
|
8
|
-
export { batch, computed, effect, signal as originSignal } from '@preact/signals-react';
|
|
9
|
-
export { useSignal as originUseSignal, useComputed, useSignalEffect, useSignals } from '@preact/signals-react/runtime';
|
|
6
|
+
import { ReactNode, ReactElement, Component } from 'react';
|
|
10
7
|
|
|
11
8
|
/**
|
|
12
9
|
* Returns a constant value that is created by the given function.
|
|
13
10
|
*/
|
|
14
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
|
+
};
|
|
15
79
|
|
|
16
80
|
type FaasReactClientInstance = {
|
|
17
81
|
id: string
|
|
@@ -40,7 +104,7 @@ type FaasDataInjection<Data = any> = {
|
|
|
40
104
|
data: Data
|
|
41
105
|
error: any
|
|
42
106
|
promise: Promise<Response<Data>>
|
|
43
|
-
reload(params?: Record<string, any>):
|
|
107
|
+
reload(params?: Record<string, any>): Promise<Response<Data>>
|
|
44
108
|
setData: React.Dispatch<React.SetStateAction<Data>>
|
|
45
109
|
setLoading: React.Dispatch<React.SetStateAction<boolean>>
|
|
46
110
|
setPromise: React.Dispatch<React.SetStateAction<Promise<Response<Data>>>>
|
|
@@ -181,44 +245,4 @@ declare class ErrorBoundary extends Component<ErrorBoundaryProps, {
|
|
|
181
245
|
render(): string | number | boolean | Iterable<ReactNode> | react_jsx_runtime.JSX.Element;
|
|
182
246
|
}
|
|
183
247
|
|
|
184
|
-
type
|
|
185
|
-
debugName?: string;
|
|
186
|
-
};
|
|
187
|
-
/**
|
|
188
|
-
* Create a [signal](https://preactjs.com/guide/v10/signals) with options
|
|
189
|
-
*
|
|
190
|
-
* @param initialValue
|
|
191
|
-
* @param options
|
|
192
|
-
* @param options.debugName - debug name for signal, will print signal value to console.debug
|
|
193
|
-
*
|
|
194
|
-
* @example
|
|
195
|
-
* ```ts
|
|
196
|
-
* import { signal } from '@faasjs/react'
|
|
197
|
-
*
|
|
198
|
-
* const count = signal(0, { debugName: 'count' })
|
|
199
|
-
*
|
|
200
|
-
* count.value = 1
|
|
201
|
-
* ```
|
|
202
|
-
*/
|
|
203
|
-
declare function signal<T = any>(initialValue: any, options?: SignalOptions): _preact_signals_core.Signal<T>;
|
|
204
|
-
/**
|
|
205
|
-
* Create a [signal](https://preactjs.com/guide/v10/signals) like useState.
|
|
206
|
-
*
|
|
207
|
-
* ```tsx
|
|
208
|
-
* import { useSignalState, useSignalEffect } from '@faasjs/react'
|
|
209
|
-
*
|
|
210
|
-
* function App () {
|
|
211
|
-
* const [count, setCount] = useSignalState(0, { debugName: 'count' })
|
|
212
|
-
*
|
|
213
|
-
* useSignalEffect(() => console.log('count', count))
|
|
214
|
-
*
|
|
215
|
-
* return <div>
|
|
216
|
-
* <button onClick={() => setCount(count + 1)}>+</button>
|
|
217
|
-
* <span>{count}</span>
|
|
218
|
-
* </div>
|
|
219
|
-
* }
|
|
220
|
-
* ```
|
|
221
|
-
*/
|
|
222
|
-
declare function useSignalState<T = any>(initialValue: T, options?: SignalOptions): readonly [T, (changes: SetStateAction<T>) => void];
|
|
223
|
-
|
|
224
|
-
export { ErrorBoundary, type ErrorBoundaryProps, type ErrorChildrenProps, type FaasDataInjection, FaasDataWrapper, type FaasDataWrapperProps, FaasReactClient, type FaasReactClientInstance, type FaasReactClientOptions, type SignalOptions, faas, getClient, signal, useConstant, useFaas, type useFaasOptions, useSignalState };
|
|
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
|
@@ -3,15 +3,79 @@ export { FaasAction, FaasData, FaasParams } from '@faasjs/types';
|
|
|
3
3
|
import { Options, ResponseError, Response as Response$1 } from '@faasjs/browser';
|
|
4
4
|
export { Options, Response, ResponseError, ResponseHeaders } from '@faasjs/browser';
|
|
5
5
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
6
|
-
import { ReactNode, ReactElement, Component
|
|
7
|
-
import * as _preact_signals_core from '@preact/signals-core';
|
|
8
|
-
export { batch, computed, effect, signal as originSignal } from '@preact/signals-react';
|
|
9
|
-
export { useSignal as originUseSignal, useComputed, useSignalEffect, useSignals } from '@preact/signals-react/runtime';
|
|
6
|
+
import { ReactNode, ReactElement, Component } from 'react';
|
|
10
7
|
|
|
11
8
|
/**
|
|
12
9
|
* Returns a constant value that is created by the given function.
|
|
13
10
|
*/
|
|
14
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
|
+
};
|
|
15
79
|
|
|
16
80
|
type FaasReactClientInstance = {
|
|
17
81
|
id: string
|
|
@@ -40,7 +104,7 @@ type FaasDataInjection<Data = any> = {
|
|
|
40
104
|
data: Data
|
|
41
105
|
error: any
|
|
42
106
|
promise: Promise<Response<Data>>
|
|
43
|
-
reload(params?: Record<string, any>):
|
|
107
|
+
reload(params?: Record<string, any>): Promise<Response<Data>>
|
|
44
108
|
setData: React.Dispatch<React.SetStateAction<Data>>
|
|
45
109
|
setLoading: React.Dispatch<React.SetStateAction<boolean>>
|
|
46
110
|
setPromise: React.Dispatch<React.SetStateAction<Promise<Response<Data>>>>
|
|
@@ -181,44 +245,4 @@ declare class ErrorBoundary extends Component<ErrorBoundaryProps, {
|
|
|
181
245
|
render(): string | number | boolean | Iterable<ReactNode> | react_jsx_runtime.JSX.Element;
|
|
182
246
|
}
|
|
183
247
|
|
|
184
|
-
type
|
|
185
|
-
debugName?: string;
|
|
186
|
-
};
|
|
187
|
-
/**
|
|
188
|
-
* Create a [signal](https://preactjs.com/guide/v10/signals) with options
|
|
189
|
-
*
|
|
190
|
-
* @param initialValue
|
|
191
|
-
* @param options
|
|
192
|
-
* @param options.debugName - debug name for signal, will print signal value to console.debug
|
|
193
|
-
*
|
|
194
|
-
* @example
|
|
195
|
-
* ```ts
|
|
196
|
-
* import { signal } from '@faasjs/react'
|
|
197
|
-
*
|
|
198
|
-
* const count = signal(0, { debugName: 'count' })
|
|
199
|
-
*
|
|
200
|
-
* count.value = 1
|
|
201
|
-
* ```
|
|
202
|
-
*/
|
|
203
|
-
declare function signal<T = any>(initialValue: any, options?: SignalOptions): _preact_signals_core.Signal<T>;
|
|
204
|
-
/**
|
|
205
|
-
* Create a [signal](https://preactjs.com/guide/v10/signals) like useState.
|
|
206
|
-
*
|
|
207
|
-
* ```tsx
|
|
208
|
-
* import { useSignalState, useSignalEffect } from '@faasjs/react'
|
|
209
|
-
*
|
|
210
|
-
* function App () {
|
|
211
|
-
* const [count, setCount] = useSignalState(0, { debugName: 'count' })
|
|
212
|
-
*
|
|
213
|
-
* useSignalEffect(() => console.log('count', count))
|
|
214
|
-
*
|
|
215
|
-
* return <div>
|
|
216
|
-
* <button onClick={() => setCount(count + 1)}>+</button>
|
|
217
|
-
* <span>{count}</span>
|
|
218
|
-
* </div>
|
|
219
|
-
* }
|
|
220
|
-
* ```
|
|
221
|
-
*/
|
|
222
|
-
declare function useSignalState<T = any>(initialValue: T, options?: SignalOptions): readonly [T, (changes: SetStateAction<T>) => void];
|
|
223
|
-
|
|
224
|
-
export { ErrorBoundary, type ErrorBoundaryProps, type ErrorChildrenProps, type FaasDataInjection, FaasDataWrapper, type FaasDataWrapperProps, FaasReactClient, type FaasReactClientInstance, type FaasReactClientOptions, type SignalOptions, faas, getClient, signal, useConstant, useFaas, type useFaasOptions, useSignalState };
|
|
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,10 +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');
|
|
6
|
-
var
|
|
7
|
-
var runtime = require('@preact/signals-react/runtime');
|
|
5
|
+
var browser = require('@faasjs/browser');
|
|
8
6
|
|
|
9
7
|
// src/constant.ts
|
|
10
8
|
function useConstant(fn) {
|
|
@@ -14,6 +12,38 @@ function useConstant(fn) {
|
|
|
14
12
|
}
|
|
15
13
|
return ref.current.v;
|
|
16
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
|
+
}
|
|
17
47
|
var clients = {};
|
|
18
48
|
function FaasReactClient({
|
|
19
49
|
domain,
|
|
@@ -110,6 +140,7 @@ function FaasReactClient({
|
|
|
110
140
|
if (params2)
|
|
111
141
|
setParams(params2);
|
|
112
142
|
setReloadTimes((prev) => prev + 1);
|
|
143
|
+
return promise;
|
|
113
144
|
},
|
|
114
145
|
[params]
|
|
115
146
|
);
|
|
@@ -241,63 +272,13 @@ var ErrorBoundary = class extends react.Component {
|
|
|
241
272
|
}
|
|
242
273
|
};
|
|
243
274
|
ErrorBoundary.whyDidYouRender = true;
|
|
244
|
-
|
|
245
|
-
const state = signalsReact.signal(initialValue);
|
|
246
|
-
if (options.debugName)
|
|
247
|
-
signalsReact.effect(() => console.debug(options.debugName, state.value));
|
|
248
|
-
return state;
|
|
249
|
-
}
|
|
250
|
-
function useSignalState(initialValue, options = {}) {
|
|
251
|
-
const state = runtime.useSignal(initialValue);
|
|
252
|
-
if (options.debugName) {
|
|
253
|
-
runtime.useSignalEffect(() => console.log(options.debugName, state.value));
|
|
254
|
-
}
|
|
255
|
-
return [
|
|
256
|
-
state.value,
|
|
257
|
-
(changes) => {
|
|
258
|
-
state.value = typeof changes === "function" ? changes(state.value) : changes;
|
|
259
|
-
}
|
|
260
|
-
];
|
|
261
|
-
}
|
|
275
|
+
ErrorBoundary.whyDidYouRender = true;
|
|
262
276
|
|
|
263
|
-
Object.defineProperty(exports, "batch", {
|
|
264
|
-
enumerable: true,
|
|
265
|
-
get: function () { return signalsReact.batch; }
|
|
266
|
-
});
|
|
267
|
-
Object.defineProperty(exports, "computed", {
|
|
268
|
-
enumerable: true,
|
|
269
|
-
get: function () { return signalsReact.computed; }
|
|
270
|
-
});
|
|
271
|
-
Object.defineProperty(exports, "effect", {
|
|
272
|
-
enumerable: true,
|
|
273
|
-
get: function () { return signalsReact.effect; }
|
|
274
|
-
});
|
|
275
|
-
Object.defineProperty(exports, "originSignal", {
|
|
276
|
-
enumerable: true,
|
|
277
|
-
get: function () { return signalsReact.signal; }
|
|
278
|
-
});
|
|
279
|
-
Object.defineProperty(exports, "originUseSignal", {
|
|
280
|
-
enumerable: true,
|
|
281
|
-
get: function () { return runtime.useSignal; }
|
|
282
|
-
});
|
|
283
|
-
Object.defineProperty(exports, "useComputed", {
|
|
284
|
-
enumerable: true,
|
|
285
|
-
get: function () { return runtime.useComputed; }
|
|
286
|
-
});
|
|
287
|
-
Object.defineProperty(exports, "useSignalEffect", {
|
|
288
|
-
enumerable: true,
|
|
289
|
-
get: function () { return runtime.useSignalEffect; }
|
|
290
|
-
});
|
|
291
|
-
Object.defineProperty(exports, "useSignals", {
|
|
292
|
-
enumerable: true,
|
|
293
|
-
get: function () { return runtime.useSignals; }
|
|
294
|
-
});
|
|
295
277
|
exports.ErrorBoundary = ErrorBoundary;
|
|
296
278
|
exports.FaasDataWrapper = FaasDataWrapper;
|
|
297
279
|
exports.FaasReactClient = FaasReactClient;
|
|
280
|
+
exports.createSplitedContext = createSplitedContext;
|
|
298
281
|
exports.faas = faas;
|
|
299
282
|
exports.getClient = getClient;
|
|
300
|
-
exports.signal = signal;
|
|
301
283
|
exports.useConstant = useConstant;
|
|
302
284
|
exports.useFaas = useFaas;
|
|
303
|
-
exports.useSignalState = useSignalState;
|
package/dist/index.mjs
CHANGED
|
@@ -1,10 +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
|
-
import { signal as signal$1, effect } from '@preact/signals-react';
|
|
5
|
-
export { batch, computed, effect, signal as originSignal } from '@preact/signals-react';
|
|
6
|
-
import { useSignal, useSignalEffect } from '@preact/signals-react/runtime';
|
|
7
|
-
export { useSignal as originUseSignal, useComputed, useSignalEffect, useSignals } from '@preact/signals-react/runtime';
|
|
8
4
|
|
|
9
5
|
// src/constant.ts
|
|
10
6
|
function useConstant(fn) {
|
|
@@ -14,6 +10,38 @@ function useConstant(fn) {
|
|
|
14
10
|
}
|
|
15
11
|
return ref.current.v;
|
|
16
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
|
+
}
|
|
17
45
|
var clients = {};
|
|
18
46
|
function FaasReactClient({
|
|
19
47
|
domain,
|
|
@@ -110,6 +138,7 @@ function FaasReactClient({
|
|
|
110
138
|
if (params2)
|
|
111
139
|
setParams(params2);
|
|
112
140
|
setReloadTimes((prev) => prev + 1);
|
|
141
|
+
return promise;
|
|
113
142
|
},
|
|
114
143
|
[params]
|
|
115
144
|
);
|
|
@@ -241,23 +270,6 @@ var ErrorBoundary = class extends Component {
|
|
|
241
270
|
}
|
|
242
271
|
};
|
|
243
272
|
ErrorBoundary.whyDidYouRender = true;
|
|
244
|
-
|
|
245
|
-
const state = signal$1(initialValue);
|
|
246
|
-
if (options.debugName)
|
|
247
|
-
effect(() => console.debug(options.debugName, state.value));
|
|
248
|
-
return state;
|
|
249
|
-
}
|
|
250
|
-
function useSignalState(initialValue, options = {}) {
|
|
251
|
-
const state = useSignal(initialValue);
|
|
252
|
-
if (options.debugName) {
|
|
253
|
-
useSignalEffect(() => console.log(options.debugName, state.value));
|
|
254
|
-
}
|
|
255
|
-
return [
|
|
256
|
-
state.value,
|
|
257
|
-
(changes) => {
|
|
258
|
-
state.value = typeof changes === "function" ? changes(state.value) : changes;
|
|
259
|
-
}
|
|
260
|
-
];
|
|
261
|
-
}
|
|
273
|
+
ErrorBoundary.whyDidYouRender = true;
|
|
262
274
|
|
|
263
|
-
export { ErrorBoundary, FaasDataWrapper, FaasReactClient, faas, getClient,
|
|
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": "1.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -22,17 +22,14 @@
|
|
|
22
22
|
"dist"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@faasjs/browser": "1.
|
|
25
|
+
"@faasjs/browser": "2.1.0"
|
|
26
26
|
},
|
|
27
27
|
"peerDependencies": {
|
|
28
|
-
"react": "*"
|
|
29
|
-
"@preact/signals-react": "*"
|
|
28
|
+
"react": "*"
|
|
30
29
|
},
|
|
31
30
|
"devDependencies": {
|
|
32
31
|
"@types/react": "*",
|
|
33
|
-
"react": "*"
|
|
34
|
-
"@preact/signals-react": "*",
|
|
35
|
-
"@types/use-sync-external-store": "*"
|
|
32
|
+
"react": "*"
|
|
36
33
|
},
|
|
37
34
|
"engines": {
|
|
38
35
|
"npm": ">=9.0.0",
|