@hairy/react-lib 1.15.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
- defineAsyncStoreLayered: () => defineAsyncStoreLayered,
109
87
  defineStore: () => defineStore,
110
88
  proxyWithPersistant: () => proxyWithPersistant,
111
89
  useAsyncCallback: () => useAsyncCallback,
@@ -330,80 +308,26 @@ var LibReact = (() => {
330
308
  // src/hooks/useAsyncCallback.ts
331
309
  var import_react7 = __toESM(require_react(), 1);
332
310
  function useAsyncCallback(fun) {
333
- const [error, setError] = (0, import_react7.useState)();
334
- const [loading, setLoading] = (0, import_react7.useState)(false);
311
+ const [state, set] = (0, import_react7.useState)({ loading: false });
335
312
  async function execute(...args) {
336
- try {
337
- setLoading(true);
338
- const result = await fun(...args);
339
- setLoading(false);
340
- return result;
341
- } catch (error2) {
342
- setLoading(false);
343
- setError(error2);
344
- throw error2;
345
- }
346
- }
347
- return [loading, execute, error];
348
- }
349
-
350
- // ../../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
351
- init_tslib_es6();
352
- var import_react9 = __toESM(require_react());
353
-
354
- // ../../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
355
- var import_react8 = __toESM(require_react());
356
- function useMountedState() {
357
- var mountedRef = (0, import_react8.useRef)(false);
358
- var get = (0, import_react8.useCallback)(function() {
359
- return mountedRef.current;
360
- }, []);
361
- (0, import_react8.useEffect)(function() {
362
- mountedRef.current = true;
363
- return function() {
364
- mountedRef.current = false;
365
- };
366
- }, []);
367
- return get;
368
- }
369
-
370
- // ../../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
371
- function useAsyncFn(fn, deps, initialState) {
372
- if (deps === void 0) {
373
- deps = [];
374
- }
375
- if (initialState === void 0) {
376
- initialState = { loading: false };
377
- }
378
- var lastCallId = (0, import_react9.useRef)(0);
379
- var isMounted = useMountedState();
380
- var _a = (0, import_react9.useState)(initialState), state = _a[0], set = _a[1];
381
- var callback = (0, import_react9.useCallback)(function() {
382
- var args = [];
383
- for (var _i = 0; _i < arguments.length; _i++) {
384
- args[_i] = arguments[_i];
385
- }
386
- var callId = ++lastCallId.current;
387
- if (!state.loading) {
388
- set(function(prevState) {
389
- return __assign(__assign({}, prevState), { loading: true });
390
- });
391
- }
392
- return fn.apply(void 0, args).then(function(value) {
393
- isMounted() && callId === lastCallId.current && set({ value, loading: false });
313
+ return fun(...args).then((value) => {
314
+ set({ loading: false });
394
315
  return value;
395
- }, function(error) {
396
- isMounted() && callId === lastCallId.current && set({ error, loading: false });
397
- return error;
316
+ }).catch((err) => {
317
+ set({ loading: false, error: err });
318
+ return Promise.reject(err);
398
319
  });
399
- }, deps);
400
- return [state, callback];
320
+ }
321
+ return [state.loading, execute, state.error];
401
322
  }
402
323
 
324
+ // src/hooks/useAsyncState.ts
325
+ var import_react9 = __toESM(require_react(), 1);
326
+
403
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
404
- var import_react10 = __toESM(require_react());
328
+ var import_react8 = __toESM(require_react());
405
329
  var useEffectOnce = function(effect) {
406
- (0, import_react10.useEffect)(effect, []);
330
+ (0, import_react8.useEffect)(effect, []);
407
331
  };
408
332
  var useEffectOnce_default = useEffectOnce;
409
333
 
@@ -416,17 +340,21 @@ var LibReact = (() => {
416
340
  var useMount_default = useMount;
417
341
 
418
342
  // src/hooks/useAsyncState.ts
419
- function useAsyncState(fn, deps, options) {
420
- const [state, _fn] = useAsyncFn(fn, deps, options?.initial);
421
- useMount_default(() => options?.immediate && _fn());
422
- 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];
423
351
  }
424
352
 
425
353
  // src/hooks/useDebounce.ts
426
- var import_react11 = __toESM(require_react(), 1);
354
+ var import_react10 = __toESM(require_react(), 1);
427
355
  function useDebounce(value, delay) {
428
- const [debouncedValue, setDebouncedValue] = (0, import_react11.useState)(value);
429
- (0, import_react11.useEffect)(() => {
356
+ const [debouncedValue, setDebouncedValue] = (0, import_react10.useState)(value);
357
+ (0, import_react10.useEffect)(() => {
430
358
  const handler = setTimeout(() => setDebouncedValue(value), delay);
431
359
  return () => clearTimeout(handler);
432
360
  }, [value, delay]);
@@ -452,14 +380,14 @@ var LibReact = (() => {
452
380
  }
453
381
 
454
382
  // src/hooks/useEventBus.ts
455
- var import_react12 = __toESM(require_react(), 1);
383
+ var import_react11 = __toESM(require_react(), 1);
456
384
  var emitter = mitt_default();
457
385
  function useEventBus(key) {
458
- const onRef = (0, import_react12.useRef)();
386
+ const onRef = (0, import_react11.useRef)();
459
387
  function on(listener) {
460
388
  emitter.on(key, listener);
461
389
  onRef.current = listener;
462
- (0, import_react12.useEffect)(() => {
390
+ (0, import_react11.useEffect)(() => {
463
391
  if (!onRef.current)
464
392
  return;
465
393
  emitter.off(key, onRef.current);
@@ -505,23 +433,23 @@ var LibReact = (() => {
505
433
  }
506
434
 
507
435
  // src/hooks/useMounted.ts
508
- var import_react13 = __toESM(require_react(), 1);
436
+ var import_react12 = __toESM(require_react(), 1);
509
437
  function useMounted() {
510
- const [mounted, setMounted] = (0, import_react13.useState)(false);
511
- (0, import_react13.useEffect)(() => setMounted(true), []);
438
+ const [mounted, setMounted] = (0, import_react12.useState)(false);
439
+ (0, import_react12.useEffect)(() => setMounted(true), []);
512
440
  return mounted;
513
441
  }
514
442
 
515
443
  // src/hooks/useWatch.ts
516
- var import_react14 = __toESM(require_react(), 1);
444
+ var import_react13 = __toESM(require_react(), 1);
517
445
  function useWatch(source, callback, options = {}) {
518
- const firstUpdate = (0, import_react14.useRef)(false);
519
- const then = (0, import_react14.useRef)();
520
- 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)(
521
449
  () => Array.isArray(source) ? source : [source],
522
450
  [source]
523
451
  );
524
- (0, import_react14.useEffect)(() => {
452
+ (0, import_react13.useEffect)(() => {
525
453
  if (!firstUpdate.current)
526
454
  recordFirst();
527
455
  else
@@ -543,7 +471,7 @@ var LibReact = (() => {
543
471
  }
544
472
 
545
473
  // src/storage/defineStore.ts
546
- var import_react16 = __toESM(require_react(), 1);
474
+ var import_react15 = __toESM(require_react(), 1);
547
475
 
548
476
  // ../../node_modules/.pnpm/proxy-compare@3.0.1/node_modules/proxy-compare/dist/index.js
549
477
  var TRACK_MEMO_SYMBOL = Symbol();
@@ -982,27 +910,27 @@ var LibReact = (() => {
982
910
  }
983
911
 
984
912
  // ../../node_modules/.pnpm/valtio@2.1.4_@types+react@18.3.18_react@18.3.1/node_modules/valtio/esm/react.mjs
985
- var import_react15 = __toESM(require_react(), 1);
913
+ var import_react14 = __toESM(require_react(), 1);
986
914
  var import_meta2 = {};
987
915
  var useAffectedDebugValue = (state, affected) => {
988
- const pathList = (0, import_react15.useRef)(void 0);
989
- (0, import_react15.useEffect)(() => {
916
+ const pathList = (0, import_react14.useRef)(void 0);
917
+ (0, import_react14.useEffect)(() => {
990
918
  pathList.current = affectedToPathList(state, affected, true);
991
919
  });
992
- (0, import_react15.useDebugValue)(pathList.current);
920
+ (0, import_react14.useDebugValue)(pathList.current);
993
921
  };
994
922
  var condUseAffectedDebugValue = useAffectedDebugValue;
995
923
  var targetCache = /* @__PURE__ */ new WeakMap();
996
924
  function useSnapshot(proxyObject, options) {
997
925
  const notifyInSync = options == null ? void 0 : options.sync;
998
- const affected = (0, import_react15.useMemo)(
926
+ const affected = (0, import_react14.useMemo)(
999
927
  () => proxyObject && /* @__PURE__ */ new WeakMap(),
1000
928
  [proxyObject]
1001
929
  );
1002
- const lastSnapshot = (0, import_react15.useRef)(void 0);
930
+ const lastSnapshot = (0, import_react14.useRef)(void 0);
1003
931
  let inRender = true;
1004
- const currSnapshot = (0, import_react15.useSyncExternalStore)(
1005
- (0, import_react15.useCallback)(
932
+ const currSnapshot = (0, import_react14.useSyncExternalStore)(
933
+ (0, import_react14.useCallback)(
1006
934
  (callback) => {
1007
935
  const unsub = subscribe(proxyObject, callback, notifyInSync);
1008
936
  callback();
@@ -1028,13 +956,13 @@ var LibReact = (() => {
1028
956
  () => snapshot(proxyObject)
1029
957
  );
1030
958
  inRender = false;
1031
- (0, import_react15.useLayoutEffect)(() => {
959
+ (0, import_react14.useLayoutEffect)(() => {
1032
960
  lastSnapshot.current = currSnapshot;
1033
961
  });
1034
962
  if ((import_meta2.env ? import_meta2.env.MODE : void 0) !== "production") {
1035
963
  condUseAffectedDebugValue(currSnapshot, affected);
1036
964
  }
1037
- const proxyCache2 = (0, import_react15.useMemo)(() => /* @__PURE__ */ new WeakMap(), []);
965
+ const proxyCache2 = (0, import_react14.useMemo)(() => /* @__PURE__ */ new WeakMap(), []);
1038
966
  return createProxy(currSnapshot, affected, proxyCache2, targetCache);
1039
967
  }
1040
968
 
@@ -1047,7 +975,7 @@ var LibReact = (() => {
1047
975
  }
1048
976
  }
1049
977
 
1050
- // src/storage/proxyWithPersistant.ts
978
+ // src/storage/persistant.ts
1051
979
  function proxyWithPersistant(keyOrOptions, initialObject) {
1052
980
  let options;
1053
981
  if (typeof keyOrOptions === "string") {
@@ -1081,12 +1009,15 @@ var LibReact = (() => {
1081
1009
  const $status = proxy(status);
1082
1010
  const $actions = {};
1083
1011
  const $getters = {};
1084
- markActions($state, actions, $actions);
1085
- markGetters(state, $state, getters, $getters);
1086
- markStatus($actions, $status);
1012
+ setupActions($state, actions, $actions, $status);
1013
+ setupGetters(state, $state, getters, $getters);
1014
+ setupStatus($actions, $status);
1087
1015
  function $subscribe(listener) {
1088
1016
  return subscribe($state, () => listener($state));
1089
1017
  }
1018
+ $subscribe.status = function(listener) {
1019
+ return subscribe($status, () => listener($status));
1020
+ };
1090
1021
  function $patch(patch) {
1091
1022
  if (typeof patch === "function")
1092
1023
  patch($state);
@@ -1094,10 +1025,10 @@ var LibReact = (() => {
1094
1025
  Object.assign($state, patch);
1095
1026
  }
1096
1027
  function $signal(fn) {
1097
- return (0, import_react16.createElement)(() => fn(useSnapshot($state)));
1028
+ return (0, import_react15.createElement)(() => fn(useSnapshot($state)));
1098
1029
  }
1099
1030
  $signal.status = function(fn) {
1100
- return (0, import_react16.createElement)(() => fn(useSnapshot($status)));
1031
+ return (0, import_react15.createElement)(() => fn(useSnapshot($status)));
1101
1032
  };
1102
1033
  return {
1103
1034
  $subscribe,
@@ -1107,15 +1038,41 @@ var LibReact = (() => {
1107
1038
  $actions,
1108
1039
  $getters,
1109
1040
  $signal,
1110
- ...$actions,
1111
- ...$state
1041
+ ...$actions
1042
+ };
1043
+ }
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
+ }
1112
1067
  };
1113
1068
  }
1114
- function markActions($state, actions, $actions) {
1115
- for (const key in actions)
1116
- $actions[key] = actions[key].bind($state);
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
+ }
1117
1074
  }
1118
- function markGetters(state, $state, getters, $getters) {
1075
+ function setupGetters(state, $state, getters, $getters) {
1119
1076
  for (const key in getters) {
1120
1077
  Object.defineProperty(state, key, {
1121
1078
  get: () => getters[key].call($state),
@@ -1127,7 +1084,7 @@ var LibReact = (() => {
1127
1084
  });
1128
1085
  }
1129
1086
  }
1130
- function markStatus($actions, $status) {
1087
+ function setupStatus($actions, $status) {
1131
1088
  Object.defineProperty($status, "loading", {
1132
1089
  get: () => Object.keys($actions).some((key) => $status[key].loading),
1133
1090
  enumerable: true
@@ -1140,90 +1097,33 @@ var LibReact = (() => {
1140
1097
  get: () => Object.keys($actions).find((key) => $status[key].error),
1141
1098
  enumerable: true
1142
1099
  });
1143
- for (const key in $actions) {
1144
- let subscribe3 = function() {
1145
- if (subscribers === 0)
1146
- $status[key].loading = true;
1147
- subscribers++;
1148
- }, done2 = function() {
1149
- subscribers--;
1150
- if (!subscribers)
1151
- $status[key].loading = false;
1152
- };
1153
- var subscribe2 = subscribe3, done = done2;
1154
- const action = $actions[key];
1155
- $status[key] = { finished: false, loading: false, error: null };
1156
- let subscribers = 0;
1157
- $actions[key] = function(...args) {
1158
- subscribe3();
1159
- try {
1160
- const result = action(...args);
1161
- if (result instanceof Promise) {
1162
- result.then(() => $status[key].finished = true).catch((error) => $status[key].error = error).finally(done2);
1163
- } else {
1164
- $status[key].finished = true;
1165
- done2();
1166
- }
1167
- } catch (error) {
1168
- $status[key].error = error;
1169
- done2();
1170
- }
1171
- };
1172
- }
1173
- }
1174
-
1175
- // src/storage/useStore.ts
1176
- function useStore(store) {
1177
- return useSnapshot(store.$state);
1178
1100
  }
1179
1101
 
1180
1102
  // src/storage/defineAsyncStore.ts
1181
- function defineAsyncStore(options) {
1103
+ function defineAsyncStore(fetch, options = {}) {
1182
1104
  const store = defineStore(
1183
1105
  {
1184
- state: () => ({
1185
- promise: void 0,
1186
- value: options.initial,
1187
- loading: false,
1188
- error: void 0
1189
- })
1106
+ state: () => ({ value: options.initial }),
1107
+ actions: {
1108
+ fetch,
1109
+ refresh(value) {
1110
+ this.value = value || options.initial;
1111
+ }
1112
+ }
1190
1113
  },
1191
- { persist: options.persist ? { id: options.persist, pick: ["value"] } : void 0 }
1114
+ { persist: options.persist }
1192
1115
  );
1193
- function use() {
1194
- const fn = options.setup();
1195
- const state = useStore(store);
1196
- function fetch(...args) {
1197
- if (state.loading)
1198
- return;
1199
- store.$state.loading = true;
1200
- store.$state.promise = fn(...args);
1201
- store.$state.promise.then((value) => store.$state.value = value).finally(() => store.$state.loading = false).catch((error) => {
1202
- store.$state.error = error;
1203
- throw error;
1204
- });
1205
- return store.$state.promise;
1206
- }
1207
- function refresh(value) {
1208
- store.$state.value = value || options.initial;
1209
- }
1210
- return [state, fetch, refresh];
1211
- }
1212
- return use;
1213
- }
1214
-
1215
- // src/storage/defineAsyncStoreLayered.ts
1216
- function defineAsyncStoreLayered(fn, options = {}) {
1217
- return defineAsyncStore({
1218
- setup: () => fn,
1219
- initial: options.initial,
1220
- persist: options.persist
1221
- });
1116
+ return store;
1222
1117
  }
1223
1118
 
1224
1119
  // src/storage/useStatus.tsx
1225
1120
  function useStatus(store) {
1226
1121
  return useSnapshot(store.$status);
1227
1122
  }
1123
+
1124
+ // src/storage/useStore.ts
1125
+ function useStore(store) {
1126
+ return useSnapshot(store.$state);
1127
+ }
1228
1128
  return __toCommonJS(index_exports);
1229
1129
  })();