Skip to content

Instantly share code, notes, and snippets.

@coolwanglu
Last active December 30, 2015 06:09
Show Gist options
  • Save coolwanglu/7786903 to your computer and use it in GitHub Desktop.
Save coolwanglu/7786903 to your computer and use it in GitHub Desktop.
Hynopic Demos
/*
* Hypnotic can be extended
* Here we have a simple semaphore introduced
* which supports only one producer/consumer
*/
// jQuery still works, of course!
function print(msg){ $('#output').html(msg); }
/*
* Hypnotic extensions must be run outside Hypnotic!
*/
var semaphore_def = [' function SemaphoreOne() { };',
'SemaphoreOne.prototype = {',
' wait: new Hypnotic(function(cb) { this.cb = cb; }),',
' release: function() { if(this.cb) { setTimeout(this.cb, 1); this.cb = null; }}',
'}'];
window.eval(semaphore_def.join(''));
var sema = new SemaphoreOne();
$(document).on('keypress', function(){
sema.release();
});
print('Press any key to continue.');
sema.wait(); // hmm... How can you do this without a sync sleep() ?
print('Got it!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment