A EventTarget polyfill.
- CDN, as global patch, via https://unpkg.com/@ungap/event-target
- ESM via
import EventTarget from '@ungap/event-target'
- CJS via
const EventTarget = require('@ungap/event-target')
An EventTarget polyfill
License: ISC License
A EventTarget polyfill.
import EventTarget from '@ungap/event-target'
const EventTarget = require('@ungap/event-target')
In order to make modules importable/requirable via NodeJS, follow the dual module approach described in here so that latest NodeJS won't have issues.
Unable to extend EventTarget
in NodeJS and Jest
const EventTarget = require('@ungap/event-target')
class L extends EventTarget {}
const listener = new L();
const event = new CustomEvent('a');
listener.addEventListener('a', console.log);
listener.dispatchEvent(event);
Hello!
I'm using this polyfill in ie11 and it crashes sometimes at this line: https://github.com/ungap/event-target/blob/master/index.js#L68
I get the error: "Unable to get property 'handleEvent' of undefined or null reference". When I check the info object, it's listener and options are undefined. Anyone has a clue of why? And what I can do to fix it?
Currently when dispatching an event to a node, that event won't bubble up to parents.
This is also used in linkedom which is the reason I started looking at it.
I pulled the code and started an update:
define(proto, 'dispatchEvent', function (event) {
var secret = wm.get(this);
if (secret[event.type]) {
var listeners = secret[event.type].slice(0);
define(event, 'target', this);
define(event, 'currentTarget', this);
// if an event listener invoke stopImmediatePropagation it should stop calling other listeners
for (var i = 0; i < listeners.length && !event._stopImmediatePropagationFlag; i++) {
dispatch.call(event, listeners[i]);
}
delete event.currentTarget;
delete event.target;
}
// should probably be done differently: https://dom.spec.whatwg.org/#event-path
// but should not be too much of an issue for now
if (event.bubbles && !event._stopPropagationFlag) {
if (this.parentNode) {
this.parentNode.dispatchEvent(event);
}
}
return true;
});
This is a very simple bubbling but it should works fine and could be improved in more PR eventually.
I created a branch, maybe I could push it and create a PR?
Thanks!
Hi
In the process of "typing" your event-target polyfill I came across the following issues:
The attempt to delete these fields produces errors as they are read-only. So are they in plain JS. Does this mean these lines could be safely removed from the code?
delete event.currentTarget;
delete event.target;
The last line of the addEventListener definition produces an error, as listeners
is only declared locally in the for-loop. Into which context could the declaration for listeners
be moved without breaking the code?
listeners.push({target: this, listener: listener, options: options});
Thanks for any suggestions
dsheyp
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.