@thednp/event-listener
Advanced tools
Comparing version 2.0.0-alpha4 to 2.0.0-alpha5
@@ -12,13 +12,22 @@ // Generated by dts-bundle-generator v6.12.0 | ||
export declare type EventsRegistry = Record<string, Map<EventTarget, ListenerObject>>; | ||
declare const _default: { | ||
on: (element: EventTarget, eventType: string, listener: EventListener, options?: AddEventListenerOptions | undefined) => void; | ||
off: (element: EventTarget, eventType: string, listener: EventListener, options?: AddEventListenerOptions | undefined) => void; | ||
globalListener: (e: Event) => void; | ||
registry: EventsRegistry; | ||
}; | ||
export declare const registry: EventsRegistry; | ||
/** | ||
* The global event listener. This function must be a Function. | ||
* eslint-ignore-next | ||
*/ | ||
export declare const globalListener: (e: Event) => void; | ||
/** | ||
* Register a new listener with its options and attach the `globalListener` | ||
* to the target if this is the first listener. | ||
*/ | ||
export declare const addListener: (element: EventTarget, eventType: string, listener: EventListener, options?: AddEventListenerOptions) => void; | ||
/** | ||
* Remove a listener from registry and detach the `globalListener` | ||
* if no listeners are found in the registry. | ||
* | ||
*/ | ||
export declare const removeListener: (element: EventTarget, eventType: string, listener: EventListener, options?: AddEventListenerOptions) => void; | ||
export declare const on: typeof addListener; | ||
export declare const off: typeof removeListener; | ||
export { | ||
_default as default, | ||
}; | ||
export {}; |
@@ -1,2 +0,2 @@ | ||
var Listener=function(){"use strict";const e={},f=n=>{const{type:o,target:i,currentTarget:a}=n;[...e[o]].forEach(([t,s])=>{[a,i].includes(t)&&[...s].forEach(([c,r])=>{c.apply(t,[n]),typeof r=="object"&&r.once&&d(t,o,c,r)})})},g=(n,o,i,a)=>{e[o]||(e[o]=new Map);const t=e[o];t.has(n)||t.set(n,new Map);const s=t.get(n),{size:c}=s;s.set(i,a),c||n.addEventListener(o,f,a)},d=(n,o,i,a)=>{const t=e[o],s=t&&t.get(n),c=s&&s.get(i),r=c!==void 0?c:a;s&&s.has(i)&&s.delete(i),t&&(!s||!s.size)&&t.delete(n),(!t||!t.size)&&delete e[o],(!s||!s.size)&&n.removeEventListener(o,f,r)};return{on:g,off:d,globalListener:f,registry:e}}(); | ||
var Listener=function(s){"use strict";const i={},d=n=>{const{type:o,target:a,currentTarget:r}=n;[...i[o]].forEach(([t,e])=>{[r,a].includes(t)&&[...e].forEach(([c,f])=>{c.apply(t,[n]),typeof f=="object"&&f.once&&g(t,o,c,f)})})},u=(n,o,a,r)=>{i[o]||(i[o]=new Map);const t=i[o];t.has(n)||t.set(n,new Map);const e=t.get(n),{size:c}=e;e.set(a,r),c||n.addEventListener(o,d,r)},g=(n,o,a,r)=>{const t=i[o],e=t&&t.get(n),c=e&&e.get(a),f=c!==void 0?c:r;e&&e.has(a)&&e.delete(a),t&&(!e||!e.size)&&t.delete(n),(!t||!t.size)&&delete i[o],(!e||!e.size)&&n.removeEventListener(o,d,f)},L=u,v=g;return s.addListener=u,s.globalListener=d,s.off=v,s.on=L,s.registry=i,s.removeListener=g,Object.defineProperties(s,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}}),s}({}); | ||
//# sourceMappingURL=event-listener.js.map |
{ | ||
"name": "@thednp/event-listener", | ||
"author": "thednp", | ||
"version": "2.0.0alpha4", | ||
"version": "2.0.0alpha5", | ||
"description": "Modern event listener for efficient web applications based on subscribe-publish pattern.", | ||
@@ -44,2 +44,3 @@ "license": "MIT", | ||
}, | ||
"sideEffects": false, | ||
"scripts": { | ||
@@ -55,3 +56,4 @@ "pre-test": "npm run clean-coverage", | ||
"fix:ts": "eslint -c .eslintrc.js --ext .ts src --fix", | ||
"build": "npm run clean && npm run lint:ts && vite build && npm run docs && dts-bundle-generator --config ./dts.config.ts", | ||
"build:ts": "dts-bundle-generator --config ./dts.config.ts", | ||
"build": "npm run clean && npm run lint:ts && vite build && npm run docs && npm run build:ts", | ||
"docs": "ncp dist/event-listener.js docs/event-listener.js && ncp dist/event-listener.js.map docs/event-listener.js.map" | ||
@@ -78,2 +80,3 @@ }, | ||
"rimraf": "^3.0.2", | ||
"terser": "^5.15.0", | ||
"typescript": "^4.7.4", | ||
@@ -80,0 +83,0 @@ "vite": "^3.0.9" |
@@ -39,3 +39,3 @@ ## EventListener | ||
```js | ||
import Listener from '@thednp/event-listener'; | ||
import * as Listener from '@thednp/event-listener'; | ||
@@ -89,2 +89,10 @@ // execute a listener once | ||
// 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 | ||
@@ -91,0 +99,0 @@ const myListenerOptions = documentClickListeners && documentClickListeners.get(handleMyClick); |
@@ -11,3 +11,3 @@ /** | ||
type EventsRegistry = Record<string, Map<EventTarget, ListenerObject>>; | ||
const eventsRegistry: EventsRegistry = {}; | ||
const registry: EventsRegistry = {}; | ||
@@ -21,3 +21,3 @@ /** | ||
[...eventsRegistry[type]].forEach(([element, listenersMap]) => { | ||
[...registry[type]].forEach(([element, listenersMap]) => { | ||
/* istanbul ignore else */ | ||
@@ -47,6 +47,6 @@ if ([currentTarget, target].includes(element)) { | ||
// get element listeners first | ||
if (!eventsRegistry[eventType]) { | ||
eventsRegistry[eventType] = new Map(); | ||
if (!registry[eventType]) { | ||
registry[eventType] = new Map(); | ||
} | ||
const oneEventMap = eventsRegistry[eventType]; | ||
const oneEventMap = registry[eventType]; | ||
@@ -82,3 +82,3 @@ if (!oneEventMap.has(element)) { | ||
// get listener first | ||
const oneEventMap = eventsRegistry[eventType]; | ||
const oneEventMap = registry[eventType]; | ||
const oneElementMap = oneEventMap && oneEventMap.get(element); | ||
@@ -93,3 +93,3 @@ const savedOptions = oneElementMap && oneElementMap.get(listener); | ||
if (oneEventMap && (!oneElementMap || !oneElementMap.size)) oneEventMap.delete(element); | ||
if (!oneEventMap || !oneEventMap.size) delete eventsRegistry[eventType]; | ||
if (!oneEventMap || !oneEventMap.size) delete registry[eventType]; | ||
@@ -103,7 +103,6 @@ // remove listener last | ||
export default { | ||
on: addListener, | ||
off: removeListener, | ||
globalListener, | ||
registry: eventsRegistry, | ||
}; | ||
// alias main methods | ||
const on: typeof addListener = addListener; | ||
const off: typeof removeListener = removeListener; | ||
export { addListener, removeListener, on, off, globalListener, registry }; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
31107
151
125
1
21