@hairy/react-lib 1.17.0 → 1.19.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);
@@ -446,10 +449,12 @@ function track(action, status) {
446
449
  tracking();
447
450
  try {
448
451
  const result = action(...args);
449
- if (result instanceof Promise)
450
- result.then(fulfilled).catch(rejected);
451
- else
452
+ if (result instanceof Promise) {
453
+ return result.then(fulfilled).catch(rejected);
454
+ } else {
452
455
  fulfilled();
456
+ return result;
457
+ }
453
458
  } catch (error) {
454
459
  rejected(error);
455
460
  }
@@ -492,9 +497,16 @@ function setupStatus($actions, $status) {
492
497
  function defineAsyncStore(fetch, options = {}) {
493
498
  const store = defineStore(
494
499
  {
495
- state: () => ({ value: options.initial }),
500
+ state: () => ({
501
+ value: options.initial,
502
+ error: void 0,
503
+ loading: false,
504
+ finished: false
505
+ }),
496
506
  actions: {
497
- fetch,
507
+ async fetch(...args) {
508
+ return this.value = await fetch(...args);
509
+ },
498
510
  refresh(value) {
499
511
  this.value = value || options.initial;
500
512
  }
@@ -502,6 +514,10 @@ function defineAsyncStore(fetch, options = {}) {
502
514
  },
503
515
  { persist: options.persist }
504
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);
520
+ options.immediate && store.fetch();
505
521
  return store;
506
522
  }
507
523
 
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,33 @@ 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
+ 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);
927
+ }
928
+ },
929
+ notifyInSync
930
+ );
931
+ }
932
+ var DEVTOOLS = Symbol();
933
+ var { proxyStateMap: proxyStateMap$1, snapCache: snapCache$1 } = unstable_getInternalStates();
934
+ var { proxyStateMap: proxyStateMap2, snapCache: snapCache2 } = unstable_getInternalStates();
911
935
 
912
936
  // ../../node_modules/.pnpm/valtio@2.1.4_@types+react@18.3.18_react@18.3.1/node_modules/valtio/esm/react.mjs
913
937
  var import_react14 = __toESM(require_react(), 1);
@@ -966,6 +990,9 @@ var LibReact = (() => {
966
990
  return createProxy(currSnapshot, affected, proxyCache2, targetCache);
967
991
  }
968
992
 
993
+ // src/storage/defineStore.ts
994
+ var import_react15 = __toESM(require_react(), 1);
995
+
969
996
  // ../util-core/src/util/json.ts
970
997
  function jsonTryParse(text) {
971
998
  try {
@@ -1005,8 +1032,8 @@ var LibReact = (() => {
1005
1032
  status.finished = false;
1006
1033
  status.loading = false;
1007
1034
  status.error = null;
1008
- const $state = options.persist ? proxyWithPersistant(options.persist, state) : proxy(state);
1009
1035
  const $status = proxy(status);
1036
+ const $state = options.persist ? proxyWithPersistant(options.persist, state) : proxy(state);
1010
1037
  const $actions = {};
1011
1038
  const $getters = {};
1012
1039
  setupActions($state, actions, $actions, $status);
@@ -1057,10 +1084,12 @@ var LibReact = (() => {
1057
1084
  tracking();
1058
1085
  try {
1059
1086
  const result = action(...args);
1060
- if (result instanceof Promise)
1061
- result.then(fulfilled).catch(rejected);
1062
- else
1087
+ if (result instanceof Promise) {
1088
+ return result.then(fulfilled).catch(rejected);
1089
+ } else {
1063
1090
  fulfilled();
1091
+ return result;
1092
+ }
1064
1093
  } catch (error) {
1065
1094
  rejected(error);
1066
1095
  }
@@ -1103,9 +1132,16 @@ var LibReact = (() => {
1103
1132
  function defineAsyncStore(fetch, options = {}) {
1104
1133
  const store = defineStore(
1105
1134
  {
1106
- state: () => ({ value: options.initial }),
1135
+ state: () => ({
1136
+ value: options.initial,
1137
+ error: void 0,
1138
+ loading: false,
1139
+ finished: false
1140
+ }),
1107
1141
  actions: {
1108
- fetch,
1142
+ async fetch(...args) {
1143
+ return this.value = await fetch(...args);
1144
+ },
1109
1145
  refresh(value) {
1110
1146
  this.value = value || options.initial;
1111
1147
  }
@@ -1113,6 +1149,10 @@ var LibReact = (() => {
1113
1149
  },
1114
1150
  { persist: options.persist }
1115
1151
  );
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);
1155
+ options.immediate && store.fetch();
1116
1156
  return store;
1117
1157
  }
1118
1158
 
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 { subscribeKey } 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);
@@ -386,10 +389,12 @@ function track(action, status) {
386
389
  tracking();
387
390
  try {
388
391
  const result = action(...args);
389
- if (result instanceof Promise)
390
- result.then(fulfilled).catch(rejected);
391
- else
392
+ if (result instanceof Promise) {
393
+ return result.then(fulfilled).catch(rejected);
394
+ } else {
392
395
  fulfilled();
396
+ return result;
397
+ }
393
398
  } catch (error) {
394
399
  rejected(error);
395
400
  }
@@ -432,9 +437,16 @@ function setupStatus($actions, $status) {
432
437
  function defineAsyncStore(fetch, options = {}) {
433
438
  const store = defineStore(
434
439
  {
435
- state: () => ({ value: options.initial }),
440
+ state: () => ({
441
+ value: options.initial,
442
+ error: void 0,
443
+ loading: false,
444
+ finished: false
445
+ }),
436
446
  actions: {
437
- fetch,
447
+ async fetch(...args) {
448
+ return this.value = await fetch(...args);
449
+ },
438
450
  refresh(value) {
439
451
  this.value = value || options.initial;
440
452
  }
@@ -442,6 +454,10 @@ function defineAsyncStore(fetch, options = {}) {
442
454
  },
443
455
  { persist: options.persist }
444
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);
460
+ options.immediate && store.fetch();
445
461
  return store;
446
462
  }
447
463
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hairy/react-lib",
3
3
  "type": "module",
4
- "version": "1.17.0",
4
+ "version": "1.19.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.17.0"
41
+ "@hairy/utils": "1.19.0"
42
42
  },
43
43
  "scripts": {
44
44
  "build": "tsup",