Skip to content

Commit bf9822f

Browse files
authored
Merge pull request #82 from TENSIILE/release/v2.4.0
release/v2.4.0
2 parents ab4bdc4 + 0996363 commit bf9822f

31 files changed

Lines changed: 766 additions & 114 deletions

‎CHANGELOG.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Saborter Changelog
22

3+
## v2.4.0 (April 23th, 2026)
4+
5+
### New Features
6+
7+
- Added `isTimeoutError` function to determine whether the given error is a timeout error [#80](https://github.com/TENSIILE/saborter/pull/82/changes/9adbc0237df3944b21cb60488105b0238d36d10d)
8+
- Added `Provision API` that injects the `Aborter` context into the `Http API` [#81](https://github.com/TENSIILE/saborter/pull/82/changes/7b9a6ce4f8050aceb6c3e1c13d93a42c41b9a497)
9+
310
## v2.3.0 (April 15th, 2026)
411

512
### New Features

‎docs/libs.md‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,37 @@ try {
115115
}
116116
```
117117

118+
### `isTimeoutError`
119+
120+
This function determines whether a given error represents a timeout‑induced abort error. It checks that the error is an `AbortError`, its initiator property is exactly `'timeout'`, and its `cause` is an instance of `TimeoutError`. This is useful for distinguishing timeouts from other abort reasons (e.g., user cancellation or system aborts).
121+
122+
**Parameters:**
123+
124+
- `error: any` - The error object to inspect.
125+
126+
**Returns:**
127+
128+
`error is TimeoutError`
129+
130+
- Returns `true` if the error is an `AbortError` with `initiator === 'timeout'` and `cause instanceof TimeoutError`.
131+
- Returns `false` otherwise.
132+
133+
**Example:**
134+
135+
```typescript
136+
try {
137+
await abortableRequest();
138+
} catch (error) {
139+
if (isTimeoutError(error)) {
140+
console.log('Request timed out');
141+
// Handle timeout gracefully
142+
} else {
143+
// Re‑throw other errors
144+
throw error;
145+
}
146+
}
147+
```
148+
118149
### `debounce`
119150

120151
Creates a debounced function that delays invoking the provided handler until after a specified timeout has elapsed since the last call. This is a leading‑edge debounce – the first call in a burst schedules the execution, and subsequent calls reset the timer. The debounced function accepts an `AbortSignal` and returns a promise that resolves with the handler's result or rejects if the handler throws or the signal is aborted.
@@ -126,7 +157,7 @@ Creates a debounced function that delays invoking the provided handler until aft
126157

127158
**Returns:**
128159

129-
`(signal: AbortSignal) => R | Promise<R>`
160+
`(signal: AbortSignal) => Promise<R>`
130161

131162
A function that accepts an `AbortSignal` and returns a `Promise<R>`, where `R` is the return type of the handler.
132163

@@ -210,7 +241,7 @@ Schedules the execution of a handler after a specified delay. The operation can
210241

211242
**Parameters:**
212243

213-
- `handler: ((signal: AbortSignal) => T | Promise<T>)`:
244+
- `handler: ((signal: AbortSignal, requestOptions: AbortableRequestOptions) => T | Promise<T>)`:
214245
- A function that accepts an `AbortSignal` and returns a value or a `Promise`. This function will be called with the signal to allow cleanup on abort.
215246
- `delay?: number` - The time in milliseconds to wait before executing the handler. If omitted, the handler is scheduled without a delay (i.e., as soon as possible).
216247
- `options?: Object`:

‎integrations/project/package-lock.json‎

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎integrations/project/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"dependencies": {
1313
"react": "^19.2.0",
1414
"react-dom": "^19.2.0",
15-
"saborter": "file:../../unreleased-packages/saborter-2.3.0.tgz"
15+
"saborter": "file:../../unreleased-packages/saborter-2.4.0.tgz"
1616
},
1717
"devDependencies": {
1818
"@eslint/js": "^9.39.1",

‎integrations/project/src/app.component.tsx‎

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,21 @@ interface User {
66
name: string;
77
}
88

9-
const getUsers = async (signal: AbortSignal) => {
10-
const response = await fetch('https://jsonplaceholder.typicode.com/users', { signal });
9+
interface Post {
10+
id: number;
11+
title: string;
12+
}
1113

12-
return (await response.json()) as { data: User[] };
14+
const getUsers = async () => {
15+
const response = await fetch('https://jsonplaceholder.typicode.com/users');
16+
17+
return (await response.json()) as User[];
18+
};
19+
20+
const getPosts = async () => {
21+
const response = await fetch('https://jsonplaceholder.typicode.com/posts');
22+
23+
return (await response.json()) as Post[];
1324
};
1425

1526
const useAborter = <D,>() => {
@@ -21,8 +32,6 @@ const useAborter = <D,>() => {
2132
() =>
2233
new Aborter({
2334
onInit: (aborter) => {
24-
console.log('state:', aborter.listeners.state.value);
25-
2635
aborter.listeners.addEventListener('fulfilled', (d) => {
2736
setData(d);
2837
});
@@ -31,7 +40,7 @@ const useAborter = <D,>() => {
3140
setError(er);
3241
});
3342

34-
aborter.listeners.state.subscribe((s) => {
43+
aborter.listeners.state.subscribe((s: any) => {
3544
setLoading(s === 'pending');
3645
setError(null);
3746
setData(null);
@@ -44,30 +53,45 @@ const useAborter = <D,>() => {
4453
};
4554

4655
export const App = () => {
47-
const { aborter, data, error, loading } = useAborter<User[]>();
56+
const { aborter: usersAborter, data: users, error, loading } = useAborter<User[]>();
57+
const { aborter: postsAborter, data: posts, loading: postsLoading } = useAborter<Post[]>();
4858

49-
const handleLoad = async () => {
50-
aborter.try(getUsers);
59+
const handlePostsLoad = async () => {
60+
postsAborter.try(getPosts, { timeout: 1000 });
5161
};
5262

53-
const handleCancel = () => aborter.abort();
63+
const handleUsersLoad = async () => {
64+
usersAborter.try(getUsers);
65+
handlePostsLoad();
66+
};
67+
68+
const handleCancel = () => {
69+
postsAborter.abort();
70+
usersAborter.abort();
71+
};
5472

5573
return (
5674
<>
5775
<h1>Saborter Check</h1>
5876
<div className='card'>
5977
<div className='gap-2'>
60-
<button onClick={handleLoad}>Load</button>
78+
<button onClick={handleUsersLoad}>Load</button>
6179
<button onClick={handleCancel}>Cancel</button>
6280
</div>
6381

64-
{loading && <p>Loading...</p>}
82+
{(loading || postsLoading) && <p>Loading...</p>}
6583
{error && <p>{String(error)}</p>}
6684
<ul>
67-
{data?.map((user) => {
85+
{users?.map((user) => {
6886
return <li key={user.id}>{user.name}</li>;
6987
})}
7088
</ul>
89+
<hr />
90+
<ul>
91+
{posts?.map((post) => {
92+
return <li key={post.id}>{post.title}</li>;
93+
})}
94+
</ul>
7195
</div>
7296
</>
7397
);

‎jest.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const config = {
1010
'^.+\\.tsx?$': 'ts-jest',
1111
'^.+\\.ts?$': 'ts-jest'
1212
},
13+
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
1314
transformIgnorePatterns: ['node_modules'],
1415
testPathIgnorePatterns: ['/node_modules/', '/dist/'],
1516
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$',

‎jest.setup.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('whatwg-fetch');

‎package-lock.json‎

Lines changed: 11 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "saborter",
3-
"version": "2.3.0",
3+
"version": "2.4.0",
44
"description": "A simple and efficient library for canceling asynchronous requests using AbortController",
55
"main": "dist/index.cjs.js",
66
"module": "dist/index.es.js",
@@ -110,7 +110,8 @@
110110
"vite": "^7.3.0",
111111
"vite-plugin-circular-dependency": "^0.5.0",
112112
"vite-plugin-clean": "^2.0.1",
113-
"vite-plugin-dts": "^4.5.4"
113+
"vite-plugin-dts": "^4.5.4",
114+
"whatwg-fetch": "^3.6.20"
114115
},
115116
"lint-staged": {
116117
"*.{js,jsx,ts,tsx}": [

0 commit comments

Comments
 (0)