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

ember-singularity

v2.0.0

Published

Unified event handling for Ember.

Downloads

9,371

Readme

Ember Singularity

npm version Build Status

Ember Singularity integrates a Unified Event Handler service to help control DOM event listeners by taking normal DOM events and binding them to Ember Events that are triggered by a singular DOM listener.

In other words, this means that instead of having multiple listeners for a single DOM event, you have one listener for a single DOM event that then triggers multiple callbacks via Ember events.

Why do this? There are two primary motivations:

  1. Centralize control of DOM listeners: this has numerous benefits. Primary would be that it reduces the risk of memory leaks and allows optimization of the number of handlers being used. It allows an easy choke point for throttling "spammy" events. Essentially greater and easier control over DOM events not already handled by Ember.
  2. Leverage Ember's event system: this helps ensure events that cause modifications to application state or the DOM get batched into the Ember run-loop. Helping reduce churn (especially in cases such as scrolling) is a huge win when trying to make performant applications.

Usage

The available interface for the UnifiedEventHandler is pretty simple and only contains 3 available methods. That said, it is recommended that you abstract away any usage of the service via mixins or base-level components; this helps ensure the benefits described in the above motivations.

register(target, eventName, callback)

This registers a callback to be tied to a specific target and event type. The target and eventName are expected to be of type string and callback is a function. The callback will receive the original event. Here's an example:

let ScrollMixin = Ember.Mixin.extend({
  unifiedEventHandler: Ember.inject.service(),

  _registerScrollCallback: Ember.on('init', function() {
    this.get('unifiedEventHandler').register('window', 'scroll', (event) => {
      console.log('scrolled!');
      console.log(event);
    });
  })
});

unregister(target, eventName, callback)

This is the exact opposite of register() and expects the arguments to be the same as were used to register the handler. Here's an example:

let ScrollMixin = Ember.Mixin.extend({
  unifiedEventHandler: Ember.inject.service(),

  scroll() { console.log('scrolled!'); },

  _registerScrollCallback: Ember.on('init', function() {
    this.get('unifiedEventHandler').register('window', 'scroll', this.scroll);
  }),

  _unregisterScrollCallback: Ember.on('willDestroy', function() {
    this.get('unifiedEventHandler').unregister('window', 'scroll', this.scroll);
  })
});

triggerEvent(eventName)

This allows you to trigger the Ember event that your callback got bound to. As of version 1.1.0, after you register a handler it's Ember event will be of the form <event-name>.<target>. This probably won't be used often, but is available for flexibility in testing, debugging, and extending functionality. Here's a final example of the total API:

let ScrollMixin = Ember.Mixin.extend({
  unifiedEventHandler: Ember.inject.service(),

  scroll() { console.log('scrolled!'); },

  triggerScroll() {
    this.get('unifiedEventHandler').triggerEvent('window.scroll');
  },

  _registerScrollCallback: Ember.on('init', function() {
    this.get('unifiedEventHandler').register('window', 'scroll', this.scroll);
  }),

  _unregisterScrollCallback: Ember.on('willDestroy', function() {
    this.get('unifiedEventHandler').unregister('window', 'scroll', this.scroll);
  })
});