@hairy/react-lib 1.19.0 → 1.20.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
@@ -512,11 +512,14 @@ function defineAsyncStore(fetch, options = {}) {
512
512
  }
513
513
  }
514
514
  },
515
- { persist: options.persist }
515
+ { persist: options.persist ? { id: options.persist, pick: ["value"] } : void 0 }
516
516
  );
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);
517
+ (0, import_utils8.watch)((get) => {
518
+ const status = get(store.$status.fetch);
519
+ store.$state.error = status.error;
520
+ store.$state.loading = status.loading;
521
+ store.$state.finished = status.finished;
522
+ });
520
523
  options.immediate && store.fetch();
521
524
  return store;
522
525
  }
@@ -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();
@@ -1147,11 +1190,14 @@ var LibReact = (() => {
1147
1190
  }
1148
1191
  }
1149
1192
  },
1150
- { persist: options.persist }
1193
+ { persist: options.persist ? { id: options.persist, pick: ["value"] } : void 0 }
1151
1194
  );
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);
1195
+ watch((get) => {
1196
+ const status = get(store.$status.fetch);
1197
+ store.$state.error = status.error;
1198
+ store.$state.loading = status.loading;
1199
+ store.$state.finished = status.finished;
1200
+ });
1155
1201
  options.immediate && store.fetch();
1156
1202
  return store;
1157
1203
  }
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";
@@ -452,11 +452,14 @@ function defineAsyncStore(fetch, options = {}) {
452
452
  }
453
453
  }
454
454
  },
455
- { persist: options.persist }
455
+ { persist: options.persist ? { id: options.persist, pick: ["value"] } : void 0 }
456
456
  );
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);
457
+ watch((get) => {
458
+ const status = get(store.$status.fetch);
459
+ store.$state.error = status.error;
460
+ store.$state.loading = status.loading;
461
+ store.$state.finished = status.finished;
462
+ });
460
463
  options.immediate && store.fetch();
461
464
  return store;
462
465
  }
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.20.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.20.0"
42
42
  },
43
43
  "scripts": {
44
44
  "build": "tsup",