-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
revising API, fixing tests, getting ready for new release
- Loading branch information
1 parent
55224a1
commit 1d78dca
Showing
6 changed files
with
123 additions
and
76 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,36 @@ | ||
var idCount = 0; | ||
|
||
function BeatEvent(heart, interval, name, repeat, fn){ | ||
this.id = name; | ||
this.heart = heart; | ||
function BeatEvent(heart, interval, name, repeat, fn) { | ||
this.id = name || "event_" + (Math.random()).toString(36) + idCount++; | ||
this.heart = heart; | ||
|
||
this.repeat = repeat; | ||
this.executionCount = 0; | ||
this.repeat = repeat || 0; | ||
this.executionCount = 0; | ||
|
||
this.done = false; | ||
this.count = 0; | ||
this.interval = Math.round(interval, 10); | ||
this.fn = fn; | ||
this.done = false; | ||
this.count = 0; | ||
this.schedule = Math.round(interval, 10); | ||
this.fn = fn; | ||
} | ||
|
||
BeatEvent.prototype.execute = function(){ | ||
|
||
this.count++; | ||
BeatEvent.prototype.execute = function() { | ||
|
||
if(this.count === this.interval){ | ||
this.executionCount++; | ||
this.fn(this.heart.heartbeat); | ||
this.count = 0; | ||
this.count++; | ||
|
||
if(this.repeat !== 0 && this.executionCount >= this.repeat){ | ||
this.done = true; | ||
if (this.count === this.schedule) { | ||
this.executionCount++; | ||
if (this.repeat !== 0 && this.executionCount >= this.repeat) { | ||
this.done = true; | ||
this.fn(this.heart.heartbeat, true); | ||
} else { | ||
this.fn(this.heart.heartbeat, false); | ||
} | ||
this.count = 0; | ||
} | ||
} | ||
}; | ||
|
||
BeatEvent.prototype.kill = function() { | ||
this.heart.killEvent(this.id); | ||
this.heart.killEvent(this.id, this); | ||
}; | ||
|
||
exports = module.exports = BeatEvent; | ||
exports = module.exports = BeatEvent; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
var Pulse = require("./pulse.js"); | ||
var BeatEvent = require("./beatevent.js"); | ||
|
||
var idCount = 0; | ||
var hearts = {}; | ||
var idCount = 0, | ||
hearts; | ||
|
||
function initialize(global_hearts) { | ||
hearts = global_hearts; | ||
return Heart; | ||
} | ||
|
||
|
||
// Heart | ||
|
||
function Heart(heartrate, name) { | ||
|
||
this.heartbeat = 0; | ||
|
@@ -61,9 +64,11 @@ 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); | ||
this.pulses[pulse.id] = pulse; | ||
if (name) { | ||
this.killPulse(name); | ||
this.pulses[pulse.id] = pulse; | ||
} | ||
return pulse; | ||
}; | ||
|
||
|
@@ -87,14 +92,17 @@ Heart.prototype.createEvent = function(modulo, options, fn) { | |
options = {}; | ||
} | ||
|
||
var name = options.name || "event_" + (Math.random()).toString(36) + idCount++; | ||
var repeat = options.repeat || 0; | ||
var name = options.name; | ||
var repeat = options.repeat; | ||
var event = new BeatEvent(this, modulo, name, repeat, fn); | ||
|
||
prepEvents(this); | ||
this.killEvent(name); | ||
prepHeartForEvents(this); | ||
|
||
if (name) { | ||
this.killEvent(name); | ||
this.eventList[name] = event; | ||
} | ||
|
||
var event = new BeatEvent(this, modulo, name, repeat, fn); | ||
this.eventList[name] = event; | ||
this.events.push(event); | ||
event.index = this.events.length - 1; | ||
|
||
|
@@ -105,11 +113,14 @@ Heart.prototype.event = function(name) { | |
return this.eventList[name]; | ||
}; | ||
|
||
Heart.prototype.killEvent = function(name) { | ||
Heart.prototype.killEvent = function(name, beatevent) { | ||
if (this.eventList[name] !== undefined) { | ||
this.events[this.eventList[name].index] = undefined; | ||
this.eventList[name] = undefined; | ||
} | ||
else{ | ||
this.events[beatevent.index] = undefined; | ||
} | ||
}; | ||
|
||
Heart.prototype.killAllEvents = function() { | ||
|
@@ -120,7 +131,7 @@ Heart.prototype.killAllEvents = function() { | |
this.interval = setInterval(createInterval(this), this.heartrate); | ||
}; | ||
|
||
function prepEvents(heart) { | ||
function prepHeartForEvents(heart) { | ||
if (heart.eventsExist === false) { | ||
heart.eventsExist = true; | ||
clearInterval(heart.interval); | ||
|
@@ -129,8 +140,7 @@ function prepEvents(heart) { | |
} | ||
|
||
|
||
|
||
// Heat Beat Loop | ||
// Heart Beat Loop | ||
|
||
function createInterval(heart) { | ||
|
||
|
@@ -144,11 +154,12 @@ function createInterval(heart) { | |
return function() { | ||
|
||
heart.heartbeat++; | ||
|
||
for (var i = 0; i < events.length; i++) { | ||
if (events[i] !== undefined) { | ||
events[i].execute(); | ||
if (events[i].done === true) { | ||
console.log("DONE DONE DONE EVENT"); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
arjunmehta
Author
Owner
|
||
events[i].kill(); | ||
} | ||
} | ||
|
This file contains 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
This file contains 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
Oops, something went wrong.
Why is the console.log line here? If we get to this point, and the modules writes to the console, have we done something incorrectly, or should this line have been removed prior to the commit?