@choksheak/react-utils 0.1.8 → 0.2.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.
Files changed (33) hide show
  1. package/docs/assets/search.js +1 -1
  2. package/docs/classes/sharedQuery.SharedQuery.html +5 -5
  3. package/docs/classes/sharedState.SharedState.html +3 -2
  4. package/docs/classes/sharedState.SharedStateConfig.html +3 -3
  5. package/docs/classes/utils_storage.InMemoryStorageAdapter.html +2 -2
  6. package/docs/classes/utils_storage.IndexedDbAdapter.html +2 -2
  7. package/docs/classes/utils_storage.LocalStorageAdapter.html +2 -2
  8. package/docs/functions/sharedQuery.sharedQuery.html +1 -1
  9. package/docs/functions/sharedQuery.useQuery.html +1 -1
  10. package/docs/functions/sharedState.sharedState.html +1 -1
  11. package/docs/functions/sharedState.useSharedState.html +1 -1
  12. package/docs/functions/sharedState.useSharedStateSetter.html +1 -1
  13. package/docs/functions/sharedState.useSharedStateValue.html +1 -1
  14. package/docs/functions/utils_storage.getStorageAdapter.html +1 -1
  15. package/docs/functions/utils_stringify.stringifyDeterministicForKeys.html +1 -1
  16. package/docs/types/sharedQuery.FetchFn.html +1 -1
  17. package/docs/types/sharedQuery.FetchedData.html +2 -2
  18. package/docs/types/sharedQuery.QueryStateValue.html +2 -2
  19. package/docs/types/sharedQuery.SharedQueryOptions.html +1 -1
  20. package/docs/types/sharedQuery.SharedQueryState.html +1 -1
  21. package/docs/types/sharedState.SharedStateOptions.html +1 -1
  22. package/docs/types/utils_storage.StorageAdapter.html +2 -2
  23. package/docs/types/utils_storage.StorageOptions.html +2 -2
  24. package/docs/variables/fetcher.fetcher.html +1 -1
  25. package/docs/variables/fetcher.fetcherWithRetries.html +1 -1
  26. package/package.json +8 -1
  27. package/sharedQuery.js +3 -1
  28. package/sharedQuery.mjs +3 -1
  29. package/sharedState.d.mts +1 -0
  30. package/sharedState.d.ts +1 -0
  31. package/sharedState.js +3 -1
  32. package/sharedState.mjs +3 -1
  33. package/src/sharedState.ts +5 -1
package/sharedQuery.js CHANGED
@@ -200,6 +200,7 @@ var SharedState = class {
200
200
  this.defaultValue = defaultValue;
201
201
  this.pubSubKey = String(stateKey++);
202
202
  this.storageAdapter = getStorageAdapter(options, import_kvStore2.DEFAULT_EXPIRY_DELTA_MS);
203
+ this.setValueBounded = (arg) => this.setValue(arg);
203
204
  pubSubStore.setNoNotify(this.pubSubKey, defaultValue);
204
205
  const lazyLoad = options?.lazyLoad;
205
206
  const lazy = lazyLoad !== void 0 ? lazyLoad : SharedStateConfig.lazyLoad;
@@ -209,6 +210,7 @@ var SharedState = class {
209
210
  }
210
211
  pubSubKey;
211
212
  storageAdapter;
213
+ setValueBounded;
212
214
  initDone = false;
213
215
  // `subscribe` function required by useSyncExternalStore
214
216
  subscribe(subscriber) {
@@ -253,7 +255,7 @@ function useSharedState(state) {
253
255
  // getServerSnapshot is needed to prevent SSR dev errors in nextjs.
254
256
  () => state.getSnapshot()
255
257
  );
256
- return [value, state.setValue];
258
+ return [value, state.setValueBounded];
257
259
  }
258
260
 
259
261
  // src/utils/stringify.ts
package/sharedQuery.mjs CHANGED
@@ -174,6 +174,7 @@ var SharedState = class {
174
174
  this.defaultValue = defaultValue;
175
175
  this.pubSubKey = String(stateKey++);
176
176
  this.storageAdapter = getStorageAdapter(options, DEFAULT_EXPIRY_DELTA_MS);
177
+ this.setValueBounded = (arg) => this.setValue(arg);
177
178
  pubSubStore.setNoNotify(this.pubSubKey, defaultValue);
178
179
  const lazyLoad = options?.lazyLoad;
179
180
  const lazy = lazyLoad !== void 0 ? lazyLoad : SharedStateConfig.lazyLoad;
@@ -183,6 +184,7 @@ var SharedState = class {
183
184
  }
184
185
  pubSubKey;
185
186
  storageAdapter;
187
+ setValueBounded;
186
188
  initDone = false;
187
189
  // `subscribe` function required by useSyncExternalStore
188
190
  subscribe(subscriber) {
@@ -227,7 +229,7 @@ function useSharedState(state) {
227
229
  // getServerSnapshot is needed to prevent SSR dev errors in nextjs.
228
230
  () => state.getSnapshot()
229
231
  );
230
- return [value, state.setValue];
232
+ return [value, state.setValueBounded];
231
233
  }
232
234
 
233
235
  // src/utils/stringify.ts
package/sharedState.d.mts CHANGED
@@ -34,6 +34,7 @@ declare class SharedState<T> {
34
34
  private readonly defaultValue;
35
35
  private readonly pubSubKey;
36
36
  private readonly storageAdapter;
37
+ readonly setValueBounded: (next: T | ((prev: T) => T)) => void;
37
38
  initDone: boolean;
38
39
  constructor(defaultValue: T, options?: SharedStateOptions<T>);
39
40
  subscribe(subscriber: () => void): () => void;
package/sharedState.d.ts CHANGED
@@ -33,6 +33,7 @@ export declare class SharedState<T> {
33
33
  private readonly defaultValue;
34
34
  private readonly pubSubKey;
35
35
  private readonly storageAdapter;
36
+ readonly setValueBounded: (next: T | ((prev: T) => T)) => void;
36
37
  initDone: boolean;
37
38
  constructor(defaultValue: T, options?: SharedStateOptions<T>);
38
39
  subscribe(subscriber: () => void): () => void;
package/sharedState.js CHANGED
@@ -199,6 +199,7 @@ var SharedState = class {
199
199
  this.defaultValue = defaultValue;
200
200
  this.pubSubKey = String(stateKey++);
201
201
  this.storageAdapter = getStorageAdapter(options, import_kvStore2.DEFAULT_EXPIRY_DELTA_MS);
202
+ this.setValueBounded = (arg) => this.setValue(arg);
202
203
  pubSubStore.setNoNotify(this.pubSubKey, defaultValue);
203
204
  const lazyLoad = options?.lazyLoad;
204
205
  const lazy = lazyLoad !== void 0 ? lazyLoad : SharedStateConfig.lazyLoad;
@@ -208,6 +209,7 @@ var SharedState = class {
208
209
  }
209
210
  pubSubKey;
210
211
  storageAdapter;
212
+ setValueBounded;
211
213
  initDone = false;
212
214
  // `subscribe` function required by useSyncExternalStore
213
215
  subscribe(subscriber) {
@@ -252,7 +254,7 @@ function useSharedState(state) {
252
254
  // getServerSnapshot is needed to prevent SSR dev errors in nextjs.
253
255
  () => state.getSnapshot()
254
256
  );
255
- return [value, state.setValue];
257
+ return [value, state.setValueBounded];
256
258
  }
257
259
  function useSharedStateValue(state) {
258
260
  return useSharedState(state)[0];
package/sharedState.mjs CHANGED
@@ -170,6 +170,7 @@ var SharedState = class {
170
170
  this.defaultValue = defaultValue;
171
171
  this.pubSubKey = String(stateKey++);
172
172
  this.storageAdapter = getStorageAdapter(options, DEFAULT_EXPIRY_DELTA_MS);
173
+ this.setValueBounded = (arg) => this.setValue(arg);
173
174
  pubSubStore.setNoNotify(this.pubSubKey, defaultValue);
174
175
  const lazyLoad = options?.lazyLoad;
175
176
  const lazy = lazyLoad !== void 0 ? lazyLoad : SharedStateConfig.lazyLoad;
@@ -179,6 +180,7 @@ var SharedState = class {
179
180
  }
180
181
  pubSubKey;
181
182
  storageAdapter;
183
+ setValueBounded;
182
184
  initDone = false;
183
185
  // `subscribe` function required by useSyncExternalStore
184
186
  subscribe(subscriber) {
@@ -223,7 +225,7 @@ function useSharedState(state) {
223
225
  // getServerSnapshot is needed to prevent SSR dev errors in nextjs.
224
226
  () => state.getSnapshot()
225
227
  );
226
- return [value, state.setValue];
228
+ return [value, state.setValueBounded];
227
229
  }
228
230
  function useSharedStateValue(state) {
229
231
  return useSharedState(state)[0];
@@ -121,6 +121,7 @@ let stateKey = 0;
121
121
  export class SharedState<T> {
122
122
  private readonly pubSubKey: string;
123
123
  private readonly storageAdapter: StorageAdapter<T> | null;
124
+ public readonly setValueBounded: (next: T | ((prev: T) => T)) => void;
124
125
  public initDone = false;
125
126
 
126
127
  public constructor(
@@ -132,6 +133,9 @@ export class SharedState<T> {
132
133
  // Use max of 1 storage adapter per shared state.
133
134
  this.storageAdapter = getStorageAdapter(options, DEFAULT_EXPIRY_DELTA_MS);
134
135
 
136
+ // Same as this.setValue, but binding the `this` reference.
137
+ this.setValueBounded = (arg) => this.setValue(arg);
138
+
135
139
  // Always set the default value first to avoid returning undefineds if that
136
140
  // is not part of T.
137
141
  pubSubStore.setNoNotify(this.pubSubKey, defaultValue);
@@ -211,7 +215,7 @@ export function useSharedState<T>(
211
215
  () => state.getSnapshot(),
212
216
  );
213
217
 
214
- return [value, state.setValue];
218
+ return [value, state.setValueBounded];
215
219
  }
216
220
 
217
221
  /** E.g. const my = useSharedStateValue(myState) */