Created
March 14, 2011 06:38
-
-
Save duncanbeevers/868846 to your computer and use it in GitHub Desktop.
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
function AsynchDispatcher() { | |
var listeners = [], | |
m = 'message', | |
w = window; | |
function receiveMessage(index) { | |
index = parseInt(index.data || index); | |
setTimeout(listeners[index], 10); | |
if (index) { | |
w.postMessage(index - 1, location.origin); | |
} else { | |
w.removeEventListener(m, receiveMessage, false); | |
} | |
} | |
function add(listener) { | |
listeners.push(listener); | |
} | |
function fire() { | |
w.addEventListener(m, receiveMessage, false) | |
receiveMessage(listeners.length - 1); | |
} | |
this.add = add; | |
this.fire = fire; | |
} |
Author
duncanbeevers
commented
Mar 14, 2011
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment