Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
34 changes: 34 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: CI

on:
pull_request:
branches:
- main
- develop
- 'release/v*.*.*'
push:
branches:
- main
- develop
- 'release/v*.*.*'

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Install dependencies
run: npm ci

- name: Run checks
run: npm run verify

- name: Build project
run: npm run build
2 changes: 1 addition & 1 deletion .husky/pre-push
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
npm run fix
npm run format
npm run verify
npx lint-staged
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog

## v1.0.0
## v2.0.0 (March 4th, 2026)

- `useReusableAborter` **hook** - hook wrapper over the `ReusableAborter` class
- Increases the `saborter` version to 2 majors

## v1.0.0 (February 4th, 2026)

### New Features

Expand Down
Binary file modified assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions cspell.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"version": "0.2",
"language": "en,ru",
"words": ["Saborter", "saborter", "Laptev", "Vladislav", "tgz", "Сalls"],
"flagWords": [],
"ignorePaths": [
"node_modules/**",
"dist/**",
"build/**",
".git/**",
"coverage/**",
"*.log",
"package-lock.json",
"./**/*.test.ts",
"./cspell.json",
"./integrations"
],
"dictionaries": ["typescript", "node", "softwareTerms", "en_US", "ru_RU"],
"useGitignore": true,
"patterns": [
{
"name": "markdown-links",
"pattern": "\\(.*\\)",
"description": "Ignore URLs in Markdown"
},
{
"name": "html-entities",
"pattern": "&\\w+;",
"description": "Ignore HTML entities"
}
],
"ignoreRegExpList": ["/\\[.*\\]\\(.*\\)/g", "/&[a-z]+;/g", "/0x[a-fA-F0-9]+/g", "/\\$[^{][\\w.]+/g"],
"caseSensitive": false,
"allowCompoundWords": true,
"minWordLength": 3
}
10 changes: 5 additions & 5 deletions package-lock.json

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

16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@saborter/react",
"version": "1.0.0",
"version": "2.0.0",
"description": "A library for canceling asynchronous requests with React integration",
"type": "module",
"publishConfig": {
Expand Down Expand Up @@ -47,9 +47,9 @@
"verify:prettier": "npx prettier . --check",
"verify:eslint": "npx eslint \"./**/*.{js,jsx,ts,tsx}\" --max-warnings=0",
"verify": "npm run typecheck && npm run verify:prettier && npm run verify:eslint",
"fix:eslint": "npx eslint \"./**/*.{js,jsx,ts,tsx}\" --fix",
"fix:prettier": "npx prettier --write .",
"fix": "npm run fix:eslint && npm run fix:prettier",
"format:eslint": "npx eslint \"./**/*.{js,jsx,ts,tsx}\" --fix",
"format:prettier": "npx prettier --write .",
"format": "npm run format:eslint && npm run format:prettier",
"prepare": "husky"
},
"devDependencies": {
Expand All @@ -71,7 +71,7 @@
"prettier": "^3.7.4",
"react": "^17.0.2",
"rimraf": "^6.1.2",
"saborter": "^1.4.2",
"saborter": "^2.0.1",
"ts-node": "^10.9.2",
"typescript": "^5.9.3",
"vite": "^7.3.0",
Expand All @@ -80,14 +80,14 @@
},
"peerDependencies": {
"react": ">=17.0.0 <=20.0.0",
"saborter": "^1.4.2"
"saborter": "^2.0.1"
},
"lint-staged": {
"*.{js,jsx,ts,tsx}": [
"npm run fix:prettier"
"npm run format:prettier"
],
"*.{json,md}": [
"npm run fix:prettier"
"npm run format:prettier"
]
}
}
107 changes: 103 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
![Logo](./assets/logo.png)

[![Npm package](https://img.shields.io/npm/v/@saborter/react?color=red&label=npm%20package)](https://www.npmjs.com/package/@saborter/react)
[![License](https://img.shields.io/badge/license-MIT-blue)](./LICENSE)
[![Github](https://img.shields.io/badge/repository-github-color)](https://github.com/TENSIILE/@saborter/react)
<p align='center'>
<a href="https://www.npmjs.com/package/@saborter/react" alt="Npm package">
<img src="https://img.shields.io/npm/v/@saborter/react?color=red&label=npm%20package" /></a>
<a href="https://www.npmjs.com/package/@saborter/react" alt="Npm downloads">
<img src="https://img.shields.io/npm/dm/@saborter/react.svg" /></a>
<a href="https://github.com/TENSIILE/saborter-react/actions/workflows/publish.yml" alt="Release">
<img src="https://github.com/TENSIILE/saborter-react/actions/workflows/publish.yml/badge.svg" /></a>
<a href="https://github.com/TENSIILE/saborter-react/blob/develop/LICENSE" alt="License">
<img src="https://img.shields.io/badge/license-MIT-blue" /></a>
<a href="https://github.com/TENSIILE/saborter-react" alt="Github">
<img src="https://img.shields.io/badge/repository-github-color" /></a>
</p>

A library for canceling asynchronous requests that combines the `Saborter` library and `React`.

Expand Down Expand Up @@ -126,6 +135,63 @@ const { requestState } = useAborter();
console.log(requestState); // 'cancelled' / 'pending' / 'fulfilled' / 'rejected' / 'aborted'
```

### `useReusableAborter`

#### Props

```typescript
// The type can be found in `saborter/types`
const reusableAborter = new useReusableAborter(props?: ReusableAborterProps);
```

#### Props Parameters

| Parameter | Type | Description | Required |
| --------- | ---------------------- | ------------------------------------- | -------- |
| `props` | `ReusableAborterProps` | ReusableAborter configuration options | No |

**ReusableAborterProps:**

```typescript
{
/**
* Determines which listeners are carried over when the abort signal is reset.
* - If `true`, all listeners (both `onabort` and event listeners) are preserved.
* - If `false`, no listeners are preserved.
* - If an object, specific listener types can be enabled/disabled individually.
*/
attractListeners?: boolean | AttractListeners;
}
```

#### Properties

`signal: AbortSignal`

Returns the `AbortSignal` associated with the current controller.

```javascript
const reusableAborter = useReusableAborter();

// Using signal in the request
fetch('/api/data', {
signal: reusableAborter.signal
});
```

#### Methods

`abort(reason?): void`

**Parameters:**

- `reason?: any` - the reason for aborting the request.

Immediately cancels the currently executing request.

> [!NOTE]
> Can be called multiple times. Each call will restore the `signal`, and the `aborted` property will always be `false`.

## 🎯 Usage Examples

### Basic Usage
Expand Down Expand Up @@ -163,7 +229,7 @@ const Component = () => {
};
```

### The `AbortError` `initiator` changed while unmounting the component.
### The `AbortError` `initiator` changed while unmounting the component

```javascript
import { AbortError } from 'saborter';
Expand All @@ -184,6 +250,39 @@ const Component = () => {
};
```

### Using `useReusableAborter`

```typescript
const aborter = new useReusableAborter();

// Get the current signal
const signal = aborter.signal;

// Attach listeners
signal.addEventListener('abort', () => console.log('Listener 1'));
signal.addEventListener('abort', () => console.log('Listener 2'), { once: true }); // won't be recovered

// Set onabort handler
signal.onabort = () => console.log('Onabort handler');

// First abort
aborter.abort('First reason');
// Output:
// Listener 1
// Listener 2 (once)
// Onabort handler

// The signal is now a fresh one, but the non‑once listeners and onabort are reattached
signal.addEventListener('abort', () => console.log('Listener 3')); // new listener, will survive next abort

// Second abort
aborter.abort('Second reason');
// Output:
// Listener 1
// Onabort handler
// Listener 3
```

## 📋 License

MIT License - see [LICENSE](./LICENSE) for details.
1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './use-aborter';
export * from './use-reusable-aborter';
9 changes: 6 additions & 3 deletions src/hooks/use-aborter/use-aborter.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { useRef, useEffect, useState } from 'react';
import { Aborter, RequestState, AbortError } from 'saborter';
import { Aborter } from 'saborter';
import { AbortError } from 'saborter/errors';
import { RequestState } from 'saborter/types';
import { dispose as disposeFn } from 'saborter/lib';
import * as Shared from '../../shared';
import * as Constants from './use-aborter.constants';
import * as Types from './use-aborter.types';

export const useAborter = (props: Types.UseAborterProps = {}): Types.UseAborterResult => {
const { onAbort, onStateChange, dispose = true } = props;

const aborterRef = useRef(new Aborter({ onAbort, onStateChange }));
const aborterRef = useRef(new Aborter({ onAbort: onAbort as any, onStateChange }));
const [requestState, setRequestState] = useState<RequestState | null>(null);

const isDisposeEnabledRef = Shared.Hooks.useMutableRef(dispose);
Expand All @@ -27,7 +30,7 @@ export const useAborter = (props: Types.UseAborterProps = {}): Types.UseAborterR
})
);
if (isDisposeEnabledCurrent) {
currentAborter.dispose();
disposeFn(currentAborter);
}
};
}, [isDisposeEnabledRef]);
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/use-aborter/use-aborter.types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Aborter, OnAbortCallback, OnStateChangeCallback, RequestState, AbortInitiator as Initiator } from 'saborter';
import { Aborter } from 'saborter';
import { OnAbortCallback, OnStateChangeCallback, RequestState, AbortInitiator as Initiator } from 'saborter/types';
import { ABORTABLE_UNMOUNTED_INITIATOR } from './use-aborter.constants';

export interface UseAborterResult {
Expand Down
1 change: 1 addition & 0 deletions src/hooks/use-reusable-aborter/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './use-reusable-aborter';
9 changes: 9 additions & 0 deletions src/hooks/use-reusable-aborter/use-reusable-aborter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useRef } from 'react';
import { ReusableAborter } from 'saborter';
import { ReusableAborterProps } from 'saborter/types';

export const useReusableAborter = (props: ReusableAborterProps = {}) => {
const reusableAborter = useRef(new ReusableAborter(props));

return reusableAborter.current;
};