Repository navigation
📝 Fix docs pages that are empty or don't match the package #220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7d48e04
docs: fix examples that don't match the package
claude 7106907
docs(utils): write the pages that said only "todo", and update the co…
claude 0a23646
fix(utils): compare the holes of a sparse array in arrayEqual
claude 7c4c9ba
docs(utils): say what isShallowEqual compares
claude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,18 @@ | ||
| # arrayEqual | ||
|
|
||
| todo | ||
| Returns true when two arrays have the same length and the same items at each index, compared with `===`. | ||
|
|
||
| ### Usage | ||
|
|
||
| ```typescript | ||
| import { arrayEqual } from "reactjrx" | ||
|
|
||
| arrayEqual([1, 2], [1, 2]) // true | ||
| arrayEqual([{ id: 1 }], [{ id: 1 }]) // false, the two objects are different references | ||
| ``` | ||
|
|
||
| ### Reference | ||
|
|
||
| ```typescript | ||
| function arrayEqual<A, B>(a: readonly A[], b: readonly B[]): boolean | ||
| ``` | ||
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,21 @@ | ||
| # isDefined | ||
|
|
||
| todo | ||
| Returns true when the value is neither `null` nor `undefined`. | ||
|
|
||
| {% hint style="info" %} | ||
| This is a type guard function: it removes `null` and `undefined` from the type, which makes it handy with `filter`. | ||
| {% endhint %} | ||
|
|
||
| ### Usage | ||
|
|
||
| ```typescript | ||
| import { isDefined } from "reactjrx" | ||
|
|
||
| const ids = [1, undefined, 3, null].filter(isDefined) // number[] | ||
| ``` | ||
|
|
||
| ### Reference | ||
|
|
||
| ```typescript | ||
| function isDefined<T>(arg: T | null | undefined): arg is T extends null | undefined ? never : T | ||
| ``` |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # isShallowEqual | ||
|
|
||
| Returns true when two values are equal one level deep: the same primitive, or two objects with the same `constructor` and the same own enumerable string keys holding `===` values. Symbol and non-enumerable properties are not compared. | ||
|
|
||
| ### Usage | ||
|
|
||
| ```typescript | ||
| import { isShallowEqual } from "reactjrx" | ||
|
|
||
| isShallowEqual({ a: 1, b: "x" }, { a: 1, b: "x" }) // true | ||
| isShallowEqual({ a: { b: 1 } }, { a: { b: 1 } }) // false, `a` holds two different objects | ||
| ``` | ||
|
|
||
| Pass it as the `compareFn` of [useObserve](../../bindings/react/useobserve.md) to skip the render of a new object that holds the same values. | ||
|
|
||
| ### Reference | ||
|
|
||
| ```typescript | ||
| function isShallowEqual<A, B>(objA: A, objB: B): boolean | ||
| ``` |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,24 @@ | ||
| # useConstant | ||
|
|
||
| todo | ||
| Creates a value on the first render and returns that same value on every render after it. | ||
|
|
||
| ### Usage | ||
|
|
||
| ```tsx | ||
| import { useConstant } from "reactjrx" | ||
| import { BehaviorSubject } from "rxjs" | ||
|
|
||
| const Counter = () => { | ||
| const count$ = useConstant(() => new BehaviorSubject(0)) | ||
|
|
||
| // ... | ||
| } | ||
| ``` | ||
|
|
||
| The value lives in a ref, so it is never recomputed. React can drop a `useMemo` value and compute it again, which would create a second subject. | ||
|
|
||
| ### Reference | ||
|
|
||
| ```typescript | ||
| function useConstant<T>(fn: () => T): T | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,30 @@ | ||
| # useLiveRef | ||
|
|
||
| todo | ||
| Returns a ref that holds the value passed on the latest render. A callback or a subscription that outlives the render reads the current value from it, without listing that value as a dependency. | ||
|
|
||
| ### Usage | ||
|
|
||
| ```tsx | ||
| import { useEffect } from "react" | ||
| import { useLiveRef } from "reactjrx" | ||
|
|
||
| const MessageLogger = ({ onMessage }: { onMessage: (message: string) => void }) => { | ||
| const onMessageRef = useLiveRef(onMessage) | ||
|
|
||
| useEffect(() => { | ||
| const subscription = messages$.subscribe((message) => onMessageRef.current(message)) | ||
|
|
||
| return () => subscription.unsubscribe() | ||
| }, [onMessageRef]) | ||
|
|
||
| return null | ||
| } | ||
| ``` | ||
|
|
||
| The subscription stays open across renders and calls the latest `onMessage`, so a new callback doesn't resubscribe. | ||
|
|
||
| ### Reference | ||
|
|
||
| ```typescript | ||
| function useLiveRef<T>(value: T): RefObject<T> | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,42 @@ | ||
| # retryBackoff | ||
|
|
||
| todo | ||
| An operator that resubscribes to the source when it errors, after a delay that grows with each error. By default it retries forever: pass `retry` to stop. | ||
|
|
||
| ### Usage | ||
|
|
||
| ```typescript | ||
| import { ajax } from "rxjs/ajax" | ||
| import { retryBackoff } from "reactjrx" | ||
|
|
||
| const user$ = ajax.getJSON("/api/user").pipe( | ||
| retryBackoff({ | ||
| initialInterval: 500, | ||
| maxInterval: 5_000, | ||
| retry: (attempt) => attempt < 5, | ||
| }), | ||
| ) | ||
| ``` | ||
|
|
||
| This retries 5 times, after 500 ms, 1 s, 2 s, 4 s and 5 s. The sixth error goes through. | ||
|
|
||
| ### Options | ||
|
|
||
| | Option | Default | Description | | ||
| | --- | --- | --- | | ||
| | `initialInterval` | `100` | Base delay in ms. Retry `n` (from 0) waits `2^n * initialInterval`. | | ||
| | `maxInterval` | `Infinity` | Longest delay in ms. | | ||
| | `retry` | always `true` | `(attempt, error) => boolean`, with `attempt` from 0. Return `false` to stop retrying and let the error through. | | ||
| | `resetOnSuccess` | `false` | Each value the source emits resets the delay and the attempt count. | | ||
| | `backoffDelay` | `exponentialBackoffDelay` | `(attempt, initialInterval) => number`: the delay before retry `attempt`, capped at `maxInterval`. | | ||
| | `retryAfter` | none | `() => Observable`: each retry waits for its first value before its delay starts, to wait for the network to come back, for example. | | ||
| | `retryAfterDelay` | none | `(attempt, error) => boolean`, checked once the delay has passed. Return `false` to let the error through instead of retrying. | | ||
| | `caughtError` | none | `(errorCount, error) => Observable \| undefined`, called for each error that will be retried. The values of the Observable it returns are emitted before the retry. | | ||
| | `catchError` | none | `(errorCount, error) => Observable`, called once retrying stops. The Observable it returns replaces the error. | | ||
|
|
||
| ### Reference | ||
|
|
||
| ```typescript | ||
| function retryBackoff<T, TError>( | ||
| config: RetryBackoffConfig<T, TError>, | ||
| ): <T>(source: Observable<T>) => Observable<T> | ||
| ``` |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { describe, expect, it } from "vitest" | ||
| import { arrayEqual } from "./arrayEqual" | ||
|
|
||
| describe("arrayEqual", () => { | ||
| it("should compare the items at each index", () => { | ||
| expect(arrayEqual([1, 2], [1, 2])).toBe(true) | ||
| expect(arrayEqual([1, 2], [2, 1])).toBe(false) | ||
| expect(arrayEqual([1], [1, 2])).toBe(false) | ||
| }) | ||
|
|
||
| it("should compare the holes of a sparse array too", () => { | ||
| expect(arrayEqual(new Array<number>(1), [1])).toBe(false) | ||
| expect(arrayEqual([1], new Array<number>(1))).toBe(false) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,10 @@ | ||
| export const arrayEqual = <A, B>(a: readonly A[], b: readonly B[]) => | ||
| a.length === b.length && a.every((v, i) => (v as unknown) === b[i]) | ||
| export const arrayEqual = <A, B>(a: readonly A[], b: readonly B[]) => { | ||
| if (a.length !== b.length) return false | ||
|
|
||
| // A loop rather than `every`, which skips the holes of a sparse array. | ||
| for (let index = 0; index < a.length; index++) { | ||
| if ((a[index] as unknown) !== b[index]) return false | ||
| } | ||
|
|
||
| return true | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.