![Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility](https://cdn.sanity.io/images/cgdhsj6q/production/97774ea8c88cc8f4bed2766c31994ebc38116948-1664x1366.png?w=400&fit=max&auto=format)
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@thednp/event-listener
Advanced tools
Modern event listener for efficient web applications based on subscribe-publish pattern.
A TypeScript sourced event listener for efficient applications based on the subscribe-publish pattern, less than 900 bytes when minified and packs a surprising amount of power.
globalListener
to call them all at once when event is triggered;once
, meaning that when the option is true
, the listener is automatically un-subscribed and detached from target;npm i @thednp/event-listener
<script src="https://cdn.jsdelivr.net/npm/@thednp/event-listener/dist/event-listener.js"></script>
import * as Listener from '@thednp/event-listener';
// execute a listener once
Listener.on(document, 'DOMContentLoaded', () => {
console.log('document is now loaded');
},
{ once: true },
);
// add a listener with `useCapture: false`
function handleMyClick(e) {
if (e.target.tagName === 'button') {
e.preventDefault();
e.stopImmediatePropagation();
}
console.log('do something else instead');
}
Listener.on(document, 'click', handleMyClick, false);
// remove a listener, `EventListener` will get listener options from registry
Listener.off(document, 'click', handleMyClick);
// add listener to `window`, this listener has no name and cannot be removed
Listener.on(window, 'scroll', () => console.log(window.scrollY));
Since we're implementing Map
, you can make use of its prototype to access registry:
// get element listener registry
const documentClickListeners = Listener.registry['click'].get(document);
// returns
Map(1) {
Entries(Array) => [
0: {
key: handleMyClick() // listener
value: false // listener options
}
],
size: 1, // size of the Map
prototype: [Prototype(Object)]
}
// check if element has listener
if (documentClickListeners && documentClickListeners.has(handleMyClick)) {
// do something about it
}
// check if a listener is the one you're looking for
if (documentClickListeners) {
const [eventListener] = documentClickListeners;
if (eventListener === handleMyClick) {
// do something about it
}
}
// get listener options
const myListenerOptions = documentClickListeners && documentClickListeners.get(handleMyClick);
// returns false, which is the `useCapture` option value added for `handleMyClick`
You can also make use of "tree shaking" to import only the module you want, for instance:
import { on } from '@thednp/event-listener';
on(document, handleMyClick, true);
For more advanced use, check out the demo, showcasing the EventListener usage with a demo component.
npm install
or npm update
, takes a few minutes to download the Electron browser;npm run cypress
to open the Cypress console OR npm run test
to run the tests in headless mode.EventListener is released under the MIT License.
FAQs
🚅 Modern event listener for efficient web applications based on subscribe-publish pattern.
The npm package @thednp/event-listener receives a total of 243,066 weekly downloads. As such, @thednp/event-listener popularity was classified as popular.
We found that @thednp/event-listener demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.