|
| 1 | +# event-target-shim |
| 2 | + |
| 3 | +[](https://www.npmjs.com/package/event-target-shim) |
| 4 | +[](http://www.npmtrends.com/event-target-shim) |
| 5 | +[](https://github.com/mysticatea/event-target-shim/actions) |
| 6 | +[](https://codecov.io/gh/mysticatea/event-target-shim) |
| 7 | +[](https://david-dm.org/mysticatea/event-target-shim) |
| 8 | + |
| 9 | +An implementation of [WHATWG `EventTarget` interface](https://dom.spec.whatwg.org/#interface-eventtarget) and [WHATWG `Event` interface](https://dom.spec.whatwg.org/#interface-event). This implementation supports constructor, `passive`, `once`, and `signal`. |
| 10 | + |
| 11 | +This implementation is designed ... |
| 12 | + |
| 13 | +- Working fine on both browsers and Node.js. |
| 14 | +- TypeScript friendly. |
| 15 | + |
| 16 | +## 💿 Installation |
| 17 | + |
| 18 | +Use [npm](https://www.npmjs.com/) or a compatible tool. |
| 19 | + |
| 20 | +``` |
| 21 | +npm install event-target-shim |
| 22 | +``` |
| 23 | + |
| 24 | +## 📖 Getting started |
| 25 | + |
| 26 | +```js |
| 27 | +import { EventTarget, Event } from "event-target-shim"; |
| 28 | + |
| 29 | +// constructor (was added to the standard on 8 Jul 2017) |
| 30 | +const myNode = new EventTarget(); |
| 31 | + |
| 32 | +// passive flag (was added to the standard on 6 Jan 2016) |
| 33 | +myNode.addEventListener( |
| 34 | + "hello", |
| 35 | + (e) => { |
| 36 | + e.preventDefault(); // ignored and print warning on console. |
| 37 | + }, |
| 38 | + { passive: true } |
| 39 | +); |
| 40 | + |
| 41 | +// once flag (was added to the standard on 15 Apr 2016) |
| 42 | +myNode.addEventListener("hello", listener, { once: true }); |
| 43 | +myNode.dispatchEvent(new Event("hello")); // remove the listener after call. |
| 44 | + |
| 45 | +// signal (was added to the standard on 4 Dec 2020) |
| 46 | +const ac = new AbortController(); |
| 47 | +myNode.addEventListener("hello", listener, { signal: ac.signal }); |
| 48 | +ac.abort(); // remove the listener. |
| 49 | +``` |
| 50 | + |
| 51 | +- For browsers, use a bundler such as [Webpack](https://webpack.js.org/) to bundle. |
| 52 | + - If you want to support IE11, use `import {} from "event-target-shim/es5"` instead. It's a transpiled code by babel. It depends on `@baebl/runtime` (`^7.12.0`) package. |
| 53 | +- The `AbortController` class was added to the standard on 14 Jul 2017. If you want the shim of that, use [abort-controller](https://www.npmjs.com/package/abort-controller) package. |
| 54 | + |
| 55 | +## 📚 API Reference |
| 56 | + |
| 57 | +See [docs/reference.md](docs/reference.md). |
| 58 | + |
| 59 | +## 💥 Migrating to v6 |
| 60 | + |
| 61 | +See [docs/migrating-to-v6.md](docs/migrating-to-v6.md). |
| 62 | + |
| 63 | +## 📰 Changelog |
| 64 | + |
| 65 | +See [GitHub releases](https://github.com/mysticatea/event-target-shim/releases). |
| 66 | + |
| 67 | +## 🍻 Contributing |
| 68 | + |
| 69 | +Contributing is welcome ❤️ |
| 70 | + |
| 71 | +Please use GitHub issues/PRs. |
| 72 | + |
| 73 | +### Development tools |
| 74 | + |
| 75 | +- `npm install` installs dependencies for development. |
| 76 | +- `npm test` runs tests and measures code coverage. |
| 77 | +- `npm run watch:mocha` runs tests on each file change. |
0 commit comments