From 55224a1f84e29739ba0588e49d7a66d95443b595 Mon Sep 17 00:00:00 2001 From: Arjun Mehta Date: Fri, 19 Dec 2014 16:15:10 -0400 Subject: [PATCH] updating documentation and benchmarks --- README.md | 4 +- lib/beatevent.js | 19 ++++++++-- lib/heart.js | 98 +++++++++++++++++++++++++++++------------------- lib/pulse.js | 3 +- test/test.js | 14 +++---- 5 files changed, 86 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index 095f65f..3dab770 100644 --- a/README.md +++ b/README.md @@ -249,7 +249,9 @@ This method is slightly different from the other creation methods (ie. `createHe This method will add a reoccuring event to the heart. Every `nth` beat specified by `beatInterval` will execute the supplied function. This method counts from the time you add the event. It's kind of like `setInterval`. -Use the options to set the `name` (so you can reference it, kill it, or modify it), and the number of times to `repeat` the event (0 for infinite). +##### Options +`name`: Give the Event a custom name, so you can reference it, kill it, or modify it using `heart.event(name)` +`repeat`: Repeat the event a specified number of times (use `0` for infinite). By default, this is set to `0` or infinite. If set to a finite number, the event will be killed and cleared from memory once executed the last time. ```javascript var event = heartbeats.heart("heartA").createEvent(5, {name: "checkA", repeat: 0}, function(){ diff --git a/lib/beatevent.js b/lib/beatevent.js index 777774c..93f2d98 100644 --- a/lib/beatevent.js +++ b/lib/beatevent.js @@ -1,10 +1,16 @@ -function BeatEvent(heart, interval, once, fn){ +var idCount = 0; + +function BeatEvent(heart, interval, name, repeat, fn){ + this.id = name; this.heart = heart; - this.once = once || false; + + this.repeat = repeat; + this.executionCount = 0; + this.done = false; this.count = 0; this.interval = Math.round(interval, 10); - this.fn = fn; + this.fn = fn; } BeatEvent.prototype.execute = function(){ @@ -12,13 +18,18 @@ BeatEvent.prototype.execute = function(){ this.count++; if(this.count === this.interval){ + this.executionCount++; this.fn(this.heart.heartbeat); this.count = 0; - if(this.once === true){ + if(this.repeat !== 0 && this.executionCount >= this.repeat){ this.done = true; } } }; +BeatEvent.prototype.kill = function() { + this.heart.killEvent(this.id); +}; + exports = module.exports = BeatEvent; \ No newline at end of file diff --git a/lib/heart.js b/lib/heart.js index c970d60..30faf0d 100644 --- a/lib/heart.js +++ b/lib/heart.js @@ -14,32 +14,42 @@ function Heart(heartrate, name) { this.heartbeat = 0; this.events = []; + this.eventList = {}; this.eventsExist = false; - this.id = name || "heart_" + (Math.random()).toString(36) + idCount; + this.id = name || "heart_" + (Math.random()).toString(36) + idCount++; this.heartrate = heartrate || 2500; this.pulses = {}; this.interval = setInterval(createInterval(this), heartrate); - - idCount++; } Object.defineProperty(Heart.prototype, "age", { enumerable: true, - get: function() { + get: function() { return this.heartbeat; } }); Object.defineProperty(Heart.prototype, "name", { enumerable: true, - get: function() { + get: function() { return this.id; } }); +Heart.prototype.setHeartrate = function(heartrate) { + + if (heartrate) { + this.heartrate = heartrate; + clearInterval(this.interval); + this.interval = setInterval(createInterval(this), heartrate); + } + + return this; +}; + Heart.prototype.kill = Heart.prototype.destroy = function() { clearInterval(this.interval); if (hearts[this.id] !== undefined) { @@ -47,6 +57,9 @@ Heart.prototype.kill = Heart.prototype.destroy = function() { } }; + +// Pulses + Heart.prototype.newPulse = Heart.prototype.createPulse = function(name) { this.killPulse(name); var pulse = new Pulse(this, name); @@ -64,71 +77,80 @@ Heart.prototype.pulse = function(name) { return this.pulses[name]; }; -Heart.prototype.setHeartrate = function(heartrate) { - if (heartrate) { - this.heartrate = heartrate; - clearInterval(this.interval); - this.interval = setInterval(createInterval(this), heartrate); - } +// Events - return this; -}; +Heart.prototype.createEvent = function(modulo, options, fn) { + if (typeof options === 'function') { + fn = options; + options = {}; + } -Heart.prototype.setInterval = function(fn, modulo) { - this.onBeat(modulo, fn); -}; + var name = options.name || "event_" + (Math.random()).toString(36) + idCount++; + var repeat = options.repeat || 0; -Heart.prototype.onBeat = function(modulo, fn) { - this.prepEvents(); - this.events.push(new BeatEvent(this, modulo, false, fn)); -}; + prepEvents(this); + this.killEvent(name); -Heart.prototype.setTimeout = function(fn, modulo) { - this.onceOnBeat(modulo, fn); -}; + var event = new BeatEvent(this, modulo, name, repeat, fn); + this.eventList[name] = event; + this.events.push(event); + event.index = this.events.length - 1; -Heart.prototype.onceOnBeat = function(modulo, fn) { - this.prepEvents(); - this.events.push(new BeatEvent(this, modulo, true, fn)); + return event; }; -Heart.prototype.prepEvents = function() { +Heart.prototype.event = function(name) { + return this.eventList[name]; +}; - if (this.eventsExist === false) { - this.eventsExist = true; - clearInterval(this.interval); - this.interval = setInterval(createInterval(this), this.heartrate); +Heart.prototype.killEvent = function(name) { + if (this.eventList[name] !== undefined) { + this.events[this.eventList[name].index] = undefined; + this.eventList[name] = undefined; } }; -Heart.prototype.clearEvents = function() { +Heart.prototype.killAllEvents = function() { this.eventsExist = false; this.events = []; + this.eventList = {}; clearInterval(this.interval); this.interval = setInterval(createInterval(this), this.heartrate); }; +function prepEvents(heart) { + if (heart.eventsExist === false) { + heart.eventsExist = true; + clearInterval(heart.interval); + heart.interval = setInterval(createInterval(heart), heart.heartrate); + } +} + + + +// Heat Beat Loop function createInterval(heart) { var events = heart.events; if (heart.eventsExist === false) { - return function() { heart.heartbeat++; }; } else { - return function() { + heart.heartbeat++; + for (var i = 0; i < events.length; i++) { - events[i].execute(); - if (events[i].done === true) { - events.splice(i, 1); - i--; + if (events[i] !== undefined) { + events[i].execute(); + if (events[i].done === true) { + events[i].kill(); + } } } }; diff --git a/lib/pulse.js b/lib/pulse.js index 876211a..71546b5 100644 --- a/lib/pulse.js +++ b/lib/pulse.js @@ -1,10 +1,9 @@ var idCount = 0; function Pulse(homeheart, name) { - this.id = name || "pulse_" + (Math.random()).toString(36) + idCount; + this.id = name || "pulse_" + (Math.random()).toString(36) + idCount++; this.heart = homeheart; this.heartbeat = homeheart.heartbeat; - idCount++; } Object.defineProperty(Pulse.prototype, "missedBeats", { diff --git a/test/test.js b/test/test.js index 1b19518..08cd803 100644 --- a/test/test.js +++ b/test/test.js @@ -61,13 +61,13 @@ exports.addEvent = function(test) { var presentInit = heartbeats.heart("globalBeat").heartbeat; - heartbeats.heart("globalBeat").onBeat(3, function(heartbeat) { + heartbeats.heart("globalBeat").createEvent(3, function(heartbeat) { console.log("onBeat 3", heartbeat - presentInit); test.equal(true, true); }); - heartbeats.heart("globalBeat").onBeat(7, function(heartbeat) { + heartbeats.heart("globalBeat").createEvent(7, function(heartbeat) { console.log("onBeat 7", heartbeat - presentInit); test.equal(true, true); test.done(); @@ -91,22 +91,22 @@ exports.addSingleEvent = function(test) { var presentInit = heartbeats.heart("globalBeat").heartbeat; - heartbeats.heart("globalBeat").onceOnBeat(1, function(heartbeat) { + heartbeats.heart("globalBeat").createEvent(1, {repeat: 1}, function(heartbeat) { console.log("onceOnBeat 1", heartbeat - presentInit); test.equal(true, true); }); - heartbeats.heart("globalBeat").onceOnBeat(2, function(heartbeat) { + heartbeats.heart("globalBeat").createEvent(2, {repeat: 1}, function(heartbeat) { console.log("onceOnBeat 2", heartbeat - presentInit); test.equal(true, true); }); - heartbeats.heart("globalBeat").onceOnBeat(2, function(heartbeat) { + heartbeats.heart("globalBeat").createEvent(2, {repeat: 1}, function(heartbeat) { console.log("onceOnBeat 2", heartbeat - presentInit); test.equal(true, true); }); - heartbeats.heart("globalBeat").onceOnBeat(3, function(heartbeat) { + heartbeats.heart("globalBeat").createEvent(3, {repeat: 1}, function(heartbeat) { console.log("onceOnBeat 3", heartbeat - presentInit); test.equal(true, true); test.done(); @@ -116,7 +116,7 @@ exports.addSingleEvent = function(test) { exports.removeEvents = function(test) { test.expect(1); - heartbeats.heart("globalBeat").clearEvents(); + heartbeats.heart("globalBeat").killAllEvents(); test.equal(heartbeats.heart("globalBeat").events.length, 0); test.done();