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

dom-event-handler

v1.0.4

Published

A generic dom event handler as a class property

Downloads

14

Readme

dom-event-handler

npm version build status coverage downloads js-standard-style

A generic DOM event handler. Works great in the browser. Does't work great in the Node.js ecosystem.

Installation

$ npm install dom-event-handler

Usage

const DOMEventHandler = require("dom-event-handler")

class MyWSController extends SomeOtherClass {
  constructor () {
    this.ws = new WebSocket('ws://localhost:8080')
    this.handler = new DOMEventHandler(this, this.ws)
  }

  // These methods handle the websocket events
  onmessage (ev) {}
  onopen (ev) {}
  onerror (ev) {}
  onclose (ev) {}
}

Isn't that nicer than this?

const ws = new WebSocket('ws://localhost:8080')

class VerboseWSController {
  constructor () {
    this.foo = 'bar'

    // You have to bind since you don't pass a full context.
    // Static class properties assigned to arrow functions are less verbose
    // but are structurally similar to binding, and have poor env support still.
    // They don't reside on the prototype.  That may or may not matter to the use case.
    this.onmessage = this.onmessage.bind(this)
    this.onopen = this.onopen.bind(this)s
    this.onerror = this.onerror.bind(this)
    this.onclose = this.onclose.bind(this)
  }

  onmessage (ev) {}
  onopen (ev) {}
  onerror (ev) {}
  onclose (ev) {}
}

const c = new VerboseWSController()

ws.addEventListener('message', c.onmessage)
ws.addEventListener('open', c.onopen)
ws.addEventListener('error', c.onerror)
ws.addEventListener('close', c.onclose)

API

handler = new DOMEventHandler(ctx, [node])

Create a new instance of DOMEventHandler passing in a context ctx (often this when created within a class) and optionally a DOM event target node (e.g. an event emitting DOM node) to attach listeners to on instantiation.

The ctx should be an object who's prototype contains event handler methods. Event handler methods must take the form of on{eventname} where eventname is the name of the event you want to listen on and handle (the name you would pass to node.addEventListener). In practice, you can pass a class instance as a ctx, or this when the instance owns the DOMEventHanlder instance.

handler.addEventListeners(node)

Attach all event handler methods on ctx to the DOM event target node.

handler.removeEventListeners(node)

Remove all event handler event names on ctx from the DOM event target node.

Internal Methods

You don't need to call these, but they are there.

handler.handleEvent(event)

Implements the eventListener.handleEvent method for the events found on the ctx prototype. This is where the magic happens.

handler.events

A getter that returns all events found on the ctx the handler is bound to. The events returned from this getter are what get attached and detached in the above methods.

See also

This module was inspired by a @webreflection article.

This is a slick API, but has poor support in the Node.js ecosystem due to poor support for the EventListener interface (Node.js style events do not support handleEvent or similar). For a similar API see node-event-handler, which lacks the bind free benefits of this approach, but can provide a stand-in api when writing universal Node.js code.

License

MIT