{"name":"event-target-shim","version":"5.0.0-beta.0","description":"An implementation of WHATWG EventTarget interface.","main":"dist/event-target-shim","types":"index.d.ts","engines":{"node":">=6"},"scripts":{"preversion":"npm test","version":"npm run build && git add dist/*","postversion":"git push && git push --tags","clean":"rimraf .nyc_output coverage","coverage":"nyc report --reporter lcov && opener coverage/lcov-report/index.html","lint":"eslint src test scripts --ext .js,.mjs","build":"rollup -c scripts/rollup.config.js","pretest":"npm run lint","test":"run-s test:*","test:mocha":"nyc --require ./scripts/babel-register mocha test/*.mjs","test:karma":"karma start scripts/karma.conf.js --single-run","watch":"run-p watch:*","watch:mocha":"mocha test/*.mjs --require ./scripts/babel-register --watch --watch-extensions js,mjs --growl","watch:karma":"karma start scripts/karma.conf.js --watch","codecov":"codecov"},"devDependencies":{"@babel/core":"^7.1.6","@babel/plugin-transform-modules-commonjs":"^7.2.0","@babel/preset-env":"^7.1.6","@babel/register":"^7.0.0","@mysticatea/eslint-plugin":"^7.0.0","@mysticatea/spy":"^0.1.2","assert":"^1.4.1","codecov":"^3.1.0","eslint":"^5.9.0","karma":"^3.1.1","karma-chrome-launcher":"^2.2.0","karma-coverage":"^1.1.2","karma-firefox-launcher":"^1.0.0","karma-growl-reporter":"^1.0.0","karma-ie-launcher":"^1.0.0","karma-mocha":"^1.3.0","karma-rollup-preprocessor":"^6.1.1","mocha":"^5.2.0","npm-run-all":"^4.1.5","nyc":"^13.1.0","opener":"^1.5.1","rimraf":"^2.6.2","rollup":"^0.66.6","rollup-plugin-babel":"^4.0.3","rollup-plugin-babel-minify":"^6.2.0","rollup-plugin-commonjs":"^9.2.0","rollup-plugin-json":"^3.1.0","rollup-plugin-node-resolve":"^3.4.0","rollup-watch":"^4.3.1","typescript":"~3.2.1"},"repository":{"type":"git","url":"git+https://github.com/mysticatea/event-target-shim.git"},"keywords":["w3c","whatwg","eventtarget","event","events","shim"],"author":{"name":"Toru Nagashima"},"license":"MIT","bugs":{"url":"https://github.com/mysticatea/event-target-shim/issues"},"homepage":"https://github.com/mysticatea/event-target-shim","readme":"# event-target-shim\n\n[![npm version](https://img.shields.io/npm/v/event-target-shim.svg)](https://www.npmjs.com/package/event-target-shim)\n[![Downloads/month](https://img.shields.io/npm/dm/event-target-shim.svg)](http://www.npmtrends.com/event-target-shim)\n[![Build Status](https://travis-ci.org/mysticatea/event-target-shim.svg?branch=master)](https://travis-ci.org/mysticatea/event-target-shim)\n[![Coverage Status](https://codecov.io/gh/mysticatea/event-target-shim/branch/master/graph/badge.svg)](https://codecov.io/gh/mysticatea/event-target-shim)\n[![Dependency Status](https://david-dm.org/mysticatea/event-target-shim.svg)](https://david-dm.org/mysticatea/event-target-shim)\n\nAn implementation of [WHATWG EventTarget interface](https://dom.spec.whatwg.org/#interface-eventtarget), plus few extensions.\n\n- This provides `EventTarget` constructor that can inherit for your custom object.\n- This provides an utility that defines properties of attribute listeners (e.g. `obj.onclick`).\n\n```js\nimport {EventTarget, defineEventAttribute} from \"event-target-shim\"\n\nclass Foo extends EventTarget {\n    // ...\n}\n\n// Define `foo.onhello` property.\ndefineEventAttribute(Foo.prototype, \"hello\")\n\n// Use\nconst foo = new Foo()\nfoo.addEventListener(\"hello\", e => console.log(\"hello\", e))\nfoo.onhello = e => console.log(\"onhello:\", e)\nfoo.dispatchEvent(new CustomEvent(\"hello\"))\n```\n\n## 💿 Installation\n\nUse [npm](https://www.npmjs.com/) to install then use a bundler.\n\n```\nnpm install event-target-shim\n```\n\nOr download from [`dist` directory](./dist).\n\n- [dist/event-target-shim.mjs](dist/event-target-shim.mjs) ... ES modules version.\n- [dist/event-target-shim.js](dist/event-target-shim.js) ... Common JS version.\n- [dist/event-target-shim.umd.js](dist/event-target-shim.umd.js) ... UMD (Universal Module Definition) version. This is transpiled by [Babel](https://babeljs.io/) for IE 11.\n\n## 📖 Usage\n\n```js\nimport {EventTarget, defineEventAttribute} from \"event-target-shim\"\n// or\nconst {EventTarget, defineEventAttribute} = require(\"event-target-shim\")\n\n// or UMD version defines a global variable:\nconst {EventTarget, defineEventAttribute} = window.EventTargetShim\n```\n\n### EventTarget\n\n> https://dom.spec.whatwg.org/#interface-eventtarget\n\n#### eventTarget.addEventListener(type, callback, options)\n\nRegister an event listener.\n\n- `type` is a string. This is the event name to register.\n- `callback` is a function. This is the event listener to register.\n- `options` is a boolean or an object `{ capture?: boolean, passive?: boolean, once?: boolean }`. If this is a boolean, it's same meaning as `{ capture: options }`.\n    - `capture` is the flag to register the event listener for capture phase.\n    - `passive` is the flag to ignore `event.preventDefault()` method in the event listener.\n    - `once` is the flag to remove the event listener automatically after the first call.\n\n#### eventTarget.removeEventListener(type, callback, options)\n\nUnregister an event listener.\n\n- `type` is a string. This is the event name to unregister.\n- `callback` is a function. This is the event listener to unregister.\n- `options` is a boolean or an object `{ capture?: boolean }`. If this is a boolean, it's same meaning as `{ capture: options }`.\n    - `capture` is the flag to register the event listener for capture phase.\n\n#### eventTarget.dispatchEvent(event)\n\nDispatch an event.\n\n- `event` is a [Event](https://dom.spec.whatwg.org/#event) object or an object `{ type: string, [key: string]: any }`. The latter is non-standard but useful. In both cases, listeners receive the event as implementing [Event](https://dom.spec.whatwg.org/#event) interface.\n\n### defineEventAttribute(proto, type)\n\nDefine an event attribute (e.g. `onclick`) to `proto`. This is non-standard.\n\n- `proto` is an object (assuming it's a prototype object). This function defines a getter/setter pair for the event attribute.\n- `type` is a string. This is the event name to define.\n\nFor example:\n\n```js\nclass AbortSignal extends EventTarget {\n    constructor() {\n        this.aborted = false\n    }\n}\n// Define `onabort` property.\ndefineEventAttribute(AbortSignal.prototype, \"abort\")\n```\n\n### EventTarget(types)\n\nDefine a custom `EventTarget` class with event attributes. This is non-standard.\n\n- `types` is a string or an array of strings. This is the event name to define.\n\nFor example:\n\n```js\n// This has `onabort` property.\nclass AbortSignal extends EventTarget(\"abort\") {\n    constructor() {\n        this.aborted = false\n    }\n}\n```\n\n## 📚 Examples\n\n### ES2015 and later\n\n> https://jsfiddle.net/636vea92/\n\n```js\nconst {EventTarget, defineEventAttribute} = EventTargetShim\n\n// Define a derived class.\nclass Foo extends EventTarget {\n    // ...\n}\n\n// Define `foo.onhello` property.\ndefineEventAttribute(Foo.prototype, \"hello\")\n\n// Register event listeners.\nconst foo = new Foo()\nfoo.addEventListener(\"hello\", (e) => {\n    console.log(\"hello\", e)\n})\nfoo.onhello = (e) => {\n    console.log(\"onhello\", e)\n}\n\n// Dispatching events\nfoo.dispatchEvent(new CustomEvent(\"hello\", { detail: \"detail\" }))\n```\n\n### Typescript\n\n```ts\nimport { EventTarget, defineEventAttribute } from \"event-target-shim\";\n\n// Define events\ntype FooEvents = {\n    hello: CustomEvent\n}\ntype FooEventAttributes = {\n    onhello: CustomEvent\n}\n\n// Define a derived class.\nclass Foo extends EventTarget<FooEvents, FooEventAttributes> {\n    // ...\n}\n// Define `foo.onhello` property's implementation.\ndefineEventAttribute(Foo.prototype, \"hello\")\n\n// Register event listeners.\nconst foo = new Foo()\nfoo.addEventListener(\"hello\", (e) => {\n    console.log(\"hello\", e.detail)\n})\nfoo.onhello = (e) => {\n    console.log(\"onhello\", e.detail)\n}\n\n// Dispatching events\nfoo.dispatchEvent(new CustomEvent(\"hello\", { detail: \"detail\" }))\n```\n\nUnfortunately, both `FooEvents` and `FooEventAttributes` are needed because TypeScript doesn't allow the mutation of string literal types. If TypeScript allowed us to compute `\"onhello\"` from `\"hello\"` in types, `FooEventAttributes` will be optional.\n\nThis `EventTarget` type is compatible with `EventTarget` interface of `lib.dom.d.ts`.\n\n#### To disallow unknown events\n\nBy default, methods such as `addEventListener` accept unknown events. You can disallow unknown events by the third type parameter `\"strict\"`.\n\n```ts\ntype FooEvents = {\n    hello: CustomEvent\n}\nclass Foo extends EventTarget<FooEvents, {}, \"strict\"> {\n    // ...\n}\n\n// OK because `hello` is defined in FooEvents.\nfoo.addEventListener(\"hello\", (e) => {\n})\n// Error because `unknown` is not defined in FooEvents.\nfoo.addEventListener(\"unknown\", (e) => {\n})\n```\n\nHowever, if you use `\"strict\"` parameter, it loses compatibility with `EventTarget` interface of `lib.dom.d.ts`.\n\n#### To infer the type of `dispatchEvent()` method\n\nTypeScript cannot infer the event type of `dispatchEvent()` method properly from the argument in most cases. You can improve this behavior with the following steps:\n\n1. Use the third type parameter `\"strict\"`. This prevents inferring to `dispatchEvent<string>()`.\n2. Make the `type` property of event definitions stricter.\n\n```ts\ntype FooEvents = {\n    hello: CustomEvent & { type: \"hello\" }\n    hey: Event & { type: \"hey\" }\n}\nclass Foo extends EventTarget<FooEvents, {}, \"strict\"> {\n    // ...\n}\n\n// Error because `detail` property is lacking.\nfoo.dispatchEvent({ type: \"hello\" })\n```\n\n### ES5\n\n> https://jsfiddle.net/522zc9de/\n\n```js\n// Define a derived class.\nfunction Foo() {\n    EventTarget.call(this)\n}\nFoo.prototype = Object.create(EventTarget.prototype, {\n    constructor: { value: Foo, configurable: true, writable: true }\n    // ...\n})\n\n// Define `foo.onhello` property.\ndefineEventAttribute(Foo.prototype, \"hello\")\n\n// Register event listeners.\nvar foo = new Foo()\nfoo.addEventListener(\"hello\", function(e) {\n    console.log(\"hello\", e)\n})\nfoo.onhello = function(e) {\n    console.log(\"onhello\", e)\n}\n\n// Dispatching events\nfunction isSupportEventConstrucor() { // IE does not support.\n    try {\n        new CusomEvent(\"hello\")\n        return true\n    } catch (_err) {\n        return false\n    }\n}\nif (isSupportEventConstrucor()) {\n    foo.dispatchEvent(new CustomEvent(\"hello\", { detail: \"detail\" }))\n} else {\n    var e = document.createEvent(\"CustomEvent\")\n    e.initCustomEvent(\"hello\", false, false, \"detail\")\n    foo.dispatchEvent(e)\n}\n```\n\n## 📰 Changelog\n\n- See [GitHub releases](https://github.com/mysticatea/event-target-shim/releases).\n\n## 🍻 Contributing\n\nContributing is welcome ❤️\n\nPlease use GitHub issues/PRs.\n\n### Development tools\n\n- `npm install` installs dependencies for development.\n- `npm test` runs tests and measures code coverage.\n- `npm run clean` removes temporary files of tests.\n- `npm run coverage` opens code coverage of the previous test with your default browser.\n- `npm run lint` runs ESLint.\n- `npm run build` generates `dist` codes.\n- `npm run watch` runs tests on each file change.\n","readmeFilename":"README.md","gitHead":"0b85ee9d3fa48e4ca1a0abdc3696d7bbd419d73c","_id":"event-target-shim@5.0.0-beta.0","_npmVersion":"6.5.0","_nodeVersion":"11.1.0","_npmUser":{"name":"mysticatea","email":"star.ctor@gmail.com"},"dist":{"integrity":"sha512-0pQ5ZGKLtUbUAEN6fPTHkZVi9HXKOut0RbUJDdf5/mYGfmEjFiJybnjMh73gtQghbfbacbS/nk9EMo/QtDzjKw==","shasum":"00f535458f32b46c990cfd03c0c33ace2006b58d","tarball":"http://registry-npm.ywwl.com/event-target-shim/download/event-target-shim-5.0.0-beta.0.tgz","fileCount":10,"unpackedSize":189344,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJcQch3CRA9TVsSAnZWagAAmTcP/AzoBFdFC8fIVQfc003b\n5DzMUspkAxxTzkWj+8E/tFIugl50cm11ZFDm0whawMYCr28Vkq+Jex0ujI4h\n4OycFwNQtX6TbdNMZrK8R11+i1rMGE1mSj9yyM6ehcbmeg8QEfH2OPNMIJFm\n0yFOtEbsX36eiju2/OdfEziJ4PHA0UAc9mj0q7p96eaA9qkzh7Cp5SnRnal5\nQ5cKktCGuqHBTzJ3qPm7wprp/j3qfnQT/clUg6//dazuOK3cKNplBPjDYz+/\nJrb2ptqk/W5Yi3SDyAc0tqZQQLEGqu5N87LZwl8fnL4L4wH2X/awrbWBQYO+\n4Mv9mNqhfuh7S49pGOPVxTLd5eyYKi9RRopqI1yI3EvLL02LvrkHmEpOplid\ncVmcHUooV4mWoi1r/kv6u2H9CjAscob8dQ3/67PmeptYUabXfwulmiN7MZwn\nxmIt9uGD8QL8/mg8h7ClqSJgEmyt/jGAkSIeJOhItQtw/xlXxVU/0fNJYAhW\nj/peqMCi8FkqbctrgtOwjNd8Ydz4cKSC/E1ip8GXo1LiDLbYUL1liU5rtJvk\nihsQdEy2E2WOhM9NhFoBQSW01DJaqRSCXHZOXkdEJKy1QtLHC8k1VWBNZJWI\nHnXlf/gViJmhtrFCk0Fy9JoETfTkXbBSkF9w1A2Y7qVVJg/c+zxOJXqJuRDP\nh1w0\r\n=Dj/L\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIBBN8n7J1cK41nkw0aJIC+t3gxRM4GaK54OKmui/9bZsAiAzKR7xYErzcl7do9HPavPWqhePWyCuNXJ4Ily5T/QdVQ=="}],"size":37231,"noattachment":false,"key":"/event-target-shim/-/event-target-shim-5.0.0-beta.0.tgz"},"maintainers":[{"name":"mysticatea","email":"star.ctor@gmail.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/event-target-shim_5.0.0-beta.0_1547815030429_0.7861757433505683"},"_hasShrinkwrap":false,"publish_time":1547815030557,"_cnpm_publish_time":1547815030557,"dist-tags":{"latest":"6.0.2"}}