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

@dhlx/event-target

v0.0.1

Published

A lightweight TypeScript implementation of the EventTarget interface, supporting event listeners, event bubbling, and propagation control, designed for custom event-driven systems.

Downloads

6

Readme

@dhlx/event-target

A lightweight implementation of an event-driven architecture in TypeScript, inspired by the DOM EventTarget. This project provides a basic structure to simulate event bubbling, event listeners, and propagation control in a custom event system.

Features

  • Event Bubbling: Events triggered on child targets bubble up to their parent targets, similar to the DOM's event model.
  • Event Listeners: Add and remove listeners for specific event types.
  • Propagation Control: Stop event propagation at any level using event.stopPropagation().

Table of Contents

Installation

You can clone this repository and install its dependencies by running:

npm install @dhlx/event-target

Usage

Example: Basic Event Bubbling

Here’s an example of how you can create an event bubbling system using the EventTarget class.

import { EventTarget, Event } from '@dhlx/event-target';

// Create instances
const grandparent = new EventTarget('grandparent');
const parent = new EventTarget('parent');
const child = new EventTarget('child');

// Set up parent-child relationships
parent.setParent(grandparent);
child.setParent(parent);

// Add event listeners
grandparent.addEventListener('click', () => {
    console.log('Grandparent received event');
});

parent.addEventListener('click', () => {
    console.log('Parent received event');
});

child.addEventListener('click', () => {
    console.log('Child received event');
});

// Dispatch event from child
const clickEvent = new Event('click');
child.dispatchEvent(clickEvent);

// Expected Output:
// Child received event
// Parent received event
// Grandparent received event

Example: Stopping Event Propagation

You can stop the event from propagating further up the hierarchy by using event.stopPropagation().

parent.addEventListener('click', (event: Event) => {
    console.log('Parent received event');
    event.stopPropagation();  // Stops the event from reaching grandparent
});

child.dispatchEvent(new Event('click'));

// Expected Output:
// Child received event
// Parent received event

API

EventTarget

new EventTarget(name: string)

Creates a new EventTarget instance.

  • name: A string that gives the instance a name (for reference in event handling).

addEventListener(type: string, listener: (event: Event) => void): void

Registers an event listener for a specific event type.

  • type: A string representing the type of the event (e.g., 'click').
  • listener: The function to execute when the event is triggered.

removeEventListener(type: string, listener: (event: Event) => void): void

Removes a previously registered event listener.

  • type: The event type from which the listener should be removed.
  • listener: The function to remove.

dispatchEvent(event: Event): void

Dispatches an event, triggering the event listeners and starting the event propagation process.

  • event: An instance of the Event class representing the event to be dispatched.

setParent(parent: EventTarget): void

Sets a parent EventTarget for the current target, creating a hierarchy for event bubbling.

Event

new Event(type: string)

Creates a new Event instance.

  • type: The type of the event (e.g., 'click').

stopPropagation(): void

Stops the event from propagating to parent targets.

Testing

This project uses AVA for testing. To run the tests, first install the necessary dependencies:

npm install

Then run the tests using the following command:

npx ava

Example Test

The EventTarget class is tested to ensure correct behavior, including event bubbling, stopping propagation, and event listener registration/removal.

Here’s an example of a test:

import test from 'ava';
import EventTarget, { Event } from './eventtarget';

test('Event should bubble up from child to parent', t => {
    const grandparent = new EventTarget('grandparent');
    const parent = new EventTarget('parent');
    const child = new EventTarget('child');

    parent.setParent(grandparent);
    child.setParent(parent);

    let grandparentCalled = false;
    let parentCalled = false;
    let childCalled = false;

    grandparent.addEventListener('click', () => grandparentCalled = true);
    parent.addEventListener('click', () => parentCalled = true);
    child.addEventListener('click', () => childCalled = true);

    const clickEvent = new Event('click');
    child.dispatchEvent(clickEvent);

    t.true(childCalled);
    t.true(parentCalled);
    t.true(grandparentCalled);
});

License

This project is licensed under the MIT License.