feat: add abort/cancel support for confirmations #130
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
確認ダイアログを外部から中止(abort)できる機能を追加しました。
abort(promise)で個別のPromiseをキャンセルabortAll()で全ての未解決PromiseをキャンセルKey Changes
新規ファイル
src/controls.ts: abort機能の実装AbortErrorクラスabort(),abortAll(),attachAbortSignal()関数__tests__/abort.test.ts: 7つのテストケース変更ファイル
src/types.ts:ConfirmationOptions型を追加src/createConfirmation.ts: Promiseレジストリへの登録とAbortSignal対応src/index.ts:abort/abortAllをexportREADME.md: 使用例とエラーハンドリングの説明を追加Usage
AbortController経由(推奨)
```typescript
import { confirm } from './confirm';
const handleOperation = async (): Promise => {
const abortController = new AbortController();
const resultPromise = confirm(
{ message: 'Continue?' },
{ signal: abortController.signal }
);
// 後でキャンセル可能
abortController.abort();
try {
const result = await resultPromise;
} catch (error) {
if (error instanceof Error && error.name === 'AbortError') {
console.log('Dialog was aborted');
}
}
};
```
ユーティリティ関数
```typescript
import { confirm, abort, abortAll } from 'react-confirm';
const p1 = confirm({ message: 'Delete item 1?' });
const p2 = confirm({ message: 'Delete item 2?' });
abort(p1); // 個別にキャンセル
abortAll(); // 全てキャンセル
```
Testing
テストを実行:
```bash
npm test
npm run typecheck
npm run build
```
Compatibility
createConfirmationの第2引数はオプショナルAbortError(name === 'AbortError')でrejectPR #127からの改善点
controlからoptionsに変更(より一般的な命名)AbortErrorクラスを定義(型安全性の向上)🤖 Generated with Claude Code