npm

Need private packages and team management tools?Check out npm Orgs. »

synthetic-event

1.0.0 • Public • Published

Synthetic Event

Build status NPM version Dependencies

This library provides a concise implementation of the two interfaces Event and EventTarget. The included base classes allow you to easily create synthetic events and custom event targets in any environment.

In case your focus lies on DOM events, you may want to take a look at the native CustomEvent class but bear in mind that its target property is read-only.

An alternative way to create custom event targets is to use a DocumentFragment as a dummy. This solution, however, only works in a browser environment.

API Reference

Installation

npm install synthetic-event

Usage

Basics
import { Event, EventTarget } from "synthetic-event";
 
const eventTarget = new EventTarget();
 
eventTarget.addEventListener("test", function(event) {
 
    console.log("listener function", event.target);
 
});
 
eventTarget.addEventListener("test", {
 
    handleEvent(event) {
 
        console.log("listener object", event.target);
 
    }
 
});
 
eventTarget.dispatchEvent(new Event("test"));
Custom Events
import { Event } from "synthetic-event";
 
export class MyEvent extends Event {
 
    constructor(type) {
 
        super(type);
 
        this.myData = null;
 
    }
 
}
Custom EventTargets
import { Event, EventTarget } from "synthetic-event";
 
export class MyEventTarget extends EventTarget {
 
    constructor() {
 
        super();
 
        this.myEvent = new Event("tick");
 
        setInterval(() => this.dispatchEvent(this.myEvent), 1000);
 
    }
 
}

Contributing

Maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.

install

npm i synthetic-event

Downloadsweekly downloads

4

version

1.0.0

license

Zlib

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability