Skip to content

Commit

Permalink
feat: don't throw on abort by default
Browse files Browse the repository at this point in the history
  • Loading branch information
valcol committed Jan 4, 2023
1 parent 5f7bd0a commit bbae633
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<h1>
<br/>
<br/>
🪡
🧵
<br />
react-use-scheduler
<br />
Expand Down Expand Up @@ -113,10 +113,11 @@ postTask(fn, options);

**Options**

| Name | Type | Default | Description |
| ------------ | --------- | ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **priority** | `string` | `TASK_PRIORITIES.userBlocking` | Override the `priority` set in the options argument to the `useInView` hook. |
| **detached** | `boolean` | `false` | By setting `detached` to `true`, you can ensure that the task is not affected by the component's lifecycle. The priority will stay the same and the task will not be cancelled if the component is unmounted. |
| Name | Type | Default | Description |
| ---------------- | --------- | ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **priority** | `string` | `TASK_PRIORITIES.userBlocking` | Override the `priority` set in the options argument to the `useInView` hook. |
| **detached** | `boolean` | `false` | If set to `true`, the task will not be affected by the component's lifecycle. The priority will stay the same and the task will not be cancelled if the component is unmounted. |
| **throwOnAbort** | `boolean` | `false` | If set to `true` an error will be throw if the task is aborted before completion |

### `ref`

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-use-scheduler",
"version": "1.0.1",
"version": "1.1.0",
"description": "A React hook that allows you to schedule tasks and automatically orchestrate them based on your component lifecycle and visibility.",
"main": "dist/index.js",
"scripts": {
Expand Down
26 changes: 16 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,23 @@ const useScheduler = ({
useEffect(() => {
if (entry) {
Object.entries(controllers.current).forEach(([priority, controller]) =>
controller.setPriority?.(inView ? priority : TASK_PRIORITIES.background)
controller?.setPriority?.(inView ? priority : TASK_PRIORITIES.background)
);
}
}, [inView, entry]);

useEffect(
() => () =>
Object.values(controllers.current).forEach((controller) =>
controller?.abort()
),
Object.entries(controllers.current).forEach(([priority, controller]) => {
controller?.abort();
controllers.current[priority] = null;
}),
[]
);

const postTask = async (
task = Function.prototype,
{ detached = false, priority = defaultPriority, ...options } = {}
{ detached = false, priority = defaultPriority, throwOnAbort = false, ...options } = {}
) => {
try {
if (!window?.scheduler) return task();
Expand All @@ -63,13 +64,18 @@ const useScheduler = ({
priority: taskControllerPriority,
});
}

const signal = !detached
? controllers.current[taskPriority].signal
: undefined;
return window.scheduler.postTask(task, {
signal: !detached
? controllers.current[taskPriority].signal
: undefined,
signal,
priority: detached ? taskPriority : undefined,
...options,
}).catch((e) => {
if (signal?.aborted && !throwOnAbort)
return;

throw(e);
});
} catch (e) {
// eslint-disable-next-line no-console
Expand Down

0 comments on commit bbae633

Please sign in to comment.