@hairy/react-lib 1.18.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
@@ -350,6 +350,9 @@ function useWhenever(source, cb, options) {
350
350
  useWatch(source, () => source && cb(source), options);
351
351
  }
352
352
 
353
+ // src/storage/defineAsyncStore.ts
354
+ var import_utils8 = require("valtio/utils");
355
+
353
356
  // src/storage/defineStore.ts
354
357
  var import_react14 = require("react");
355
358
  var import_valtio2 = require("valtio");
@@ -394,8 +397,8 @@ function defineStore(store, options = {}) {
394
397
  status.finished = false;
395
398
  status.loading = false;
396
399
  status.error = null;
397
- const $state = options.persist ? proxyWithPersistant(options.persist, state) : (0, import_valtio2.proxy)(state);
398
400
  const $status = (0, import_valtio2.proxy)(status);
401
+ const $state = options.persist ? proxyWithPersistant(options.persist, state) : (0, import_valtio2.proxy)(state);
399
402
  const $actions = {};
400
403
  const $getters = {};
401
404
  setupActions($state, actions, $actions, $status);
@@ -494,16 +497,30 @@ function setupStatus($actions, $status) {
494
497
  function defineAsyncStore(fetch, options = {}) {
495
498
  const store = defineStore(
496
499
  {
497
- state: () => ({ value: options.initial }),
500
+ state: () => ({
501
+ value: options.initial,
502
+ error: void 0,
503
+ loading: false,
504
+ finished: false
505
+ }),
498
506
  actions: {
499
- fetch,
507
+ async fetch(...args) {
508
+ return this.value = await fetch(...args);
509
+ },
500
510
  refresh(value) {
501
511
  this.value = value || options.initial;
502
512
  }
503
513
  }
504
514
  },
505
- { persist: options.persist }
515
+ { persist: options.persist ? { id: options.persist, pick: ["value"] } : void 0 }
506
516
  );
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
+ });
523
+ options.immediate && store.fetch();
507
524
  return store;
508
525
  }
509
526
 
package/dist/index.d.ts CHANGED
@@ -201,16 +201,31 @@ type Store<S, A extends Actions<S>, G extends Getters<S>> = {
201
201
  interface AsyncStoreOptions<T extends AnyFn> {
202
202
  initial?: ReturnType<T> extends Promise<infer U> ? U : undefined;
203
203
  persist?: string;
204
+ immediate?: boolean;
204
205
  }
205
206
  declare function defineAsyncStore<T extends AnyFn>(fetch: T, options?: AsyncStoreOptions<T>): Store<{
206
207
  value: (ReturnType<T> extends Promise<infer U> ? U : undefined) | undefined;
208
+ error: Error | null | undefined;
209
+ loading: boolean;
210
+ finished: boolean;
207
211
  }, {
208
- fetch: T;
212
+ fetch(this: {
213
+ value: (ReturnType<T> extends Promise<infer U> ? U : undefined) | undefined;
214
+ error: Error | null | undefined;
215
+ loading: boolean;
216
+ finished: boolean;
217
+ }, ...args: Parameters<T>): Promise<any>;
209
218
  refresh(this: {
210
219
  value: (ReturnType<T> extends Promise<infer U> ? U : undefined) | undefined;
220
+ error: Error | null | undefined;
221
+ loading: boolean;
222
+ finished: boolean;
211
223
  }, value?: (ReturnType<T> extends Promise<infer U_1> ? U_1 : undefined)): void;
212
224
  }, Getters<{
213
225
  value: (ReturnType<T> extends Promise<infer U> ? U : undefined) | undefined;
226
+ error: Error | null | undefined;
227
+ loading: boolean;
228
+ finished: boolean;
214
229
  }>>;
215
230
 
216
231
  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>;
@@ -470,9 +470,6 @@ var LibReact = (() => {
470
470
  useWatch(source, () => source && cb(source), options);
471
471
  }
472
472
 
473
- // src/storage/defineStore.ts
474
- var import_react15 = __toESM(require_react(), 1);
475
-
476
473
  // ../../node_modules/.pnpm/proxy-compare@3.0.1/node_modules/proxy-compare/dist/index.js
477
474
  var TRACK_MEMO_SYMBOL = Symbol();
478
475
  var GET_ORIGINAL_SYMBOL = Symbol();
@@ -908,6 +905,76 @@ var LibReact = (() => {
908
905
  const [target, ensureVersion] = proxyState;
909
906
  return createSnapshot(target, ensureVersion());
910
907
  }
908
+ function unstable_getInternalStates() {
909
+ return {
910
+ proxyStateMap,
911
+ refSet,
912
+ snapCache,
913
+ versionHolder,
914
+ proxyCache
915
+ };
916
+ }
917
+
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
+ 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
+ }
958
+ }
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;
974
+ }
975
+ var DEVTOOLS = Symbol();
976
+ var { proxyStateMap: proxyStateMap$1, snapCache: snapCache$1 } = unstable_getInternalStates();
977
+ var { proxyStateMap: proxyStateMap2, snapCache: snapCache2 } = unstable_getInternalStates();
911
978
 
912
979
  // ../../node_modules/.pnpm/valtio@2.1.4_@types+react@18.3.18_react@18.3.1/node_modules/valtio/esm/react.mjs
913
980
  var import_react14 = __toESM(require_react(), 1);
@@ -966,6 +1033,9 @@ var LibReact = (() => {
966
1033
  return createProxy(currSnapshot, affected, proxyCache2, targetCache);
967
1034
  }
968
1035
 
1036
+ // src/storage/defineStore.ts
1037
+ var import_react15 = __toESM(require_react(), 1);
1038
+
969
1039
  // ../util-core/src/util/json.ts
970
1040
  function jsonTryParse(text) {
971
1041
  try {
@@ -1005,8 +1075,8 @@ var LibReact = (() => {
1005
1075
  status.finished = false;
1006
1076
  status.loading = false;
1007
1077
  status.error = null;
1008
- const $state = options.persist ? proxyWithPersistant(options.persist, state) : proxy(state);
1009
1078
  const $status = proxy(status);
1079
+ const $state = options.persist ? proxyWithPersistant(options.persist, state) : proxy(state);
1010
1080
  const $actions = {};
1011
1081
  const $getters = {};
1012
1082
  setupActions($state, actions, $actions, $status);
@@ -1105,16 +1175,30 @@ var LibReact = (() => {
1105
1175
  function defineAsyncStore(fetch, options = {}) {
1106
1176
  const store = defineStore(
1107
1177
  {
1108
- state: () => ({ value: options.initial }),
1178
+ state: () => ({
1179
+ value: options.initial,
1180
+ error: void 0,
1181
+ loading: false,
1182
+ finished: false
1183
+ }),
1109
1184
  actions: {
1110
- fetch,
1185
+ async fetch(...args) {
1186
+ return this.value = await fetch(...args);
1187
+ },
1111
1188
  refresh(value) {
1112
1189
  this.value = value || options.initial;
1113
1190
  }
1114
1191
  }
1115
1192
  },
1116
- { persist: options.persist }
1193
+ { persist: options.persist ? { id: options.persist, pick: ["value"] } : void 0 }
1117
1194
  );
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
+ });
1201
+ options.immediate && store.fetch();
1118
1202
  return store;
1119
1203
  }
1120
1204
 
package/dist/index.js CHANGED
@@ -290,6 +290,9 @@ function useWhenever(source, cb, options) {
290
290
  useWatch(source, () => source && cb(source), options);
291
291
  }
292
292
 
293
+ // src/storage/defineAsyncStore.ts
294
+ import { watch } from "valtio/utils";
295
+
293
296
  // src/storage/defineStore.ts
294
297
  import { createElement as createElement4 } from "react";
295
298
  import { proxy as proxy2, subscribe as subscribe2, useSnapshot } from "valtio";
@@ -334,8 +337,8 @@ function defineStore(store, options = {}) {
334
337
  status.finished = false;
335
338
  status.loading = false;
336
339
  status.error = null;
337
- const $state = options.persist ? proxyWithPersistant(options.persist, state) : proxy2(state);
338
340
  const $status = proxy2(status);
341
+ const $state = options.persist ? proxyWithPersistant(options.persist, state) : proxy2(state);
339
342
  const $actions = {};
340
343
  const $getters = {};
341
344
  setupActions($state, actions, $actions, $status);
@@ -434,16 +437,30 @@ function setupStatus($actions, $status) {
434
437
  function defineAsyncStore(fetch, options = {}) {
435
438
  const store = defineStore(
436
439
  {
437
- state: () => ({ value: options.initial }),
440
+ state: () => ({
441
+ value: options.initial,
442
+ error: void 0,
443
+ loading: false,
444
+ finished: false
445
+ }),
438
446
  actions: {
439
- fetch,
447
+ async fetch(...args) {
448
+ return this.value = await fetch(...args);
449
+ },
440
450
  refresh(value) {
441
451
  this.value = value || options.initial;
442
452
  }
443
453
  }
444
454
  },
445
- { persist: options.persist }
455
+ { persist: options.persist ? { id: options.persist, pick: ["value"] } : void 0 }
446
456
  );
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
+ });
463
+ options.immediate && store.fetch();
447
464
  return store;
448
465
  }
449
466
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hairy/react-lib",
3
3
  "type": "module",
4
- "version": "1.18.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.18.0"
41
+ "@hairy/utils": "1.20.0"
42
42
  },
43
43
  "scripts": {
44
44
  "build": "tsup",