Created
May 25, 2012 13:39
-
-
Save Fonsan/2788223 to your computer and use it in GitHub Desktop.
Mock window
This file contains 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 createWindow = function() { | |
var events = kafkaFactory(); | |
var register = function(wantedObj, wantedType, callback) { | |
events.register(function(obj, type, event) { | |
if (wantedObj === obj && wantedType === type) { | |
callback(event); | |
} | |
}); | |
}; | |
var evented = function() { | |
var obj = {}; | |
obj.addEventListener = function(type, callback) { | |
register(obj, type, callback); | |
}; | |
obj.fireEvent = function(event) { | |
events.invoke(obj, event.type, event); | |
}; | |
return obj; | |
}; | |
var win = evented(); | |
win.document = evented(); | |
win.document.body = evented(); | |
win.timeOuts = {}; | |
win.timeOutCallbacks = {}; | |
var id = 0; | |
win.setTimeout = function(callback, time) { | |
win.timeOuts[time] = win.timeOuts[time] || []; | |
var i = id++; | |
win.timeOutCallbacks[i] = callback | |
win.timeOuts[time].push(i); | |
}; | |
win.time = 0; | |
win.tick = function(t) { | |
each(win.timeOuts, function(v, k) { | |
if (k > win.time && k <= t) { | |
var callback = win.timeOutCallbacks[v]; | |
if (callback) { | |
callback(); | |
} | |
} | |
}); | |
}; | |
win.clearTimeout = function(id) { | |
var cbs = win.timeOutCallbacks; | |
delete cbs[id]; | |
}; | |
return win; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment