@hairy/react-lib 1.34.0 → 1.36.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 +49 -36
- package/dist/index.d.ts +66 -28
- package/dist/index.global.js +45 -31
- package/dist/index.js +46 -34
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -40,8 +40,9 @@ __export(index_exports, {
|
|
|
40
40
|
Trigger: () => Trigger,
|
|
41
41
|
Unless: () => Unless,
|
|
42
42
|
cls: () => cls,
|
|
43
|
-
|
|
43
|
+
defienAsyncStore: () => defienAsyncStore,
|
|
44
44
|
defineStore: () => defineStore,
|
|
45
|
+
defineStoreAsync: () => defineStoreAsync,
|
|
45
46
|
proxyWithPersistant: () => proxyWithPersistant,
|
|
46
47
|
track: () => track,
|
|
47
48
|
tryUseCallback: () => tryUseCallback,
|
|
@@ -567,9 +568,6 @@ function useWhenever(source, cb, options) {
|
|
|
567
568
|
useWatch(source, () => source && cb(source), options);
|
|
568
569
|
}
|
|
569
570
|
|
|
570
|
-
// src/storage/defineAsyncStore.ts
|
|
571
|
-
var import_utils11 = require("valtio/utils");
|
|
572
|
-
|
|
573
571
|
// src/storage/defineStore.ts
|
|
574
572
|
var import_react20 = require("react");
|
|
575
573
|
var import_valtio4 = require("valtio");
|
|
@@ -596,6 +594,32 @@ function proxyWithPersistant(keyOrOptions, initialObject) {
|
|
|
596
594
|
return state;
|
|
597
595
|
}
|
|
598
596
|
|
|
597
|
+
// src/storage/utils/index.ts
|
|
598
|
+
function track2(action, status) {
|
|
599
|
+
let loadings = 0;
|
|
600
|
+
const tracking = () => loadings++ === 0 && (status.loading = true);
|
|
601
|
+
const done = () => !--loadings && (status.loading = false);
|
|
602
|
+
const fulfilled = (value) => {
|
|
603
|
+
status.finished = true;
|
|
604
|
+
done();
|
|
605
|
+
return value;
|
|
606
|
+
};
|
|
607
|
+
const rejected = (error) => {
|
|
608
|
+
status.error = error;
|
|
609
|
+
done();
|
|
610
|
+
throw error;
|
|
611
|
+
};
|
|
612
|
+
return function(...args) {
|
|
613
|
+
tracking();
|
|
614
|
+
try {
|
|
615
|
+
const value = action(...args);
|
|
616
|
+
return value instanceof Promise ? value.then(fulfilled, rejected) : fulfilled(value);
|
|
617
|
+
} catch (error) {
|
|
618
|
+
rejected(error);
|
|
619
|
+
}
|
|
620
|
+
};
|
|
621
|
+
}
|
|
622
|
+
|
|
599
623
|
// src/storage/defineStore.ts
|
|
600
624
|
function defineStore(store, options = {}) {
|
|
601
625
|
const state = typeof store.state === "function" ? store.state() : store.state;
|
|
@@ -641,34 +665,13 @@ function defineStore(store, options = {}) {
|
|
|
641
665
|
...$actions
|
|
642
666
|
};
|
|
643
667
|
}
|
|
644
|
-
function track2(action, status) {
|
|
645
|
-
let loadings = 0;
|
|
646
|
-
const tracking = () => loadings++ === 0 && (status.loading = true);
|
|
647
|
-
const done = () => !--loadings && (status.loading = false);
|
|
648
|
-
const fulfilled = (value) => {
|
|
649
|
-
status.finished = true;
|
|
650
|
-
done();
|
|
651
|
-
return value;
|
|
652
|
-
};
|
|
653
|
-
const rejected = (error) => {
|
|
654
|
-
status.error = error;
|
|
655
|
-
done();
|
|
656
|
-
throw error;
|
|
657
|
-
};
|
|
658
|
-
return function(...args) {
|
|
659
|
-
tracking();
|
|
660
|
-
try {
|
|
661
|
-
const value = action(...args);
|
|
662
|
-
return value instanceof Promise ? value.then(fulfilled, rejected) : fulfilled(value);
|
|
663
|
-
} catch (error) {
|
|
664
|
-
rejected(error);
|
|
665
|
-
}
|
|
666
|
-
};
|
|
667
|
-
}
|
|
668
668
|
function setupActions($state, actions, $actions, $status) {
|
|
669
669
|
for (const key in actions) {
|
|
670
670
|
$status[key] = { finished: false, loading: false, error: null };
|
|
671
671
|
$actions[key] = track2(actions[key].bind($state), $status[key]);
|
|
672
|
+
Object.defineProperty($state, key, {
|
|
673
|
+
get: () => $actions[key]
|
|
674
|
+
});
|
|
672
675
|
}
|
|
673
676
|
}
|
|
674
677
|
function setupGetters(state, $state, getters, $getters) {
|
|
@@ -698,8 +701,16 @@ function setupStatus($actions, $status) {
|
|
|
698
701
|
});
|
|
699
702
|
}
|
|
700
703
|
|
|
701
|
-
// src/storage/
|
|
702
|
-
|
|
704
|
+
// src/storage/defineStoreAsync.ts
|
|
705
|
+
var import_utils12 = require("valtio/utils");
|
|
706
|
+
function defineStoreAsync(fetch, options = {}) {
|
|
707
|
+
let persist;
|
|
708
|
+
if (typeof options.persist === "string") {
|
|
709
|
+
persist = { id: options.persist, pick: ["value"] };
|
|
710
|
+
}
|
|
711
|
+
if (typeof options.persist === "object") {
|
|
712
|
+
persist = { ...options.persist, pick: ["value"] };
|
|
713
|
+
}
|
|
703
714
|
const store = defineStore(
|
|
704
715
|
{
|
|
705
716
|
state: () => ({
|
|
@@ -709,18 +720,18 @@ function defineAsyncStore(fetch, options = {}) {
|
|
|
709
720
|
finished: false
|
|
710
721
|
}),
|
|
711
722
|
actions: {
|
|
712
|
-
async
|
|
723
|
+
async refetch(...args) {
|
|
713
724
|
return this.value = await fetch(...args);
|
|
714
725
|
},
|
|
715
|
-
|
|
726
|
+
reset(value) {
|
|
716
727
|
this.value = value || options.initial;
|
|
717
728
|
}
|
|
718
729
|
}
|
|
719
730
|
},
|
|
720
|
-
{ persist
|
|
731
|
+
{ persist }
|
|
721
732
|
);
|
|
722
|
-
(0,
|
|
723
|
-
const status = get(store.$status.
|
|
733
|
+
(0, import_utils12.watch)((get) => {
|
|
734
|
+
const status = get(store.$status.refetch);
|
|
724
735
|
store.$state.error = status.error;
|
|
725
736
|
store.$state.loading = status.loading;
|
|
726
737
|
store.$state.finished = status.finished;
|
|
@@ -728,6 +739,7 @@ function defineAsyncStore(fetch, options = {}) {
|
|
|
728
739
|
options.immediate && store.fetch();
|
|
729
740
|
return store;
|
|
730
741
|
}
|
|
742
|
+
var defienAsyncStore = defineStoreAsync;
|
|
731
743
|
|
|
732
744
|
// src/storage/useStatus.tsx
|
|
733
745
|
var import_valtio5 = require("valtio");
|
|
@@ -752,8 +764,9 @@ function useStore(store) {
|
|
|
752
764
|
Trigger,
|
|
753
765
|
Unless,
|
|
754
766
|
cls,
|
|
755
|
-
|
|
767
|
+
defienAsyncStore,
|
|
756
768
|
defineStore,
|
|
769
|
+
defineStoreAsync,
|
|
757
770
|
proxyWithPersistant,
|
|
758
771
|
track,
|
|
759
772
|
tryUseCallback,
|
package/dist/index.d.ts
CHANGED
|
@@ -249,49 +249,87 @@ interface StorePatch<S, G extends Getters<S>> {
|
|
|
249
249
|
type Store<S, A extends Actions<S>, G extends Getters<S>> = {
|
|
250
250
|
$subscribe: StoreSubscribe<S, A, G>;
|
|
251
251
|
$patch: StorePatch<S, G>;
|
|
252
|
-
$state: S & GettersReturnType<G>;
|
|
252
|
+
$state: S & GettersReturnType<G> & ActionsOmitThisParameter<A>;
|
|
253
253
|
$actions: ActionsOmitThisParameter<A>;
|
|
254
254
|
$getters: GettersReturnType<G>;
|
|
255
255
|
$status: ActionsStatus<A>;
|
|
256
256
|
$signal: StoreSignal<S, A, G>;
|
|
257
257
|
} & ActionsOmitThisParameter<A>;
|
|
258
258
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
259
|
+
/**
|
|
260
|
+
* @description Define a store
|
|
261
|
+
* @example
|
|
262
|
+
* ```tsx
|
|
263
|
+
* const store = defineStore({
|
|
264
|
+
* state: () => ({ count: 0 }),
|
|
265
|
+
* actions: {
|
|
266
|
+
* increment() {
|
|
267
|
+
* this.count++
|
|
268
|
+
* },
|
|
269
|
+
* },
|
|
270
|
+
* })
|
|
271
|
+
*
|
|
272
|
+
* store.increment()
|
|
273
|
+
* console.log(store.$state.count) // 1
|
|
274
|
+
*
|
|
275
|
+
* function Component() {
|
|
276
|
+
* const store = useStore(store)
|
|
277
|
+
* return (
|
|
278
|
+
* <div>
|
|
279
|
+
* <button onClick={store.increment}>Increment</button>
|
|
280
|
+
* <div>{store.count}</div>
|
|
281
|
+
* </div>
|
|
282
|
+
* )
|
|
283
|
+
* }
|
|
284
|
+
*
|
|
285
|
+
* ```
|
|
286
|
+
*/
|
|
287
|
+
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>;
|
|
288
|
+
|
|
289
|
+
type StoreAsyncInitial<T extends AnyFn> = ReturnType<T> extends Promise<infer U> ? U : ReturnType<T>;
|
|
290
|
+
interface StoreAsyncOptions<T extends AnyFn> {
|
|
291
|
+
initial?: StoreAsyncInitial<T>;
|
|
292
|
+
persist?: string | Omit<PersistantOptions, 'pick'> | undefined;
|
|
262
293
|
immediate?: boolean;
|
|
263
294
|
}
|
|
264
|
-
|
|
265
|
-
|
|
295
|
+
interface StoreAsyncInitialOptions<T extends AnyFn> extends StoreAsyncOptions<T> {
|
|
296
|
+
initial: StoreAsyncInitial<T>;
|
|
297
|
+
}
|
|
298
|
+
type StoreAsync<T extends AnyFn, Initial = StoreAsyncInitial<T> | undefined> = Store<{
|
|
299
|
+
value: Initial;
|
|
266
300
|
error: Error | null | undefined;
|
|
267
301
|
loading: boolean;
|
|
268
302
|
finished: boolean;
|
|
269
303
|
}, {
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
304
|
+
refetch: (...args: Parameters<T>) => ReturnType<T>;
|
|
305
|
+
reset: (value?: StoreAsyncInitial<T>) => void;
|
|
306
|
+
}, {}>;
|
|
307
|
+
/**
|
|
308
|
+
* @description Define a store async
|
|
309
|
+
* @example
|
|
310
|
+
* ```tsx
|
|
311
|
+
* const store = defineStoreAsync(
|
|
312
|
+
* () => fetch('https://api.example.com/data').then(response => response.json()),
|
|
313
|
+
* {
|
|
314
|
+
* initial: [],
|
|
315
|
+
* // data will be obtained immediately upon defining the store
|
|
316
|
+
* immediate: true,
|
|
317
|
+
* },
|
|
318
|
+
* )
|
|
319
|
+
* ```
|
|
320
|
+
*/
|
|
321
|
+
declare function defineStoreAsync<T extends AnyFn>(fetch: T, options: StoreAsyncInitialOptions<T>): StoreAsync<T, StoreAsyncInitial<T>>;
|
|
322
|
+
declare function defineStoreAsync<T extends AnyFn>(fetch: T, options?: StoreAsyncOptions<T>): StoreAsync<T>;
|
|
323
|
+
/**
|
|
324
|
+
* @deprecated
|
|
325
|
+
* use defineStoreAsync instead
|
|
326
|
+
*/
|
|
327
|
+
declare const defienAsyncStore: typeof defineStoreAsync;
|
|
290
328
|
|
|
291
329
|
declare function useStatus<S extends object, A extends Actions<S>, G extends Getters<S>>(store: Store<S, A, G>): valtio.Snapshot<ActionsStatus<A>>;
|
|
292
330
|
|
|
293
|
-
declare function useStore<S extends object, A extends Actions<S>, G extends Getters<S>>(store: Store<S, A, G>): valtio.Snapshot<S & GettersReturnType<G>>;
|
|
331
|
+
declare function useStore<S extends object, A extends Actions<S>, G extends Getters<S>>(store: Store<S, A, G>): valtio.Snapshot<S & GettersReturnType<G> & ActionsOmitThisParameter<A>>;
|
|
294
332
|
|
|
295
333
|
type PropsWithDetailedHTML<T = HTMLDivElement> = DetailedHTMLProps<HTMLAttributes<T>, T>;
|
|
296
334
|
|
|
297
|
-
export { type Argument, type ArgumentArray,
|
|
335
|
+
export { type Argument, type ArgumentArray, Case, type CaseProps, Default, type DefaultProps, Else, type ElseProps, type EventBusListener, type Exposer, type FetchRequestInterceptCallback, type FetchResponseInterceptCallback, If, type IfProps, type InjectComponent, Injector, type InjectorProps, type Mapping, type PersistantOptions, type PropsWithDetailedHTML, type ReadonlyArgumentArray, type StoreAsync, type StoreAsyncInitial, type StoreAsyncInitialOptions, type StoreAsyncOptions, Switch, type SwitchProps, Then, type ThenProps, Trigger, Unless, type UnlessProps, type UseAsyncStateOptions, type UseWatchCallback, type UseWatchOptions, type Value, type WrapperProps, type WrapperTag, cls, defienAsyncStore, defineStore, defineStoreAsync, proxyWithPersistant, track, tryUseCallback, tryUseEffect, tryUseInsertionEffect, tryUseReducer, tryUseRef, tryUseState, tryUseUpdate, useAsyncCallback, useAsyncState, useDebounce, useEventBus, useFetchRequestIntercept, useFetchResponseIntercept, useMounted, useStatus, useStore, useUpdate, useWatch, useWhenever, wrapper };
|
package/dist/index.global.js
CHANGED
|
@@ -52,8 +52,9 @@ var LibReact = (() => {
|
|
|
52
52
|
Trigger: () => Trigger,
|
|
53
53
|
Unless: () => Unless,
|
|
54
54
|
cls: () => cls,
|
|
55
|
-
|
|
55
|
+
defienAsyncStore: () => defienAsyncStore,
|
|
56
56
|
defineStore: () => defineStore,
|
|
57
|
+
defineStoreAsync: () => defineStoreAsync,
|
|
57
58
|
proxyWithPersistant: () => proxyWithPersistant,
|
|
58
59
|
track: () => track,
|
|
59
60
|
tryUseCallback: () => tryUseCallback,
|
|
@@ -1321,6 +1322,32 @@ var LibReact = (() => {
|
|
|
1321
1322
|
return state;
|
|
1322
1323
|
}
|
|
1323
1324
|
|
|
1325
|
+
// src/storage/utils/index.ts
|
|
1326
|
+
function track2(action, status) {
|
|
1327
|
+
let loadings = 0;
|
|
1328
|
+
const tracking = () => loadings++ === 0 && (status.loading = true);
|
|
1329
|
+
const done = () => !--loadings && (status.loading = false);
|
|
1330
|
+
const fulfilled = (value) => {
|
|
1331
|
+
status.finished = true;
|
|
1332
|
+
done();
|
|
1333
|
+
return value;
|
|
1334
|
+
};
|
|
1335
|
+
const rejected = (error) => {
|
|
1336
|
+
status.error = error;
|
|
1337
|
+
done();
|
|
1338
|
+
throw error;
|
|
1339
|
+
};
|
|
1340
|
+
return function(...args) {
|
|
1341
|
+
tracking();
|
|
1342
|
+
try {
|
|
1343
|
+
const value = action(...args);
|
|
1344
|
+
return value instanceof Promise ? value.then(fulfilled, rejected) : fulfilled(value);
|
|
1345
|
+
} catch (error) {
|
|
1346
|
+
rejected(error);
|
|
1347
|
+
}
|
|
1348
|
+
};
|
|
1349
|
+
}
|
|
1350
|
+
|
|
1324
1351
|
// src/storage/defineStore.ts
|
|
1325
1352
|
function defineStore(store, options = {}) {
|
|
1326
1353
|
const state = typeof store.state === "function" ? store.state() : store.state;
|
|
@@ -1366,34 +1393,13 @@ var LibReact = (() => {
|
|
|
1366
1393
|
...$actions
|
|
1367
1394
|
};
|
|
1368
1395
|
}
|
|
1369
|
-
function track2(action, status) {
|
|
1370
|
-
let loadings = 0;
|
|
1371
|
-
const tracking = () => loadings++ === 0 && (status.loading = true);
|
|
1372
|
-
const done = () => !--loadings && (status.loading = false);
|
|
1373
|
-
const fulfilled = (value) => {
|
|
1374
|
-
status.finished = true;
|
|
1375
|
-
done();
|
|
1376
|
-
return value;
|
|
1377
|
-
};
|
|
1378
|
-
const rejected = (error) => {
|
|
1379
|
-
status.error = error;
|
|
1380
|
-
done();
|
|
1381
|
-
throw error;
|
|
1382
|
-
};
|
|
1383
|
-
return function(...args) {
|
|
1384
|
-
tracking();
|
|
1385
|
-
try {
|
|
1386
|
-
const value = action(...args);
|
|
1387
|
-
return value instanceof Promise ? value.then(fulfilled, rejected) : fulfilled(value);
|
|
1388
|
-
} catch (error) {
|
|
1389
|
-
rejected(error);
|
|
1390
|
-
}
|
|
1391
|
-
};
|
|
1392
|
-
}
|
|
1393
1396
|
function setupActions($state, actions, $actions, $status) {
|
|
1394
1397
|
for (const key in actions) {
|
|
1395
1398
|
$status[key] = { finished: false, loading: false, error: null };
|
|
1396
1399
|
$actions[key] = track2(actions[key].bind($state), $status[key]);
|
|
1400
|
+
Object.defineProperty($state, key, {
|
|
1401
|
+
get: () => $actions[key]
|
|
1402
|
+
});
|
|
1397
1403
|
}
|
|
1398
1404
|
}
|
|
1399
1405
|
function setupGetters(state, $state, getters, $getters) {
|
|
@@ -1423,8 +1429,15 @@ var LibReact = (() => {
|
|
|
1423
1429
|
});
|
|
1424
1430
|
}
|
|
1425
1431
|
|
|
1426
|
-
// src/storage/
|
|
1427
|
-
function
|
|
1432
|
+
// src/storage/defineStoreAsync.ts
|
|
1433
|
+
function defineStoreAsync(fetch, options = {}) {
|
|
1434
|
+
let persist;
|
|
1435
|
+
if (typeof options.persist === "string") {
|
|
1436
|
+
persist = { id: options.persist, pick: ["value"] };
|
|
1437
|
+
}
|
|
1438
|
+
if (typeof options.persist === "object") {
|
|
1439
|
+
persist = { ...options.persist, pick: ["value"] };
|
|
1440
|
+
}
|
|
1428
1441
|
const store = defineStore(
|
|
1429
1442
|
{
|
|
1430
1443
|
state: () => ({
|
|
@@ -1434,18 +1447,18 @@ var LibReact = (() => {
|
|
|
1434
1447
|
finished: false
|
|
1435
1448
|
}),
|
|
1436
1449
|
actions: {
|
|
1437
|
-
async
|
|
1450
|
+
async refetch(...args) {
|
|
1438
1451
|
return this.value = await fetch(...args);
|
|
1439
1452
|
},
|
|
1440
|
-
|
|
1453
|
+
reset(value) {
|
|
1441
1454
|
this.value = value || options.initial;
|
|
1442
1455
|
}
|
|
1443
1456
|
}
|
|
1444
1457
|
},
|
|
1445
|
-
{ persist
|
|
1458
|
+
{ persist }
|
|
1446
1459
|
);
|
|
1447
1460
|
watch((get) => {
|
|
1448
|
-
const status = get(store.$status.
|
|
1461
|
+
const status = get(store.$status.refetch);
|
|
1449
1462
|
store.$state.error = status.error;
|
|
1450
1463
|
store.$state.loading = status.loading;
|
|
1451
1464
|
store.$state.finished = status.finished;
|
|
@@ -1453,6 +1466,7 @@ var LibReact = (() => {
|
|
|
1453
1466
|
options.immediate && store.fetch();
|
|
1454
1467
|
return store;
|
|
1455
1468
|
}
|
|
1469
|
+
var defienAsyncStore = defineStoreAsync;
|
|
1456
1470
|
|
|
1457
1471
|
// src/storage/useStatus.tsx
|
|
1458
1472
|
function useStatus(store) {
|
package/dist/index.js
CHANGED
|
@@ -498,9 +498,6 @@ function useWhenever(source, cb, options) {
|
|
|
498
498
|
useWatch(source, () => source && cb(source), options);
|
|
499
499
|
}
|
|
500
500
|
|
|
501
|
-
// src/storage/defineAsyncStore.ts
|
|
502
|
-
import { watch } from "valtio/utils";
|
|
503
|
-
|
|
504
501
|
// src/storage/defineStore.ts
|
|
505
502
|
import { createElement as createElement4 } from "react";
|
|
506
503
|
import { proxy as proxy2, subscribe as subscribe2, useSnapshot as useSnapshot2 } from "valtio";
|
|
@@ -527,6 +524,32 @@ function proxyWithPersistant(keyOrOptions, initialObject) {
|
|
|
527
524
|
return state;
|
|
528
525
|
}
|
|
529
526
|
|
|
527
|
+
// src/storage/utils/index.ts
|
|
528
|
+
function track2(action, status) {
|
|
529
|
+
let loadings = 0;
|
|
530
|
+
const tracking = () => loadings++ === 0 && (status.loading = true);
|
|
531
|
+
const done = () => !--loadings && (status.loading = false);
|
|
532
|
+
const fulfilled = (value) => {
|
|
533
|
+
status.finished = true;
|
|
534
|
+
done();
|
|
535
|
+
return value;
|
|
536
|
+
};
|
|
537
|
+
const rejected = (error) => {
|
|
538
|
+
status.error = error;
|
|
539
|
+
done();
|
|
540
|
+
throw error;
|
|
541
|
+
};
|
|
542
|
+
return function(...args) {
|
|
543
|
+
tracking();
|
|
544
|
+
try {
|
|
545
|
+
const value = action(...args);
|
|
546
|
+
return value instanceof Promise ? value.then(fulfilled, rejected) : fulfilled(value);
|
|
547
|
+
} catch (error) {
|
|
548
|
+
rejected(error);
|
|
549
|
+
}
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
|
|
530
553
|
// src/storage/defineStore.ts
|
|
531
554
|
function defineStore(store, options = {}) {
|
|
532
555
|
const state = typeof store.state === "function" ? store.state() : store.state;
|
|
@@ -572,34 +595,13 @@ function defineStore(store, options = {}) {
|
|
|
572
595
|
...$actions
|
|
573
596
|
};
|
|
574
597
|
}
|
|
575
|
-
function track2(action, status) {
|
|
576
|
-
let loadings = 0;
|
|
577
|
-
const tracking = () => loadings++ === 0 && (status.loading = true);
|
|
578
|
-
const done = () => !--loadings && (status.loading = false);
|
|
579
|
-
const fulfilled = (value) => {
|
|
580
|
-
status.finished = true;
|
|
581
|
-
done();
|
|
582
|
-
return value;
|
|
583
|
-
};
|
|
584
|
-
const rejected = (error) => {
|
|
585
|
-
status.error = error;
|
|
586
|
-
done();
|
|
587
|
-
throw error;
|
|
588
|
-
};
|
|
589
|
-
return function(...args) {
|
|
590
|
-
tracking();
|
|
591
|
-
try {
|
|
592
|
-
const value = action(...args);
|
|
593
|
-
return value instanceof Promise ? value.then(fulfilled, rejected) : fulfilled(value);
|
|
594
|
-
} catch (error) {
|
|
595
|
-
rejected(error);
|
|
596
|
-
}
|
|
597
|
-
};
|
|
598
|
-
}
|
|
599
598
|
function setupActions($state, actions, $actions, $status) {
|
|
600
599
|
for (const key in actions) {
|
|
601
600
|
$status[key] = { finished: false, loading: false, error: null };
|
|
602
601
|
$actions[key] = track2(actions[key].bind($state), $status[key]);
|
|
602
|
+
Object.defineProperty($state, key, {
|
|
603
|
+
get: () => $actions[key]
|
|
604
|
+
});
|
|
603
605
|
}
|
|
604
606
|
}
|
|
605
607
|
function setupGetters(state, $state, getters, $getters) {
|
|
@@ -629,8 +631,16 @@ function setupStatus($actions, $status) {
|
|
|
629
631
|
});
|
|
630
632
|
}
|
|
631
633
|
|
|
632
|
-
// src/storage/
|
|
633
|
-
|
|
634
|
+
// src/storage/defineStoreAsync.ts
|
|
635
|
+
import { watch } from "valtio/utils";
|
|
636
|
+
function defineStoreAsync(fetch, options = {}) {
|
|
637
|
+
let persist;
|
|
638
|
+
if (typeof options.persist === "string") {
|
|
639
|
+
persist = { id: options.persist, pick: ["value"] };
|
|
640
|
+
}
|
|
641
|
+
if (typeof options.persist === "object") {
|
|
642
|
+
persist = { ...options.persist, pick: ["value"] };
|
|
643
|
+
}
|
|
634
644
|
const store = defineStore(
|
|
635
645
|
{
|
|
636
646
|
state: () => ({
|
|
@@ -640,18 +650,18 @@ function defineAsyncStore(fetch, options = {}) {
|
|
|
640
650
|
finished: false
|
|
641
651
|
}),
|
|
642
652
|
actions: {
|
|
643
|
-
async
|
|
653
|
+
async refetch(...args) {
|
|
644
654
|
return this.value = await fetch(...args);
|
|
645
655
|
},
|
|
646
|
-
|
|
656
|
+
reset(value) {
|
|
647
657
|
this.value = value || options.initial;
|
|
648
658
|
}
|
|
649
659
|
}
|
|
650
660
|
},
|
|
651
|
-
{ persist
|
|
661
|
+
{ persist }
|
|
652
662
|
);
|
|
653
663
|
watch((get) => {
|
|
654
|
-
const status = get(store.$status.
|
|
664
|
+
const status = get(store.$status.refetch);
|
|
655
665
|
store.$state.error = status.error;
|
|
656
666
|
store.$state.loading = status.loading;
|
|
657
667
|
store.$state.finished = status.finished;
|
|
@@ -659,6 +669,7 @@ function defineAsyncStore(fetch, options = {}) {
|
|
|
659
669
|
options.immediate && store.fetch();
|
|
660
670
|
return store;
|
|
661
671
|
}
|
|
672
|
+
var defienAsyncStore = defineStoreAsync;
|
|
662
673
|
|
|
663
674
|
// src/storage/useStatus.tsx
|
|
664
675
|
import { useSnapshot as useSnapshot3 } from "valtio";
|
|
@@ -682,8 +693,9 @@ export {
|
|
|
682
693
|
Trigger,
|
|
683
694
|
Unless,
|
|
684
695
|
cls,
|
|
685
|
-
|
|
696
|
+
defienAsyncStore,
|
|
686
697
|
defineStore,
|
|
698
|
+
defineStoreAsync,
|
|
687
699
|
proxyWithPersistant,
|
|
688
700
|
track,
|
|
689
701
|
tryUseCallback,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hairy/react-lib",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.36.0",
|
|
5
5
|
"description": "Library for react",
|
|
6
6
|
"author": "Hairyf <wwu710632@gmail.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"react-dom": "^19.1.0",
|
|
39
39
|
"react-i18next": "^14.1.2",
|
|
40
40
|
"react-use": "^17.6.0",
|
|
41
|
-
"@hairy/utils": "1.
|
|
41
|
+
"@hairy/utils": "1.36.0"
|
|
42
42
|
},
|
|
43
43
|
"scripts": {
|
|
44
44
|
"build": "tsup",
|