Skip to content

Commit

Permalink
updating documentation and benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
arjunmehta committed Dec 19, 2014
1 parent b20187d commit 55224a1
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 52 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down
19 changes: 15 additions & 4 deletions lib/beatevent.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
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(){

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;
98 changes: 60 additions & 38 deletions lib/heart.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,52 @@ 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) {
hearts[this.id] = undefined;
}
};


// Pulses

Heart.prototype.newPulse = Heart.prototype.createPulse = function(name) {
this.killPulse(name);
var pulse = new Pulse(this, name);
Expand All @@ -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();
}
}
}
};
Expand Down
3 changes: 1 addition & 2 deletions lib/pulse.js
Original file line number Diff line number Diff line change
@@ -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", {
Expand Down
14 changes: 7 additions & 7 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit 55224a1

Please sign in to comment.