@hairy/react-lib 1.35.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 CHANGED
@@ -704,6 +704,13 @@ function setupStatus($actions, $status) {
704
704
  // src/storage/defineStoreAsync.ts
705
705
  var import_utils12 = require("valtio/utils");
706
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
+ }
707
714
  const store = defineStore(
708
715
  {
709
716
  state: () => ({
@@ -713,18 +720,18 @@ function defineStoreAsync(fetch, options = {}) {
713
720
  finished: false
714
721
  }),
715
722
  actions: {
716
- async fetch(...args) {
723
+ async refetch(...args) {
717
724
  return this.value = await fetch(...args);
718
725
  },
719
- refresh(value) {
726
+ reset(value) {
720
727
  this.value = value || options.initial;
721
728
  }
722
729
  }
723
730
  },
724
- { persist: options.persist ? { id: options.persist, pick: ["value"] } : void 0 }
731
+ { persist }
725
732
  );
726
733
  (0, import_utils12.watch)((get) => {
727
- const status = get(store.$status.fetch);
734
+ const status = get(store.$status.refetch);
728
735
  store.$state.error = status.error;
729
736
  store.$state.loading = status.loading;
730
737
  store.$state.finished = status.finished;
package/dist/index.d.ts CHANGED
@@ -256,37 +256,70 @@ type Store<S, A extends Actions<S>, G extends Getters<S>> = {
256
256
  $signal: StoreSignal<S, A, G>;
257
257
  } & ActionsOmitThisParameter<A>;
258
258
 
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
+ */
259
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>;
260
288
 
289
+ type StoreAsyncInitial<T extends AnyFn> = ReturnType<T> extends Promise<infer U> ? U : ReturnType<T>;
261
290
  interface StoreAsyncOptions<T extends AnyFn> {
262
- initial?: ReturnType<T> extends Promise<infer U> ? U : undefined;
263
- persist?: string;
291
+ initial?: StoreAsyncInitial<T>;
292
+ persist?: string | Omit<PersistantOptions, 'pick'> | undefined;
264
293
  immediate?: boolean;
265
294
  }
266
- declare function defineStoreAsync<T extends AnyFn>(fetch: T, options?: StoreAsyncOptions<T>): Store<{
267
- value: (ReturnType<T> extends Promise<infer U> ? U : undefined) | undefined;
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;
268
300
  error: Error | null | undefined;
269
301
  loading: boolean;
270
302
  finished: boolean;
271
303
  }, {
272
- fetch(this: {
273
- value: (ReturnType<T> extends Promise<infer U> ? U : undefined) | undefined;
274
- error: Error | null | undefined;
275
- loading: boolean;
276
- finished: boolean;
277
- }, ...args: Parameters<T>): Promise<any>;
278
- refresh(this: {
279
- value: (ReturnType<T> extends Promise<infer U> ? U : undefined) | undefined;
280
- error: Error | null | undefined;
281
- loading: boolean;
282
- finished: boolean;
283
- }, value?: (ReturnType<T> extends Promise<infer U_1> ? U_1 : undefined)): void;
284
- }, Getters<{
285
- value: (ReturnType<T> extends Promise<infer U> ? U : undefined) | undefined;
286
- error: Error | null | undefined;
287
- loading: boolean;
288
- finished: boolean;
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>;
290
323
  /**
291
324
  * @deprecated
292
325
  * use defineStoreAsync instead
@@ -299,4 +332,4 @@ declare function useStore<S extends object, A extends Actions<S>, G extends Gett
299
332
 
300
333
  type PropsWithDetailedHTML<T = HTMLDivElement> = DetailedHTMLProps<HTMLAttributes<T>, T>;
301
334
 
302
- 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 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 };
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 };
@@ -1431,6 +1431,13 @@ var LibReact = (() => {
1431
1431
 
1432
1432
  // src/storage/defineStoreAsync.ts
1433
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
+ }
1434
1441
  const store = defineStore(
1435
1442
  {
1436
1443
  state: () => ({
@@ -1440,18 +1447,18 @@ var LibReact = (() => {
1440
1447
  finished: false
1441
1448
  }),
1442
1449
  actions: {
1443
- async fetch(...args) {
1450
+ async refetch(...args) {
1444
1451
  return this.value = await fetch(...args);
1445
1452
  },
1446
- refresh(value) {
1453
+ reset(value) {
1447
1454
  this.value = value || options.initial;
1448
1455
  }
1449
1456
  }
1450
1457
  },
1451
- { persist: options.persist ? { id: options.persist, pick: ["value"] } : void 0 }
1458
+ { persist }
1452
1459
  );
1453
1460
  watch((get) => {
1454
- const status = get(store.$status.fetch);
1461
+ const status = get(store.$status.refetch);
1455
1462
  store.$state.error = status.error;
1456
1463
  store.$state.loading = status.loading;
1457
1464
  store.$state.finished = status.finished;
package/dist/index.js CHANGED
@@ -634,6 +634,13 @@ function setupStatus($actions, $status) {
634
634
  // src/storage/defineStoreAsync.ts
635
635
  import { watch } from "valtio/utils";
636
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
+ }
637
644
  const store = defineStore(
638
645
  {
639
646
  state: () => ({
@@ -643,18 +650,18 @@ function defineStoreAsync(fetch, options = {}) {
643
650
  finished: false
644
651
  }),
645
652
  actions: {
646
- async fetch(...args) {
653
+ async refetch(...args) {
647
654
  return this.value = await fetch(...args);
648
655
  },
649
- refresh(value) {
656
+ reset(value) {
650
657
  this.value = value || options.initial;
651
658
  }
652
659
  }
653
660
  },
654
- { persist: options.persist ? { id: options.persist, pick: ["value"] } : void 0 }
661
+ { persist }
655
662
  );
656
663
  watch((get) => {
657
- const status = get(store.$status.fetch);
664
+ const status = get(store.$status.refetch);
658
665
  store.$state.error = status.error;
659
666
  store.$state.loading = status.loading;
660
667
  store.$state.finished = status.finished;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hairy/react-lib",
3
3
  "type": "module",
4
- "version": "1.35.0",
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.35.0"
41
+ "@hairy/utils": "1.36.0"
42
42
  },
43
43
  "scripts": {
44
44
  "build": "tsup",