Skip to content

Commit

Permalink
chore: rename package
Browse files Browse the repository at this point in the history
yuhr committed Jul 19, 2021
1 parent 584a525 commit 8361bd1
Showing 3 changed files with 22 additions and 20 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# React Hook: `useAwait`
# React Hook: `useAwaitData`

[![npm](https://img.shields.io/npm/v/use-await)](https://www.npmjs.com/package/use-await)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
[![Test](https://github.com/yuhr/use-await/actions/workflows/test.yml/badge.svg)](https://github.com/yuhr/use-await/actions/workflows/test.yml)

`useAwait` is a React hook that executes an async task to populate the desired data.
`useAwaitData` is a React hook that executes an async task to populate the desired data.

## Features

@@ -19,10 +19,10 @@
## Example

```tsx
import useAwait from "use-await"
import useAwaitData from "use-await"

const ShowData = ({ query }: { query: Query }) => {
const result = useAwait(
const result = useAwaitData(
async ({ tick, signal }) => {
const a = await someTask(query)
tick = await tick()
@@ -69,7 +69,7 @@ You may notice there're already similar packages in the wild, such as [`use-asyn

This package has of course a functionality to abort manually, and also a functionality to automatically “invalidate” the previous run of the async function when the dependencies have been updated and the stale task is not settled yet. This will reduce unexpected state changes and thus re-renders of the component.

The returned object of `useAwait` always stands for the current run of the async function. Even if the invalidated run has settled while the current run is ongoing, the result of the invalidated run will be ignored and no re-render occurs.
The returned object of `useAwaitData` always stands for the current run of the async function. Even if the invalidated run has settled while the current run is ongoing, the result of the invalidated run will be ignored and no re-render occurs.

## Detailed Behavior and Usage

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "use-await",
"name": "use-await-data",
"version": "0.0.0",
"description": "A React hook that executes an async task to populate the desired data.",
"type": "module",
30 changes: 16 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ const symbolAborted: unique symbol = Symbol()
*
* > ```tsx
* > const ShowData = ({ query }: { query: Query }) => {
* > const result = useAwait(
* > const result = useAwaitData(
* > async ({ tick, signal }) => {
* > const a = await someTask(query)
* > tick = await tick()
@@ -53,8 +53,8 @@ const symbolAborted: unique symbol = Symbol()
* otherwise it's just a no-op.
*
* The first parameter of `task` function will be a
* {@link useAwait.Scheduler `Scheduler`} object, whose shape is `{ tick: Tick;
* signal: AbortSignal }` and {@link useAwait.Tick `Tick`} is `() =>
* {@link useAwaitData.Scheduler `Scheduler`} object, whose shape is `{ tick: Tick;
* signal: AbortSignal }` and {@link useAwaitData.Tick `Tick`} is `() =>
* PromiseLike<Tick>`. You may want to call the `tick` function with some
* idiomatic syntax like `tick = await tick()` periodically in the task
* function, in order to handle aborting of the task — `tick` immediately
@@ -105,15 +105,17 @@ const symbolAborted: unique symbol = Symbol()
* status: |<-running------>|<-aborted--->|<-running--->|<-fulfilled------------------>
* ```
*/
const useAwait = <Value>(
task: useAwait.AsyncTask<Value>,
const useAwaitData = <Value>(
task: useAwaitData.AsyncTask<Value>,
dependencies: unknown[] = [],
): useAwait.Result<Value> => {
): useAwaitData.Result<Value> => {
const statuses = useMemo(
() => new Map<useAwait.AsyncTask<Value>, useAwait.Status>(),
() => new Map<useAwaitData.AsyncTask<Value>, useAwaitData.Status>(),
[],
)
const oldEffectRef = useRef<useAwait.AsyncTask<Value> | undefined>(undefined)
const oldEffectRef = useRef<useAwaitData.AsyncTask<Value> | undefined>(
undefined,
)

const abortController = useMemo(() => new AbortController(), dependencies)
const abort = useCallback(() => {
@@ -123,11 +125,11 @@ const useAwait = <Value>(
}
}, dependencies)

const [result, setResult] = useState<useAwait.Result<Value>>({
const [result, setResult] = useState<useAwaitData.Result<Value>>({
status: "running",
abort,
})
const updateResult = useCallback((result: useAwait.Result<Value>) => {
const updateResult = useCallback((result: useAwaitData.Result<Value>) => {
statuses.set(task, result.status)
setResult(result)
}, dependencies)
@@ -156,7 +158,7 @@ const useAwait = <Value>(
}

const tick = () =>
new Promise<useAwait.Tick>((resolve, reject) => {
new Promise<useAwaitData.Tick>((resolve, reject) => {
switch (statuses.get(task)) {
case undefined: // This means it should be invalidated
reject(symbolInvalidate)
@@ -169,7 +171,7 @@ const useAwait = <Value>(
}
})
const { signal } = abortController
const scheduler: useAwait.Scheduler = { tick, signal }
const scheduler: useAwaitData.Scheduler = { tick, signal }

// Set initial status for the current task
updateResult({ status: "running", abort })
@@ -182,7 +184,7 @@ const useAwait = <Value>(
return result
}

namespace useAwait {
namespace useAwaitData {
export type Result<Value> =
| { status: "fulfilled"; value: Value }
| { status: "rejected"; error: unknown }
@@ -198,4 +200,4 @@ namespace useAwait {
export type Scheduler = { tick: Tick; signal: AbortSignal }
}

export default useAwait
export default useAwaitData

0 comments on commit 8361bd1

Please sign in to comment.