Skip to content

Instantly share code, notes, and snippets.

@Sebmaster
Created September 13, 2015 05:52
Show Gist options
  • Save Sebmaster/a056fd240cfa9afac1ac to your computer and use it in GitHub Desktop.
Save Sebmaster/a056fd240cfa9afac1ac to your computer and use it in GitHub Desktop.
"use strict";
const conversions = require("webidl-conversions");
const Impl = require("../implementation/EventTarget.js");
const initialized = require("./utils.js").initialized;
class EventTarget {
constructor() {
if (arguments[0] !== module.exports.secret && !this[initialized]) {
throw new TypeError("Illegal constructor");
}
this[initialized] = true;
const args = [];
for (let i = 1; i < arguments.length; ++i) {
args[i - 1] = arguments[i];
}
}
addEventListener(type, callback) {
if (!this[initialized] || !(this instanceof EventTarget)) {
throw new TypeError("Illegal invocation");
}
if (arguments.length < 2) {
throw new TypeError("Failed to execute 'addEventListener' on 'EventTarget': 2 argument required, but only " + arguments.length + " present.");
}
const args = [];
for (let i = 0; i < arguments.length && i < 3; ++i) {
args[i] = arguments[i];
}
args[0] = conversions["DOMString"](args[0]);
return Impl.interface.prototype.addEventListener.apply(this, args);
}
removeEventListener(type, callback) {
if (!this[initialized] || !(this instanceof EventTarget)) {
throw new TypeError("Illegal invocation");
}
if (arguments.length < 2) {
throw new TypeError("Failed to execute 'removeEventListener' on 'EventTarget': 2 argument required, but only " + arguments.length + " present.");
}
const args = [];
for (let i = 0; i < arguments.length && i < 3; ++i) {
args[i] = arguments[i];
}
args[0] = conversions["DOMString"](args[0]);
return Impl.interface.prototype.removeEventListener.apply(this, args);
}
dispatchEvent(event) {
if (!this[initialized] || !(this instanceof EventTarget)) {
throw new TypeError("Illegal invocation");
}
if (arguments.length < 1) {
throw new TypeError("Failed to execute 'dispatchEvent' on 'EventTarget': 1 argument required, but only " + arguments.length + " present.");
}
const args = [];
for (let i = 0; i < arguments.length && i < 1; ++i) {
args[i] = arguments[i];
}
return Impl.interface.prototype.dispatchEvent.apply(this, args);
}
}
module.exports = {
init() {
let obj = this;
if (!obj) {
obj = Object.create(EventTarget.prototype);
}
this[initialized] = true;
return obj;
},
interface: EventTarget,
expose: {
Window: { EventTarget: EventTarget },
Worker: { EventTarget: EventTarget }
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment