reactjrx 1.6.2 → 1.7.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
@@ -1493,6 +1493,101 @@ const useMutation = (query, options = {}) => {
1493
1493
  );
1494
1494
  return { ...result, mutate };
1495
1495
  };
1496
+ const arrayEqual = (a, b) => a.length === b.length && a.every((v, i) => v === b[i]);
1497
+ function useQuery(key, fn, options = {}) {
1498
+ const params$ = React.useRef(new rxjs.BehaviorSubject({ key, options }));
1499
+ const queryRef = useLiveRef(fn);
1500
+ const refetchSubject = useConstant(() => new rxjs.Subject());
1501
+ const data$ = useConstant(
1502
+ () => new rxjs.BehaviorSubject({
1503
+ data: void 0,
1504
+ error: void 0,
1505
+ isLoading: false
1506
+ })
1507
+ );
1508
+ React.useEffect(() => {
1509
+ params$.current.next({
1510
+ key,
1511
+ options
1512
+ });
1513
+ }, [key, options]);
1514
+ React.useEffect(() => {
1515
+ const options$ = params$.current.pipe(
1516
+ rxjs.map(({ options: options2 }) => options2),
1517
+ rxjs.distinctUntilChanged(shallowEqual)
1518
+ );
1519
+ const key$ = params$.current.pipe(
1520
+ rxjs.map(({ key: key2 }) => key2),
1521
+ rxjs.distinctUntilChanged(arrayEqual)
1522
+ );
1523
+ const enabledOption$ = options$.pipe(
1524
+ rxjs.map(({ enabled = true }) => enabled),
1525
+ rxjs.distinctUntilChanged()
1526
+ );
1527
+ const executeFn$ = rxjs.combineLatest([
1528
+ key$,
1529
+ enabledOption$,
1530
+ refetchSubject.pipe(rxjs.startWith(void 0))
1531
+ ]).pipe(
1532
+ rxjs.tap(([, enabled]) => {
1533
+ if (!enabled) {
1534
+ data$.next({
1535
+ ...data$.getValue(),
1536
+ isLoading: false
1537
+ });
1538
+ }
1539
+ }),
1540
+ rxjs.filter(([, enabled]) => enabled),
1541
+ rxjs.map(([fn2]) => fn2)
1542
+ );
1543
+ const disabled$ = enabledOption$.pipe(rxjs.filter((v) => !v));
1544
+ const sub = executeFn$.pipe(
1545
+ rxjs.withLatestFrom(options$),
1546
+ rxjs.tap(() => {
1547
+ data$.next({
1548
+ ...data$.getValue(),
1549
+ error: void 0,
1550
+ isLoading: true
1551
+ });
1552
+ }),
1553
+ rxjs.switchMap(
1554
+ ([, options2]) => rxjs.from(rxjs.defer(() => queryRef.current())).pipe(
1555
+ querx(options2),
1556
+ rxjs.map((response) => [response]),
1557
+ rxjs.catchError((error) => {
1558
+ return rxjs.of([void 0, error]);
1559
+ }),
1560
+ rxjs.takeUntil(disabled$)
1561
+ )
1562
+ ),
1563
+ rxjs.tap(([response, error]) => {
1564
+ data$.next({
1565
+ ...data$.getValue(),
1566
+ isLoading: false,
1567
+ error,
1568
+ data: response
1569
+ });
1570
+ })
1571
+ ).subscribe({
1572
+ next: () => {
1573
+ },
1574
+ complete: () => {
1575
+ console.error("useQuerx has completed");
1576
+ },
1577
+ error: console.error
1578
+ });
1579
+ return () => sub.unsubscribe();
1580
+ }, [refetchSubject, data$]);
1581
+ const result = useObserve(data$, {
1582
+ defaultValue: data$.getValue()
1583
+ });
1584
+ const refetch = React.useCallback(
1585
+ () => refetchSubject.next(void 0),
1586
+ [refetchSubject]
1587
+ );
1588
+ return { ...result, refetch };
1589
+ }
1496
1590
  exports.signal = signal;
1497
1591
  exports.useMutation = useMutation;
1498
1592
  exports.useObserve = useObserve;
1593
+ exports.useQuery = useQuery;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from "./lib/signal";
2
2
  export * from "./lib/useObserve";
3
3
  export * from "./lib/queries/useMutation";
4
+ export * from "./lib/queries/useQuery";
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { distinctUntilChanged, tap as tap$1, BehaviorSubject, defer, iif, timer, throwError, Subject as Subject$1, switchMap, from, map as map$1, catchError, of } from "rxjs";
1
+ import { distinctUntilChanged, tap as tap$1, BehaviorSubject, defer, iif, timer, throwError, Subject as Subject$1, switchMap, from, map as map$1, catchError, of, combineLatest, startWith, filter, withLatestFrom, takeUntil } from "rxjs";
2
2
  import * as React from "react";
3
3
  import { useRef, useCallback, useSyncExternalStore, useMemo, useEffect } from "react";
4
4
  function shallowEqual(objA, objB) {
@@ -1475,8 +1475,103 @@ const useMutation = (query, options = {}) => {
1475
1475
  );
1476
1476
  return { ...result, mutate };
1477
1477
  };
1478
+ const arrayEqual = (a, b) => a.length === b.length && a.every((v, i) => v === b[i]);
1479
+ function useQuery(key, fn, options = {}) {
1480
+ const params$ = useRef(new BehaviorSubject({ key, options }));
1481
+ const queryRef = useLiveRef(fn);
1482
+ const refetchSubject = useConstant(() => new Subject$1());
1483
+ const data$ = useConstant(
1484
+ () => new BehaviorSubject({
1485
+ data: void 0,
1486
+ error: void 0,
1487
+ isLoading: false
1488
+ })
1489
+ );
1490
+ useEffect(() => {
1491
+ params$.current.next({
1492
+ key,
1493
+ options
1494
+ });
1495
+ }, [key, options]);
1496
+ useEffect(() => {
1497
+ const options$ = params$.current.pipe(
1498
+ map$1(({ options: options2 }) => options2),
1499
+ distinctUntilChanged(shallowEqual)
1500
+ );
1501
+ const key$ = params$.current.pipe(
1502
+ map$1(({ key: key2 }) => key2),
1503
+ distinctUntilChanged(arrayEqual)
1504
+ );
1505
+ const enabledOption$ = options$.pipe(
1506
+ map$1(({ enabled = true }) => enabled),
1507
+ distinctUntilChanged()
1508
+ );
1509
+ const executeFn$ = combineLatest([
1510
+ key$,
1511
+ enabledOption$,
1512
+ refetchSubject.pipe(startWith(void 0))
1513
+ ]).pipe(
1514
+ tap$1(([, enabled]) => {
1515
+ if (!enabled) {
1516
+ data$.next({
1517
+ ...data$.getValue(),
1518
+ isLoading: false
1519
+ });
1520
+ }
1521
+ }),
1522
+ filter(([, enabled]) => enabled),
1523
+ map$1(([fn2]) => fn2)
1524
+ );
1525
+ const disabled$ = enabledOption$.pipe(filter((v) => !v));
1526
+ const sub = executeFn$.pipe(
1527
+ withLatestFrom(options$),
1528
+ tap$1(() => {
1529
+ data$.next({
1530
+ ...data$.getValue(),
1531
+ error: void 0,
1532
+ isLoading: true
1533
+ });
1534
+ }),
1535
+ switchMap(
1536
+ ([, options2]) => from(defer(() => queryRef.current())).pipe(
1537
+ querx(options2),
1538
+ map$1((response) => [response]),
1539
+ catchError((error) => {
1540
+ return of([void 0, error]);
1541
+ }),
1542
+ takeUntil(disabled$)
1543
+ )
1544
+ ),
1545
+ tap$1(([response, error]) => {
1546
+ data$.next({
1547
+ ...data$.getValue(),
1548
+ isLoading: false,
1549
+ error,
1550
+ data: response
1551
+ });
1552
+ })
1553
+ ).subscribe({
1554
+ next: () => {
1555
+ },
1556
+ complete: () => {
1557
+ console.error("useQuerx has completed");
1558
+ },
1559
+ error: console.error
1560
+ });
1561
+ return () => sub.unsubscribe();
1562
+ }, [refetchSubject, data$]);
1563
+ const result = useObserve(data$, {
1564
+ defaultValue: data$.getValue()
1565
+ });
1566
+ const refetch = useCallback(
1567
+ () => refetchSubject.next(void 0),
1568
+ [refetchSubject]
1569
+ );
1570
+ return { ...result, refetch };
1571
+ }
1478
1572
  export {
1479
1573
  signal,
1480
1574
  useMutation,
1481
- useObserve
1575
+ useObserve,
1576
+ useQuery
1482
1577
  };
@@ -0,0 +1,7 @@
1
+ import { QuerxOptions } from "./types";
2
+ export declare function useQuery<T>(key: any[], fn: () => Promise<T>, options?: QuerxOptions): {
3
+ data: T | undefined;
4
+ isLoading: boolean;
5
+ error: unknown;
6
+ refetch: () => void;
7
+ };
@@ -0,0 +1 @@
1
+ export declare const arrayEqual: (a: any[], b: any[]) => boolean;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "reactjrx",
3
3
  "private": false,
4
- "version": "1.6.2",
4
+ "version": "1.7.0",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "dist"