Socket
Socket
Sign inDemoInstall

event-target-shim

Package Overview
Dependencies
0
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    event-target-shim

A polyfill for W3C EventTarget Constructor.


Version published
Maintainers
1
Install size
29.5 kB
Created

Package description

What is event-target-shim?

The event-target-shim npm package provides a cross-platform, shim implementation of the EventTarget interface, allowing developers to use a consistent API for event handling across different environments. It supports the standard addEventListener, removeEventListener, and dispatchEvent methods, along with custom event types. This makes it useful for projects that need to handle events in both browser and non-browser environments, such as Node.js applications or web components.

What are event-target-shim's main functionalities?

Basic Event Handling

This demonstrates how to create an instance of EventTarget, add an event listener for a custom event type, and then dispatch an event of that type. It's the basic usage for handling custom events.

const EventTargetShim = require('event-target-shim');
const myEventTarget = new EventTargetShim();

myEventTarget.addEventListener('customEvent', function(event) {
  console.log(`Received: ${event.type}`);
});

myEventTarget.dispatchEvent(new EventTargetShim.Event('customEvent'));

Using Options for addEventListener

This example shows how to use the `once` option with `addEventListener` to automatically remove the event listener after it has been invoked once, demonstrating the package's support for event listener options.

const EventTargetShim = require('event-target-shim');
const myEventTarget = new EventTargetShim();

myEventTarget.addEventListener('customEvent', function(event) {
  console.log(`Received: ${event.type}`);
}, { once: true });

myEventTarget.dispatchEvent(new EventTargetShim.Event('customEvent'));
myEventTarget.dispatchEvent(new EventTargetShim.Event('customEvent'));

Other packages similar to event-target-shim

Readme

Source

event-target-shim

npm version

A polyfill for W3C EventTarget, plus few extensions.

See Also: https://dom.spec.whatwg.org/#interface-eventtarget

Overview

  • This module provides EventTarget constructor that is possible to inherit for your custom object.
  • This module provides an utility in order to define properties for attribute listeners (e.g. obj.onclick).

If window.EventTarget exists, EventTarget is inherit from window.EventTarget. In short, obj instanceof window.EventTarget === true.

declare class EventTarget {
  constructor();
  addEventListener(type: string, listener: (event: Event) => void, capture?: boolean): void;
  removeEventListener(type: string, listener: (event: Event) => void, capture?: boolean): void;
  dispatchEvent(event: Event): void;
}

// Define EventTarget type with attribute listeners.
declare function EventTarget(...types: string[]): typeof EventTarget;

Installation

npm install event-target-shim

Usage

import EventTarget from "event-target-shim";

class YourCoolType extends EventTarget {
  // ...
}

// This prototype has getters/setters of `onmessage` and `onerror`.
class YourAwesomeType extends EventTarget("message", "error") {
  // ...
}

I prefer use together with Browserify.

But we can use together with RequireJS, instead. In this case, please download a file from dist directory of repo.

define("MagicalBox", ["event-target-shim"], function (EventTarget) {
  function MagicalBox() {
    EventTarget.call(this);
  }

  MagicalBox.prototype = Object.create(EventTarget.prototype, {
    constructor: {
      value: MagicalBox,
      configurable: true,
      writable: true
    },

    // ...
  });

  return MagicalBox;
});

Keywords

FAQs

Last updated on 27 May 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc