frosty 0.0.13 → 0.0.14
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/dist/_native.js +3 -4
- package/dist/_native.js.map +1 -1
- package/dist/_native.mjs +2 -3
- package/dist/_native.mjs.map +1 -1
- package/dist/dom.js +3 -4
- package/dist/dom.js.map +1 -1
- package/dist/dom.mjs +3 -4
- package/dist/dom.mjs.map +1 -1
- package/dist/index.d.ts +307 -6
- package/dist/index.js +668 -27
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +639 -6
- package/dist/index.mjs.map +1 -1
- package/dist/internals/{common-CEjwzc0j.js → common-FG7T3c2f.js} +2 -2
- package/dist/internals/{common-CEjwzc0j.js.map → common-FG7T3c2f.js.map} +1 -1
- package/dist/internals/{common-CQOkvSXp.mjs → common-KSKXTzR8.mjs} +2 -2
- package/dist/internals/{common-CQOkvSXp.mjs.map → common-KSKXTzR8.mjs.map} +1 -1
- package/dist/internals/{renderer-CTxeL82F.mjs → renderer-GTnF-hYa.mjs} +2 -3
- package/dist/internals/{renderer-CTxeL82F.mjs.map → renderer-GTnF-hYa.mjs.map} +1 -1
- package/dist/internals/{renderer-BL3b1IbL.js → renderer-jjw2FwKT.js} +8 -9
- package/dist/internals/{renderer-BL3b1IbL.js.map → renderer-jjw2FwKT.js.map} +1 -1
- package/dist/internals/{sync-Cq3pCYO3.mjs → sync-B9zfuWYQ.mjs} +60 -60
- package/dist/internals/sync-B9zfuWYQ.mjs.map +1 -0
- package/dist/internals/{sync-CSxmMfGm.js → sync-BIbWmXEq.js} +74 -74
- package/dist/internals/sync-BIbWmXEq.js.map +1 -0
- package/dist/internals/{state-BiYNImPF.mjs → utils-BJL3yvTI.mjs} +61 -48
- package/dist/internals/utils-BJL3yvTI.mjs.map +1 -0
- package/dist/internals/{state-DH_D-jro.js → utils-Bipfwp1R.js} +61 -47
- package/dist/internals/utils-Bipfwp1R.js.map +1 -0
- package/dist/jsx-runtime.js +29 -2
- package/dist/jsx-runtime.js.map +1 -1
- package/dist/jsx-runtime.mjs +29 -2
- package/dist/jsx-runtime.mjs.map +1 -1
- package/dist/server-dom.js +3 -4
- package/dist/server-dom.js.map +1 -1
- package/dist/server-dom.mjs +3 -4
- package/dist/server-dom.mjs.map +1 -1
- package/dist/web.d.ts +2 -2
- package/dist/web.js +30 -15
- package/dist/web.js.map +1 -1
- package/dist/web.mjs +26 -11
- package/dist/web.mjs.map +1 -1
- package/package.json +1 -1
- package/dist/hooks.d.ts +0 -301
- package/dist/hooks.d.ts.map +0 -1
- package/dist/hooks.js +0 -456
- package/dist/hooks.js.map +0 -1
- package/dist/hooks.mjs +0 -447
- package/dist/hooks.mjs.map +0 -1
- package/dist/internals/fragment-B0rB4qEp.js +0 -32
- package/dist/internals/fragment-B0rB4qEp.js.map +0 -1
- package/dist/internals/fragment-DPs1AriN.mjs +0 -30
- package/dist/internals/fragment-DPs1AriN.mjs.map +0 -1
- package/dist/internals/state-0jEjYdYV.js +0 -203
- package/dist/internals/state-0jEjYdYV.js.map +0 -1
- package/dist/internals/state-BiYNImPF.mjs.map +0 -1
- package/dist/internals/state-DH_D-jro.js.map +0 -1
- package/dist/internals/state-zydBhS8M.mjs +0 -197
- package/dist/internals/state-zydBhS8M.mjs.map +0 -1
- package/dist/internals/sync-CSxmMfGm.js.map +0 -1
- package/dist/internals/sync-Cq3pCYO3.mjs.map +0 -1
- package/dist/internals/utils-DAEKakog.mjs +0 -42
- package/dist/internals/utils-DAEKakog.mjs.map +0 -1
- package/dist/internals/utils-Dc66ARNS.js +0 -44
- package/dist/internals/utils-Dc66ARNS.js.map +0 -1
package/dist/index.mjs
CHANGED
|
@@ -1,13 +1,638 @@
|
|
|
1
|
-
import { u as useAsyncDebounce, a as useDebounce, b as useRef, c as useRefHandle, d as useState } from './internals/state-zydBhS8M.mjs';
|
|
2
|
-
import { _ as _useMemo, u as useCallback, a as useEffect, b as useMemo, c as useSyncExternalStore } from './internals/sync-Cq3pCYO3.mjs';
|
|
3
|
-
import { r as reconciler, E as ErrorBoundary, P as PropsProvider, c as createContext, u as useContext } from './internals/state-BiYNImPF.mjs';
|
|
4
1
|
import _ from 'lodash';
|
|
2
|
+
import { _ as _useEffect, a as _useMemo, u as useCallback, b as useMemo, c as useEffect, d as useSyncExternalStore } from './internals/sync-B9zfuWYQ.mjs';
|
|
3
|
+
import { c as createContext, u as useContext, r as reconciler, E as ErrorBoundary, P as PropsProvider, m as mergeRefs } from './internals/utils-BJL3yvTI.mjs';
|
|
4
|
+
import { jsx, Fragment } from './jsx-runtime.mjs';
|
|
5
5
|
import { C as ComponentNode } from './internals/component-tyF62qKh.mjs';
|
|
6
|
-
import { F as Fragment } from './internals/fragment-DPs1AriN.mjs';
|
|
7
6
|
import { c as createElement } from './internals/runtime-BN27rc51.mjs';
|
|
8
|
-
import { m as mergeRefs } from './internals/utils-DAEKakog.mjs';
|
|
9
7
|
import 'myers.js';
|
|
10
8
|
|
|
9
|
+
//
|
|
10
|
+
// memo.ts
|
|
11
|
+
//
|
|
12
|
+
// The MIT License
|
|
13
|
+
// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.
|
|
14
|
+
//
|
|
15
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
16
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
17
|
+
// in the Software without restriction, including without limitation the rights
|
|
18
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
19
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
20
|
+
// furnished to do so, subject to the following conditions:
|
|
21
|
+
//
|
|
22
|
+
// The above copyright notice and this permission notice shall be included in
|
|
23
|
+
// all copies or substantial portions of the Software.
|
|
24
|
+
//
|
|
25
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
26
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
27
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
28
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
29
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
30
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
31
|
+
// THE SOFTWARE.
|
|
32
|
+
//
|
|
33
|
+
function useRef(initialValue) {
|
|
34
|
+
return _useMemo('useRef', () => ({ current: initialValue }), null);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Associates a reference with a value created by an initializer function.
|
|
38
|
+
*
|
|
39
|
+
* @template T The type of the reference.
|
|
40
|
+
* @template R The type of the value created by the initializer function.
|
|
41
|
+
* @param ref A reference object or a callback function to receive the value.
|
|
42
|
+
* @param init A function that initializes and returns the value to associate with the reference.
|
|
43
|
+
* @param deps An optional dependency array. The initializer function is re-executed when the dependencies change.
|
|
44
|
+
*/
|
|
45
|
+
const useRefHandle = (ref, init, deps) => _useEffect('useRefHandle', () => {
|
|
46
|
+
try {
|
|
47
|
+
if (ref) {
|
|
48
|
+
const _ref = init();
|
|
49
|
+
if (typeof ref === 'function')
|
|
50
|
+
ref(_ref);
|
|
51
|
+
else if (typeof ref === 'object')
|
|
52
|
+
ref.current = _ref;
|
|
53
|
+
}
|
|
54
|
+
return () => void 0;
|
|
55
|
+
}
|
|
56
|
+
catch (e) {
|
|
57
|
+
console.error(e);
|
|
58
|
+
return () => void 0;
|
|
59
|
+
}
|
|
60
|
+
}, deps);
|
|
61
|
+
|
|
62
|
+
//
|
|
63
|
+
// memo.ts
|
|
64
|
+
//
|
|
65
|
+
// The MIT License
|
|
66
|
+
// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.
|
|
67
|
+
//
|
|
68
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
69
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
70
|
+
// in the Software without restriction, including without limitation the rights
|
|
71
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
72
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
73
|
+
// furnished to do so, subject to the following conditions:
|
|
74
|
+
//
|
|
75
|
+
// The above copyright notice and this permission notice shall be included in
|
|
76
|
+
// all copies or substantial portions of the Software.
|
|
77
|
+
//
|
|
78
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
79
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
80
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
81
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
82
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
83
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
84
|
+
// THE SOFTWARE.
|
|
85
|
+
//
|
|
86
|
+
function useState(initialState) {
|
|
87
|
+
const { value, setValue } = _useMemo('useState', ({ node }) => {
|
|
88
|
+
const state = {
|
|
89
|
+
value: _.isFunction(initialState) ? initialState() : initialState,
|
|
90
|
+
setValue: (dispatch) => {
|
|
91
|
+
state.value = _.isFunction(dispatch) ? dispatch(state.value) : dispatch;
|
|
92
|
+
node?._setDirty();
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
return state;
|
|
96
|
+
}, null);
|
|
97
|
+
return [value, setValue];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
//
|
|
101
|
+
// animate.ts
|
|
102
|
+
//
|
|
103
|
+
// The MIT License
|
|
104
|
+
// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.
|
|
105
|
+
//
|
|
106
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
107
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
108
|
+
// in the Software without restriction, including without limitation the rights
|
|
109
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
110
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
111
|
+
// furnished to do so, subject to the following conditions:
|
|
112
|
+
//
|
|
113
|
+
// The above copyright notice and this permission notice shall be included in
|
|
114
|
+
// all copies or substantial portions of the Software.
|
|
115
|
+
//
|
|
116
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
117
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
118
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
119
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
120
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
121
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
122
|
+
// THE SOFTWARE.
|
|
123
|
+
//
|
|
124
|
+
const interpolate = (value) => ({ inputRange, outputRange }) => {
|
|
125
|
+
const [inputMin, inputMax] = inputRange;
|
|
126
|
+
const [outputMin, outputMax] = outputRange;
|
|
127
|
+
// Safeguard against division by zero
|
|
128
|
+
if (inputMax === inputMin) {
|
|
129
|
+
throw new Error('Input range must have distinct values.');
|
|
130
|
+
}
|
|
131
|
+
const t = (value - inputMin) / (inputMax - inputMin);
|
|
132
|
+
const interpolatedValue = outputMin + t * (outputMax - outputMin);
|
|
133
|
+
return {
|
|
134
|
+
value: interpolatedValue,
|
|
135
|
+
interpolate: interpolate(interpolatedValue),
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
/**
|
|
139
|
+
* A hook to manage animations with support for starting, stopping, and interpolating values.
|
|
140
|
+
*
|
|
141
|
+
* @param initialValue - The initial value of the animation.
|
|
142
|
+
*
|
|
143
|
+
* @returns An object containing:
|
|
144
|
+
* - `value`: The current animated value.
|
|
145
|
+
* - `stop`: A function to stop the animation.
|
|
146
|
+
* - `start`: A function to start the animation with specified options.
|
|
147
|
+
* - `interpolate`: A function to interpolate the current value based on input and output ranges.
|
|
148
|
+
*/
|
|
149
|
+
const useAnimate = (initialValue) => {
|
|
150
|
+
const [value, setValue] = useState(initialValue);
|
|
151
|
+
const ref = useRef();
|
|
152
|
+
const _stop = () => {
|
|
153
|
+
const { interval, callback } = ref.current ?? {};
|
|
154
|
+
ref.current = undefined;
|
|
155
|
+
if (interval)
|
|
156
|
+
clearInterval(interval);
|
|
157
|
+
return callback;
|
|
158
|
+
};
|
|
159
|
+
const stop = useCallback(() => {
|
|
160
|
+
const callback = _stop();
|
|
161
|
+
if (_.isFunction(callback))
|
|
162
|
+
callback({ value, finished: false });
|
|
163
|
+
});
|
|
164
|
+
const start = useCallback(({ fromValue = value, toValue, duration, easing = (x) => x, delay = 0, onCompleted, }) => {
|
|
165
|
+
_stop();
|
|
166
|
+
const start = Date.now();
|
|
167
|
+
if (duration > 0) {
|
|
168
|
+
ref.current = {
|
|
169
|
+
interval: setInterval(() => {
|
|
170
|
+
const t = (Date.now() - start) / duration - delay;
|
|
171
|
+
if (t >= 1) {
|
|
172
|
+
clearInterval(ref.current?.interval);
|
|
173
|
+
ref.current = undefined;
|
|
174
|
+
setValue(toValue);
|
|
175
|
+
if (_.isFunction(onCompleted))
|
|
176
|
+
onCompleted({ value: toValue, finished: true });
|
|
177
|
+
}
|
|
178
|
+
else if (t >= 0) {
|
|
179
|
+
setValue((toValue - fromValue) * easing(_.clamp(t, 0, 1)) + fromValue);
|
|
180
|
+
}
|
|
181
|
+
}, 16),
|
|
182
|
+
callback: onCompleted,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
return {
|
|
187
|
+
value,
|
|
188
|
+
stop,
|
|
189
|
+
start,
|
|
190
|
+
interpolate: interpolate(value),
|
|
191
|
+
};
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
//
|
|
195
|
+
// debounce.ts
|
|
196
|
+
//
|
|
197
|
+
// The MIT License
|
|
198
|
+
// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.
|
|
199
|
+
//
|
|
200
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
201
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
202
|
+
// in the Software without restriction, including without limitation the rights
|
|
203
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
204
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
205
|
+
// furnished to do so, subject to the following conditions:
|
|
206
|
+
//
|
|
207
|
+
// The above copyright notice and this permission notice shall be included in
|
|
208
|
+
// all copies or substantial portions of the Software.
|
|
209
|
+
//
|
|
210
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
211
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
212
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
213
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
214
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
215
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
216
|
+
// THE SOFTWARE.
|
|
217
|
+
//
|
|
218
|
+
const debounce = (callback, settings) => {
|
|
219
|
+
const { wait, ...options } = settings;
|
|
220
|
+
return _.debounce(callback, wait, {
|
|
221
|
+
...options,
|
|
222
|
+
leading: 'leading' in options ? !!options.leading : true,
|
|
223
|
+
trailing: 'trailing' in options ? !!options.trailing : true,
|
|
224
|
+
});
|
|
225
|
+
};
|
|
226
|
+
const asyncDebounce = (func, settings) => {
|
|
227
|
+
let preflight;
|
|
228
|
+
const debounced = debounce(async (resolve, ...args) => {
|
|
229
|
+
const result = func(...args);
|
|
230
|
+
if (_.isFunction(resolve))
|
|
231
|
+
resolve(result);
|
|
232
|
+
return result;
|
|
233
|
+
}, settings);
|
|
234
|
+
return (...args) => {
|
|
235
|
+
if (_.isNil(preflight)) {
|
|
236
|
+
preflight = new Promise(r => debounced(r, ...args));
|
|
237
|
+
return preflight;
|
|
238
|
+
}
|
|
239
|
+
return debounced(undefined, ...args) ?? preflight;
|
|
240
|
+
};
|
|
241
|
+
};
|
|
242
|
+
/**
|
|
243
|
+
* A hook that creates a debounced version of a function.
|
|
244
|
+
* The debounced function delays invoking the callback until after
|
|
245
|
+
* the specified wait time has elapsed since the last time it was called.
|
|
246
|
+
*
|
|
247
|
+
* This is useful for optimizing performance in scenarios where frequent
|
|
248
|
+
* function calls (e.g., during user input or window resizing) can be expensive.
|
|
249
|
+
*
|
|
250
|
+
* @template T The type of the callback function.
|
|
251
|
+
* @param callback The function to debounce.
|
|
252
|
+
* @param settings Configuration options for debouncing, including:
|
|
253
|
+
* - `wait` (number): The number of milliseconds to delay.
|
|
254
|
+
* - Other lodash debounce options such as `leading` and `trailing`.
|
|
255
|
+
* @returns A debounced version of the callback function.
|
|
256
|
+
*/
|
|
257
|
+
const useDebounce = (callback, settings) => {
|
|
258
|
+
const store = _useMemo('useDebounce', () => {
|
|
259
|
+
const store = {
|
|
260
|
+
current: callback,
|
|
261
|
+
stable: debounce(((...args) => store.current(...args)), settings),
|
|
262
|
+
};
|
|
263
|
+
return store;
|
|
264
|
+
}, null);
|
|
265
|
+
store.current = callback;
|
|
266
|
+
return store.stable;
|
|
267
|
+
};
|
|
268
|
+
/**
|
|
269
|
+
* A hook that creates a debounced version of an asynchronous function.
|
|
270
|
+
* The debounced function delays invoking the callback until after
|
|
271
|
+
* the specified wait time has elapsed since the last time it was called.
|
|
272
|
+
*
|
|
273
|
+
* This is particularly useful for scenarios where frequent API calls
|
|
274
|
+
* or other asynchronous operations need to be throttled to improve performance.
|
|
275
|
+
*
|
|
276
|
+
* @template T The type of the asynchronous callback function.
|
|
277
|
+
* @param callback The asynchronous function to debounce.
|
|
278
|
+
* @param settings Configuration options for debouncing, including:
|
|
279
|
+
* - `wait` (number): The number of milliseconds to delay.
|
|
280
|
+
* - Other lodash debounce options such as `leading` and `trailing`.
|
|
281
|
+
* @returns A debounced version of the asynchronous callback function.
|
|
282
|
+
*/
|
|
283
|
+
const useAsyncDebounce = (callback, settings) => {
|
|
284
|
+
const store = _useMemo('useAsyncDebounce', () => {
|
|
285
|
+
const store = {
|
|
286
|
+
current: callback,
|
|
287
|
+
stable: asyncDebounce(((...args) => store.current(...args)), settings),
|
|
288
|
+
};
|
|
289
|
+
return store;
|
|
290
|
+
}, null);
|
|
291
|
+
store.current = callback;
|
|
292
|
+
return store.stable;
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
const Context = createContext({
|
|
296
|
+
errors: [],
|
|
297
|
+
setErrors: () => { },
|
|
298
|
+
});
|
|
299
|
+
/**
|
|
300
|
+
* A context provider component for managing asynchronous resource errors.
|
|
301
|
+
*
|
|
302
|
+
* This component provides a shared context for tracking errors encountered during
|
|
303
|
+
* asynchronous operations. It allows child components to access and manage these errors
|
|
304
|
+
* using the `useResourceErrors` hook.
|
|
305
|
+
*
|
|
306
|
+
* ### Usage:
|
|
307
|
+
* Wrap your application or specific parts of it with this component to enable error tracking:
|
|
308
|
+
*
|
|
309
|
+
* ```tsx
|
|
310
|
+
* <ResourceErrors>
|
|
311
|
+
* <YourComponent />
|
|
312
|
+
* </ResourceErrors>
|
|
313
|
+
* ```
|
|
314
|
+
*
|
|
315
|
+
* @param children - The child components that will have access to the error context.
|
|
316
|
+
*
|
|
317
|
+
* @returns A context provider that wraps the provided children.
|
|
318
|
+
*/
|
|
319
|
+
const ResourceErrors = ({ children }) => {
|
|
320
|
+
const [errors, setErrors] = useState([]);
|
|
321
|
+
const value = useMemo(() => ({ errors, setErrors }), [errors, setErrors]);
|
|
322
|
+
return (jsx(Context, { value: value, children: children }));
|
|
323
|
+
};
|
|
324
|
+
/**
|
|
325
|
+
* A hook to access the list of asynchronous resource errors.
|
|
326
|
+
*
|
|
327
|
+
* This hook allows components to retrieve the current list of errors being tracked
|
|
328
|
+
* in the `ResourceErrors` context. It must be used within a component that is
|
|
329
|
+
* a descendant of the `ResourceErrors` provider.
|
|
330
|
+
*
|
|
331
|
+
* ### Usage:
|
|
332
|
+
* ```tsx
|
|
333
|
+
* const errors = useResourceErrors();
|
|
334
|
+
*
|
|
335
|
+
* errors.forEach(({ token, error, refresh }) => {
|
|
336
|
+
* console.error(`Error [${token}]:`, error);
|
|
337
|
+
* // Optionally call refresh() to retry the operation
|
|
338
|
+
* });
|
|
339
|
+
* ```
|
|
340
|
+
*
|
|
341
|
+
* @returns The list of errors currently being tracked in the context. Each error includes:
|
|
342
|
+
* - `token`: A unique identifier for the error.
|
|
343
|
+
* - `error`: The error object or message.
|
|
344
|
+
* - `refresh`: A function to retry the operation that caused the error.
|
|
345
|
+
*/
|
|
346
|
+
const useResourceErrors = () => useContext(Context).errors;
|
|
347
|
+
|
|
348
|
+
//
|
|
349
|
+
// index.ts
|
|
350
|
+
//
|
|
351
|
+
// The MIT License
|
|
352
|
+
// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.
|
|
353
|
+
//
|
|
354
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
355
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
356
|
+
// in the Software without restriction, including without limitation the rights
|
|
357
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
358
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
359
|
+
// furnished to do so, subject to the following conditions:
|
|
360
|
+
//
|
|
361
|
+
// The above copyright notice and this permission notice shall be included in
|
|
362
|
+
// all copies or substantial portions of the Software.
|
|
363
|
+
//
|
|
364
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
365
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
366
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
367
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
368
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
369
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
370
|
+
// THE SOFTWARE.
|
|
371
|
+
//
|
|
372
|
+
/**
|
|
373
|
+
* A hook to manage asynchronous resources with support for debouncing, error handling, and state management.
|
|
374
|
+
*
|
|
375
|
+
* @template T - The type of the resource being fetched.
|
|
376
|
+
* @template P - The type of the parameters passed to the fetch function.
|
|
377
|
+
*
|
|
378
|
+
* @param config - The fetch function or a configuration object containing the fetch function and optional debounce settings.
|
|
379
|
+
* @param deps - An optional dependency array to control when the resource is refreshed.
|
|
380
|
+
*
|
|
381
|
+
* @returns An object containing:
|
|
382
|
+
* - `count`: The number of times the resource has been fetched.
|
|
383
|
+
* - `refreshing`: A boolean indicating if the resource is currently being refreshed.
|
|
384
|
+
* - `loading`: A boolean indicating if the resource is currently being loaded.
|
|
385
|
+
* - `resource`: The fetched resource.
|
|
386
|
+
* - `error`: Any error encountered during the fetch.
|
|
387
|
+
* - `cancel`: A function to cancel the current fetch operation.
|
|
388
|
+
* - `refresh`: A function to refresh the resource.
|
|
389
|
+
* - `next`: A function to fetch the next set of data (for paginated resources).
|
|
390
|
+
* - `setResource`: A function to manually update the resource state.
|
|
391
|
+
*/
|
|
392
|
+
const useResource = (config, deps) => {
|
|
393
|
+
const fetch = _.isFunction(config) ? config : config.fetch;
|
|
394
|
+
const debounce = _.isFunction(config) ? {} : config.debounce;
|
|
395
|
+
const [state, setState] = useState({});
|
|
396
|
+
const _dispatch = (token, next) => setState(state => state.token === token ? ({
|
|
397
|
+
...(_.isFunction(next) ? next(state.flag ? state : _.omit(state, 'resource', 'error')) : next),
|
|
398
|
+
count: state.flag ? state.count : (state.count ?? 0) + 1,
|
|
399
|
+
flag: true,
|
|
400
|
+
}) : state);
|
|
401
|
+
const _fetch = useAsyncDebounce(async (type, abort, reset, param, prevState) => {
|
|
402
|
+
const token = _.uniqueId();
|
|
403
|
+
setState(state => ({ ...state, type, token, abort, flag: !reset }));
|
|
404
|
+
try {
|
|
405
|
+
const resource = await fetch({
|
|
406
|
+
param,
|
|
407
|
+
prevState,
|
|
408
|
+
abortSignal: abort.signal,
|
|
409
|
+
dispatch: (next) => {
|
|
410
|
+
_dispatch(token, state => ({
|
|
411
|
+
...state,
|
|
412
|
+
resource: _.isFunction(next) ? next(state.resource) : next,
|
|
413
|
+
}));
|
|
414
|
+
},
|
|
415
|
+
});
|
|
416
|
+
_dispatch(token, state => ({ resource: resource ?? state.resource }));
|
|
417
|
+
}
|
|
418
|
+
catch (error) {
|
|
419
|
+
_dispatch(token, state => ({
|
|
420
|
+
resource: state.resource,
|
|
421
|
+
error,
|
|
422
|
+
}));
|
|
423
|
+
}
|
|
424
|
+
}, debounce ?? {});
|
|
425
|
+
useEffect(() => {
|
|
426
|
+
const controller = new AbortController();
|
|
427
|
+
void _fetch('refresh', controller, true);
|
|
428
|
+
return () => controller.abort();
|
|
429
|
+
}, deps ?? []);
|
|
430
|
+
const _cancelRef = useCallback((reason) => { state.abort?.abort(reason); });
|
|
431
|
+
const _refreshRef = useCallback((param) => _fetch('refresh', new AbortController(), true, param));
|
|
432
|
+
const _nextRef = useCallback((param) => _fetch('next', new AbortController(), false, param, state.resource));
|
|
433
|
+
const _setResRef = useCallback((resource) => setState(state => ({
|
|
434
|
+
..._.omit(state, 'resource', 'error'),
|
|
435
|
+
resource: _.isFunction(resource) ? resource(state.resource) : resource,
|
|
436
|
+
})));
|
|
437
|
+
const { setErrors } = useContext(Context);
|
|
438
|
+
useEffect(() => {
|
|
439
|
+
const { type, abort, token = _.uniqueId(), error } = state;
|
|
440
|
+
if (!error)
|
|
441
|
+
return;
|
|
442
|
+
setErrors(v => [...v, {
|
|
443
|
+
token,
|
|
444
|
+
error,
|
|
445
|
+
refresh: _refreshRef,
|
|
446
|
+
refreshing: !_.isNil(abort) && type === 'refresh',
|
|
447
|
+
loading: !_.isNil(abort),
|
|
448
|
+
}]);
|
|
449
|
+
return () => setErrors(v => _.filter(v, x => x.token !== token));
|
|
450
|
+
}, [state]);
|
|
451
|
+
return {
|
|
452
|
+
count: state.count ?? 0,
|
|
453
|
+
refreshing: !_.isNil(state.abort) && state.type === 'refresh',
|
|
454
|
+
loading: !_.isNil(state.abort),
|
|
455
|
+
resource: state.resource,
|
|
456
|
+
error: state.error,
|
|
457
|
+
cancel: _cancelRef,
|
|
458
|
+
refresh: _refreshRef,
|
|
459
|
+
next: _nextRef,
|
|
460
|
+
setResource: _setResRef,
|
|
461
|
+
};
|
|
462
|
+
};
|
|
463
|
+
/**
|
|
464
|
+
* A hook to manage asynchronous iterable resources, such as streams or paginated data.
|
|
465
|
+
*
|
|
466
|
+
* @template T - The type of the resource items being fetched.
|
|
467
|
+
* @template P - The type of the parameters passed to the fetch function.
|
|
468
|
+
*
|
|
469
|
+
* @param config - The fetch function or a configuration object containing the fetch function and optional debounce settings.
|
|
470
|
+
* @param deps - An optional dependency array to control when the resource is refreshed.
|
|
471
|
+
*
|
|
472
|
+
* @returns An object containing the same properties as `useResource`, but optimized for iterable resources.
|
|
473
|
+
*/
|
|
474
|
+
const useIterableResource = (config, deps) => {
|
|
475
|
+
const fetch = _.isFunction(config) ? config : config.fetch;
|
|
476
|
+
const debounce = _.isFunction(config) ? {} : config.debounce;
|
|
477
|
+
const { next, ...result } = useResource({
|
|
478
|
+
fetch: async ({ dispatch, abortSignal, param }) => {
|
|
479
|
+
const resource = await fetch({ abortSignal, param });
|
|
480
|
+
for await (const item of resource) {
|
|
481
|
+
dispatch(items => items ? [...items, item] : [item]);
|
|
482
|
+
}
|
|
483
|
+
},
|
|
484
|
+
debounce,
|
|
485
|
+
}, deps);
|
|
486
|
+
return result;
|
|
487
|
+
};
|
|
488
|
+
|
|
489
|
+
//
|
|
490
|
+
// interval.ts
|
|
491
|
+
//
|
|
492
|
+
// The MIT License
|
|
493
|
+
// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.
|
|
494
|
+
//
|
|
495
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
496
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
497
|
+
// in the Software without restriction, including without limitation the rights
|
|
498
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
499
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
500
|
+
// furnished to do so, subject to the following conditions:
|
|
501
|
+
//
|
|
502
|
+
// The above copyright notice and this permission notice shall be included in
|
|
503
|
+
// all copies or substantial portions of the Software.
|
|
504
|
+
//
|
|
505
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
506
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
507
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
508
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
509
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
510
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
511
|
+
// THE SOFTWARE.
|
|
512
|
+
//
|
|
513
|
+
/**
|
|
514
|
+
* A hook that repeatedly calls the provided callback function at the specified interval.
|
|
515
|
+
*
|
|
516
|
+
* @param callback - The function to be executed at each interval.
|
|
517
|
+
* @param ms - The delay in milliseconds between each call to the callback. If not provided, the interval will not be set.
|
|
518
|
+
* @returns void
|
|
519
|
+
*
|
|
520
|
+
* @example
|
|
521
|
+
* useInterval(() => {
|
|
522
|
+
* // Code to run every 1000ms
|
|
523
|
+
* }, 1000);
|
|
524
|
+
*/
|
|
525
|
+
const useInterval = (callback, ms) => useEffect(() => {
|
|
526
|
+
const interval = setInterval(() => {
|
|
527
|
+
callback();
|
|
528
|
+
}, ms);
|
|
529
|
+
return () => clearInterval(interval);
|
|
530
|
+
}, []);
|
|
531
|
+
|
|
532
|
+
//
|
|
533
|
+
// store.ts
|
|
534
|
+
//
|
|
535
|
+
// The MIT License
|
|
536
|
+
// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.
|
|
537
|
+
//
|
|
538
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
539
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
540
|
+
// in the Software without restriction, including without limitation the rights
|
|
541
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
542
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
543
|
+
// furnished to do so, subject to the following conditions:
|
|
544
|
+
//
|
|
545
|
+
// The above copyright notice and this permission notice shall be included in
|
|
546
|
+
// all copies or substantial portions of the Software.
|
|
547
|
+
//
|
|
548
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
549
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
550
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
551
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
552
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
553
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
554
|
+
// THE SOFTWARE.
|
|
555
|
+
//
|
|
556
|
+
/**
|
|
557
|
+
* A class representing a store that holds a value and allows for subscription
|
|
558
|
+
* to changes in that value.
|
|
559
|
+
*
|
|
560
|
+
* @template T - The type of the value stored in the store.
|
|
561
|
+
*
|
|
562
|
+
* @example
|
|
563
|
+
* const store = createStore(0);
|
|
564
|
+
* store.setValue(1);
|
|
565
|
+
* store.subscribe((oldVal, newVal) => {
|
|
566
|
+
* console.log(`Value changed from ${oldVal} to ${newVal}`);
|
|
567
|
+
* });
|
|
568
|
+
*/
|
|
569
|
+
class Store {
|
|
570
|
+
#listeners = new Set();
|
|
571
|
+
#value;
|
|
572
|
+
/** @internal */
|
|
573
|
+
constructor(initialValue) {
|
|
574
|
+
this.#value = initialValue;
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* Gets the current value of the store.
|
|
578
|
+
*
|
|
579
|
+
* @returns The current value of the store.
|
|
580
|
+
*/
|
|
581
|
+
get value() {
|
|
582
|
+
return this.#value;
|
|
583
|
+
}
|
|
584
|
+
/**
|
|
585
|
+
* Sets the value of the store and notifies all subscribers.
|
|
586
|
+
*
|
|
587
|
+
* @param dispatch - The new value or a function that returns the new value.
|
|
588
|
+
*/
|
|
589
|
+
setValue(dispatch) {
|
|
590
|
+
const oldVal = this.#value;
|
|
591
|
+
dispatch = _.isFunction(dispatch) ? dispatch(this.#value) : this.#value;
|
|
592
|
+
this.#listeners.forEach(listener => void listener(oldVal, this.#value));
|
|
593
|
+
}
|
|
594
|
+
/**
|
|
595
|
+
* Subscribes to changes in the store's value.
|
|
596
|
+
*
|
|
597
|
+
* @param callback - The function to call when the value changes.
|
|
598
|
+
* @returns A function to unsubscribe from the store.
|
|
599
|
+
*/
|
|
600
|
+
subscribe(callback) {
|
|
601
|
+
this.#listeners.add(callback);
|
|
602
|
+
return () => { this.#listeners.delete(callback); };
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
/**
|
|
606
|
+
* Creates a new store with the given initial value.
|
|
607
|
+
*
|
|
608
|
+
* @param initialValue - The initial value to be stored.
|
|
609
|
+
* @returns {Store<T>} A new store instance.
|
|
610
|
+
*
|
|
611
|
+
* @example
|
|
612
|
+
* const counterStore = createStore(0);
|
|
613
|
+
*/
|
|
614
|
+
const createStore = (initialValue) => new Store(initialValue);
|
|
615
|
+
/**
|
|
616
|
+
* A hook to subscribe to a store and select a slice of its state.
|
|
617
|
+
* The component will re-render when the selected state changes.
|
|
618
|
+
*
|
|
619
|
+
* @param store - The store instance to subscribe to.
|
|
620
|
+
* @param selector - A function to select a part of the store's state. Defaults to the entire state.
|
|
621
|
+
* @param equal - A function to compare selected values for equality. Defaults to deep equality.
|
|
622
|
+
* @returns The selected slice of the store's state.
|
|
623
|
+
*
|
|
624
|
+
* @example
|
|
625
|
+
* const count = useStore(counterStore);
|
|
626
|
+
*
|
|
627
|
+
* @example
|
|
628
|
+
* // Using a selector
|
|
629
|
+
* const userName = useStore(userStore, user => user.name);
|
|
630
|
+
*/
|
|
631
|
+
const useStore = (store, selector = v => v, equal = _.isEqual) => useSyncExternalStore((onStoreChange) => store.subscribe((oldVal, newVal) => {
|
|
632
|
+
if (equal(selector(oldVal), selector(newVal)))
|
|
633
|
+
onStoreChange();
|
|
634
|
+
}), () => selector(store.value));
|
|
635
|
+
|
|
11
636
|
//
|
|
12
637
|
// stack.ts
|
|
13
638
|
//
|
|
@@ -118,22 +743,30 @@ var common = /*#__PURE__*/Object.freeze({
|
|
|
118
743
|
ErrorBoundary: ErrorBoundary,
|
|
119
744
|
Fragment: Fragment,
|
|
120
745
|
PropsProvider: PropsProvider,
|
|
746
|
+
ResourceErrors: ResourceErrors,
|
|
121
747
|
createContext: createContext,
|
|
122
748
|
createElement: createElement,
|
|
749
|
+
createStore: createStore,
|
|
123
750
|
mergeRefs: mergeRefs,
|
|
751
|
+
useAnimate: useAnimate,
|
|
124
752
|
useAsyncDebounce: useAsyncDebounce,
|
|
125
753
|
useCallback: useCallback,
|
|
126
754
|
useContext: useContext,
|
|
127
755
|
useDebounce: useDebounce,
|
|
128
756
|
useEffect: useEffect,
|
|
757
|
+
useInterval: useInterval,
|
|
758
|
+
useIterableResource: useIterableResource,
|
|
129
759
|
useMemo: useMemo,
|
|
130
760
|
useReducer: useReducer,
|
|
131
761
|
useRef: useRef,
|
|
132
762
|
useRefHandle: useRefHandle,
|
|
763
|
+
useResource: useResource,
|
|
764
|
+
useResourceErrors: useResourceErrors,
|
|
133
765
|
useStack: useStack,
|
|
134
766
|
useState: useState,
|
|
767
|
+
useStore: useStore,
|
|
135
768
|
useSyncExternalStore: useSyncExternalStore
|
|
136
769
|
});
|
|
137
770
|
|
|
138
|
-
export { ComponentNode, ErrorBoundary, Fragment, PropsProvider, createContext, createElement, common as default, mergeRefs, useAsyncDebounce, useCallback, useContext, useDebounce, useEffect, useMemo, useReducer, useRef, useRefHandle, useStack, useState, useSyncExternalStore };
|
|
771
|
+
export { ComponentNode, ErrorBoundary, Fragment, PropsProvider, ResourceErrors, createContext, createElement, createStore, common as default, mergeRefs, useAnimate, useAsyncDebounce, useCallback, useContext, useDebounce, useEffect, useInterval, useIterableResource, useMemo, useReducer, useRef, useRefHandle, useResource, useResourceErrors, useStack, useState, useStore, useSyncExternalStore };
|
|
139
772
|
//# sourceMappingURL=index.mjs.map
|