Skip to content

Instantly share code, notes, and snippets.

@lloydwatkin
Created May 2, 2014 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lloydwatkin/e1da3b76f65ffff16e27 to your computer and use it in GitHub Desktop.
Save lloydwatkin/e1da3b76f65ffff16e27 to your computer and use it in GitHub Desktop.
Making tea
#!/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