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
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
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.

92 changes: 91 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,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 @@ -172,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 @@ -193,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';
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;
};