{"id":17002512,"url":"https://github.com/thednp/event-listener","last_synced_at":"2025-12-26T08:36:05.415Z","repository":{"id":57231224,"uuid":"331888388","full_name":"thednp/event-listener","owner":"thednp","description":"🚅 Modern event listener for efficient applications based on the subscribe-publish pattern.","archived":false,"fork":false,"pushed_at":"2025-05-28T12:11:10.000Z","size":901,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-10-12T04:25:06.602Z","etag":null,"topics":["event-listener","events","javascript","javascript-events","typescript"],"latest_commit_sha":null,"homepage":"https://thednp.github.io/event-listener","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thednp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":["thednp"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"lfx_crowdfunding":null,"polar":null,"buy_me_a_coffee":null,"thanks_dev":null,"custom":null}},"created_at":"2021-01-22T08:48:56.000Z","updated_at":"2025-07-01T23:08:15.000Z","dependencies_parsed_at":"2024-06-19T16:24:12.219Z","dependency_job_id":"863cb85d-f025-4d77-881b-6da1c8d3d6ac","html_url":"https://github.com/thednp/event-listener","commit_stats":{"total_commits":84,"total_committers":2,"mean_commits":42.0,"dds":"0.33333333333333337","last_synced_commit":"04af572d7e3ce91572296380d7d7ecee60120c8e"},"previous_names":["thednp/event-listener.js"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/thednp/event-listener","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thednp%2Fevent-listener","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thednp%2Fevent-listener/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thednp%2Fevent-listener/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thednp%2Fevent-listener/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thednp","download_url":"https://codeload.github.com/thednp/event-listener/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thednp%2Fevent-listener/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28050355,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-12-26T02:00:06.189Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["event-listener","events","javascript","javascript-events","typescript"],"created_at":"2024-10-14T04:28:15.388Z","updated_at":"2025-12-26T08:36:05.407Z","avatar_url":"https://github.com/thednp.png","language":"TypeScript","funding_links":["https://github.com/sponsors/thednp"],"categories":[],"sub_categories":[],"readme":"## EventListener\r\n\r\n[![Coverage Status](https://coveralls.io/repos/github/thednp/event-listener/badge.svg)](https://coveralls.io/github/thednp/event-listener)\r\n[![ci](https://github.com/thednp/event-listener/actions/workflows/ci.yml/badge.svg)](https://github.com/thednp/event-listener/actions/workflows/ci.yml)\r\n[![NPM Version](https://img.shields.io/npm/v/@thednp/event-listener.svg)](https://www.npmjs.com/package/@thednp/event-listener)\r\n[![NPM Downloads](https://img.shields.io/npm/dm/@thednp/event-listener.svg)](http://npm-stat.com/charts.html?package=@thednp/event-listener)\r\n[![jsDeliver](https://img.shields.io/jsdelivr/npm/hw/@thednp/event-listener)](https://www.jsdelivr.com/package/npm/@thednp/event-listener)\r\n[![typescript version](https://img.shields.io/badge/typescript-5.9.3-brightgreen)](https://www.typescriptlang.org/)\r\n[![vitest version](https://img.shields.io/badge/vitest-4.0.16-brightgreen)](https://vitest.dev/)\r\n[![vite version](https://img.shields.io/badge/vite-7.3.0-brightgreen)](https://github.com/vitejs)\r\n\r\nA TypeScript sourced event listener for efficient applications based on the [subscribe-publish](https://hackernoon.com/do-you-still-register-window-event-listeners-in-each-component-react-in-example-31a4b1f6f1c8) pattern, around 700 bytes when minified and packs a surprising amount of power.\r\n\r\n## Features\r\n\r\n- **EventListener** is TypeScript sourced, with some types addapted from React;\r\n- **EventListener** makes use of the native [Map](https://caniuse.com/mdn-javascript_builtins_map) to subscribe/register or unsubscribe/remove listeners, which is perfect since we need to make sure the exact listeners are added/removed; this completely invalidates the need to [deconstruct function objects](https://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript) for comparison's sake to make sure event listeners are properly handled;\r\n- **EventListener** allows you to register multiple listeners for the same target, even of the same type, but always uses a single `globalListener` to call them all at once when event is triggered;\r\n- **EventListener** \"should\" be able to manage event options, especially `once`, meaning that when the option is `true`, the listener is automatically un-subscribed and detached from target;\r\n- **EventListener** will unsubscribe and detach listeners with the same options used when attached, which means you can \"lazy\" remove listeners on the fly.\r\n\r\n\r\n# Install\r\n\r\n```bash\r\npnpm add @thednp/event-listener\r\n```\r\n\r\n```bash\r\nyarn add @thednp/event-listener\r\n```\r\n\r\n```bash\r\nnpm install @thednp/event-listener\r\n```\r\n\r\n```bash\r\ndeno add npm:@thednp/event-listener@latest\r\n```\r\n\r\n\r\n## CDN\r\n\r\n```html\r\n\u003cscript src=\"https://cdn.jsdelivr.net/npm/@thednp/event-listener/dist/event-listener.js\"\u003e\u003c/script\u003e\r\n```\r\n\r\n## Use\r\n\r\n```js\r\nimport * as Listener from '@thednp/event-listener';\r\n\r\n// execute a listener once\r\nListener.on(document, 'DOMContentLoaded', () =\u003e {\r\n    console.log('document is now loaded');\r\n  },\r\n  { once: true },\r\n);\r\n\r\n// add a listener with `useCapture: false`\r\nfunction handleMyClick(e: Listener.NativeEvent) {\r\n  if (e.target.tagName === 'button') {\r\n    e.preventDefault();\r\n    e.stopImmediatePropagation();\r\n  }\r\n  console.log('do something else instead');\r\n}\r\nListener.on(document, 'click', handleMyClick, false);\r\n\r\n// remove a listener, `EventListener` will get listener options from registry\r\nListener.off(document, 'click', handleMyClick);\r\n\r\n// add listener to `window`, this listener has no name and cannot be removed\r\nListener.on(window, 'scroll', () =\u003e console.log(window.scrollY));\r\n```\r\n\r\nSince we're implementing `Map`, you can make use of its prototype to access registry:\r\n\r\n```js\r\n// get element listener registry\r\nconst documentClickListeners = Listener.registry['click'].get(document);\r\n\r\n// returns\r\nMap(1) {\r\n  Entries =\u003e [\r\n    0: {\r\n      key: handleMyClick() // listener\r\n      value: false // listener options\r\n    }\r\n  ],\r\n  size: 1, // size of the Map\r\n  prototype: [Prototype(Map)]\r\n}\r\n\r\n// check if element has listener\r\nif (documentClickListeners \u0026\u0026 documentClickListeners.has(handleMyClick)) {\r\n  // do something about it\r\n}\r\n\r\n// check if a listener is the one you're looking for\r\nif (documentClickListeners) {\r\n  const [eventListener] = documentClickListeners;\r\n  if (eventListener === handleMyClick) {\r\n    // do something about it\r\n  }\r\n}\r\n\r\n// get listener options\r\nconst myListenerOptions = documentClickListeners \u0026\u0026 documentClickListeners.get(handleMyClick);\r\n\r\n// returns false, which is the `useCapture` option value added for `handleMyClick`\r\n```\r\n\r\n## Advanced Use\r\n\r\nYou can also make use of the types for more consistent code:\r\n\r\n```ts\r\nimport { on, FocusEventHandler } from '@thednp/event-listener';\r\n\r\nconst handleMyFocus: FocusEventHandler\u003cHTMLInputElement\u003e = (e) =\u003e {\r\n  console.log(e)\r\n}\r\n\r\nconst myInput = document.querySelector('input') || document.createElement('input');\r\non(myInput, 'focus', handleMyFocus);\r\n```\r\n\r\nFor more advanced use, check out the [demo](https://thednp.github.io/event-listener), showcasing the **EventListener** usage with a demo component.\r\n\r\n## Run the tests suite\r\n\r\n- [Download](https://github.com/thednp/event-listener/archive/refs/heads/master.zip) the package from Github;\r\n- unpack/unzip and open the folder with your editor;\r\n- open your terminal and navigate to the root of the unpacked folder;\r\n- run `npm install` or `npm update`, takes a few minutes to download the Electron browser;\r\n- run `npm run test-ui` to open the browser mode testing OR `npm run test` to run the tests in headless mode.\r\n\r\n## License\r\n\r\n**EventListener** is released under the [MIT License](https://github.com/thednp/event-listener/blob/main/LICENSE).\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthednp%2Fevent-listener","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthednp%2Fevent-listener","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthednp%2Fevent-listener/lists"}