@hairy/react-lib 1.15.0 → 1.17.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/dist/index.cjs +94 -195
- package/dist/index.d.ts +42 -55
- package/dist/index.global.js +103 -203
- package/dist/index.js +83 -185
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -5,9 +5,6 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
5
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
6
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __esm = (fn, res) => function __init() {
|
|
9
|
-
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
10
|
-
};
|
|
11
8
|
var __export = (target, all) => {
|
|
12
9
|
for (var name in all)
|
|
13
10
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -30,24 +27,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
30
27
|
));
|
|
31
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
32
29
|
|
|
33
|
-
// ../../node_modules/.pnpm/tslib@2.8.1/node_modules/tslib/tslib.es6.mjs
|
|
34
|
-
var __assign;
|
|
35
|
-
var init_tslib_es6 = __esm({
|
|
36
|
-
"../../node_modules/.pnpm/tslib@2.8.1/node_modules/tslib/tslib.es6.mjs"() {
|
|
37
|
-
"use strict";
|
|
38
|
-
__assign = function() {
|
|
39
|
-
__assign = Object.assign || function __assign2(t) {
|
|
40
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
41
|
-
s = arguments[i];
|
|
42
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
43
|
-
}
|
|
44
|
-
return t;
|
|
45
|
-
};
|
|
46
|
-
return __assign.apply(this, arguments);
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
|
|
51
30
|
// src/index.ts
|
|
52
31
|
var index_exports = {};
|
|
53
32
|
__export(index_exports, {
|
|
@@ -62,7 +41,6 @@ __export(index_exports, {
|
|
|
62
41
|
Unless: () => Unless,
|
|
63
42
|
cls: () => cls,
|
|
64
43
|
defineAsyncStore: () => defineAsyncStore,
|
|
65
|
-
defineAsyncStoreLayered: () => defineAsyncStoreLayered,
|
|
66
44
|
defineStore: () => defineStore,
|
|
67
45
|
proxyWithPersistant: () => proxyWithPersistant,
|
|
68
46
|
useAsyncCallback: () => useAsyncCallback,
|
|
@@ -227,80 +205,26 @@ function renderNodes(tokens, values) {
|
|
|
227
205
|
// src/hooks/useAsyncCallback.ts
|
|
228
206
|
var import_react7 = require("react");
|
|
229
207
|
function useAsyncCallback(fun) {
|
|
230
|
-
const [
|
|
231
|
-
const [loading, setLoading] = (0, import_react7.useState)(false);
|
|
208
|
+
const [state, set] = (0, import_react7.useState)({ loading: false });
|
|
232
209
|
async function execute(...args) {
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
setError(error2);
|
|
241
|
-
throw error2;
|
|
242
|
-
}
|
|
210
|
+
return fun(...args).then((value) => {
|
|
211
|
+
set({ loading: false });
|
|
212
|
+
return value;
|
|
213
|
+
}).catch((err) => {
|
|
214
|
+
set({ loading: false, error: err });
|
|
215
|
+
return Promise.reject(err);
|
|
216
|
+
});
|
|
243
217
|
}
|
|
244
|
-
return [loading, execute, error];
|
|
218
|
+
return [state.loading, execute, state.error];
|
|
245
219
|
}
|
|
246
220
|
|
|
247
|
-
//
|
|
248
|
-
init_tslib_es6();
|
|
221
|
+
// src/hooks/useAsyncState.ts
|
|
249
222
|
var import_react9 = require("react");
|
|
250
223
|
|
|
251
|
-
// ../../node_modules/.pnpm/react-use@17.6.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/react-use/esm/useMountedState.js
|
|
252
|
-
var import_react8 = require("react");
|
|
253
|
-
function useMountedState() {
|
|
254
|
-
var mountedRef = (0, import_react8.useRef)(false);
|
|
255
|
-
var get = (0, import_react8.useCallback)(function() {
|
|
256
|
-
return mountedRef.current;
|
|
257
|
-
}, []);
|
|
258
|
-
(0, import_react8.useEffect)(function() {
|
|
259
|
-
mountedRef.current = true;
|
|
260
|
-
return function() {
|
|
261
|
-
mountedRef.current = false;
|
|
262
|
-
};
|
|
263
|
-
}, []);
|
|
264
|
-
return get;
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
// ../../node_modules/.pnpm/react-use@17.6.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/react-use/esm/useAsyncFn.js
|
|
268
|
-
function useAsyncFn(fn, deps, initialState) {
|
|
269
|
-
if (deps === void 0) {
|
|
270
|
-
deps = [];
|
|
271
|
-
}
|
|
272
|
-
if (initialState === void 0) {
|
|
273
|
-
initialState = { loading: false };
|
|
274
|
-
}
|
|
275
|
-
var lastCallId = (0, import_react9.useRef)(0);
|
|
276
|
-
var isMounted = useMountedState();
|
|
277
|
-
var _a = (0, import_react9.useState)(initialState), state = _a[0], set = _a[1];
|
|
278
|
-
var callback = (0, import_react9.useCallback)(function() {
|
|
279
|
-
var args = [];
|
|
280
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
281
|
-
args[_i] = arguments[_i];
|
|
282
|
-
}
|
|
283
|
-
var callId = ++lastCallId.current;
|
|
284
|
-
if (!state.loading) {
|
|
285
|
-
set(function(prevState) {
|
|
286
|
-
return __assign(__assign({}, prevState), { loading: true });
|
|
287
|
-
});
|
|
288
|
-
}
|
|
289
|
-
return fn.apply(void 0, args).then(function(value) {
|
|
290
|
-
isMounted() && callId === lastCallId.current && set({ value, loading: false });
|
|
291
|
-
return value;
|
|
292
|
-
}, function(error) {
|
|
293
|
-
isMounted() && callId === lastCallId.current && set({ error, loading: false });
|
|
294
|
-
return error;
|
|
295
|
-
});
|
|
296
|
-
}, deps);
|
|
297
|
-
return [state, callback];
|
|
298
|
-
}
|
|
299
|
-
|
|
300
224
|
// ../../node_modules/.pnpm/react-use@17.6.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/react-use/esm/useEffectOnce.js
|
|
301
|
-
var
|
|
225
|
+
var import_react8 = require("react");
|
|
302
226
|
var useEffectOnce = function(effect) {
|
|
303
|
-
(0,
|
|
227
|
+
(0, import_react8.useEffect)(effect, []);
|
|
304
228
|
};
|
|
305
229
|
var useEffectOnce_default = useEffectOnce;
|
|
306
230
|
|
|
@@ -313,17 +237,21 @@ var useMount = function(fn) {
|
|
|
313
237
|
var useMount_default = useMount;
|
|
314
238
|
|
|
315
239
|
// src/hooks/useAsyncState.ts
|
|
316
|
-
function useAsyncState(
|
|
317
|
-
const [
|
|
318
|
-
|
|
319
|
-
|
|
240
|
+
function useAsyncState(fun, options) {
|
|
241
|
+
const [value, set] = (0, import_react9.useState)(options?.initial);
|
|
242
|
+
const [loading, execute, error] = useAsyncCallback(async (...args) => fun(...args).then(set));
|
|
243
|
+
useMount_default(() => options?.immediate && execute());
|
|
244
|
+
(0, import_react9.useEffect)(() => {
|
|
245
|
+
execute();
|
|
246
|
+
}, [execute]);
|
|
247
|
+
return [{ value, loading, error }, execute];
|
|
320
248
|
}
|
|
321
249
|
|
|
322
250
|
// src/hooks/useDebounce.ts
|
|
323
|
-
var
|
|
251
|
+
var import_react10 = require("react");
|
|
324
252
|
function useDebounce(value, delay) {
|
|
325
|
-
const [debouncedValue, setDebouncedValue] = (0,
|
|
326
|
-
(0,
|
|
253
|
+
const [debouncedValue, setDebouncedValue] = (0, import_react10.useState)(value);
|
|
254
|
+
(0, import_react10.useEffect)(() => {
|
|
327
255
|
const handler = setTimeout(() => setDebouncedValue(value), delay);
|
|
328
256
|
return () => clearTimeout(handler);
|
|
329
257
|
}, [value, delay]);
|
|
@@ -332,14 +260,14 @@ function useDebounce(value, delay) {
|
|
|
332
260
|
|
|
333
261
|
// src/hooks/useEventBus.ts
|
|
334
262
|
var import_mitt = __toESM(require("mitt"), 1);
|
|
335
|
-
var
|
|
263
|
+
var import_react11 = require("react");
|
|
336
264
|
var emitter = (0, import_mitt.default)();
|
|
337
265
|
function useEventBus(key) {
|
|
338
|
-
const onRef = (0,
|
|
266
|
+
const onRef = (0, import_react11.useRef)();
|
|
339
267
|
function on(listener) {
|
|
340
268
|
emitter.on(key, listener);
|
|
341
269
|
onRef.current = listener;
|
|
342
|
-
(0,
|
|
270
|
+
(0, import_react11.useEffect)(() => {
|
|
343
271
|
if (!onRef.current)
|
|
344
272
|
return;
|
|
345
273
|
emitter.off(key, onRef.current);
|
|
@@ -385,23 +313,23 @@ function fetchRequestIntercept(intercept) {
|
|
|
385
313
|
}
|
|
386
314
|
|
|
387
315
|
// src/hooks/useMounted.ts
|
|
388
|
-
var
|
|
316
|
+
var import_react12 = require("react");
|
|
389
317
|
function useMounted() {
|
|
390
|
-
const [mounted, setMounted] = (0,
|
|
391
|
-
(0,
|
|
318
|
+
const [mounted, setMounted] = (0, import_react12.useState)(false);
|
|
319
|
+
(0, import_react12.useEffect)(() => setMounted(true), []);
|
|
392
320
|
return mounted;
|
|
393
321
|
}
|
|
394
322
|
|
|
395
323
|
// src/hooks/useWatch.ts
|
|
396
|
-
var
|
|
324
|
+
var import_react13 = require("react");
|
|
397
325
|
function useWatch(source, callback, options = {}) {
|
|
398
|
-
const firstUpdate = (0,
|
|
399
|
-
const then = (0,
|
|
400
|
-
const deps = (0,
|
|
326
|
+
const firstUpdate = (0, import_react13.useRef)(false);
|
|
327
|
+
const then = (0, import_react13.useRef)();
|
|
328
|
+
const deps = (0, import_react13.useMemo)(
|
|
401
329
|
() => Array.isArray(source) ? source : [source],
|
|
402
330
|
[source]
|
|
403
331
|
);
|
|
404
|
-
(0,
|
|
332
|
+
(0, import_react13.useEffect)(() => {
|
|
405
333
|
if (!firstUpdate.current)
|
|
406
334
|
recordFirst();
|
|
407
335
|
else
|
|
@@ -423,7 +351,7 @@ function useWhenever(source, cb, options) {
|
|
|
423
351
|
}
|
|
424
352
|
|
|
425
353
|
// src/storage/defineStore.ts
|
|
426
|
-
var
|
|
354
|
+
var import_react14 = require("react");
|
|
427
355
|
var import_valtio2 = require("valtio");
|
|
428
356
|
|
|
429
357
|
// ../util-core/src/util/json.ts
|
|
@@ -435,7 +363,7 @@ function jsonTryParse(text) {
|
|
|
435
363
|
}
|
|
436
364
|
}
|
|
437
365
|
|
|
438
|
-
// src/storage/
|
|
366
|
+
// src/storage/persistant.ts
|
|
439
367
|
var import_valtio = require("valtio");
|
|
440
368
|
function proxyWithPersistant(keyOrOptions, initialObject) {
|
|
441
369
|
let options;
|
|
@@ -470,12 +398,15 @@ function defineStore(store, options = {}) {
|
|
|
470
398
|
const $status = (0, import_valtio2.proxy)(status);
|
|
471
399
|
const $actions = {};
|
|
472
400
|
const $getters = {};
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
401
|
+
setupActions($state, actions, $actions, $status);
|
|
402
|
+
setupGetters(state, $state, getters, $getters);
|
|
403
|
+
setupStatus($actions, $status);
|
|
476
404
|
function $subscribe(listener) {
|
|
477
405
|
return (0, import_valtio2.subscribe)($state, () => listener($state));
|
|
478
406
|
}
|
|
407
|
+
$subscribe.status = function(listener) {
|
|
408
|
+
return (0, import_valtio2.subscribe)($status, () => listener($status));
|
|
409
|
+
};
|
|
479
410
|
function $patch(patch) {
|
|
480
411
|
if (typeof patch === "function")
|
|
481
412
|
patch($state);
|
|
@@ -483,10 +414,10 @@ function defineStore(store, options = {}) {
|
|
|
483
414
|
Object.assign($state, patch);
|
|
484
415
|
}
|
|
485
416
|
function $signal(fn) {
|
|
486
|
-
return (0,
|
|
417
|
+
return (0, import_react14.createElement)(() => fn((0, import_valtio2.useSnapshot)($state)));
|
|
487
418
|
}
|
|
488
419
|
$signal.status = function(fn) {
|
|
489
|
-
return (0,
|
|
420
|
+
return (0, import_react14.createElement)(() => fn((0, import_valtio2.useSnapshot)($status)));
|
|
490
421
|
};
|
|
491
422
|
return {
|
|
492
423
|
$subscribe,
|
|
@@ -496,15 +427,41 @@ function defineStore(store, options = {}) {
|
|
|
496
427
|
$actions,
|
|
497
428
|
$getters,
|
|
498
429
|
$signal,
|
|
499
|
-
...$actions
|
|
500
|
-
|
|
430
|
+
...$actions
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
function track(action, status) {
|
|
434
|
+
let loadings = 0;
|
|
435
|
+
const tracking = () => loadings++ === 0 && (status.loading = true);
|
|
436
|
+
const done = () => !--loadings && (status.loading = false);
|
|
437
|
+
const fulfilled = () => {
|
|
438
|
+
status.finished = true;
|
|
439
|
+
done();
|
|
440
|
+
};
|
|
441
|
+
const rejected = (error) => {
|
|
442
|
+
status.error = error;
|
|
443
|
+
done();
|
|
444
|
+
};
|
|
445
|
+
return function(...args) {
|
|
446
|
+
tracking();
|
|
447
|
+
try {
|
|
448
|
+
const result = action(...args);
|
|
449
|
+
if (result instanceof Promise)
|
|
450
|
+
result.then(fulfilled).catch(rejected);
|
|
451
|
+
else
|
|
452
|
+
fulfilled();
|
|
453
|
+
} catch (error) {
|
|
454
|
+
rejected(error);
|
|
455
|
+
}
|
|
501
456
|
};
|
|
502
457
|
}
|
|
503
|
-
function
|
|
504
|
-
for (const key in actions)
|
|
505
|
-
$
|
|
458
|
+
function setupActions($state, actions, $actions, $status) {
|
|
459
|
+
for (const key in actions) {
|
|
460
|
+
$status[key] = { finished: false, loading: false, error: null };
|
|
461
|
+
$actions[key] = track(actions[key].bind($state), $status[key]);
|
|
462
|
+
}
|
|
506
463
|
}
|
|
507
|
-
function
|
|
464
|
+
function setupGetters(state, $state, getters, $getters) {
|
|
508
465
|
for (const key in getters) {
|
|
509
466
|
Object.defineProperty(state, key, {
|
|
510
467
|
get: () => getters[key].call($state),
|
|
@@ -516,7 +473,7 @@ function markGetters(state, $state, getters, $getters) {
|
|
|
516
473
|
});
|
|
517
474
|
}
|
|
518
475
|
}
|
|
519
|
-
function
|
|
476
|
+
function setupStatus($actions, $status) {
|
|
520
477
|
Object.defineProperty($status, "loading", {
|
|
521
478
|
get: () => Object.keys($actions).some((key) => $status[key].loading),
|
|
522
479
|
enumerable: true
|
|
@@ -529,92 +486,35 @@ function markStatus($actions, $status) {
|
|
|
529
486
|
get: () => Object.keys($actions).find((key) => $status[key].error),
|
|
530
487
|
enumerable: true
|
|
531
488
|
});
|
|
532
|
-
for (const key in $actions) {
|
|
533
|
-
let subscribe4 = function() {
|
|
534
|
-
if (subscribers === 0)
|
|
535
|
-
$status[key].loading = true;
|
|
536
|
-
subscribers++;
|
|
537
|
-
}, done2 = function() {
|
|
538
|
-
subscribers--;
|
|
539
|
-
if (!subscribers)
|
|
540
|
-
$status[key].loading = false;
|
|
541
|
-
};
|
|
542
|
-
var subscribe3 = subscribe4, done = done2;
|
|
543
|
-
const action = $actions[key];
|
|
544
|
-
$status[key] = { finished: false, loading: false, error: null };
|
|
545
|
-
let subscribers = 0;
|
|
546
|
-
$actions[key] = function(...args) {
|
|
547
|
-
subscribe4();
|
|
548
|
-
try {
|
|
549
|
-
const result = action(...args);
|
|
550
|
-
if (result instanceof Promise) {
|
|
551
|
-
result.then(() => $status[key].finished = true).catch((error) => $status[key].error = error).finally(done2);
|
|
552
|
-
} else {
|
|
553
|
-
$status[key].finished = true;
|
|
554
|
-
done2();
|
|
555
|
-
}
|
|
556
|
-
} catch (error) {
|
|
557
|
-
$status[key].error = error;
|
|
558
|
-
done2();
|
|
559
|
-
}
|
|
560
|
-
};
|
|
561
|
-
}
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
// src/storage/useStore.ts
|
|
565
|
-
var import_valtio3 = require("valtio");
|
|
566
|
-
function useStore(store) {
|
|
567
|
-
return (0, import_valtio3.useSnapshot)(store.$state);
|
|
568
489
|
}
|
|
569
490
|
|
|
570
491
|
// src/storage/defineAsyncStore.ts
|
|
571
|
-
function defineAsyncStore(options) {
|
|
492
|
+
function defineAsyncStore(fetch, options = {}) {
|
|
572
493
|
const store = defineStore(
|
|
573
494
|
{
|
|
574
|
-
state: () => ({
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
495
|
+
state: () => ({ value: options.initial }),
|
|
496
|
+
actions: {
|
|
497
|
+
fetch,
|
|
498
|
+
refresh(value) {
|
|
499
|
+
this.value = value || options.initial;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
580
502
|
},
|
|
581
|
-
{ persist: options.persist
|
|
503
|
+
{ persist: options.persist }
|
|
582
504
|
);
|
|
583
|
-
|
|
584
|
-
const fn = options.setup();
|
|
585
|
-
const state = useStore(store);
|
|
586
|
-
function fetch(...args) {
|
|
587
|
-
if (state.loading)
|
|
588
|
-
return;
|
|
589
|
-
store.$state.loading = true;
|
|
590
|
-
store.$state.promise = fn(...args);
|
|
591
|
-
store.$state.promise.then((value) => store.$state.value = value).finally(() => store.$state.loading = false).catch((error) => {
|
|
592
|
-
store.$state.error = error;
|
|
593
|
-
throw error;
|
|
594
|
-
});
|
|
595
|
-
return store.$state.promise;
|
|
596
|
-
}
|
|
597
|
-
function refresh(value) {
|
|
598
|
-
store.$state.value = value || options.initial;
|
|
599
|
-
}
|
|
600
|
-
return [state, fetch, refresh];
|
|
601
|
-
}
|
|
602
|
-
return use;
|
|
505
|
+
return store;
|
|
603
506
|
}
|
|
604
507
|
|
|
605
|
-
// src/storage/
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
initial: options.initial,
|
|
610
|
-
persist: options.persist
|
|
611
|
-
});
|
|
508
|
+
// src/storage/useStatus.tsx
|
|
509
|
+
var import_valtio3 = require("valtio");
|
|
510
|
+
function useStatus(store) {
|
|
511
|
+
return (0, import_valtio3.useSnapshot)(store.$status);
|
|
612
512
|
}
|
|
613
513
|
|
|
614
|
-
// src/storage/
|
|
514
|
+
// src/storage/useStore.ts
|
|
615
515
|
var import_valtio4 = require("valtio");
|
|
616
|
-
function
|
|
617
|
-
return (0, import_valtio4.useSnapshot)(store.$
|
|
516
|
+
function useStore(store) {
|
|
517
|
+
return (0, import_valtio4.useSnapshot)(store.$state);
|
|
618
518
|
}
|
|
619
519
|
// Annotate the CommonJS export names for ESM import in node:
|
|
620
520
|
0 && (module.exports = {
|
|
@@ -629,7 +529,6 @@ function useStatus(store) {
|
|
|
629
529
|
Unless,
|
|
630
530
|
cls,
|
|
631
531
|
defineAsyncStore,
|
|
632
|
-
defineAsyncStoreLayered,
|
|
633
532
|
defineStore,
|
|
634
533
|
proxyWithPersistant,
|
|
635
534
|
useAsyncCallback,
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { BooleanLike } from '@hairy/utils';
|
|
1
|
+
import { BooleanLike, PromiseFn, PromiseType, AnyFn } from '@hairy/utils';
|
|
2
2
|
import * as react from 'react';
|
|
3
|
-
import { JSX, ReactNode, PropsWithChildren, ReactElement, FC, ComponentClass,
|
|
4
|
-
import { FunctionReturningPromise as FunctionReturningPromise$1, PromiseType as PromiseType$1 } from './misc/types';
|
|
3
|
+
import { JSX, ReactNode, PropsWithChildren, ReactElement, FC, ComponentClass, DetailedHTMLProps, HTMLAttributes } from 'react';
|
|
5
4
|
import * as valtio from 'valtio';
|
|
6
5
|
|
|
7
6
|
type Value = string | boolean | undefined | null;
|
|
@@ -80,12 +79,9 @@ interface TransProps {
|
|
|
80
79
|
}
|
|
81
80
|
declare function Trans({ i18nKey, ...additionalProps }: TransProps): ReactNode[];
|
|
82
81
|
|
|
83
|
-
declare function useAsyncCallback<T extends
|
|
82
|
+
declare function useAsyncCallback<T extends PromiseFn>(fun: T): readonly [boolean, T, Error | undefined];
|
|
84
83
|
|
|
85
|
-
|
|
86
|
-
declare type FunctionReturningPromise = (...args: any[]) => Promise<any>;
|
|
87
|
-
|
|
88
|
-
declare type AsyncState<T> = {
|
|
84
|
+
type AsyncState<T> = {
|
|
89
85
|
loading: boolean;
|
|
90
86
|
error?: undefined;
|
|
91
87
|
value?: undefined;
|
|
@@ -102,18 +98,20 @@ declare type AsyncState<T> = {
|
|
|
102
98
|
error?: undefined;
|
|
103
99
|
value: T;
|
|
104
100
|
};
|
|
105
|
-
|
|
106
|
-
declare type
|
|
107
|
-
|
|
101
|
+
type StateFromFnReturningPromise<T extends PromiseFn = PromiseFn> = AsyncState<PromiseType<ReturnType<T>>>;
|
|
102
|
+
declare type AsyncStateReturn<T extends PromiseFn = PromiseFn> = [
|
|
103
|
+
StateFromFnReturningPromise<T>,
|
|
108
104
|
T
|
|
109
105
|
];
|
|
110
106
|
|
|
111
|
-
type
|
|
112
|
-
interface UseAsyncStateOptions<T extends FunctionReturningPromise> {
|
|
107
|
+
type UseAsyncStateOptions<T extends AnyFn> = {
|
|
113
108
|
immediate?: boolean;
|
|
114
|
-
initial?:
|
|
115
|
-
}
|
|
116
|
-
|
|
109
|
+
initial?: PromiseType<ReturnType<T>>;
|
|
110
|
+
} | {
|
|
111
|
+
immediate?: boolean;
|
|
112
|
+
initial: PromiseType<ReturnType<T>>;
|
|
113
|
+
};
|
|
114
|
+
declare function useAsyncState<T extends PromiseFn>(fun: T, options?: UseAsyncStateOptions<T>): AsyncStateReturn<T>;
|
|
117
115
|
|
|
118
116
|
declare function useDebounce<T>(value: T, delay: number): T;
|
|
119
117
|
|
|
@@ -147,39 +145,6 @@ declare function useWatch<T>(source: T, callback: WatchCallback<T>, options?: Wa
|
|
|
147
145
|
|
|
148
146
|
declare function useWhenever<T>(source: T, cb: WatchCallback<Exclude<T, null | undefined>>, options?: WatchOptions): void;
|
|
149
147
|
|
|
150
|
-
interface AsyncStoreOptions<T extends FunctionReturningPromise> {
|
|
151
|
-
initial?: ReturnType<T> extends Promise<infer U> ? U : undefined;
|
|
152
|
-
setup: () => T;
|
|
153
|
-
persist?: string;
|
|
154
|
-
}
|
|
155
|
-
declare function defineAsyncStore<T extends FunctionReturningPromise>(options: AsyncStoreOptions<T>): () => readonly [{
|
|
156
|
-
readonly [x: string]: any;
|
|
157
|
-
readonly promise: {
|
|
158
|
-
readonly then: <TResult1 = any, TResult2 = never>(onfulfilled?: ((value: any) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined) => Promise<TResult1 | TResult2>;
|
|
159
|
-
readonly catch: <TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined) => Promise<any>;
|
|
160
|
-
readonly finally: (onfinally?: (() => void) | null | undefined) => Promise<any>;
|
|
161
|
-
readonly [Symbol.toStringTag]: string;
|
|
162
|
-
} | undefined;
|
|
163
|
-
readonly value: valtio.Snapshot<ReturnType<T> extends Promise<infer U> ? U : undefined> | undefined;
|
|
164
|
-
readonly loading: boolean;
|
|
165
|
-
readonly error: Error | undefined;
|
|
166
|
-
}, T, (value?: (ReturnType<T> extends Promise<infer U_1> ? U_1 : undefined)) => void];
|
|
167
|
-
|
|
168
|
-
interface AsyncStoreLayeredOptions<T extends FunctionReturningPromise> extends Omit<AsyncStoreOptions<T>, 'setup'> {
|
|
169
|
-
}
|
|
170
|
-
declare function defineAsyncStoreLayered<T extends FunctionReturningPromise>(fn: T, options?: AsyncStoreLayeredOptions<T>): () => readonly [{
|
|
171
|
-
readonly [x: string]: any;
|
|
172
|
-
readonly promise: {
|
|
173
|
-
readonly then: <TResult1 = any, TResult2 = never>(onfulfilled?: ((value: any) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined) => Promise<TResult1 | TResult2>;
|
|
174
|
-
readonly catch: <TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined) => Promise<any>;
|
|
175
|
-
readonly finally: (onfinally?: (() => void) | null | undefined) => Promise<any>;
|
|
176
|
-
readonly [Symbol.toStringTag]: string;
|
|
177
|
-
} | undefined;
|
|
178
|
-
readonly value: valtio.Snapshot<ReturnType<T> extends Promise<infer U> ? U : undefined> | undefined;
|
|
179
|
-
readonly loading: boolean;
|
|
180
|
-
readonly error: Error | undefined;
|
|
181
|
-
}, T, (value?: (ReturnType<T> extends Promise<infer U_1> ? U_1 : undefined) | undefined) => void];
|
|
182
|
-
|
|
183
148
|
interface PersistantOptions {
|
|
184
149
|
id: string;
|
|
185
150
|
storage?: Storage;
|
|
@@ -212,19 +177,41 @@ interface StoreDefine<S extends object, A extends Actions<S>, G extends Getters<
|
|
|
212
177
|
interface StoreOptions {
|
|
213
178
|
persist?: string | PersistantOptions;
|
|
214
179
|
}
|
|
215
|
-
interface
|
|
180
|
+
interface StoreSignal<S, A extends Actions<S>, G extends Getters<S>> {
|
|
216
181
|
<T>(fn: (state: S & GettersReturnType<G>) => T): T;
|
|
217
182
|
status: <T>(fn: (status: ActionsStatus<A>) => T) => T;
|
|
218
183
|
}
|
|
184
|
+
interface StoreSubscribe<S, A extends Actions<S>, G extends Getters<S>> {
|
|
185
|
+
(listener: (state: S & GettersReturnType<G>) => void): () => void;
|
|
186
|
+
status: (listener: (status: ActionsStatus<A>) => void) => () => void;
|
|
187
|
+
}
|
|
188
|
+
interface StorePatch<S, G extends Getters<S>> {
|
|
189
|
+
(patch: Partial<S> | ((state: S & GettersReturnType<G>) => void)): void;
|
|
190
|
+
}
|
|
219
191
|
type Store<S, A extends Actions<S>, G extends Getters<S>> = {
|
|
220
|
-
$subscribe:
|
|
221
|
-
$patch:
|
|
192
|
+
$subscribe: StoreSubscribe<S, A, G>;
|
|
193
|
+
$patch: StorePatch<S, G>;
|
|
222
194
|
$state: S & GettersReturnType<G>;
|
|
223
195
|
$actions: ActionsOmitThisParameter<A>;
|
|
224
196
|
$getters: GettersReturnType<G>;
|
|
225
197
|
$status: ActionsStatus<A>;
|
|
226
|
-
$signal:
|
|
227
|
-
} & ActionsOmitThisParameter<A
|
|
198
|
+
$signal: StoreSignal<S, A, G>;
|
|
199
|
+
} & ActionsOmitThisParameter<A>;
|
|
200
|
+
|
|
201
|
+
interface AsyncStoreOptions<T extends AnyFn> {
|
|
202
|
+
initial?: ReturnType<T> extends Promise<infer U> ? U : undefined;
|
|
203
|
+
persist?: string;
|
|
204
|
+
}
|
|
205
|
+
declare function defineAsyncStore<T extends AnyFn>(fetch: T, options?: AsyncStoreOptions<T>): Store<{
|
|
206
|
+
value: (ReturnType<T> extends Promise<infer U> ? U : undefined) | undefined;
|
|
207
|
+
}, {
|
|
208
|
+
fetch: T;
|
|
209
|
+
refresh(this: {
|
|
210
|
+
value: (ReturnType<T> extends Promise<infer U> ? U : undefined) | undefined;
|
|
211
|
+
}, value?: (ReturnType<T> extends Promise<infer U_1> ? U_1 : undefined)): void;
|
|
212
|
+
}, Getters<{
|
|
213
|
+
value: (ReturnType<T> extends Promise<infer U> ? U : undefined) | undefined;
|
|
214
|
+
}>>;
|
|
228
215
|
|
|
229
216
|
declare function defineStore<S extends object, A extends Actions<S>, G extends Getters<S>>(store: StoreDefine<S, A, G>, options?: StoreOptions): Store<S, A, G>;
|
|
230
217
|
|
|
@@ -234,4 +221,4 @@ declare function useStore<S extends object, A extends Actions<S>, G extends Gett
|
|
|
234
221
|
|
|
235
222
|
type PropsWithDetailedHTML<T = HTMLDivElement> = DetailedHTMLProps<HTMLAttributes<T>, T>;
|
|
236
223
|
|
|
237
|
-
export { type Argument, type ArgumentArray, type
|
|
224
|
+
export { type Argument, type ArgumentArray, type AsyncStoreOptions, Case, type CaseProps, Default, type DefaultProps, Else, type ElseProps, type EventBusListener, type FetchRequestInterceptCallback, type FetchResponseInterceptCallback, If, type IfProps, type InjectComponent, Injector, type InjectorProps, type Mapping, type PersistantOptions, type PropsWithDetailedHTML, type ReadonlyArgumentArray, Switch, type SwitchProps, Then, type ThenProps, Trans, type TransProps, Unless, type UnlessProps, type UseAsyncStateOptions, type Value, type WatchCallback, type WatchOptions, type WrapperProps, type WrapperTag, cls, defineAsyncStore, defineStore, proxyWithPersistant, useAsyncCallback, useAsyncState, useDebounce, useEventBus, useFetchRequestIntercept, useFetchResponseIntercept, useMounted, useStatus, useStore, useWatch, useWhenever, wrapper };
|