Security News
UK Officials Consider Banning Ransomware Payments from Public Entities
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.
subscribe-ui-event
Advanced tools
A single, throttle built-in solution to subscribe to browser UI Events.
With subscribe-ui-event
, instead of calling multiple window.addEventListener('scroll', eventHandler);
by different components, call subscribe('scroll', eventHandler)
. It will only add a single event listener and dispatch event to those who subscribe to the event via eventemitter3.
npm install subscribe-ui-event
Why a single event? More performant and less memory consumption.
The jsperf runs 10 addEventListener
and 10 non-throttling subscribe
, and the outcome is that the ops/sec of subscribe
is slightly less. But in regular case, you will use throttling subscribe
, and it will be more performant.
For 10 addEventListener
, the difference of memory consumption between peak and trough is about 4.1K.
For 10 subscribe
, the difference of memory consumption between peak and trough is about 1.0K.
document.body.scrollTop
, window.innerWidth
once.requestAnimationFrame
throttle for high performance.scrollStart
(see below) those edge events.subscribe(eventType: String, callback: Function, options: Object?): Subscription
Provide throttled version of window
or document
events, such like scroll
, resize
, touch
and visibilitychange
to subscribe, see below.
Note on IE8 or the below, the throttle will be turned off because the event object is global and will be deleted for setTimeout or rAF.
Example:
import { subscribe } from 'subscribe-ui-event';
function eventHandler (e, payload) {
// e is the native event object and
// payload is the additional information
...
}
// 50ms throttle by default
const subscription = subscribe('scroll', eventHandler);
// remove later
subscription.unsubscribe();
The format of the payload is:
{
type: <String>, // could be 'scroll', 'resize' ...
// you need to pass options.enableScrollInfo = true to subscribe to get the following data
scroll: {
top: <Number>, // The scroll position, i.g., document.body.scrollTop
delta: <Number> // The delta of scroll position, it is helpful for scroll direction
},
// you need to pass options.enableResizeInfo = true to subscribe to get the following data
resize: {
width: <Number>, // The client width
height: <Number> // The client height
},
// you need to pass options.enableTouchInfo = true to subscribe to get the following data
touch: {
axisIntention: <String>, // 'x', 'y', or ''.
startX: <Number>,
startY: <Number>,
deltaX: <Number>,
deltaY: <Number>
}
}
options.throttleRate
allows of changing the throttle rate, and the default value is 50 (ms). Set 0 for no throttle. On IE8, there will be no throttle, because throttling will use setTimeout or rAF to achieve, and the event object passed into event handler will be overwritten.
options.context
allows of setting the caller of callback function.
options.useRAF = true
allows of using requestAnimationFrame
instead of setTimeout
.
options.enableScrollInfo = true
allows of getting scrollTop
.
options.enableResizeInfo = true
allows of getting width
and height
of client.
options.enableTouchInfo = true
allows of getting touch information (see above).
eventType
could be one of the following:
options.eventOptions
: An options object that specifies characteristics about the event listener (if passive event is supported by the browser)
unsubscribe(eventType: String, callback: Function): Void
Unsubscribe to an event listener, we suggest you use subscription.unsubscribe()
, because it may accidentally unsubscribe those events having the same eventType
and callback
, but different throttleRate
.
This software is free to use under the BSD license. See the LICENSE file for license text and copyright information.
FAQs
A single, throttle built-in solution to subscribe to browser UI Events.
The npm package subscribe-ui-event receives a total of 77,429 weekly downloads. As such, subscribe-ui-event popularity was classified as popular.
We found that subscribe-ui-event demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.