npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

subscribe-ui-event

v2.0.7

Published

A single, throttle built-in solution to subscribe to browser UI Events.

Downloads

437,783

Readme

subscribe-ui-event

npm version Build Status Coverage Status Dependency Status devDependency Status

With subscribe-ui-event, instead of calling multiple window.addEventListener('scroll', eventHandler); by different components, call subscribe('scroll', eventHandler). It will only add single event listener and dispatch event to those who subscribe the event via eventemitter3.

Why single event? More performance and less memory consumption.

Single Event Listener v.s. Multiple Event Listeners

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.

comparison

For 10 addEventListener, the difference of memory consumption between peak and trough is about 4.1K.

addEventListener

For 10 subscribe, the difference of memory consumption between peak and trough is about 1.0K.

subscribe

Other Benifits

  1. Do throttling by default.
  2. Get document.body.scrollTop, window.innerWidth once.
  3. Provide requestAnimationFrame throttle for the need of high performance.
  4. Be able to use like scrollStart (see below) those edge events.

Install

npm install subscribe-ui-event

API

subscribe

Subscription subscribe(String eventType, Function callback, Object? options)

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();

Addtional Payload

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

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:

  1. scroll - window.scoll
  2. scrollStart - The start of window.scoll
  3. scrollEnd - The end of window.scoll
  4. resize - window.resize
  5. resizeStart - The start window.resize
  6. resizeEnd - The end window.resize
  7. visibilitychange - document.visibilitychange (IE8 doesn't support)
  8. touchmoveStart - The start of window.touchmove
  9. touchmoveEnd - The end of window.touchmove
  10. touchmove - window.touchmove
  11. touchstart - window.touchstart
  12. touchend - window.touchend

options.eventOptions: An options object that specifies characteristics about the event listener (if passive event is supported by the browser)

unsubscribe

Void unsubscribe(String eventType, Function callback)

Unsubscribe an event listener, suggest to use subscription.unsubscribe(), because it may accidentally unsubscribe those events having the same eventType and callback but different throttleRate.

Credits

  • This library runs full browser test suite using Sauce Labs.

License

This software is free to use under the BSD license. See the LICENSE file for license text and copyright information.