@hairy/react-lib 1.18.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);
@@ -494,9 +497,16 @@ 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
  }
@@ -504,6 +514,10 @@ function defineAsyncStore(fetch, options = {}) {
504
514
  },
505
515
  { persist: options.persist }
506
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();
507
521
  return store;
508
522
  }
509
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);
@@ -1105,9 +1132,16 @@ var LibReact = (() => {
1105
1132
  function defineAsyncStore(fetch, options = {}) {
1106
1133
  const store = defineStore(
1107
1134
  {
1108
- state: () => ({ value: options.initial }),
1135
+ state: () => ({
1136
+ value: options.initial,
1137
+ error: void 0,
1138
+ loading: false,
1139
+ finished: false
1140
+ }),
1109
1141
  actions: {
1110
- fetch,
1142
+ async fetch(...args) {
1143
+ return this.value = await fetch(...args);
1144
+ },
1111
1145
  refresh(value) {
1112
1146
  this.value = value || options.initial;
1113
1147
  }
@@ -1115,6 +1149,10 @@ var LibReact = (() => {
1115
1149
  },
1116
1150
  { persist: options.persist }
1117
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();
1118
1156
  return store;
1119
1157
  }
1120
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);
@@ -434,9 +437,16 @@ 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
  }
@@ -444,6 +454,10 @@ function defineAsyncStore(fetch, options = {}) {
444
454
  },
445
455
  { persist: options.persist }
446
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();
447
461
  return store;
448
462
  }
449
463
 
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.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.18.0"
41
+ "@hairy/utils": "1.19.0"
42
42
  },
43
43
  "scripts": {
44
44
  "build": "tsup",