Last active
August 29, 2015 14:20
-
-
Save formula1/3e71972cfcc209d6639e to your computer and use it in GitHub Desktop.
Polling in the Dom
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
//Event Emitting via polling | |
var isHovering = false; | |
var hoverButton = find(button); | |
setInterval(function(){ | |
if(!(hoverButton.top < Mouse.y < hoverButton.top+hoverButton.height)){ | |
if(!isHovering) return; | |
isHovering = false; | |
return stopAction(); | |
} | |
if(!(hoverButton.left < Mouse.x < hoverButton.left+hoverButton.width)){ | |
if(!isHovering) return; | |
isHovering = false; | |
return stopAction(); | |
} | |
if(isHovering) return; | |
isHovering = true | |
return doAction(); | |
},100); |
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
//Worker queue via polling | |
var jobQueue = []; | |
var workers = []; | |
setInterval(function(){ | |
var l = jobQueue.length; | |
while(l--){ | |
var worker = workers.some(function(worker){ | |
return !worker.isBusy; | |
}); | |
if(!worker) return; | |
} | |
},100); | |
//Worker queue via event emitters | |
var ee = new EventEmitter(); | |
var queued = []; | |
var available = []; | |
ee.on("new-job",function(job){ | |
if(available.length === 0) return queued.push(job); | |
available.pop().doWork(job); | |
}); | |
ee.on("worker-available",function(worker){ | |
if(queued.length === 0) return available.push(worker); | |
worker.doWork(queued.shift()); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment