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

@hooked74/node-event-emitter

v1.2.4

Published

Event emitter for DOM nodes

Downloads

3

Readme

Build Status npm License Codecov Module Size

Table of contents

Install

npm install @hooked74/node-event-emitter

Basic Usage

// initialize
const emitter = new NodeEventEmitter(document.body);

// attach handlers
const handler1 = e => console.log(e instanceof MouseEvent);
const handler2 = e => console.log(e instanceof CustomEvent, e.detail);
emitter.on(NodeEventEmitter.CLICK, handler1);
emitter.on("custom event", handler2);

// dispatch
emitter.emitMouse(NodeEventEmitter.CLICK);
emitter.emit("custom event", "value");

// output:
// true
// true value

// detach specific handler
emitter.off(NodeEventEmitter.CLICK, handler1);

// detach all handlers for specific event
emitter.off(NodeEventEmitter.CLICK);

// detach all handlers
emitter.off();

Advanced Usage

import NodeEventEmitter, { CustomNodeEvents } from "node-event-emitter";

// create my custom event
const myCustomEvent = {
  off(emitter: NodeEventEmitter, handler: EventHandler) {
    // ...
    emitter.off("my custom event", handler);
    console.log("detach my custom event")
    // ...
  },
  on(
    emitter: NodeEventEmitter,
    handler: EventHandler,
    options: boolean | AddEventListenerOptions
  ) {
    // ...
    emitter.on("my custom event", handler);
    console.log("attach my custom event")
    // ...
  },
  // optional
  emit(
    emitter: NodeEventEmitter,
    data: any,
    options: EventInit
  ) {
    // ...
    emitter.emit("my custom event", data, options);
    console.log("dispatch my custom event")
    // ...
  }
}

// extend emitter and override custom events
class CustomNodeEventEmitter extends NodeEventEmitter {
  customEvents = { ...CustomNodeEvents, myCustomEvent };
}

// usage my custom event
const emitter = new CustomNodeEventEmitter(document.body);

const handler = e => console.log(e.detail);
emitter.on("myCustomEvent", handler);
emitter.emit("myCustomEvent", "some value");
emitter.off("myCustomEvent", handler);

// output:
// attach my custom event
// some value
// dispatch my custom event
// detach my custom event

Methods

emitter.on(name, handler[, options])

  • name <string> The name of the event.
  • handler <(e?: Event) => any> The callback function.
  • options (optional) <boolean> | <AddEventListenerOptions> An options object that specifies characteristics about the event listener.
  • Returns: <void>

Sets up a function that will be called whenever the specified event is delivered to the target. Uses addEventListener, but if the event is found in customEvents will use it.

emitter.off([name, handler])

  • name (optional) <string> The name of the event.
  • handler (optional) <(e?: Event) => any> The callback function.
  • Returns: <void>

Removes event listeners. Uses removeEventListener, but if the event is found in customEvents will use it. If no handler is specified, it will delete all handlers named name. If the name is also not specified, then all handlers will be deleted.

emitter.once(name, handler[, options])

  • name <string> The name of the event.
  • handler <(e?: Event) => any> The callback function.
  • options (optional) <boolean> | <AddEventListenerOptions> An options object that specifies characteristics about the event listener.
  • Returns: <void>

The listener should be invoked at most once after being added and automatically removed when invoked. Used the same way as emitter.on.

emitter.emit(name[, data, options])

  • name <string> The name of the event.
  • data (optional) <any> Any data passed when initializing the event.
  • options (optional) <EventInit> Event options
  • Returns: <void>

Triggers an event. Uses CustomEvent and dispatchEvent method. If the event is found in customEvents will use it. Can dispatch any events.

emitter.emitUI(name[, options])

  • name <string> The name of the event.
  • options (optional) <UIEventInit> Event options
  • Returns: <void>

Triggers an event. Uses UIEvent and dispatchEvent method. If the event is found in customEvents will use it. Can dispatch abort, beforeunload, error, load, resize, scroll, select, unload events.

emitter.emitMouse(name[, options])

  • name <string> The name of the event.
  • options (optional) <MouseEventInit> Event options
  • Returns: <void>

Triggers an event. Uses MouseEvent and dispatchEvent method. If the event is found in customEvents will use it. Can dispatch click, contextmenu, dblclick, mousedown, mouseenter, mouseleave, mousemove, mouseout, mouseover, mouseup events.

emitter.emitKeyboard(name[, options])

  • name <string> The name of the event.
  • options (optional) <KeyboardEventInit> Event options
  • Returns: <void>

Triggers an event. Uses KeyboardEvent and dispatchEvent method. If the event is found in customEvents will use it. Can dispatch keydown, keypress, keyup events.

emitter.emitWheel(name[, options])

  • name <string> The name of the event.
  • options (optional) <WheelEventInit> Event options
  • Returns: <void>

Triggers an event. Uses WheelEvent and dispatchEvent method. If the event is found in customEvents will use it. Can dispatch wheel events.

emitter.emitFocus(name[, options])

  • name <string> The name of the event.
  • options (optional) <FocusEventInit> Event options
  • Returns: <void>

Triggers an event. Uses FocusEvent and dispatchEvent method. If the event is found in customEvents will use it. Can dispatch blur, focus, focusin, focusout events.

emitter.emitTouch(name[, options])

  • name <string> The name of the event.
  • options (optional) <TouchEventInit> Event options
  • Returns: <void>

Triggers an event. Uses TouchEvent and dispatchEvent method. If the event is found in customEvents will use it. Can dispatch touchcancel, touchend, touchmove, touchstart events.

emitter.emitPointer(name[, options])

  • name <string> The name of the event.
  • options (optional) <PointerEventInit> Event options
  • Returns: <void>

Triggers an event. Uses PointerEvent and dispatchEvent method. If the event is found in customEvents will use it. Can dispatch pointerover, pointerenter, pointerdown, pointermove, pointerup, pointercancel, pointerout, pointerleave, gotpointercapture, lostpointercapture events.

Custom Events

The following custom events are available by default:

pointerTap

Emulates a click event on the desktop and tap(touchstart, touchend) on touch devices.

pointerDown

Emulates the interaction of events touchstart, mousedown, pointerdown.

pointerUp

Emulates the interaction of events touchend, mouseup, pointerup.

pointerMove

Emulates the interaction of events touchmove, mousemove, pointermove.