Skip to content

Instantly share code, notes, and snippets.

@Sebmaster
Last active August 29, 2015 14:03
Show Gist options
  • Save Sebmaster/4dc772b7dcfa2bd6aab0 to your computer and use it in GitHub Desktop.
Save Sebmaster/4dc772b7dcfa2bd6aab0 to your computer and use it in GitHub Desktop.
jsdom new structure
var WeakMap = require("weak-map");
var utils = require("./utils");
var Node = require("./Node");
var instances = new WeakMap();
function Document() {
throw new TypeError("Illegal constructor");
}
utils.inherits(Document, Node.Node);
exports.Document = Document;
exports.initInstance = function (instance) {
Node.initInstance(instance);
instances.set(instance, {});
};
exports.createInstance = utils.createInstance(Document, exports.initInstance);
var WeakMap = require("weak-map");
var instances = new WeakMap();
function EventTarget() {
throw new TypeError("Illegal constructor");
}
EventTarget.prototype.addEventListener = function (type, listener, useCapture) {
useCapture = !!useCapture;
var listeners = instances.get(this).listeners;
if (!listeners[type]) listeners[type] = [];
listeners[type].push({ fn: listener, useCapture: useCapture });
};
EventTarget.prototype.removeEventListener = function (type, listener, useCapture) {
useCapture = !!useCapture;
var listeners = instances.get(this).listeners[type];
if (!listeners) return;
for (var i = 0; i < listeners.length; ++i) {
if (listeners[i].fn === listener && listeners[i].useCapture === useCapture) {
listeners.splice(i, 1);
return;
}
}
};
EventTarget.prototype.dispatchEvent = function (evt) {
var listeners = instances.get(this).listeners[evt.name];
if (!listeners) return;
for (var i = 0; i < listeners.length; ++i) {
listeners[i].fn();
}
};
exports.initInstance = function (instance) {
instances.set(instance, { listeners: {} });
};
exports.EventTarget = EventTarget;
var WeakMap = require("weak-map");
var utils = require("./utils");
var Document = require("./Document");
var instances = new WeakMap();
function HTMLDocument() {
throw new TypeError("Illegal constructor");
}
utils.inherits(HTMLDocument, Document.Document);
exports.HTMLDocument = HTMLDocument;
exports.initInstance = function (instance) {
Document.initInstance(instance);
instances.set(instance, {});
};
exports.createInstance = utils.createInstance(HTMLDocument, exports.initInstance);
var WeakMap = require("weak-map");
var utils = require("./utils");
var EventTarget = require('./EventTarget');
var instances = new WeakMap();
function Node() {
throw new TypeError('Illegal constructor');
}
utils.inherits(Node, EventTarget.EventTarget);
exports.Node = Node;
exports.initInstance = function (instance) {
EventTarget.initInstance(instance);
instances.set(instance, {});
};
var HTMLDocumentImpl = require("./lib/interfaces/HTMLDocument");
var document = HTMLDocumentImpl.createInstance();
document.addEventListener("click", function () {
console.log("dispatched");
});
var ref = function () {
console.log("dispatched2");
};
document.addEventListener("click", ref);
document.removeEventListener("click", ref);
document.dispatchEvent({ name: "click" });
// Outputs: dispatched
exports.inherits = function (to, from) {
to.prototype = Object.create(from.prototype);
to.prototype.constructor = to;
};
exports.createInstance = function (obj, init) {
return function () {
var instance = Object.create(obj.prototype);
if (init) init(instance);
return instance;
};
};
@domenic
Copy link

domenic commented Jul 15, 2014

General idea seems sound. Some small tweaks:

  • createInstance -> create; initInstance -> init; util.createInstance -> util.creatorFactory or maybe util.makeCreator
  • I am pretty sure constructor is supposed to be non-enumerable
  • "use strict" everywhere
  • I am not sure uppercase for filenames is worth the trouble... but it could be nice, hmm.
  • Only classes should be uppercased, so EventTarget.EventTarget is not great, instead do eventTargetImpl.EventTarget or similar.
  • This function might be handy:
// utils.js
exports.makePrivatesGetter = function (instances) {
  return function (value) {
    return instances.get(value);
  };
};

// usage:
var instances = new WeakMap();
var p = makePrivatesGetter(instances);

// ... later ...
p(this).listeners[evt.name]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment