reactjrx 1.45.0 → 1.47.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 (49) hide show
  1. package/README.md +1 -1
  2. package/dist/index.cjs +27 -173
  3. package/dist/index.d.ts +3 -8
  4. package/dist/index.js +29 -175
  5. package/dist/lib/binding/useObserve.test.d.ts +1 -0
  6. package/dist/lib/queries/client/cache/cache.garbage.test.d.ts +1 -0
  7. package/dist/lib/queries/client/cache/cacheClient.test.d.ts +1 -0
  8. package/dist/lib/queries/client/cache/logger.d.ts +2 -2
  9. package/dist/lib/queries/client/createClient.d.ts +4 -0
  10. package/dist/lib/queries/client/deduplication/deduplicate.test.d.ts +1 -0
  11. package/dist/lib/queries/client/invalidation/invalidation.test.d.ts +1 -0
  12. package/dist/lib/queries/client/invalidation/logger.d.ts +2 -2
  13. package/dist/lib/queries/client/keys/compareKeys.test.d.ts +1 -0
  14. package/dist/lib/queries/client/keys/keys.test.d.ts +1 -0
  15. package/dist/lib/queries/client/keys/serializeKey.test.d.ts +1 -0
  16. package/dist/lib/queries/client/keys/withKeyComparison.d.ts +16 -0
  17. package/dist/lib/queries/client/refetch/logger.d.ts +2 -2
  18. package/dist/lib/queries/client/refetch/refetch.test.d.ts +1 -0
  19. package/dist/lib/queries/react/Provider.d.ts +2 -2
  20. package/dist/lib/queries/react/useAsyncQuery.test.d.ts +1 -0
  21. package/dist/lib/queries/react/useQuery.deduplication.test.d.ts +1 -0
  22. package/dist/lib/queries/react/useQuery.invalidation.test.d.ts +1 -0
  23. package/dist/lib/queries/react/useQuery.keys.test.d.ts +1 -0
  24. package/dist/lib/queries/react/useQuery.test.d.ts +1 -0
  25. package/dist/lib/state/persistance/{createSharedStoreAdapter.d.ts → adapters/createSharedStoreAdapter.d.ts} +1 -1
  26. package/dist/lib/state/persistance/constants.d.ts +1 -0
  27. package/dist/lib/state/persistance/types.d.ts +4 -3
  28. package/dist/lib/state/persistance/usePersistSignals.d.ts +12 -0
  29. package/dist/lib/state/signal.d.ts +15 -11
  30. package/dist/lib/state/signal.test.d.ts +1 -0
  31. package/dist/lib/state/useSignalValue.d.ts +1 -1
  32. package/dist/lib/utils/emitToSubject.d.ts +2 -0
  33. package/dist/lib/utils/shallowEqual.test.d.ts +1 -0
  34. package/dist/lib/utils/shareLatest.d.ts +10 -0
  35. package/dist/lib/utils/trackSubscriptions.d.ts +2 -0
  36. package/dist/lib/utils/useLiveRef.d.ts +1 -0
  37. package/dist/lib/utils/useLiveRef.test.d.ts +1 -0
  38. package/dist/lib/utils/useUnmountObservable.test.d.ts +1 -0
  39. package/dist/main.d.ts +1 -0
  40. package/dist/tests/testUtils.d.ts +1 -0
  41. package/dist/tests/utils.d.ts +1 -0
  42. package/package.json +10 -10
  43. package/dist/lib/state/persistance/PersistSignals.d.ts +0 -12
  44. package/dist/lib/state/persistance/withPersistance.d.ts +0 -18
  45. package/dist/lib/state/useScopeSignals.d.ts +0 -5
  46. package/dist/lib/state/useSetSignal.d.ts +0 -2
  47. package/dist/lib/state/useSignal.d.ts +0 -2
  48. /package/dist/lib/state/persistance/{createLocalStorageAdapter.d.ts → adapters/createLocalStorageAdapter.d.ts} +0 -0
  49. /package/dist/lib/state/persistance/{createLocalforageAdapter.d.ts → adapters/createLocalforageAdapter.d.ts} +0 -0
package/README.md CHANGED
@@ -2,4 +2,4 @@
2
2
 
3
3
  `reactjrx` is a javascript library which provides a simple and efficient API for handling global state, flow control, and queries in React applications using RxJS. With a small footprint and scalability to suit any project size, it is a great alternative to other popular libraries such as Recoil, Redux, React Query, Zustand, etc.
4
4
 
5
- Please visit the [documentation](https://bret-maxime.gitbook.io/reactjrx/) for more information on how to use.
5
+ Please visit the [documentation](https://bret-maxime.gitbook.io/reactjrx/) for more information on how to use.
package/dist/index.cjs CHANGED
@@ -1,9 +1,15 @@
1
1
  "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
4
+ var __publicField = (obj, key, value) => {
5
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
6
+ return value;
7
+ };
2
8
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
9
  const react = require("react");
4
10
  const rxjs = require("rxjs");
5
- const jsxRuntime = require("react/jsx-runtime");
6
11
  const operators = require("rxjs/operators");
12
+ const jsxRuntime = require("react/jsx-runtime");
7
13
  const useLiveRef = (value) => {
8
14
  const ref = react.useRef(value);
9
15
  react.useMemo(() => {
@@ -163,12 +169,10 @@ const useBehaviorSubject = (state) => {
163
169
  return subject;
164
170
  };
165
171
  const SIGNAL_RESET = Symbol("SIGNAL_RESET");
166
- function signal(options) {
167
- const { default: defaultValue2 } = options ?? {};
172
+ function signal(config) {
173
+ const { default: defaultValue2 } = config ?? {};
168
174
  const subject = new rxjs.BehaviorSubject(defaultValue2);
169
175
  const setValue = (arg) => {
170
- if (arg === subject.getValue())
171
- return;
172
176
  if (typeof arg === "function") {
173
177
  const change = arg(subject.getValue());
174
178
  if (change === subject.getValue())
@@ -184,166 +188,15 @@ function signal(options) {
184
188
  };
185
189
  const getValue = () => subject.getValue();
186
190
  return {
187
- setState: setValue,
191
+ setValue,
188
192
  getValue,
189
- options,
190
- /**
191
- * @important
192
- * We return the original behavior subject for two reasons:
193
- * - useObserve may return the default value directly instead of undefined
194
- * - the scope exist for react binding, this observable is a direct access outside of it
195
- */
193
+ config,
196
194
  subject
197
195
  };
198
196
  }
199
- const useSetSignal = (signal2) => {
200
- return signal2.setState;
201
- };
202
197
  const useSignalValue = (signal2, key) => {
203
198
  return useObserve(signal2.subject, { defaultValue: signal2.getValue(), key });
204
199
  };
205
- const useSignal = (signal2) => {
206
- return [useSignalValue(signal2), useSetSignal(signal2)];
207
- };
208
- const useScopeSignals = (signals) => {
209
- const signalsRef = useLiveRef(signals);
210
- react.useEffect(
211
- () => () => {
212
- signalsRef.current.forEach(({ setState }) => {
213
- setState(SIGNAL_RESET);
214
- });
215
- },
216
- []
217
- );
218
- };
219
- const createLocalStorageAdapter = (forage) => ({
220
- getItem: async (key) => {
221
- const serializedValue = forage.getItem(key);
222
- if (!serializedValue)
223
- return void 0;
224
- return JSON.parse(serializedValue);
225
- },
226
- setItem: async (key, value) => {
227
- forage.setItem(key, JSON.stringify(value));
228
- }
229
- });
230
- const PersistSignalsContext = react.createContext({
231
- resetSignals: () => {
232
- }
233
- });
234
- const usePersistSignalsContext = () => react.useContext(PersistSignalsContext);
235
- const PersistSignals = react.memo(
236
- ({
237
- children,
238
- signals = [],
239
- onReady,
240
- adapter = createLocalStorageAdapter(localStorage)
241
- }) => {
242
- const persistanceRef = useLiveRef(signals);
243
- const onReadyRef = useLiveRef(onReady);
244
- const adapterRef = useLiveRef(adapter);
245
- const resetSignalSubject = useSubject();
246
- const resetSignals = react.useCallback(() => {
247
- persistanceRef.current.forEach(({ setValue }) => {
248
- setValue(SIGNAL_RESET);
249
- });
250
- resetSignalSubject.current.next();
251
- }, []);
252
- const value = react.useMemo(() => ({ resetSignals }), [resetSignals]);
253
- const isHydrated = useObserve(() => {
254
- const items = persistanceRef.current;
255
- const stream = items.length === 0 ? rxjs.of(true) : rxjs.zip(
256
- ...items.map(
257
- ({ hydrateValue }) => rxjs.from(hydrateValue({ adapter: adapterRef.current }))
258
- )
259
- ).pipe(rxjs.map(() => true));
260
- return stream.pipe(
261
- rxjs.tap(() => {
262
- if (onReadyRef.current != null)
263
- onReadyRef.current();
264
- }),
265
- rxjs.catchError((error) => {
266
- console.error("Unable to hydrate", error);
267
- return rxjs.EMPTY;
268
- })
269
- );
270
- }, []) ?? false;
271
- useSubscribe(
272
- () => !isHydrated ? rxjs.EMPTY : rxjs.merge(
273
- ...persistanceRef.current.map(
274
- ({ persistValue, $ }) => (
275
- // @todo test the reset
276
- rxjs.merge(
277
- resetSignalSubject.current,
278
- $.pipe(
279
- rxjs.throttleTime(500, void 0, {
280
- trailing: true,
281
- leading: false
282
- })
283
- )
284
- ).pipe(rxjs.switchMap(() => rxjs.from(persistValue({ adapter }))))
285
- )
286
- )
287
- ),
288
- [isHydrated, adapter]
289
- );
290
- return isHydrated ? /* @__PURE__ */ jsxRuntime.jsx(PersistSignalsContext.Provider, { value, children }) : null;
291
- }
292
- );
293
- const getNormalizedPersistanceValue = (unknownValue) => {
294
- if (unknownValue === null)
295
- return void 0;
296
- if (typeof unknownValue === "object" && "__key" in unknownValue && unknownValue.__key === "reactjrx_persistance") {
297
- return unknownValue;
298
- }
299
- return void 0;
300
- };
301
- function withPersistance(_signal, { version = 0 } = {}) {
302
- if (!_signal.options.key) {
303
- console.error(
304
- "You need to specify a key to use persistance with this signal"
305
- );
306
- }
307
- const hydrateValue = async ({
308
- adapter,
309
- key = _signal.options.key
310
- }) => {
311
- if (!key)
312
- return;
313
- const value = await adapter.getItem(key);
314
- const normalizedValue = getNormalizedPersistanceValue(value);
315
- if (normalizedValue == null)
316
- return;
317
- if (normalizedValue.migrationVersion && version > normalizedValue.migrationVersion) {
318
- return;
319
- }
320
- _signal.setState(value.value);
321
- };
322
- const persistValue = async ({
323
- adapter,
324
- key = _signal.options.key
325
- }) => {
326
- if (!key)
327
- return;
328
- const state = _signal.getValue();
329
- const value = {
330
- value: state,
331
- __key: "reactjrx_persistance",
332
- migrationVersion: version
333
- };
334
- await adapter.setItem(key, value);
335
- };
336
- return [
337
- {
338
- hydrateValue,
339
- persistValue,
340
- setValue: _signal.setState,
341
- $: _signal.subject,
342
- options: _signal.options
343
- },
344
- _signal
345
- ];
346
- }
347
200
  const normalizeStore = (store) => {
348
201
  if (!store || typeof store !== "object") {
349
202
  return void 0;
@@ -428,13 +281,13 @@ function retryBackoff(config) {
428
281
  });
429
282
  }
430
283
  function shallowEqual(objA, objB) {
431
- if (objA === null || objA === void 0 || objB === null || objB === void 0) {
284
+ if (objA === null || objA === void 0 || objB === void 0) {
432
285
  return objA === objB;
433
286
  }
434
287
  if (typeof objA !== "object" || typeof objB !== "object") {
435
288
  return objA === objB;
436
289
  }
437
- if (objA.constructor !== objB.constructor) {
290
+ if (objA.constructor !== (objB == null ? void 0 : objB.constructor)) {
438
291
  return false;
439
292
  }
440
293
  const keysA = Object.keys(objA);
@@ -611,11 +464,8 @@ const ClientEffect = ({
611
464
  return null;
612
465
  };
613
466
  const Provider = react.memo(
614
- ({
615
- children,
616
- client
617
- }) => {
618
- const value = react.useMemo(() => ({ client }), [client]);
467
+ ({ children, client }) => {
468
+ const value = react.useMemo(() => ({ client: client.client }), [client]);
619
469
  return /* @__PURE__ */ jsxRuntime.jsxs(Context.Provider, { value, children: [
620
470
  /* @__PURE__ */ jsxRuntime.jsx(ClientEffect, { client: value.client }),
621
471
  children
@@ -818,7 +668,10 @@ const resetStyle = { backgroundColor: "transparent", color: "inherit" };
818
668
  function createLogger(env) {
819
669
  const _logger = {
820
670
  namespaces: [
821
- { name: "@reactjrx", style: { backgroundColor: "#d02f4e", color: "white" } }
671
+ {
672
+ name: "@reactjrx",
673
+ style: { backgroundColor: "#d02f4e", color: "white" }
674
+ }
822
675
  ],
823
676
  namespace(name, style) {
824
677
  const logger2 = createLogger(env);
@@ -1700,8 +1553,14 @@ const createClient = () => {
1700
1553
  ...refetchClient
1701
1554
  };
1702
1555
  };
1703
- exports.PersistSignals = PersistSignals;
1704
- exports.ReactjrxQueryProvider = Provider;
1556
+ class QueryClient {
1557
+ constructor() {
1558
+ __publicField(this, "client");
1559
+ this.client = createClient();
1560
+ }
1561
+ }
1562
+ exports.QueryClient = QueryClient;
1563
+ exports.QueryClientProvider = Provider;
1705
1564
  exports.SIGNAL_RESET = SIGNAL_RESET;
1706
1565
  exports.createClient = createClient;
1707
1566
  exports.createLocalforageAdapter = createLocalforageAdapter;
@@ -1716,15 +1575,10 @@ exports.useBehaviorSubject = useBehaviorSubject;
1716
1575
  exports.useLiveRef = useLiveRef;
1717
1576
  exports.useObserve = useObserve;
1718
1577
  exports.useObserveCallback = useObserveCallback;
1719
- exports.usePersistSignalsContext = usePersistSignalsContext;
1720
1578
  exports.useQuery = useQuery;
1721
1579
  exports.useQueryClient = useQueryClient;
1722
- exports.useScopeSignals = useScopeSignals;
1723
- exports.useSetSignal = useSetSignal;
1724
- exports.useSignal = useSignal;
1725
1580
  exports.useSignalValue = useSignalValue;
1726
1581
  exports.useSubject = useSubject;
1727
1582
  exports.useSubscribe = useSubscribe;
1728
1583
  exports.useSubscribeEffect = useSubscribeEffect;
1729
1584
  exports.useUnmountObservable = useUnmountObservable;
1730
- exports.withPersistance = withPersistance;
package/dist/index.d.ts CHANGED
@@ -5,15 +5,10 @@ export * from "./lib/binding/trigger";
5
5
  export * from "./lib/binding/useSubject";
6
6
  export * from "./lib/binding/useBehaviorSubject";
7
7
  export * from "./lib/state/signal";
8
- export * from "./lib/state/useSignal";
9
- export * from "./lib/state/useSetSignal";
10
8
  export * from "./lib/state/useSignalValue";
11
- export * from "./lib/state/useScopeSignals";
12
9
  export * from "./lib/state/constants";
13
- export * from "./lib/state/persistance/PersistSignals";
14
- export * from "./lib/state/persistance/withPersistance";
15
- export * from "./lib/state/persistance/createSharedStoreAdapter";
16
- export * from "./lib/state/persistance/createLocalforageAdapter";
10
+ export * from "./lib/state/persistance/adapters/createSharedStoreAdapter";
11
+ export * from "./lib/state/persistance/adapters/createLocalforageAdapter";
17
12
  export * from "./lib/utils/useUnmountObservable";
18
13
  export * from "./lib/utils/retryBackoff";
19
14
  export * from "./lib/utils/useLiveRef";
@@ -21,4 +16,4 @@ export * from "./lib/queries/react/useAsyncQuery";
21
16
  export * from "./lib/queries/react/useQuery";
22
17
  export * from "./lib/queries/react/useSubscribeEffect";
23
18
  export * from "./lib/queries/client/createClient";
24
- export { Provider as ReactjrxQueryProvider, useQueryClient } from "./lib/queries/react/Provider";
19
+ export { Provider as QueryClientProvider, useQueryClient } from "./lib/queries/react/Provider";
package/dist/index.js CHANGED
@@ -1,7 +1,13 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => {
4
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
+ return value;
6
+ };
1
7
  import { useRef, useMemo, useCallback, useSyncExternalStore, useEffect, createContext, memo, useContext } from "react";
2
- import { distinctUntilChanged, tap, finalize, catchError, EMPTY, Subject, identity, BehaviorSubject, of, zip, from, map, merge, throttleTime, switchMap, defer, iif, timer, throwError, scan, take, startWith, combineLatest, first, takeUntil, filter, concatMap as concatMap$1, mergeMap, fromEvent, skip, withLatestFrom, retry, shareReplay, endWith, delay, share, pairwise, NEVER, takeWhile } from "rxjs";
3
- import { jsx, jsxs } from "react/jsx-runtime";
8
+ import { distinctUntilChanged, tap, finalize, catchError, EMPTY, Subject, identity, BehaviorSubject, defer, iif, timer, throwError, scan, merge, map, take, startWith, of, combineLatest, from, first, takeUntil, filter, concatMap as concatMap$1, switchMap, mergeMap, fromEvent, skip, throttleTime, withLatestFrom, retry, shareReplay, endWith, delay, share, pairwise, NEVER, takeWhile } from "rxjs";
4
9
  import { retryWhen, concatMap, tap as tap$1 } from "rxjs/operators";
10
+ import { jsxs, jsx } from "react/jsx-runtime";
5
11
  const useLiveRef = (value) => {
6
12
  const ref = useRef(value);
7
13
  useMemo(() => {
@@ -161,12 +167,10 @@ const useBehaviorSubject = (state) => {
161
167
  return subject;
162
168
  };
163
169
  const SIGNAL_RESET = Symbol("SIGNAL_RESET");
164
- function signal(options) {
165
- const { default: defaultValue2 } = options ?? {};
170
+ function signal(config) {
171
+ const { default: defaultValue2 } = config ?? {};
166
172
  const subject = new BehaviorSubject(defaultValue2);
167
173
  const setValue = (arg) => {
168
- if (arg === subject.getValue())
169
- return;
170
174
  if (typeof arg === "function") {
171
175
  const change = arg(subject.getValue());
172
176
  if (change === subject.getValue())
@@ -182,166 +186,15 @@ function signal(options) {
182
186
  };
183
187
  const getValue = () => subject.getValue();
184
188
  return {
185
- setState: setValue,
189
+ setValue,
186
190
  getValue,
187
- options,
188
- /**
189
- * @important
190
- * We return the original behavior subject for two reasons:
191
- * - useObserve may return the default value directly instead of undefined
192
- * - the scope exist for react binding, this observable is a direct access outside of it
193
- */
191
+ config,
194
192
  subject
195
193
  };
196
194
  }
197
- const useSetSignal = (signal2) => {
198
- return signal2.setState;
199
- };
200
195
  const useSignalValue = (signal2, key) => {
201
196
  return useObserve(signal2.subject, { defaultValue: signal2.getValue(), key });
202
197
  };
203
- const useSignal = (signal2) => {
204
- return [useSignalValue(signal2), useSetSignal(signal2)];
205
- };
206
- const useScopeSignals = (signals) => {
207
- const signalsRef = useLiveRef(signals);
208
- useEffect(
209
- () => () => {
210
- signalsRef.current.forEach(({ setState }) => {
211
- setState(SIGNAL_RESET);
212
- });
213
- },
214
- []
215
- );
216
- };
217
- const createLocalStorageAdapter = (forage) => ({
218
- getItem: async (key) => {
219
- const serializedValue = forage.getItem(key);
220
- if (!serializedValue)
221
- return void 0;
222
- return JSON.parse(serializedValue);
223
- },
224
- setItem: async (key, value) => {
225
- forage.setItem(key, JSON.stringify(value));
226
- }
227
- });
228
- const PersistSignalsContext = createContext({
229
- resetSignals: () => {
230
- }
231
- });
232
- const usePersistSignalsContext = () => useContext(PersistSignalsContext);
233
- const PersistSignals = memo(
234
- ({
235
- children,
236
- signals = [],
237
- onReady,
238
- adapter = createLocalStorageAdapter(localStorage)
239
- }) => {
240
- const persistanceRef = useLiveRef(signals);
241
- const onReadyRef = useLiveRef(onReady);
242
- const adapterRef = useLiveRef(adapter);
243
- const resetSignalSubject = useSubject();
244
- const resetSignals = useCallback(() => {
245
- persistanceRef.current.forEach(({ setValue }) => {
246
- setValue(SIGNAL_RESET);
247
- });
248
- resetSignalSubject.current.next();
249
- }, []);
250
- const value = useMemo(() => ({ resetSignals }), [resetSignals]);
251
- const isHydrated = useObserve(() => {
252
- const items = persistanceRef.current;
253
- const stream = items.length === 0 ? of(true) : zip(
254
- ...items.map(
255
- ({ hydrateValue }) => from(hydrateValue({ adapter: adapterRef.current }))
256
- )
257
- ).pipe(map(() => true));
258
- return stream.pipe(
259
- tap(() => {
260
- if (onReadyRef.current != null)
261
- onReadyRef.current();
262
- }),
263
- catchError((error) => {
264
- console.error("Unable to hydrate", error);
265
- return EMPTY;
266
- })
267
- );
268
- }, []) ?? false;
269
- useSubscribe(
270
- () => !isHydrated ? EMPTY : merge(
271
- ...persistanceRef.current.map(
272
- ({ persistValue, $ }) => (
273
- // @todo test the reset
274
- merge(
275
- resetSignalSubject.current,
276
- $.pipe(
277
- throttleTime(500, void 0, {
278
- trailing: true,
279
- leading: false
280
- })
281
- )
282
- ).pipe(switchMap(() => from(persistValue({ adapter }))))
283
- )
284
- )
285
- ),
286
- [isHydrated, adapter]
287
- );
288
- return isHydrated ? /* @__PURE__ */ jsx(PersistSignalsContext.Provider, { value, children }) : null;
289
- }
290
- );
291
- const getNormalizedPersistanceValue = (unknownValue) => {
292
- if (unknownValue === null)
293
- return void 0;
294
- if (typeof unknownValue === "object" && "__key" in unknownValue && unknownValue.__key === "reactjrx_persistance") {
295
- return unknownValue;
296
- }
297
- return void 0;
298
- };
299
- function withPersistance(_signal, { version = 0 } = {}) {
300
- if (!_signal.options.key) {
301
- console.error(
302
- "You need to specify a key to use persistance with this signal"
303
- );
304
- }
305
- const hydrateValue = async ({
306
- adapter,
307
- key = _signal.options.key
308
- }) => {
309
- if (!key)
310
- return;
311
- const value = await adapter.getItem(key);
312
- const normalizedValue = getNormalizedPersistanceValue(value);
313
- if (normalizedValue == null)
314
- return;
315
- if (normalizedValue.migrationVersion && version > normalizedValue.migrationVersion) {
316
- return;
317
- }
318
- _signal.setState(value.value);
319
- };
320
- const persistValue = async ({
321
- adapter,
322
- key = _signal.options.key
323
- }) => {
324
- if (!key)
325
- return;
326
- const state = _signal.getValue();
327
- const value = {
328
- value: state,
329
- __key: "reactjrx_persistance",
330
- migrationVersion: version
331
- };
332
- await adapter.setItem(key, value);
333
- };
334
- return [
335
- {
336
- hydrateValue,
337
- persistValue,
338
- setValue: _signal.setState,
339
- $: _signal.subject,
340
- options: _signal.options
341
- },
342
- _signal
343
- ];
344
- }
345
198
  const normalizeStore = (store) => {
346
199
  if (!store || typeof store !== "object") {
347
200
  return void 0;
@@ -426,13 +279,13 @@ function retryBackoff(config) {
426
279
  });
427
280
  }
428
281
  function shallowEqual(objA, objB) {
429
- if (objA === null || objA === void 0 || objB === null || objB === void 0) {
282
+ if (objA === null || objA === void 0 || objB === void 0) {
430
283
  return objA === objB;
431
284
  }
432
285
  if (typeof objA !== "object" || typeof objB !== "object") {
433
286
  return objA === objB;
434
287
  }
435
- if (objA.constructor !== objB.constructor) {
288
+ if (objA.constructor !== (objB == null ? void 0 : objB.constructor)) {
436
289
  return false;
437
290
  }
438
291
  const keysA = Object.keys(objA);
@@ -609,11 +462,8 @@ const ClientEffect = ({
609
462
  return null;
610
463
  };
611
464
  const Provider = memo(
612
- ({
613
- children,
614
- client
615
- }) => {
616
- const value = useMemo(() => ({ client }), [client]);
465
+ ({ children, client }) => {
466
+ const value = useMemo(() => ({ client: client.client }), [client]);
617
467
  return /* @__PURE__ */ jsxs(Context.Provider, { value, children: [
618
468
  /* @__PURE__ */ jsx(ClientEffect, { client: value.client }),
619
469
  children
@@ -816,7 +666,10 @@ const resetStyle = { backgroundColor: "transparent", color: "inherit" };
816
666
  function createLogger(env) {
817
667
  const _logger = {
818
668
  namespaces: [
819
- { name: "@reactjrx", style: { backgroundColor: "#d02f4e", color: "white" } }
669
+ {
670
+ name: "@reactjrx",
671
+ style: { backgroundColor: "#d02f4e", color: "white" }
672
+ }
820
673
  ],
821
674
  namespace(name, style) {
822
675
  const logger2 = createLogger(env);
@@ -1698,9 +1551,15 @@ const createClient = () => {
1698
1551
  ...refetchClient
1699
1552
  };
1700
1553
  };
1554
+ class QueryClient {
1555
+ constructor() {
1556
+ __publicField(this, "client");
1557
+ this.client = createClient();
1558
+ }
1559
+ }
1701
1560
  export {
1702
- PersistSignals,
1703
- Provider as ReactjrxQueryProvider,
1561
+ QueryClient,
1562
+ Provider as QueryClientProvider,
1704
1563
  SIGNAL_RESET,
1705
1564
  createClient,
1706
1565
  createLocalforageAdapter,
@@ -1715,16 +1574,11 @@ export {
1715
1574
  useLiveRef,
1716
1575
  useObserve,
1717
1576
  useObserveCallback,
1718
- usePersistSignalsContext,
1719
1577
  useQuery,
1720
1578
  useQueryClient,
1721
- useScopeSignals,
1722
- useSetSignal,
1723
- useSignal,
1724
1579
  useSignalValue,
1725
1580
  useSubject,
1726
1581
  useSubscribe,
1727
1582
  useSubscribeEffect,
1728
- useUnmountObservable,
1729
- withPersistance
1583
+ useUnmountObservable
1730
1584
  };
@@ -0,0 +1 @@
1
+ export {};
@@ -14,8 +14,8 @@ export declare const logger: {
14
14
  namespaces: string;
15
15
  styles: string[];
16
16
  };
17
- print(method: "log" | "warn" | "error" | "group", ...message: any[]): any;
18
- printWithoutNamespace(method: "log" | "warn" | "error" | "group", ...message: any[]): any;
17
+ print(method: "error" | "log" | "warn" | "group", ...message: any[]): any;
18
+ printWithoutNamespace(method: "error" | "log" | "warn" | "group", ...message: any[]): any;
19
19
  log(...message: any): any;
20
20
  warn(...message: any): any;
21
21
  error(...message: any): any;
@@ -68,3 +68,7 @@ export declare const createClient: () => {
68
68
  start: () => () => void;
69
69
  };
70
70
  };
71
+ export declare class QueryClient {
72
+ client: ReturnType<typeof createClient>;
73
+ constructor();
74
+ }
@@ -14,8 +14,8 @@ export declare const logger: {
14
14
  namespaces: string;
15
15
  styles: string[];
16
16
  };
17
- print(method: "log" | "warn" | "error" | "group", ...message: any[]): any;
18
- printWithoutNamespace(method: "log" | "warn" | "error" | "group", ...message: any[]): any;
17
+ print(method: "error" | "log" | "warn" | "group", ...message: any[]): any;
18
+ printWithoutNamespace(method: "error" | "log" | "warn" | "group", ...message: any[]): any;
19
19
  log(...message: any): any;
20
20
  warn(...message: any): any;
21
21
  error(...message: any): any;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,16 @@
1
+ import type { Observable } from "rxjs";
2
+ export declare const withKeyComparison: <T extends {
3
+ key: any[];
4
+ }>(stream: Observable<T>) => Observable<(T & {
5
+ previousKey: undefined;
6
+ isUsingDifferentKey: boolean;
7
+ } extends infer T_1 ? T_1 extends T & {
8
+ previousKey: undefined;
9
+ isUsingDifferentKey: boolean;
10
+ } ? T_1 extends null | undefined ? never : T_1 : never : never) | (T & {
11
+ previousKey: any[];
12
+ isUsingDifferentKey: boolean;
13
+ } extends infer T_2 ? T_2 extends T & {
14
+ previousKey: any[];
15
+ isUsingDifferentKey: boolean;
16
+ } ? T_2 extends null | undefined ? never : T_2 : never : never)>;
@@ -14,8 +14,8 @@ export declare const logger: {
14
14
  namespaces: string;
15
15
  styles: string[];
16
16
  };
17
- print(method: "log" | "warn" | "error" | "group", ...message: any[]): any;
18
- printWithoutNamespace(method: "log" | "warn" | "error" | "group", ...message: any[]): any;
17
+ print(method: "error" | "log" | "warn" | "group", ...message: any[]): any;
18
+ printWithoutNamespace(method: "error" | "log" | "warn" | "group", ...message: any[]): any;
19
19
  log(...message: any): any;
20
20
  warn(...message: any): any;
21
21
  error(...message: any): any;
@@ -0,0 +1 @@
1
+ export {};
@@ -1,11 +1,11 @@
1
1
  import { type ReactNode } from "react";
2
- import { type createClient } from "../client/createClient";
2
+ import { type QueryClient, type createClient } from "../client/createClient";
3
3
  export declare const Context: import("react").Context<{
4
4
  client: ReturnType<typeof createClient>;
5
5
  }>;
6
6
  export declare const Provider: import("react").MemoExoticComponent<({ children, client }: {
7
7
  children: ReactNode;
8
- client: ReturnType<typeof createClient>;
8
+ client: QueryClient;
9
9
  }) => import("react/jsx-runtime").JSX.Element>;
10
10
  export declare const useQueryClient: () => {
11
11
  pipeQueryResult: <R extends Partial<import("../client/types").QueryResult<T>>, T>({ options$ }: {
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -1,4 +1,4 @@
1
- import { type Adapter } from "./types";
1
+ import { type Adapter } from "../types";
2
2
  /**
3
3
  * Create an adapter which use one unique store entry to store all
4
4
  * state. When using many signals it can help with maintenance to keep things
@@ -0,0 +1 @@
1
+ export declare const IDENTIFIER_PERSISTANCE_KEY = "__reactjrx";
@@ -1,9 +1,10 @@
1
+ import type { IDENTIFIER_PERSISTANCE_KEY } from "./constants";
1
2
  export interface Adapter {
2
- getItem: (key: string) => Promise<unknown> | unknown;
3
- setItem: (key: string, value: any) => Promise<unknown> | unknown;
3
+ getItem: (key: string) => Promise<unknown>;
4
+ setItem: (key: string, value: any) => Promise<unknown>;
4
5
  }
5
6
  export interface PersistanceEntry {
6
7
  value: unknown;
7
8
  migrationVersion?: number;
8
- __key: "reactjrx_persistance";
9
+ [IDENTIFIER_PERSISTANCE_KEY]: typeof IDENTIFIER_PERSISTANCE_KEY;
9
10
  }
@@ -0,0 +1,12 @@
1
+ import type { Adapter } from "./types";
2
+ import type { Signal } from "../signal";
3
+ export declare const usePersistSignals: ({ entries, onReady, adapter }: {
4
+ entries?: {
5
+ version: number;
6
+ signal: Signal<any, any, string>;
7
+ }[] | undefined;
8
+ onReady?: (() => void) | undefined;
9
+ adapter?: Adapter | undefined;
10
+ }) => {
11
+ isHydrated: boolean;
12
+ };
@@ -1,18 +1,22 @@
1
1
  import type { Observable } from "rxjs";
2
2
  import { SIGNAL_RESET } from "./constants";
3
- type Option<R = undefined> = {
4
- key?: string;
5
- } & (R extends undefined ? {
6
- default?: R;
3
+ type WithOptionalDefault<V> = V extends undefined ? {
4
+ default?: V;
7
5
  } : {
8
- default: R;
9
- });
10
- type SetState<S> = (stateOrUpdater: typeof SIGNAL_RESET | S | ((prev: S) => S)) => void;
11
- export interface Signal<S, R> {
12
- setState: SetState<S>;
6
+ default: V;
7
+ };
8
+ type WithOptionalKey<V> = V extends undefined ? {
9
+ key?: V;
10
+ } : {
11
+ key: V;
12
+ };
13
+ type Config<R = undefined, K = undefined> = WithOptionalKey<K> & WithOptionalDefault<R>;
14
+ type setValue<S> = (stateOrUpdater: typeof SIGNAL_RESET | S | ((prev: S) => S)) => void;
15
+ export interface Signal<S = undefined, R = undefined, K = undefined> {
16
+ setValue: setValue<S>;
13
17
  getValue: () => R;
14
- options: Option<S>;
18
+ config: Config<S, K>;
15
19
  subject: Observable<S>;
16
20
  }
17
- export declare function signal<T = undefined>(options: Option<T>): Signal<T, T>;
21
+ export declare function signal<T = undefined, K = undefined>(config: Config<T, K>): Signal<T, T, K>;
18
22
  export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -1,2 +1,2 @@
1
1
  import { type Signal } from "./signal";
2
- export declare const useSignalValue: <S>(signal: Signal<S, S>, key?: string) => S;
2
+ export declare const useSignalValue: <S, K>(signal: Signal<S, S, K>, key?: string) => S;
@@ -0,0 +1,2 @@
1
+ import { type MonoTypeOperatorFunction, type Subject } from "rxjs";
2
+ export declare function emitToSubject<T>(subject: Subject<T>): MonoTypeOperatorFunction<T>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,10 @@
1
+ import { type MonoTypeOperatorFunction } from "rxjs";
2
+ /**
3
+ * Share the latest value to every new observers but clean up
4
+ * otherwise.
5
+ *
6
+ * We cannot use shareReplay with react since it would never clean
7
+ * selectors and create memory leak. shareLatest ensure when the selector
8
+ * is not mounted anywhere we reset it
9
+ */
10
+ export declare const shareLatest: <T>() => MonoTypeOperatorFunction<T>;
@@ -0,0 +1,2 @@
1
+ import { type Observable } from "rxjs";
2
+ export declare function trackSubscriptions(onCountUpdate: (activeSubscriptions: number) => void): <T>(source$: Observable<T>) => Observable<T>;
@@ -1 +1,2 @@
1
+ /// <reference types="react" />
1
2
  export declare const useLiveRef: <T>(value: T) => import("react").MutableRefObject<T>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
package/dist/main.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export declare const printQuery: (data: any) => string;
@@ -0,0 +1 @@
1
+ export declare const waitForTimeout: (timeout: number) => Promise<unknown>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "reactjrx",
3
3
  "private": false,
4
- "version": "1.45.0",
4
+ "version": "1.47.0",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "dist"
@@ -39,25 +39,25 @@
39
39
  "devDependencies": {
40
40
  "@types/react": "^18.0.28",
41
41
  "@types/react-dom": "^18.0.11",
42
- "@typescript-eslint/eslint-plugin": "^5.59.0",
42
+ "@typescript-eslint/eslint-plugin": "^6.11.0",
43
43
  "@vitejs/plugin-react": "^4.0.0",
44
44
  "eslint": "^8.47.0",
45
- "eslint-config-prettier": "^8.8.0",
46
- "eslint-config-standard-with-typescript": "^34.0.1",
45
+ "eslint-config-prettier": "^9.0.0",
46
+ "eslint-config-standard-with-typescript": "^39.1.1",
47
47
  "eslint-plugin-import": "^2.27.5",
48
- "eslint-plugin-n": "^15.7.0",
48
+ "eslint-plugin-n": "^16.3.1",
49
49
  "eslint-plugin-promise": "^6.1.1",
50
50
  "eslint-plugin-react": "^7.32.2",
51
51
  "global-jsdom": "^9.0.1",
52
52
  "jsdom": "^22.0.0",
53
- "prettier": "^2.8.7",
54
- "rollup-plugin-node-externals": "^5.1.2",
53
+ "prettier": "^3.1.0",
54
+ "rollup-plugin-node-externals": "^6.1.2",
55
55
  "rxjs": "^7.8.0",
56
- "semantic-release": "^21.0.1",
56
+ "semantic-release": "^22.0.7",
57
57
  "typescript": "^5.0.4",
58
58
  "vite": "^4.2.1",
59
- "vite-plugin-dts": "^2.2.0",
60
- "vitest": "^0.31.0",
59
+ "vite-plugin-dts": "^3.6.3",
60
+ "vitest": "^0.34.6",
61
61
  "@testing-library/react": "^14.0.0",
62
62
  "@types/node": "^18.15.11"
63
63
  },
@@ -1,12 +0,0 @@
1
- import { type ReactNode } from "react";
2
- import type { WithPersistanceReturn } from "./withPersistance";
3
- import { type Adapter } from "./types";
4
- export declare const usePersistSignalsContext: () => {
5
- resetSignals: () => void;
6
- };
7
- export declare const PersistSignals: import("react").MemoExoticComponent<({ children, signals, onReady, adapter }: {
8
- children: ReactNode;
9
- signals?: WithPersistanceReturn<any>[] | undefined;
10
- onReady?: (() => void) | undefined;
11
- adapter?: Adapter | undefined;
12
- }) => import("react/jsx-runtime").JSX.Element | null>;
@@ -1,18 +0,0 @@
1
- import type { Signal } from "../signal";
2
- import { type Adapter } from "./types";
3
- export interface WithPersistanceReturn<T> {
4
- hydrateValue: (params: {
5
- adapter: Adapter;
6
- }) => Promise<void>;
7
- persistValue: (params: {
8
- adapter: Adapter;
9
- }) => Promise<void>;
10
- setValue: Signal<T, T>['setState'];
11
- $: Signal<T, T>['subject'];
12
- options: {
13
- key?: string;
14
- };
15
- }
16
- export declare function withPersistance<T>(_signal: Signal<T, T>, { version }?: {
17
- version?: number;
18
- }): [WithPersistanceReturn<T>, Signal<T, T>];
@@ -1,5 +0,0 @@
1
- import { type Signal } from "./signal";
2
- /**
3
- * Will reset signals when the scope is unmounted
4
- */
5
- export declare const useScopeSignals: (signals: Array<Signal<any, any>>) => void;
@@ -1,2 +0,0 @@
1
- import { type Signal } from "./signal";
2
- export declare const useSetSignal: <S, R>(signal: Signal<S, R>) => (stateOrUpdater: typeof import("./constants").SIGNAL_RESET | S | ((prev: S) => S)) => void;
@@ -1,2 +0,0 @@
1
- import { type Signal } from "./signal";
2
- export declare const useSignal: <S>(signal: Signal<S, S>) => readonly [S, (stateOrUpdater: typeof import("./constants").SIGNAL_RESET | S | ((prev: S) => S)) => void];