1 | # abort-controller
|
2 |
|
3 | [![npm version](https://img.shields.io/npm/v/abort-controller.svg)](https://www.npmjs.com/package/abort-controller)
|
4 | [![Downloads/month](https://img.shields.io/npm/dm/abort-controller.svg)](http://www.npmtrends.com/abort-controller)
|
5 | [![Build Status](https://travis-ci.org/mysticatea/abort-controller.svg?branch=master)](https://travis-ci.org/mysticatea/abort-controller)
|
6 | [![Coverage Status](https://codecov.io/gh/mysticatea/abort-controller/branch/master/graph/badge.svg)](https://codecov.io/gh/mysticatea/abort-controller)
|
7 | [![Dependency Status](https://david-dm.org/mysticatea/abort-controller.svg)](https://david-dm.org/mysticatea/abort-controller)
|
8 |
|
9 | An implementation of [WHATWG AbortController interface](https://dom.spec.whatwg.org/#interface-abortcontroller).
|
10 |
|
11 | ```js
|
12 | import AbortController from "abort-controller"
|
13 |
|
14 | const controller = new AbortController()
|
15 | const signal = controller.signal
|
16 |
|
17 | signal.addEventListener("abort", () => {
|
18 | console.log("aborted!")
|
19 | })
|
20 |
|
21 | controller.abort()
|
22 | ```
|
23 |
|
24 | > https://jsfiddle.net/1r2994qp/1/
|
25 |
|
26 | ## 💿 Installation
|
27 |
|
28 | Use [npm](https://www.npmjs.com/) to install then use a bundler.
|
29 |
|
30 | ```
|
31 | npm install abort-controller
|
32 | ```
|
33 |
|
34 | Or download from [`dist` directory](./dist).
|
35 |
|
36 | - [dist/abort-controller.mjs](dist/abort-controller.mjs) ... ES modules version.
|
37 | - [dist/abort-controller.js](dist/abort-controller.js) ... Common JS version.
|
38 | - [dist/abort-controller.umd.js](dist/abort-controller.umd.js) ... UMD (Universal Module Definition) version. This is transpiled by [Babel](https://babeljs.io/) for IE 11.
|
39 |
|
40 | ## 📖 Usage
|
41 |
|
42 | ### Basic
|
43 |
|
44 | ```js
|
45 | import AbortController from "abort-controller"
|
46 | // or
|
47 | const AbortController = require("abort-controller")
|
48 |
|
49 | // or UMD version defines a global variable:
|
50 | const AbortController = window.AbortControllerShim
|
51 | ```
|
52 |
|
53 | If your bundler recognizes `browser` field of `package.json`, the imported `AbortController` is the native one and it doesn't contain shim (even if the native implementation was nothing).
|
54 | If you wanted to polyfill `AbortController` for IE, use `abort-controller/polyfill`.
|
55 |
|
56 | ### Polyfilling
|
57 |
|
58 | Importing `abort-controller/polyfill` assigns the `AbortController` shim to the `AbortController` global variable if the native implementation was nothing.
|
59 |
|
60 | ```js
|
61 | import "abort-controller/polyfill"
|
62 | // or
|
63 | require("abort-controller/polyfill")
|
64 | ```
|
65 |
|
66 | ### API
|
67 |
|
68 | #### AbortController
|
69 |
|
70 | > https://dom.spec.whatwg.org/#interface-abortcontroller
|
71 |
|
72 | ##### controller.signal
|
73 |
|
74 | The [AbortSignal](https://dom.spec.whatwg.org/#interface-AbortSignal) object which is associated to this controller.
|
75 |
|
76 | ##### controller.abort()
|
77 |
|
78 | Notify `abort` event to listeners that the `signal` has.
|
79 |
|
80 | ## 📰 Changelog
|
81 |
|
82 | - See [GitHub releases](https://github.com/mysticatea/abort-controller/releases).
|
83 |
|
84 | ## 🍻 Contributing
|
85 |
|
86 | Contributing is welcome ❤️
|
87 |
|
88 | Please use GitHub issues/PRs.
|
89 |
|
90 | ### Development tools
|
91 |
|
92 | - `npm install` installs dependencies for development.
|
93 | - `npm test` runs tests and measures code coverage.
|
94 | - `npm run clean` removes temporary files of tests.
|
95 | - `npm run coverage` opens code coverage of the previous test with your default browser.
|
96 | - `npm run lint` runs ESLint.
|
97 | - `npm run build` generates `dist` codes.
|
98 | - `npm run watch` runs tests on each file change.
|