@hairy/react-lib 1.14.0 → 1.16.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
@@ -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
- defineAsyncStorePlain: () => defineAsyncStorePlain,
66
44
  defineStore: () => defineStore,
67
45
  proxyWithPersistant: () => proxyWithPersistant,
68
46
  useAsyncCallback: () => useAsyncCallback,
@@ -72,6 +50,7 @@ __export(index_exports, {
72
50
  useFetchRequestIntercept: () => useFetchRequestIntercept,
73
51
  useFetchResponseIntercept: () => useFetchResponseIntercept,
74
52
  useMounted: () => useMounted,
53
+ useStatus: () => useStatus,
75
54
  useStore: () => useStore,
76
55
  useWatch: () => useWatch,
77
56
  useWhenever: () => useWhenever,
@@ -226,80 +205,26 @@ function renderNodes(tokens, values) {
226
205
  // src/hooks/useAsyncCallback.ts
227
206
  var import_react7 = require("react");
228
207
  function useAsyncCallback(fun) {
229
- const [error, setError] = (0, import_react7.useState)();
230
- const [loading, setLoading] = (0, import_react7.useState)(false);
208
+ const [state, set] = (0, import_react7.useState)({ loading: false });
231
209
  async function execute(...args) {
232
- try {
233
- setLoading(true);
234
- const result = await fun(...args);
235
- setLoading(false);
236
- return result;
237
- } catch (error2) {
238
- setLoading(false);
239
- setError(error2);
240
- throw error2;
241
- }
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
+ });
242
217
  }
243
- return [loading, execute, error];
218
+ return [state.loading, execute, state.error];
244
219
  }
245
220
 
246
- // ../../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
247
- init_tslib_es6();
221
+ // src/hooks/useAsyncState.ts
248
222
  var import_react9 = require("react");
249
223
 
250
- // ../../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
251
- var import_react8 = require("react");
252
- function useMountedState() {
253
- var mountedRef = (0, import_react8.useRef)(false);
254
- var get = (0, import_react8.useCallback)(function() {
255
- return mountedRef.current;
256
- }, []);
257
- (0, import_react8.useEffect)(function() {
258
- mountedRef.current = true;
259
- return function() {
260
- mountedRef.current = false;
261
- };
262
- }, []);
263
- return get;
264
- }
265
-
266
- // ../../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
267
- function useAsyncFn(fn, deps, initialState) {
268
- if (deps === void 0) {
269
- deps = [];
270
- }
271
- if (initialState === void 0) {
272
- initialState = { loading: false };
273
- }
274
- var lastCallId = (0, import_react9.useRef)(0);
275
- var isMounted = useMountedState();
276
- var _a = (0, import_react9.useState)(initialState), state = _a[0], set = _a[1];
277
- var callback = (0, import_react9.useCallback)(function() {
278
- var args = [];
279
- for (var _i = 0; _i < arguments.length; _i++) {
280
- args[_i] = arguments[_i];
281
- }
282
- var callId = ++lastCallId.current;
283
- if (!state.loading) {
284
- set(function(prevState) {
285
- return __assign(__assign({}, prevState), { loading: true });
286
- });
287
- }
288
- return fn.apply(void 0, args).then(function(value) {
289
- isMounted() && callId === lastCallId.current && set({ value, loading: false });
290
- return value;
291
- }, function(error) {
292
- isMounted() && callId === lastCallId.current && set({ error, loading: false });
293
- return error;
294
- });
295
- }, deps);
296
- return [state, callback];
297
- }
298
-
299
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
300
- var import_react10 = require("react");
225
+ var import_react8 = require("react");
301
226
  var useEffectOnce = function(effect) {
302
- (0, import_react10.useEffect)(effect, []);
227
+ (0, import_react8.useEffect)(effect, []);
303
228
  };
304
229
  var useEffectOnce_default = useEffectOnce;
305
230
 
@@ -312,17 +237,21 @@ var useMount = function(fn) {
312
237
  var useMount_default = useMount;
313
238
 
314
239
  // src/hooks/useAsyncState.ts
315
- function useAsyncState(fn, deps, options) {
316
- const [state, _fn] = useAsyncFn(fn, deps, options?.initial);
317
- useMount_default(() => options?.immediate && _fn());
318
- return [state, _fn];
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];
319
248
  }
320
249
 
321
250
  // src/hooks/useDebounce.ts
322
- var import_react11 = require("react");
251
+ var import_react10 = require("react");
323
252
  function useDebounce(value, delay) {
324
- const [debouncedValue, setDebouncedValue] = (0, import_react11.useState)(value);
325
- (0, import_react11.useEffect)(() => {
253
+ const [debouncedValue, setDebouncedValue] = (0, import_react10.useState)(value);
254
+ (0, import_react10.useEffect)(() => {
326
255
  const handler = setTimeout(() => setDebouncedValue(value), delay);
327
256
  return () => clearTimeout(handler);
328
257
  }, [value, delay]);
@@ -331,14 +260,14 @@ function useDebounce(value, delay) {
331
260
 
332
261
  // src/hooks/useEventBus.ts
333
262
  var import_mitt = __toESM(require("mitt"), 1);
334
- var import_react12 = require("react");
263
+ var import_react11 = require("react");
335
264
  var emitter = (0, import_mitt.default)();
336
265
  function useEventBus(key) {
337
- const onRef = (0, import_react12.useRef)();
266
+ const onRef = (0, import_react11.useRef)();
338
267
  function on(listener) {
339
268
  emitter.on(key, listener);
340
269
  onRef.current = listener;
341
- (0, import_react12.useEffect)(() => {
270
+ (0, import_react11.useEffect)(() => {
342
271
  if (!onRef.current)
343
272
  return;
344
273
  emitter.off(key, onRef.current);
@@ -384,23 +313,23 @@ function fetchRequestIntercept(intercept) {
384
313
  }
385
314
 
386
315
  // src/hooks/useMounted.ts
387
- var import_react13 = require("react");
316
+ var import_react12 = require("react");
388
317
  function useMounted() {
389
- const [mounted, setMounted] = (0, import_react13.useState)(false);
390
- (0, import_react13.useEffect)(() => setMounted(true), []);
318
+ const [mounted, setMounted] = (0, import_react12.useState)(false);
319
+ (0, import_react12.useEffect)(() => setMounted(true), []);
391
320
  return mounted;
392
321
  }
393
322
 
394
323
  // src/hooks/useWatch.ts
395
- var import_react14 = require("react");
324
+ var import_react13 = require("react");
396
325
  function useWatch(source, callback, options = {}) {
397
- const firstUpdate = (0, import_react14.useRef)(false);
398
- const then = (0, import_react14.useRef)();
399
- const deps = (0, import_react14.useMemo)(
326
+ const firstUpdate = (0, import_react13.useRef)(false);
327
+ const then = (0, import_react13.useRef)();
328
+ const deps = (0, import_react13.useMemo)(
400
329
  () => Array.isArray(source) ? source : [source],
401
330
  [source]
402
331
  );
403
- (0, import_react14.useEffect)(() => {
332
+ (0, import_react13.useEffect)(() => {
404
333
  if (!firstUpdate.current)
405
334
  recordFirst();
406
335
  else
@@ -422,6 +351,7 @@ function useWhenever(source, cb, options) {
422
351
  }
423
352
 
424
353
  // src/storage/defineStore.ts
354
+ var import_react14 = require("react");
425
355
  var import_valtio2 = require("valtio");
426
356
 
427
357
  // ../util-core/src/util/json.ts
@@ -433,7 +363,7 @@ function jsonTryParse(text) {
433
363
  }
434
364
  }
435
365
 
436
- // src/storage/proxyWithPersistant.ts
366
+ // src/storage/persistant.ts
437
367
  var import_valtio = require("valtio");
438
368
  function proxyWithPersistant(keyOrOptions, initialObject) {
439
369
  let options;
@@ -458,77 +388,133 @@ function proxyWithPersistant(keyOrOptions, initialObject) {
458
388
  // src/storage/defineStore.ts
459
389
  function defineStore(store, options = {}) {
460
390
  const state = typeof store.state === "function" ? store.state() : store.state;
391
+ const getters = store.getters || {};
461
392
  const actions = store.actions || {};
462
- const $state = options.persistant ? proxyWithPersistant(options.persistant, state) : (0, import_valtio2.proxy)(state);
393
+ const status = {};
394
+ status.finished = false;
395
+ status.loading = false;
396
+ status.error = null;
397
+ const $state = options.persist ? proxyWithPersistant(options.persist, state) : (0, import_valtio2.proxy)(state);
398
+ const $status = (0, import_valtio2.proxy)(status);
463
399
  const $actions = {};
464
- for (const key in actions)
465
- $actions[key] = actions[key].bind($state);
400
+ const $getters = {};
401
+ setupActions($state, actions, $actions, $status);
402
+ setupGetters(state, $state, getters, $getters);
403
+ setupStatus($actions, $status);
466
404
  function $subscribe(listener) {
467
405
  return (0, import_valtio2.subscribe)($state, () => listener($state));
468
406
  }
407
+ $subscribe.status = function(listener) {
408
+ return (0, import_valtio2.subscribe)($status, () => listener($status));
409
+ };
469
410
  function $patch(patch) {
470
411
  if (typeof patch === "function")
471
412
  patch($state);
472
413
  else
473
414
  Object.assign($state, patch);
474
415
  }
416
+ function $signal(fn) {
417
+ return (0, import_react14.createElement)(() => fn((0, import_valtio2.useSnapshot)($state)));
418
+ }
419
+ $signal.status = function(fn) {
420
+ return (0, import_react14.createElement)(() => fn((0, import_valtio2.useSnapshot)($status)));
421
+ };
475
422
  return {
476
423
  $subscribe,
477
424
  $patch,
478
425
  $state,
426
+ $status,
479
427
  $actions,
428
+ $getters,
429
+ $signal,
480
430
  ...$actions
481
431
  };
482
432
  }
483
-
484
- // src/storage/useStore.ts
485
- var import_valtio3 = require("valtio");
486
- function useStore(store) {
487
- return (0, import_valtio3.useSnapshot)(store.$state);
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
+ }
456
+ };
457
+ }
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
+ }
463
+ }
464
+ function setupGetters(state, $state, getters, $getters) {
465
+ for (const key in getters) {
466
+ Object.defineProperty(state, key, {
467
+ get: () => getters[key].call($state),
468
+ enumerable: true
469
+ });
470
+ Object.defineProperty($getters, key, {
471
+ get: () => state[key],
472
+ enumerable: true
473
+ });
474
+ }
475
+ }
476
+ function setupStatus($actions, $status) {
477
+ Object.defineProperty($status, "loading", {
478
+ get: () => Object.keys($actions).some((key) => $status[key].loading),
479
+ enumerable: true
480
+ });
481
+ Object.defineProperty($status, "finished", {
482
+ get: () => Object.keys($actions).every((key) => $status[key].finished),
483
+ enumerable: true
484
+ });
485
+ Object.defineProperty($status, "error", {
486
+ get: () => Object.keys($actions).find((key) => $status[key].error),
487
+ enumerable: true
488
+ });
488
489
  }
489
490
 
490
491
  // src/storage/defineAsyncStore.ts
491
- function defineAsyncStore(options) {
492
+ function defineAsyncStore(fetch, options = {}) {
492
493
  const store = defineStore(
493
494
  {
494
- state: () => ({
495
- promise: void 0,
496
- value: options.initial,
497
- loading: false,
498
- error: void 0
499
- })
495
+ state: () => ({ value: options.initial }),
496
+ actions: {
497
+ fetch,
498
+ refresh(value) {
499
+ this.value = value || options.initial;
500
+ }
501
+ }
500
502
  },
501
- { persistant: options.persistant ? { id: options.persistant, pick: ["value"] } : void 0 }
503
+ { persist: options.persist }
502
504
  );
503
- function use() {
504
- const fn = options.setup();
505
- const state = useStore(store);
506
- function fetch(...args) {
507
- if (state.loading)
508
- return;
509
- store.$state.loading = true;
510
- store.$state.promise = fn(...args);
511
- store.$state.promise.then((value) => store.$state.value = value).finally(() => store.$state.loading = false).catch((error) => {
512
- store.$state.error = error;
513
- throw error;
514
- });
515
- return store.$state.promise;
516
- }
517
- function refresh(value) {
518
- store.$state.value = value || options.initial;
519
- }
520
- return [state, fetch, refresh];
521
- }
522
- return use;
505
+ return store;
523
506
  }
524
507
 
525
- // src/storage/defineAsyncStorePlain.ts
526
- function defineAsyncStorePlain(fn, options = {}) {
527
- return defineAsyncStore({
528
- setup: () => fn,
529
- initial: options.initial,
530
- persistant: options.persistant
531
- });
508
+ // src/storage/useStatus.tsx
509
+ var import_valtio3 = require("valtio");
510
+ function useStatus(store) {
511
+ return (0, import_valtio3.useSnapshot)(store.$status);
512
+ }
513
+
514
+ // src/storage/useStore.ts
515
+ var import_valtio4 = require("valtio");
516
+ function useStore(store) {
517
+ return (0, import_valtio4.useSnapshot)(store.$state);
532
518
  }
533
519
  // Annotate the CommonJS export names for ESM import in node:
534
520
  0 && (module.exports = {
@@ -543,7 +529,6 @@ function defineAsyncStorePlain(fn, options = {}) {
543
529
  Unless,
544
530
  cls,
545
531
  defineAsyncStore,
546
- defineAsyncStorePlain,
547
532
  defineStore,
548
533
  proxyWithPersistant,
549
534
  useAsyncCallback,
@@ -553,6 +538,7 @@ function defineAsyncStorePlain(fn, options = {}) {
553
538
  useFetchRequestIntercept,
554
539
  useFetchResponseIntercept,
555
540
  useMounted,
541
+ useStatus,
556
542
  useStore,
557
543
  useWatch,
558
544
  useWhenever,
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, DependencyList, DetailedHTMLProps, HTMLAttributes } from 'react';
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 (...args: any[]) => any>(fun: T): readonly [boolean, T, Error | undefined];
82
+ declare function useAsyncCallback<T extends PromiseFn>(fun: T): readonly [boolean, T, Error | undefined];
84
83
 
85
- declare type PromiseType<P extends Promise<any>> = P extends Promise<infer T> ? T : never;
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
- declare type StateFromFunctionReturningPromise$1<T extends FunctionReturningPromise$1> = AsyncState<PromiseType$1<ReturnType<T>>>;
106
- declare type AsyncFnReturn<T extends FunctionReturningPromise$1 = FunctionReturningPromise$1> = [
107
- StateFromFunctionReturningPromise$1<T>,
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 StateFromFunctionReturningPromise<T extends FunctionReturningPromise> = AsyncState<PromiseType<ReturnType<T>>>;
112
- interface UseAsyncStateOptions<T extends FunctionReturningPromise> {
107
+ type UseAsyncStateOptions<T extends AnyFn> = {
113
108
  immediate?: boolean;
114
- initial?: StateFromFunctionReturningPromise<T>;
115
- }
116
- declare function useAsyncState<T extends FunctionReturningPromise>(fn: T, deps?: DependencyList, options?: UseAsyncStateOptions<T>): AsyncFnReturn<T>;
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,37 +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
- persistant?: string;
154
- }
155
- declare function defineAsyncStore<T extends FunctionReturningPromise>(options: AsyncStoreOptions<T>): () => readonly [{
156
- readonly promise: {
157
- 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>;
158
- readonly catch: <TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined) => Promise<any>;
159
- readonly finally: (onfinally?: (() => void) | null | undefined) => Promise<any>;
160
- readonly [Symbol.toStringTag]: string;
161
- } | undefined;
162
- readonly value: valtio.Snapshot<ReturnType<T> extends Promise<infer U> ? U : undefined> | undefined;
163
- readonly loading: boolean;
164
- readonly error: Error | undefined;
165
- }, T, (value?: (ReturnType<T> extends Promise<infer U_1> ? U_1 : undefined)) => void];
166
-
167
- interface AsyncStorePlainOptions<T extends FunctionReturningPromise> extends Omit<AsyncStoreOptions<T>, 'setup'> {
168
- }
169
- declare function defineAsyncStorePlain<T extends FunctionReturningPromise>(fn: T, options?: AsyncStorePlainOptions<T>): () => readonly [{
170
- readonly promise: {
171
- 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>;
172
- readonly catch: <TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined) => Promise<any>;
173
- readonly finally: (onfinally?: (() => void) | null | undefined) => Promise<any>;
174
- readonly [Symbol.toStringTag]: string;
175
- } | undefined;
176
- readonly value: valtio.Snapshot<ReturnType<T> extends Promise<infer U> ? U : undefined> | undefined;
177
- readonly loading: boolean;
178
- readonly error: Error | undefined;
179
- }, T, (value?: (ReturnType<T> extends Promise<infer U_1> ? U_1 : undefined) | undefined) => void];
180
-
181
148
  interface PersistantOptions {
182
149
  id: string;
183
150
  storage?: Storage;
@@ -186,27 +153,72 @@ interface PersistantOptions {
186
153
  declare function proxyWithPersistant<T extends object>(key: string, initialObject?: T, options?: Omit<PersistantOptions, 'key'>): T;
187
154
  declare function proxyWithPersistant<T extends object>(options: PersistantOptions, initialObject?: T): T;
188
155
 
189
- interface StoreDefine<S extends object, A extends Actions<S>> {
156
+ type Actions<S> = Record<string, (this: S, ...args: any) => any>;
157
+ type Getters<S> = Record<string, (this: S) => any>;
158
+ type ActionsOmitThisParameter<A extends Actions<any>> = {
159
+ [K in keyof A]: (...args: Parameters<A[K]>) => ReturnType<A[K]>;
160
+ };
161
+ interface Status {
162
+ finished: boolean;
163
+ loading: boolean;
164
+ error: Error | null;
165
+ }
166
+ type ActionsStatus<A extends Actions<any>> = Status & {
167
+ [K in keyof A]: Status;
168
+ };
169
+ type GettersReturnType<G extends Getters<any>> = {
170
+ [K in keyof G]: ReturnType<G[K]>;
171
+ };
172
+ interface StoreDefine<S extends object, A extends Actions<S>, G extends Getters<S>> {
190
173
  state: (() => S) | S;
191
174
  actions?: A;
175
+ getters?: G;
192
176
  }
193
177
  interface StoreOptions {
194
- persistant?: string | PersistantOptions;
178
+ persist?: string | PersistantOptions;
195
179
  }
196
- type Actions<S> = Record<string, (this: S, ...args: any) => any>;
197
- type ActionsOmitThisParameter<A extends Actions<any>> = {
198
- [K in keyof A]: (...args: Parameters<A[K]>) => ReturnType<A[K]>;
199
- };
200
- type Store<S, A extends Actions<S>> = {
201
- $subscribe: (listener: (state: S) => void) => () => void;
202
- $patch: (patch: Partial<S> | ((state: S) => void)) => void;
203
- $state: S;
180
+ interface StoreSignal<S, A extends Actions<S>, G extends Getters<S>> {
181
+ <T>(fn: (state: S & GettersReturnType<G>) => T): T;
182
+ status: <T>(fn: (status: ActionsStatus<A>) => T) => T;
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
+ }
191
+ type Store<S, A extends Actions<S>, G extends Getters<S>> = {
192
+ $subscribe: StoreSubscribe<S, A, G>;
193
+ $patch: StorePatch<S, G>;
194
+ $state: S & GettersReturnType<G>;
204
195
  $actions: ActionsOmitThisParameter<A>;
196
+ $getters: GettersReturnType<G>;
197
+ $status: ActionsStatus<A>;
198
+ $signal: StoreSignal<S, A, G>;
205
199
  } & ActionsOmitThisParameter<A>;
206
- declare function defineStore<S extends object, A extends Actions<S>>(store: StoreDefine<S, A>, options?: StoreOptions): Store<S, A>;
207
200
 
208
- declare function useStore<S extends object, A extends Actions<S>>(store: Store<S, A>): valtio.Snapshot<S>;
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: AnyFn, options?: AsyncStoreOptions<T>): Store<{
206
+ value: (ReturnType<T> extends Promise<infer U> ? U : undefined) | undefined;
207
+ }, {
208
+ fetch: AnyFn;
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
+ }>>;
215
+
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>;
217
+
218
+ declare function useStatus<S extends object, A extends Actions<S>, G extends Getters<S>>(store: Store<S, A, G>): valtio.Snapshot<ActionsStatus<A>>;
219
+
220
+ declare function useStore<S extends object, A extends Actions<S>, G extends Getters<S>>(store: Store<S, A, G>): valtio.Snapshot<S & GettersReturnType<G>>;
209
221
 
210
222
  type PropsWithDetailedHTML<T = HTMLDivElement> = DetailedHTMLProps<HTMLAttributes<T>, T>;
211
223
 
212
- export { type Actions, type ActionsOmitThisParameter, type Argument, type ArgumentArray, type AsyncStoreOptions, type AsyncStorePlainOptions, 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, type StateFromFunctionReturningPromise, type Store, type StoreDefine, type StoreOptions, 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, defineAsyncStorePlain, defineStore, proxyWithPersistant, useAsyncCallback, useAsyncState, useDebounce, useEventBus, useFetchRequestIntercept, useFetchResponseIntercept, useMounted, useStore, useWatch, useWhenever, wrapper };
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 };