@hairy/react-lib 1.19.0 → 1.21.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
@@ -437,24 +437,21 @@ function track(action, status) {
437
437
  let loadings = 0;
438
438
  const tracking = () => loadings++ === 0 && (status.loading = true);
439
439
  const done = () => !--loadings && (status.loading = false);
440
- const fulfilled = () => {
440
+ const fulfilled = (value) => {
441
441
  status.finished = true;
442
442
  done();
443
+ return value;
443
444
  };
444
445
  const rejected = (error) => {
445
446
  status.error = error;
446
447
  done();
448
+ throw error;
447
449
  };
448
450
  return function(...args) {
449
451
  tracking();
450
452
  try {
451
- const result = action(...args);
452
- if (result instanceof Promise) {
453
- return result.then(fulfilled).catch(rejected);
454
- } else {
455
- fulfilled();
456
- return result;
457
- }
453
+ const value = action(...args);
454
+ return value instanceof Promise ? value.then(fulfilled, rejected) : fulfilled(value);
458
455
  } catch (error) {
459
456
  rejected(error);
460
457
  }
@@ -512,11 +509,14 @@ function defineAsyncStore(fetch, options = {}) {
512
509
  }
513
510
  }
514
511
  },
515
- { persist: options.persist }
512
+ { persist: options.persist ? { id: options.persist, pick: ["value"] } : void 0 }
516
513
  );
517
- (0, import_utils8.subscribeKey)(store.$status, "error", (error) => store.$state.error = error);
518
- (0, import_utils8.subscribeKey)(store.$status, "loading", (loading) => store.$state.loading = loading);
519
- (0, import_utils8.subscribeKey)(store.$status, "finished", (finished) => store.$state.finished = finished);
514
+ (0, import_utils8.watch)((get) => {
515
+ const status = get(store.$status.fetch);
516
+ store.$state.error = status.error;
517
+ store.$state.loading = status.loading;
518
+ store.$state.finished = status.finished;
519
+ });
520
520
  options.immediate && store.fetch();
521
521
  return store;
522
522
  }
@@ -916,18 +916,61 @@ var LibReact = (() => {
916
916
  }
917
917
 
918
918
  // ../../node_modules/.pnpm/valtio@2.1.4_@types+react@18.3.18_react@18.3.1/node_modules/valtio/esm/vanilla/utils.mjs
919
- function subscribeKey(proxyObject, key, callback, notifyInSync) {
920
- let prevValue = proxyObject[key];
921
- return subscribe(
922
- proxyObject,
923
- () => {
924
- const nextValue = proxyObject[key];
925
- if (!Object.is(prevValue, nextValue)) {
926
- callback(prevValue = nextValue);
919
+ var currentCleanups;
920
+ function watch(callback, options) {
921
+ let alive = true;
922
+ const cleanups = /* @__PURE__ */ new Set();
923
+ const subscriptions = /* @__PURE__ */ new Map();
924
+ const cleanup = () => {
925
+ if (alive) {
926
+ alive = false;
927
+ cleanups.forEach((clean) => clean());
928
+ cleanups.clear();
929
+ subscriptions.forEach((unsubscribe) => unsubscribe());
930
+ subscriptions.clear();
931
+ }
932
+ };
933
+ const revalidate = async () => {
934
+ if (!alive) {
935
+ return;
936
+ }
937
+ cleanups.forEach((clean) => clean());
938
+ cleanups.clear();
939
+ const proxiesToSubscribe = /* @__PURE__ */ new Set();
940
+ const parent = currentCleanups;
941
+ currentCleanups = cleanups;
942
+ try {
943
+ const promiseOrPossibleCleanup = callback((proxyObject) => {
944
+ proxiesToSubscribe.add(proxyObject);
945
+ if (alive && !subscriptions.has(proxyObject)) {
946
+ const unsubscribe = subscribe(proxyObject, revalidate, options == null ? void 0 : options.sync);
947
+ subscriptions.set(proxyObject, unsubscribe);
948
+ }
949
+ return proxyObject;
950
+ });
951
+ const couldBeCleanup = promiseOrPossibleCleanup && promiseOrPossibleCleanup instanceof Promise ? await promiseOrPossibleCleanup : promiseOrPossibleCleanup;
952
+ if (couldBeCleanup) {
953
+ if (alive) {
954
+ cleanups.add(couldBeCleanup);
955
+ } else {
956
+ cleanup();
957
+ }
927
958
  }
928
- },
929
- notifyInSync
930
- );
959
+ } finally {
960
+ currentCleanups = parent;
961
+ }
962
+ subscriptions.forEach((unsubscribe, proxyObject) => {
963
+ if (!proxiesToSubscribe.has(proxyObject)) {
964
+ subscriptions.delete(proxyObject);
965
+ unsubscribe();
966
+ }
967
+ });
968
+ };
969
+ if (currentCleanups) {
970
+ currentCleanups.add(cleanup);
971
+ }
972
+ revalidate();
973
+ return cleanup;
931
974
  }
932
975
  var DEVTOOLS = Symbol();
933
976
  var { proxyStateMap: proxyStateMap$1, snapCache: snapCache$1 } = unstable_getInternalStates();
@@ -1072,24 +1115,21 @@ var LibReact = (() => {
1072
1115
  let loadings = 0;
1073
1116
  const tracking = () => loadings++ === 0 && (status.loading = true);
1074
1117
  const done = () => !--loadings && (status.loading = false);
1075
- const fulfilled = () => {
1118
+ const fulfilled = (value) => {
1076
1119
  status.finished = true;
1077
1120
  done();
1121
+ return value;
1078
1122
  };
1079
1123
  const rejected = (error) => {
1080
1124
  status.error = error;
1081
1125
  done();
1126
+ throw error;
1082
1127
  };
1083
1128
  return function(...args) {
1084
1129
  tracking();
1085
1130
  try {
1086
- const result = action(...args);
1087
- if (result instanceof Promise) {
1088
- return result.then(fulfilled).catch(rejected);
1089
- } else {
1090
- fulfilled();
1091
- return result;
1092
- }
1131
+ const value = action(...args);
1132
+ return value instanceof Promise ? value.then(fulfilled, rejected) : fulfilled(value);
1093
1133
  } catch (error) {
1094
1134
  rejected(error);
1095
1135
  }
@@ -1147,11 +1187,14 @@ var LibReact = (() => {
1147
1187
  }
1148
1188
  }
1149
1189
  },
1150
- { persist: options.persist }
1190
+ { persist: options.persist ? { id: options.persist, pick: ["value"] } : void 0 }
1151
1191
  );
1152
- subscribeKey(store.$status, "error", (error) => store.$state.error = error);
1153
- subscribeKey(store.$status, "loading", (loading) => store.$state.loading = loading);
1154
- subscribeKey(store.$status, "finished", (finished) => store.$state.finished = finished);
1192
+ watch((get) => {
1193
+ const status = get(store.$status.fetch);
1194
+ store.$state.error = status.error;
1195
+ store.$state.loading = status.loading;
1196
+ store.$state.finished = status.finished;
1197
+ });
1155
1198
  options.immediate && store.fetch();
1156
1199
  return store;
1157
1200
  }
package/dist/index.js CHANGED
@@ -291,7 +291,7 @@ function useWhenever(source, cb, options) {
291
291
  }
292
292
 
293
293
  // src/storage/defineAsyncStore.ts
294
- import { subscribeKey } from "valtio/utils";
294
+ import { watch } from "valtio/utils";
295
295
 
296
296
  // src/storage/defineStore.ts
297
297
  import { createElement as createElement4 } from "react";
@@ -377,24 +377,21 @@ function track(action, status) {
377
377
  let loadings = 0;
378
378
  const tracking = () => loadings++ === 0 && (status.loading = true);
379
379
  const done = () => !--loadings && (status.loading = false);
380
- const fulfilled = () => {
380
+ const fulfilled = (value) => {
381
381
  status.finished = true;
382
382
  done();
383
+ return value;
383
384
  };
384
385
  const rejected = (error) => {
385
386
  status.error = error;
386
387
  done();
388
+ throw error;
387
389
  };
388
390
  return function(...args) {
389
391
  tracking();
390
392
  try {
391
- const result = action(...args);
392
- if (result instanceof Promise) {
393
- return result.then(fulfilled).catch(rejected);
394
- } else {
395
- fulfilled();
396
- return result;
397
- }
393
+ const value = action(...args);
394
+ return value instanceof Promise ? value.then(fulfilled, rejected) : fulfilled(value);
398
395
  } catch (error) {
399
396
  rejected(error);
400
397
  }
@@ -452,11 +449,14 @@ function defineAsyncStore(fetch, options = {}) {
452
449
  }
453
450
  }
454
451
  },
455
- { persist: options.persist }
452
+ { persist: options.persist ? { id: options.persist, pick: ["value"] } : void 0 }
456
453
  );
457
- subscribeKey(store.$status, "error", (error) => store.$state.error = error);
458
- subscribeKey(store.$status, "loading", (loading) => store.$state.loading = loading);
459
- subscribeKey(store.$status, "finished", (finished) => store.$state.finished = finished);
454
+ watch((get) => {
455
+ const status = get(store.$status.fetch);
456
+ store.$state.error = status.error;
457
+ store.$state.loading = status.loading;
458
+ store.$state.finished = status.finished;
459
+ });
460
460
  options.immediate && store.fetch();
461
461
  return store;
462
462
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hairy/react-lib",
3
3
  "type": "module",
4
- "version": "1.19.0",
4
+ "version": "1.21.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": "^18.2.0",
39
39
  "react-i18next": "^14.1.2",
40
40
  "react-use": "^17.6.0",
41
- "@hairy/utils": "1.19.0"
41
+ "@hairy/utils": "1.21.0"
42
42
  },
43
43
  "scripts": {
44
44
  "build": "tsup",