reactjrx 1.2.0 → 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.
@@ -0,0 +1,2 @@
1
+ import { QuerxOptions } from "./types";
2
+ export declare const retryFromOptions: (options: QuerxOptions) => <T>(source: import("rxjs").Observable<T>) => import("rxjs").Observable<T>;
@@ -0,0 +1,3 @@
1
+ import { Observable } from "rxjs";
2
+ import { QuerxOptions } from "./types";
3
+ export declare const querx: <T>(options: QuerxOptions) => (source: Observable<T>) => Observable<T>;
@@ -0,0 +1,9 @@
1
+ export type QuerxOptions = {
2
+ enabled?: boolean;
3
+ retry?: number | ((attempt: number, error: unknown) => boolean);
4
+ refetchOnWindowFocus?: boolean;
5
+ refetchOnMount?: boolean;
6
+ staleTime?: number;
7
+ onError?: (error: unknown) => void;
8
+ onSuccess?: () => void;
9
+ };
@@ -0,0 +1,20 @@
1
+ import { QuerxOptions } from "./types";
2
+ /**
3
+ * @important
4
+ * Your mutation function is cancelled whenever you call a new mutate or
5
+ * when the component is unmounted. Same behavior will happens with your
6
+ * callback functions regarding unmounting. None of them will be called.
7
+ *
8
+ * If you provide an observable as a return it will be automatically cancelled
9
+ * as well during unmont or if called again. If you provide anything else you
10
+ * are in charge of controlling the flow.
11
+ *
12
+ * If you need to execute mutation independently of the component lifecycle or
13
+ * execute functions in parallel you should not use this hook.
14
+ */
15
+ export declare const useMutation: <A, R>(query: (args: A) => Promise<R>, options?: QuerxOptions) => {
16
+ mutate: (args: A) => void;
17
+ data: R | undefined;
18
+ isLoading: boolean;
19
+ error: unknown;
20
+ };
@@ -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,3 @@
1
+ export declare const signal: <T>({ default: defaultValue, }: {
2
+ default: T;
3
+ }) => [() => T, (state: T | ((prev: T) => T)) => void];
@@ -0,0 +1,27 @@
1
+ import { DependencyList } from "react";
2
+ import { Observable } from "rxjs";
3
+ /**
4
+ * Hook that subscribes to an RxJS Observable and
5
+ * synchronizes its latest value with an external store.
6
+ *
7
+ * @example
8
+ * function MyComponent(props) {
9
+ * const source$ = useMemo(() => ... // Create and memoize an RxJS Observable here
10
+ *
11
+ * const value = useObserve(source$);
12
+ *
13
+ * // Render your component here
14
+ * return <div>The value is: {value}</div>;
15
+ * }
16
+ *
17
+ * @remarks
18
+ * It's important to memoize the Observable using a hook
19
+ * such as `useMemo` or `useCallback` to ensure that the same
20
+ * Observable instance is passed to `useObserve` on every render.
21
+ * Otherwise, the hook will subscribe to a new Observable
22
+ * instance every time the component renders, leading to memory
23
+ * leaks and unexpected behavior.
24
+ */
25
+ export declare function useObserve<T>(source$: Observable<T> | (() => Observable<T>), { defaultValue }: {
26
+ defaultValue: T;
27
+ }, deps?: DependencyList): T;
@@ -0,0 +1 @@
1
+ export declare const arrayEqual: (a: any[], b: any[]) => boolean;
@@ -0,0 +1,22 @@
1
+ import { Observable } from "rxjs";
2
+ export interface RetryBackoffConfig {
3
+ initialInterval: number;
4
+ maxRetries?: number;
5
+ maxInterval?: number;
6
+ resetOnSuccess?: boolean;
7
+ shouldRetry?: (attempt: number, error: any) => boolean;
8
+ backoffDelay?: (iteration: number, initialInterval: number) => number;
9
+ }
10
+ /** Calculates the actual delay which can be limited by maxInterval */
11
+ export declare function getDelay(backoffDelay: number, maxInterval: number): number;
12
+ /** Exponential backoff delay */
13
+ export declare function exponentialBackoffDelay(iteration: number, initialInterval: number): number;
14
+ /**
15
+ * Returns an Observable that mirrors the source Observable with the exception
16
+ * of an error. If the source Observable calls error, rather than propagating
17
+ * the error call this method will resubscribe to the source Observable with
18
+ * exponentially increasing interval and up to a maximum of count
19
+ * resubscriptions (if provided). Retrying can be cancelled at any point if
20
+ * shouldRetry returns false.
21
+ */
22
+ export declare function retryBackoff(config: number | RetryBackoffConfig): <T>(source: Observable<T>) => Observable<T>;
@@ -0,0 +1 @@
1
+ export declare function shallowEqual(objA: any, objB: any): boolean;
@@ -0,0 +1,21 @@
1
+ /**
2
+ * useConstant hook returns a constant value computed by
3
+ * calling the provided function only once during the
4
+ * component's lifetime.
5
+ *
6
+ * @param {() => T} fn A function that computes and returns the constant value. The function is called only once, during the first render of the component.
7
+ * @returns {T} The constant value computed by the provided function.
8
+ *
9
+ * @example
10
+ * function MyComponent(props) {
11
+ * const constantValue = useConstant(() => {
12
+ * // Compute and return the constant value here
13
+ * return "Hello, world!";
14
+ * });
15
+ *
16
+ * // Use the constant value in your component
17
+ * return <div>{constantValue}</div>;
18
+ * }
19
+ *
20
+ */
21
+ export declare const useConstant: <T>(fn: () => T) => T;
@@ -0,0 +1 @@
1
+ export declare const useLiveRef: <T>(value: T) => import("react").MutableRefObject<T>;
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "reactjrx",
3
3
  "private": false,
4
- "version": "1.2.0",
4
+ "version": "1.7.0",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "dist"
8
8
  ],
9
- "main": "./dist/index.umd.cjs",
9
+ "main": "./dist/index.js",
10
10
  "module": "./dist/index.js",
11
11
  "exports": {
12
12
  ".": {
@@ -14,12 +14,14 @@
14
14
  "require": "./dist/index.umd.cjs"
15
15
  }
16
16
  },
17
+ "types": "./dist/index.d.ts",
17
18
  "publishConfig": {
18
19
  "access": "public"
19
20
  },
20
21
  "scripts": {
21
22
  "dev": "vite",
22
23
  "build": "tsc && vite build",
24
+ "watch": "vite build --watch",
23
25
  "preview": "vite preview",
24
26
  "prepublishOnly": "npm run build",
25
27
  "semantic-release": "semantic-release"
@@ -37,9 +39,11 @@
37
39
  "semantic-release": "^21.0.1",
38
40
  "typescript": "^5.0.2",
39
41
  "vite": "^4.2.1",
42
+ "vite-plugin-dts": "^2.2.0",
40
43
  "vitest": "^0.29.8"
41
44
  },
42
45
  "dependencies": {
46
+ "@testing-library/react": "^14.0.0",
43
47
  "@types/node": "^18.15.11"
44
48
  },
45
49
  "repository": {
@@ -1,64 +0,0 @@
1
- (function(global, factory) {
2
- typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("rxjs"), require("react")) : typeof define === "function" && define.amd ? define(["exports", "rxjs", "react"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.reactjrx = {}, global.rxjs, global.react));
3
- })(this, function(exports2, rxjs, react) {
4
- "use strict";
5
- function shallowEqual(objA, objB) {
6
- if (objA === null || objA === void 0 || objB === null || objB === void 0) {
7
- return objA === objB;
8
- }
9
- if (typeof objA !== "object" || typeof objB !== "object") {
10
- return objA === objB;
11
- }
12
- if (objA.constructor !== objB.constructor) {
13
- return false;
14
- }
15
- const keysA = Object.keys(objA);
16
- const keysB = Object.keys(objB);
17
- if (keysA.length !== keysB.length) {
18
- return false;
19
- }
20
- for (let i = 0; i < keysA.length; i++) {
21
- const key = keysA[i];
22
- if (key && objA[key] !== objB[key]) {
23
- return false;
24
- }
25
- }
26
- return true;
27
- }
28
- function useObserve(source$, { defaultValue }, deps = []) {
29
- const valueRef = react.useRef(defaultValue);
30
- const subscribe = react.useCallback((next) => {
31
- const sourceAsFn = typeof source$ === "function" ? source$ : () => source$;
32
- const sub = sourceAsFn().pipe(
33
- rxjs.distinctUntilChanged(shallowEqual),
34
- rxjs.tap((value) => {
35
- valueRef.current = value;
36
- })
37
- ).subscribe(next);
38
- return () => sub.unsubscribe();
39
- }, deps);
40
- const getSnapshot = react.useCallback(() => valueRef.current, []);
41
- return react.useSyncExternalStore(subscribe, getSnapshot);
42
- }
43
- const signal = ({
44
- default: defaultValue
45
- }) => {
46
- const subject = new rxjs.BehaviorSubject(defaultValue);
47
- const hook = () => useObserve(subject.asObservable(), { defaultValue }, []);
48
- const setValue = (arg) => {
49
- if (arg === subject.getValue())
50
- return;
51
- if (typeof arg === "function") {
52
- const change = arg(subject.getValue());
53
- if (change === subject.getValue())
54
- return;
55
- return subject.next(change);
56
- }
57
- return subject.next(arg);
58
- };
59
- return [hook, setValue];
60
- };
61
- exports2.signal = signal;
62
- exports2.useObserve = useObserve;
63
- Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
64
- });