@hairy/react-lib 1.7.3 → 1.8.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
@@ -389,11 +389,22 @@ function jsonTryParse(text) {
389
389
 
390
390
  // src/storage/proxyWithPersistant.ts
391
391
  var import_valtio = require("valtio");
392
- function proxyWithPersistant(key, initialObject, options = {}) {
392
+ function proxyWithPersistant(keyOrOptions, initialObject) {
393
+ let options;
394
+ if (typeof keyOrOptions === "string") {
395
+ options = { id: keyOrOptions };
396
+ } else {
397
+ options = { ...keyOrOptions };
398
+ }
393
399
  const storage = options.storage || (typeof localStorage !== "undefined" ? localStorage : void 0);
394
- const state = (0, import_valtio.proxy)(jsonTryParse(storage?.getItem(key)) || initialObject);
400
+ typeof keyOrOptions === "string" && (keyOrOptions = { id: keyOrOptions });
401
+ const state = (0, import_valtio.proxy)(jsonTryParse(storage?.getItem(options.id)) || initialObject);
395
402
  (0, import_valtio.subscribe)(state, () => {
396
- storage?.setItem(key, JSON.stringify(state));
403
+ const pick = options.pick || Object.keys(state);
404
+ const statePick = {};
405
+ for (const key of pick)
406
+ statePick[key] = state[key];
407
+ storage?.setItem(options.id, JSON.stringify(statePick));
397
408
  });
398
409
  return state;
399
410
  }
@@ -441,7 +452,7 @@ function defineAsyncStore(options) {
441
452
  error: void 0
442
453
  })
443
454
  },
444
- { persistant: options.persistant }
455
+ { persistant: options.persistant ? { id: options.persistant, pick: ["value"] } : void 0 }
445
456
  );
446
457
  function use() {
447
458
  const fn = options.setup();
@@ -466,7 +477,7 @@ function defineAsyncStore(options) {
466
477
  }
467
478
 
468
479
  // src/storage/defineAsyncStorePlain.ts
469
- function defineAsyncStorePlain(fn, options) {
480
+ function defineAsyncStorePlain(fn, options = {}) {
470
481
  return defineAsyncStore({
471
482
  setup: () => fn,
472
483
  initial: options.initial,
package/dist/index.d.ts CHANGED
@@ -139,7 +139,7 @@ declare function defineAsyncStore<T extends FunctionReturningPromise>(options: A
139
139
 
140
140
  interface AsyncStorePlainOptions<T extends FunctionReturningPromise> extends Omit<AsyncStoreOptions<T>, 'setup'> {
141
141
  }
142
- declare function defineAsyncStorePlain<T extends FunctionReturningPromise>(fn: T, options: AsyncStorePlainOptions<T>): () => readonly [{
142
+ declare function defineAsyncStorePlain<T extends FunctionReturningPromise>(fn: T, options?: AsyncStorePlainOptions<T>): () => readonly [{
143
143
  readonly promise: {
144
144
  readonly then: <TResult1 = any, TResult2 = never>(onfulfilled?: ((value: any) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined) => Promise<TResult1 | TResult2>;
145
145
  readonly catch: <TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined) => Promise<any>;
@@ -151,12 +151,20 @@ declare function defineAsyncStorePlain<T extends FunctionReturningPromise>(fn: T
151
151
  readonly error: Error | undefined;
152
152
  }, T, (value: ReturnType<T> extends Promise<infer U_1> ? U_1 : undefined) => void];
153
153
 
154
+ interface PersistantOptions {
155
+ id: string;
156
+ storage?: Storage;
157
+ pick?: string[];
158
+ }
159
+ declare function proxyWithPersistant<T extends object>(key: string, initialObject?: T, options?: Omit<PersistantOptions, 'key'>): T;
160
+ declare function proxyWithPersistant<T extends object>(options: PersistantOptions, initialObject?: T): T;
161
+
154
162
  interface StoreDefine<S extends object, A extends Actions<S>> {
155
163
  state: (() => S) | S;
156
164
  actions?: A;
157
165
  }
158
166
  interface StoreOptions {
159
- persistant?: string;
167
+ persistant?: string | PersistantOptions;
160
168
  }
161
169
  type Actions<S> = Record<string, (this: S, ...args: any) => any>;
162
170
  type ActionsOmitThisParameter<A extends Actions<any>> = {
@@ -170,14 +178,9 @@ type Store<S, A extends Actions<S>> = {
170
178
  } & ActionsOmitThisParameter<A>;
171
179
  declare function defineStore<S extends object, A extends Actions<S>>(store: StoreDefine<S, A>, options?: StoreOptions): Store<S, A>;
172
180
 
173
- interface PersistantOptions {
174
- storage?: Storage;
175
- }
176
- declare function proxyWithPersistant<T extends object>(key: string, initialObject?: T, options?: PersistantOptions): T;
177
-
178
181
  declare function useStore<S extends object, A extends Actions<S>>(store: Store<S, A>): valtio.Snapshot<S>;
179
182
 
180
- type PropWithHtmlProps<T = HTMLDivElement> = DetailedHTMLProps<HTMLAttributes<T>, T>;
183
+ type PropsWithDetailedHTML<T = HTMLDivElement> = DetailedHTMLProps<HTMLAttributes<T>, T>;
181
184
 
182
185
  type Value = string | boolean | undefined | null;
183
186
  type Mapping = Record<string, any>;
@@ -195,4 +198,4 @@ declare namespace cls {
195
198
  var append: (value: any, newClass: any) => any;
196
199
  }
197
200
 
198
- export { type Actions, type ActionsOmitThisParameter, type Argument, type ArgumentArray, type AsyncStoreOptions, type AsyncStorePlainOptions, Case, type CaseProps, Default, Else, type EventBusListener, type FetchRequestInterceptCallback, type FetchResponseInterceptCallback, If, type IfProps, type InjectComponent, Injector, type InjectorProps, type Mapping, type PersistantOptions, type PropWithHtmlProps, type ReadonlyArgumentArray, type StateFromFunctionReturningPromise, type Store, type StoreDefine, type StoreOptions, Switch, type SwitchProps, Then, Trans, type TransProps, Unless, type UnlessProps, type UseAsyncStateOptions, type Value, type WatchCallback, type WatchOptions, cls, defineAsyncStore, defineAsyncStorePlain, defineStore, proxyWithPersistant, useAsyncCallback, useAsyncState, useDebounce, useEventBus, useFetchRequestIntercept, useFetchResponseIntercept, useMounted, useStore, useWatch, useWhenever };
201
+ export { type Actions, type ActionsOmitThisParameter, type Argument, type ArgumentArray, type AsyncStoreOptions, type AsyncStorePlainOptions, Case, type CaseProps, Default, Else, type EventBusListener, type FetchRequestInterceptCallback, type FetchResponseInterceptCallback, If, type IfProps, type InjectComponent, Injector, type InjectorProps, type Mapping, type PersistantOptions, type PropsWithDetailedHTML, type ReadonlyArgumentArray, type StateFromFunctionReturningPromise, type Store, type StoreDefine, type StoreOptions, Switch, type SwitchProps, Then, Trans, type TransProps, Unless, type UnlessProps, type UseAsyncStateOptions, type Value, type WatchCallback, type WatchOptions, cls, defineAsyncStore, defineAsyncStorePlain, defineStore, proxyWithPersistant, useAsyncCallback, useAsyncState, useDebounce, useEventBus, useFetchRequestIntercept, useFetchResponseIntercept, useMounted, useStore, useWatch, useWhenever };
@@ -998,11 +998,22 @@ var LibReact = (() => {
998
998
  }
999
999
 
1000
1000
  // src/storage/proxyWithPersistant.ts
1001
- function proxyWithPersistant(key, initialObject, options = {}) {
1001
+ function proxyWithPersistant(keyOrOptions, initialObject) {
1002
+ let options;
1003
+ if (typeof keyOrOptions === "string") {
1004
+ options = { id: keyOrOptions };
1005
+ } else {
1006
+ options = { ...keyOrOptions };
1007
+ }
1002
1008
  const storage = options.storage || (typeof localStorage !== "undefined" ? localStorage : void 0);
1003
- const state = proxy(jsonTryParse(storage?.getItem(key)) || initialObject);
1009
+ typeof keyOrOptions === "string" && (keyOrOptions = { id: keyOrOptions });
1010
+ const state = proxy(jsonTryParse(storage?.getItem(options.id)) || initialObject);
1004
1011
  subscribe(state, () => {
1005
- storage?.setItem(key, JSON.stringify(state));
1012
+ const pick = options.pick || Object.keys(state);
1013
+ const statePick = {};
1014
+ for (const key of pick)
1015
+ statePick[key] = state[key];
1016
+ storage?.setItem(options.id, JSON.stringify(statePick));
1006
1017
  });
1007
1018
  return state;
1008
1019
  }
@@ -1049,7 +1060,7 @@ var LibReact = (() => {
1049
1060
  error: void 0
1050
1061
  })
1051
1062
  },
1052
- { persistant: options.persistant }
1063
+ { persistant: options.persistant ? { id: options.persistant, pick: ["value"] } : void 0 }
1053
1064
  );
1054
1065
  function use() {
1055
1066
  const fn = options.setup();
@@ -1074,7 +1085,7 @@ var LibReact = (() => {
1074
1085
  }
1075
1086
 
1076
1087
  // src/storage/defineAsyncStorePlain.ts
1077
- function defineAsyncStorePlain(fn, options) {
1088
+ function defineAsyncStorePlain(fn, options = {}) {
1078
1089
  return defineAsyncStore({
1079
1090
  setup: () => fn,
1080
1091
  initial: options.initial,
package/dist/index.js CHANGED
@@ -332,11 +332,22 @@ function jsonTryParse(text) {
332
332
 
333
333
  // src/storage/proxyWithPersistant.ts
334
334
  import { proxy, subscribe } from "valtio";
335
- function proxyWithPersistant(key, initialObject, options = {}) {
335
+ function proxyWithPersistant(keyOrOptions, initialObject) {
336
+ let options;
337
+ if (typeof keyOrOptions === "string") {
338
+ options = { id: keyOrOptions };
339
+ } else {
340
+ options = { ...keyOrOptions };
341
+ }
336
342
  const storage = options.storage || (typeof localStorage !== "undefined" ? localStorage : void 0);
337
- const state = proxy(jsonTryParse(storage?.getItem(key)) || initialObject);
343
+ typeof keyOrOptions === "string" && (keyOrOptions = { id: keyOrOptions });
344
+ const state = proxy(jsonTryParse(storage?.getItem(options.id)) || initialObject);
338
345
  subscribe(state, () => {
339
- storage?.setItem(key, JSON.stringify(state));
346
+ const pick = options.pick || Object.keys(state);
347
+ const statePick = {};
348
+ for (const key of pick)
349
+ statePick[key] = state[key];
350
+ storage?.setItem(options.id, JSON.stringify(statePick));
340
351
  });
341
352
  return state;
342
353
  }
@@ -384,7 +395,7 @@ function defineAsyncStore(options) {
384
395
  error: void 0
385
396
  })
386
397
  },
387
- { persistant: options.persistant }
398
+ { persistant: options.persistant ? { id: options.persistant, pick: ["value"] } : void 0 }
388
399
  );
389
400
  function use() {
390
401
  const fn = options.setup();
@@ -409,7 +420,7 @@ function defineAsyncStore(options) {
409
420
  }
410
421
 
411
422
  // src/storage/defineAsyncStorePlain.ts
412
- function defineAsyncStorePlain(fn, options) {
423
+ function defineAsyncStorePlain(fn, options = {}) {
413
424
  return defineAsyncStore({
414
425
  setup: () => fn,
415
426
  initial: options.initial,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hairy/react-lib",
3
3
  "type": "module",
4
- "version": "1.7.3",
4
+ "version": "1.8.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.7.3"
41
+ "@hairy/utils": "1.8.0"
42
42
  },
43
43
  "scripts": {
44
44
  "build": "tsup",