Created
May 2, 2014 09:53
-
-
Save lloydwatkin/e1da3b76f65ffff16e27 to your computer and use it in GitHub Desktop.
Making tea
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
#!/usr/local/bin/node | |
function Mug() { | |
}; | |
Mug.prototype.drink = {}; | |
Mug.prototype.TEA_COLD_TIME = 6000; | |
Mug.prototype.TEABAG_BREW_TIME = 2000; | |
Mug.prototype.on = function(call, event) { | |
if (!Mug.prototype.events) { | |
Mug.prototype.events = []; | |
} | |
Mug.prototype.events[call] = event; | |
}; | |
Mug.prototype.emit = function(call) { | |
console.log("Event fired: " + call); | |
if (!Mug.prototype.events[call]) { | |
console.log("I don't know that event!"); | |
return; | |
} | |
console.log("'" + call + "' event happened"); | |
Mug.prototype.events[call].call(); | |
}; | |
Mug.prototype.fill = function(drink) { | |
thirstQuencher = drink.call(); | |
this.drink = thirstQuencher; | |
console.log("Mug filled with some " + thirstQuencher.name); | |
console.log(this.TEA_COLD_TIME); | |
}; | |
Mug.prototype.hasMilk = false; | |
Mug.prototype.sugarCount = 0; | |
mug = new Mug(); | |
mug.on("empty", function() { | |
console.log("Oh dear, mug is empty!"); | |
drink = function() { | |
water = { temperature: '100'}; | |
teabag = setTimeout(function() { console.log("Removing teabag"); return teabag; }, this.TEABAG_BREW_TIME); | |
extras = {}; | |
if (this.hasMilk) extras.milk = true; | |
if (this.sugarCount > 0) extras.sugar = sugarCount; | |
return {'name': 'tea', 'water':water, 'main':teabag, 'extra':extras }; | |
} | |
mug.fill(drink); | |
}); | |
mug.on("cold", function() { | |
console.log("Damn fool you let your " + this.drink.name + " go cold!"); | |
}); | |
setTimeout(function() { | |
console.log("I'm drinking tea"); | |
mug.emit('empty'); | |
setTimeout(function() { mug.emit("cold"); }, this.TEA_COLD_TIME); | |
}, 3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment