Last active
August 29, 2015 14:03
-
-
Save Sebmaster/4dc772b7dcfa2bd6aab0 to your computer and use it in GitHub Desktop.
jsdom new structure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, {}); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
General idea seems sound. Some small tweaks:
createInstance
->create
;initInstance
->init
;util.createInstance
->util.creatorFactory
or maybeutil.makeCreator
constructor
is supposed to be non-enumerable"use strict"
everywhereEventTarget.EventTarget
is not great, instead doeventTargetImpl.EventTarget
or similar.