@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.
@@ -6,9 +6,6 @@ var LibReact = (() => {
6
6
  var __getOwnPropNames = Object.getOwnPropertyNames;
7
7
  var __getProtoOf = Object.getPrototypeOf;
8
8
  var __hasOwnProp = Object.prototype.hasOwnProperty;
9
- var __esm = (fn, res) => function __init() {
10
- return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
11
- };
12
9
  var __commonJS = (cb, mod) => function __require() {
13
10
  return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
14
11
  };
@@ -73,24 +70,6 @@ var LibReact = (() => {
73
70
  }
74
71
  });
75
72
 
76
- // ../../node_modules/.pnpm/tslib@2.8.1/node_modules/tslib/tslib.es6.mjs
77
- var __assign;
78
- var init_tslib_es6 = __esm({
79
- "../../node_modules/.pnpm/tslib@2.8.1/node_modules/tslib/tslib.es6.mjs"() {
80
- "use strict";
81
- __assign = function() {
82
- __assign = Object.assign || function __assign2(t2) {
83
- for (var s2, i2 = 1, n2 = arguments.length; i2 < n2; i2++) {
84
- s2 = arguments[i2];
85
- for (var p in s2) if (Object.prototype.hasOwnProperty.call(s2, p)) t2[p] = s2[p];
86
- }
87
- return t2;
88
- };
89
- return __assign.apply(this, arguments);
90
- };
91
- }
92
- });
93
-
94
73
  // src/index.ts
95
74
  var index_exports = {};
96
75
  __export(index_exports, {
@@ -105,7 +84,6 @@ var LibReact = (() => {
105
84
  Unless: () => Unless,
106
85
  cls: () => cls,
107
86
  defineAsyncStore: () => defineAsyncStore,
108
- defineAsyncStorePlain: () => defineAsyncStorePlain,
109
87
  defineStore: () => defineStore,
110
88
  proxyWithPersistant: () => proxyWithPersistant,
111
89
  useAsyncCallback: () => useAsyncCallback,
@@ -115,6 +93,7 @@ var LibReact = (() => {
115
93
  useFetchRequestIntercept: () => useFetchRequestIntercept,
116
94
  useFetchResponseIntercept: () => useFetchResponseIntercept,
117
95
  useMounted: () => useMounted,
96
+ useStatus: () => useStatus,
118
97
  useStore: () => useStore,
119
98
  useWatch: () => useWatch,
120
99
  useWhenever: () => useWhenever,
@@ -329,80 +308,26 @@ var LibReact = (() => {
329
308
  // src/hooks/useAsyncCallback.ts
330
309
  var import_react7 = __toESM(require_react(), 1);
331
310
  function useAsyncCallback(fun) {
332
- const [error, setError] = (0, import_react7.useState)();
333
- const [loading, setLoading] = (0, import_react7.useState)(false);
311
+ const [state, set] = (0, import_react7.useState)({ loading: false });
334
312
  async function execute(...args) {
335
- try {
336
- setLoading(true);
337
- const result = await fun(...args);
338
- setLoading(false);
339
- return result;
340
- } catch (error2) {
341
- setLoading(false);
342
- setError(error2);
343
- throw error2;
344
- }
345
- }
346
- return [loading, execute, error];
347
- }
348
-
349
- // ../../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
350
- init_tslib_es6();
351
- var import_react9 = __toESM(require_react());
352
-
353
- // ../../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
354
- var import_react8 = __toESM(require_react());
355
- function useMountedState() {
356
- var mountedRef = (0, import_react8.useRef)(false);
357
- var get = (0, import_react8.useCallback)(function() {
358
- return mountedRef.current;
359
- }, []);
360
- (0, import_react8.useEffect)(function() {
361
- mountedRef.current = true;
362
- return function() {
363
- mountedRef.current = false;
364
- };
365
- }, []);
366
- return get;
367
- }
368
-
369
- // ../../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
370
- function useAsyncFn(fn, deps, initialState) {
371
- if (deps === void 0) {
372
- deps = [];
373
- }
374
- if (initialState === void 0) {
375
- initialState = { loading: false };
376
- }
377
- var lastCallId = (0, import_react9.useRef)(0);
378
- var isMounted = useMountedState();
379
- var _a = (0, import_react9.useState)(initialState), state = _a[0], set = _a[1];
380
- var callback = (0, import_react9.useCallback)(function() {
381
- var args = [];
382
- for (var _i = 0; _i < arguments.length; _i++) {
383
- args[_i] = arguments[_i];
384
- }
385
- var callId = ++lastCallId.current;
386
- if (!state.loading) {
387
- set(function(prevState) {
388
- return __assign(__assign({}, prevState), { loading: true });
389
- });
390
- }
391
- return fn.apply(void 0, args).then(function(value) {
392
- isMounted() && callId === lastCallId.current && set({ value, loading: false });
313
+ return fun(...args).then((value) => {
314
+ set({ loading: false });
393
315
  return value;
394
- }, function(error) {
395
- isMounted() && callId === lastCallId.current && set({ error, loading: false });
396
- return error;
316
+ }).catch((err) => {
317
+ set({ loading: false, error: err });
318
+ return Promise.reject(err);
397
319
  });
398
- }, deps);
399
- return [state, callback];
320
+ }
321
+ return [state.loading, execute, state.error];
400
322
  }
401
323
 
324
+ // src/hooks/useAsyncState.ts
325
+ var import_react9 = __toESM(require_react(), 1);
326
+
402
327
  // ../../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
403
- var import_react10 = __toESM(require_react());
328
+ var import_react8 = __toESM(require_react());
404
329
  var useEffectOnce = function(effect) {
405
- (0, import_react10.useEffect)(effect, []);
330
+ (0, import_react8.useEffect)(effect, []);
406
331
  };
407
332
  var useEffectOnce_default = useEffectOnce;
408
333
 
@@ -415,17 +340,21 @@ var LibReact = (() => {
415
340
  var useMount_default = useMount;
416
341
 
417
342
  // src/hooks/useAsyncState.ts
418
- function useAsyncState(fn, deps, options) {
419
- const [state, _fn] = useAsyncFn(fn, deps, options?.initial);
420
- useMount_default(() => options?.immediate && _fn());
421
- return [state, _fn];
343
+ function useAsyncState(fun, options) {
344
+ const [value, set] = (0, import_react9.useState)(options?.initial);
345
+ const [loading, execute, error] = useAsyncCallback(async (...args) => fun(...args).then(set));
346
+ useMount_default(() => options?.immediate && execute());
347
+ (0, import_react9.useEffect)(() => {
348
+ execute();
349
+ }, [execute]);
350
+ return [{ value, loading, error }, execute];
422
351
  }
423
352
 
424
353
  // src/hooks/useDebounce.ts
425
- var import_react11 = __toESM(require_react(), 1);
354
+ var import_react10 = __toESM(require_react(), 1);
426
355
  function useDebounce(value, delay) {
427
- const [debouncedValue, setDebouncedValue] = (0, import_react11.useState)(value);
428
- (0, import_react11.useEffect)(() => {
356
+ const [debouncedValue, setDebouncedValue] = (0, import_react10.useState)(value);
357
+ (0, import_react10.useEffect)(() => {
429
358
  const handler = setTimeout(() => setDebouncedValue(value), delay);
430
359
  return () => clearTimeout(handler);
431
360
  }, [value, delay]);
@@ -451,14 +380,14 @@ var LibReact = (() => {
451
380
  }
452
381
 
453
382
  // src/hooks/useEventBus.ts
454
- var import_react12 = __toESM(require_react(), 1);
383
+ var import_react11 = __toESM(require_react(), 1);
455
384
  var emitter = mitt_default();
456
385
  function useEventBus(key) {
457
- const onRef = (0, import_react12.useRef)();
386
+ const onRef = (0, import_react11.useRef)();
458
387
  function on(listener) {
459
388
  emitter.on(key, listener);
460
389
  onRef.current = listener;
461
- (0, import_react12.useEffect)(() => {
390
+ (0, import_react11.useEffect)(() => {
462
391
  if (!onRef.current)
463
392
  return;
464
393
  emitter.off(key, onRef.current);
@@ -504,23 +433,23 @@ var LibReact = (() => {
504
433
  }
505
434
 
506
435
  // src/hooks/useMounted.ts
507
- var import_react13 = __toESM(require_react(), 1);
436
+ var import_react12 = __toESM(require_react(), 1);
508
437
  function useMounted() {
509
- const [mounted, setMounted] = (0, import_react13.useState)(false);
510
- (0, import_react13.useEffect)(() => setMounted(true), []);
438
+ const [mounted, setMounted] = (0, import_react12.useState)(false);
439
+ (0, import_react12.useEffect)(() => setMounted(true), []);
511
440
  return mounted;
512
441
  }
513
442
 
514
443
  // src/hooks/useWatch.ts
515
- var import_react14 = __toESM(require_react(), 1);
444
+ var import_react13 = __toESM(require_react(), 1);
516
445
  function useWatch(source, callback, options = {}) {
517
- const firstUpdate = (0, import_react14.useRef)(false);
518
- const then = (0, import_react14.useRef)();
519
- const deps = (0, import_react14.useMemo)(
446
+ const firstUpdate = (0, import_react13.useRef)(false);
447
+ const then = (0, import_react13.useRef)();
448
+ const deps = (0, import_react13.useMemo)(
520
449
  () => Array.isArray(source) ? source : [source],
521
450
  [source]
522
451
  );
523
- (0, import_react14.useEffect)(() => {
452
+ (0, import_react13.useEffect)(() => {
524
453
  if (!firstUpdate.current)
525
454
  recordFirst();
526
455
  else
@@ -541,6 +470,9 @@ var LibReact = (() => {
541
470
  useWatch(source, () => source && cb(source), options);
542
471
  }
543
472
 
473
+ // src/storage/defineStore.ts
474
+ var import_react15 = __toESM(require_react(), 1);
475
+
544
476
  // ../../node_modules/.pnpm/proxy-compare@3.0.1/node_modules/proxy-compare/dist/index.js
545
477
  var TRACK_MEMO_SYMBOL = Symbol();
546
478
  var GET_ORIGINAL_SYMBOL = Symbol();
@@ -978,27 +910,27 @@ var LibReact = (() => {
978
910
  }
979
911
 
980
912
  // ../../node_modules/.pnpm/valtio@2.1.4_@types+react@18.3.18_react@18.3.1/node_modules/valtio/esm/react.mjs
981
- var import_react15 = __toESM(require_react(), 1);
913
+ var import_react14 = __toESM(require_react(), 1);
982
914
  var import_meta2 = {};
983
915
  var useAffectedDebugValue = (state, affected) => {
984
- const pathList = (0, import_react15.useRef)(void 0);
985
- (0, import_react15.useEffect)(() => {
916
+ const pathList = (0, import_react14.useRef)(void 0);
917
+ (0, import_react14.useEffect)(() => {
986
918
  pathList.current = affectedToPathList(state, affected, true);
987
919
  });
988
- (0, import_react15.useDebugValue)(pathList.current);
920
+ (0, import_react14.useDebugValue)(pathList.current);
989
921
  };
990
922
  var condUseAffectedDebugValue = useAffectedDebugValue;
991
923
  var targetCache = /* @__PURE__ */ new WeakMap();
992
924
  function useSnapshot(proxyObject, options) {
993
925
  const notifyInSync = options == null ? void 0 : options.sync;
994
- const affected = (0, import_react15.useMemo)(
926
+ const affected = (0, import_react14.useMemo)(
995
927
  () => proxyObject && /* @__PURE__ */ new WeakMap(),
996
928
  [proxyObject]
997
929
  );
998
- const lastSnapshot = (0, import_react15.useRef)(void 0);
930
+ const lastSnapshot = (0, import_react14.useRef)(void 0);
999
931
  let inRender = true;
1000
- const currSnapshot = (0, import_react15.useSyncExternalStore)(
1001
- (0, import_react15.useCallback)(
932
+ const currSnapshot = (0, import_react14.useSyncExternalStore)(
933
+ (0, import_react14.useCallback)(
1002
934
  (callback) => {
1003
935
  const unsub = subscribe(proxyObject, callback, notifyInSync);
1004
936
  callback();
@@ -1024,13 +956,13 @@ var LibReact = (() => {
1024
956
  () => snapshot(proxyObject)
1025
957
  );
1026
958
  inRender = false;
1027
- (0, import_react15.useLayoutEffect)(() => {
959
+ (0, import_react14.useLayoutEffect)(() => {
1028
960
  lastSnapshot.current = currSnapshot;
1029
961
  });
1030
962
  if ((import_meta2.env ? import_meta2.env.MODE : void 0) !== "production") {
1031
963
  condUseAffectedDebugValue(currSnapshot, affected);
1032
964
  }
1033
- const proxyCache2 = (0, import_react15.useMemo)(() => /* @__PURE__ */ new WeakMap(), []);
965
+ const proxyCache2 = (0, import_react14.useMemo)(() => /* @__PURE__ */ new WeakMap(), []);
1034
966
  return createProxy(currSnapshot, affected, proxyCache2, targetCache);
1035
967
  }
1036
968
 
@@ -1043,7 +975,7 @@ var LibReact = (() => {
1043
975
  }
1044
976
  }
1045
977
 
1046
- // src/storage/proxyWithPersistant.ts
978
+ // src/storage/persistant.ts
1047
979
  function proxyWithPersistant(keyOrOptions, initialObject) {
1048
980
  let options;
1049
981
  if (typeof keyOrOptions === "string") {
@@ -1067,76 +999,131 @@ var LibReact = (() => {
1067
999
  // src/storage/defineStore.ts
1068
1000
  function defineStore(store, options = {}) {
1069
1001
  const state = typeof store.state === "function" ? store.state() : store.state;
1002
+ const getters = store.getters || {};
1070
1003
  const actions = store.actions || {};
1071
- const $state = options.persistant ? proxyWithPersistant(options.persistant, state) : proxy(state);
1004
+ const status = {};
1005
+ status.finished = false;
1006
+ status.loading = false;
1007
+ status.error = null;
1008
+ const $state = options.persist ? proxyWithPersistant(options.persist, state) : proxy(state);
1009
+ const $status = proxy(status);
1072
1010
  const $actions = {};
1073
- for (const key in actions)
1074
- $actions[key] = actions[key].bind($state);
1011
+ const $getters = {};
1012
+ setupActions($state, actions, $actions, $status);
1013
+ setupGetters(state, $state, getters, $getters);
1014
+ setupStatus($actions, $status);
1075
1015
  function $subscribe(listener) {
1076
1016
  return subscribe($state, () => listener($state));
1077
1017
  }
1018
+ $subscribe.status = function(listener) {
1019
+ return subscribe($status, () => listener($status));
1020
+ };
1078
1021
  function $patch(patch) {
1079
1022
  if (typeof patch === "function")
1080
1023
  patch($state);
1081
1024
  else
1082
1025
  Object.assign($state, patch);
1083
1026
  }
1027
+ function $signal(fn) {
1028
+ return (0, import_react15.createElement)(() => fn(useSnapshot($state)));
1029
+ }
1030
+ $signal.status = function(fn) {
1031
+ return (0, import_react15.createElement)(() => fn(useSnapshot($status)));
1032
+ };
1084
1033
  return {
1085
1034
  $subscribe,
1086
1035
  $patch,
1087
1036
  $state,
1037
+ $status,
1088
1038
  $actions,
1039
+ $getters,
1040
+ $signal,
1089
1041
  ...$actions
1090
1042
  };
1091
1043
  }
1092
-
1093
- // src/storage/useStore.ts
1094
- function useStore(store) {
1095
- return useSnapshot(store.$state);
1044
+ function track(action, status) {
1045
+ let loadings = 0;
1046
+ const tracking = () => loadings++ === 0 && (status.loading = true);
1047
+ const done = () => !--loadings && (status.loading = false);
1048
+ const fulfilled = () => {
1049
+ status.finished = true;
1050
+ done();
1051
+ };
1052
+ const rejected = (error) => {
1053
+ status.error = error;
1054
+ done();
1055
+ };
1056
+ return function(...args) {
1057
+ tracking();
1058
+ try {
1059
+ const result = action(...args);
1060
+ if (result instanceof Promise)
1061
+ result.then(fulfilled).catch(rejected);
1062
+ else
1063
+ fulfilled();
1064
+ } catch (error) {
1065
+ rejected(error);
1066
+ }
1067
+ };
1068
+ }
1069
+ function setupActions($state, actions, $actions, $status) {
1070
+ for (const key in actions) {
1071
+ $status[key] = { finished: false, loading: false, error: null };
1072
+ $actions[key] = track(actions[key].bind($state), $status[key]);
1073
+ }
1074
+ }
1075
+ function setupGetters(state, $state, getters, $getters) {
1076
+ for (const key in getters) {
1077
+ Object.defineProperty(state, key, {
1078
+ get: () => getters[key].call($state),
1079
+ enumerable: true
1080
+ });
1081
+ Object.defineProperty($getters, key, {
1082
+ get: () => state[key],
1083
+ enumerable: true
1084
+ });
1085
+ }
1086
+ }
1087
+ function setupStatus($actions, $status) {
1088
+ Object.defineProperty($status, "loading", {
1089
+ get: () => Object.keys($actions).some((key) => $status[key].loading),
1090
+ enumerable: true
1091
+ });
1092
+ Object.defineProperty($status, "finished", {
1093
+ get: () => Object.keys($actions).every((key) => $status[key].finished),
1094
+ enumerable: true
1095
+ });
1096
+ Object.defineProperty($status, "error", {
1097
+ get: () => Object.keys($actions).find((key) => $status[key].error),
1098
+ enumerable: true
1099
+ });
1096
1100
  }
1097
1101
 
1098
1102
  // src/storage/defineAsyncStore.ts
1099
- function defineAsyncStore(options) {
1103
+ function defineAsyncStore(fetch, options = {}) {
1100
1104
  const store = defineStore(
1101
1105
  {
1102
- state: () => ({
1103
- promise: void 0,
1104
- value: options.initial,
1105
- loading: false,
1106
- error: void 0
1107
- })
1106
+ state: () => ({ value: options.initial }),
1107
+ actions: {
1108
+ fetch,
1109
+ refresh(value) {
1110
+ this.value = value || options.initial;
1111
+ }
1112
+ }
1108
1113
  },
1109
- { persistant: options.persistant ? { id: options.persistant, pick: ["value"] } : void 0 }
1114
+ { persist: options.persist }
1110
1115
  );
1111
- function use() {
1112
- const fn = options.setup();
1113
- const state = useStore(store);
1114
- function fetch(...args) {
1115
- if (state.loading)
1116
- return;
1117
- store.$state.loading = true;
1118
- store.$state.promise = fn(...args);
1119
- store.$state.promise.then((value) => store.$state.value = value).finally(() => store.$state.loading = false).catch((error) => {
1120
- store.$state.error = error;
1121
- throw error;
1122
- });
1123
- return store.$state.promise;
1124
- }
1125
- function refresh(value) {
1126
- store.$state.value = value || options.initial;
1127
- }
1128
- return [state, fetch, refresh];
1129
- }
1130
- return use;
1116
+ return store;
1131
1117
  }
1132
1118
 
1133
- // src/storage/defineAsyncStorePlain.ts
1134
- function defineAsyncStorePlain(fn, options = {}) {
1135
- return defineAsyncStore({
1136
- setup: () => fn,
1137
- initial: options.initial,
1138
- persistant: options.persistant
1139
- });
1119
+ // src/storage/useStatus.tsx
1120
+ function useStatus(store) {
1121
+ return useSnapshot(store.$status);
1122
+ }
1123
+
1124
+ // src/storage/useStore.ts
1125
+ function useStore(store) {
1126
+ return useSnapshot(store.$state);
1140
1127
  }
1141
1128
  return __toCommonJS(index_exports);
1142
1129
  })();