-
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.
- Loading branch information
1 parent
5c5d2e3
commit 78aa04b
Showing
3 changed files
with
138 additions
and
143 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,37 @@ | ||
let idCount = 0; | ||
|
||
function BeatEvent(heart, interval, name, countTo, fn) { | ||
this.id = name || `event_${(Math.random()).toString(36)}${idCount += 1}`; | ||
this.heart = heart; | ||
class BeatEvent { | ||
constructor(heart, interval, name, countTo, fn) { | ||
this.id = name || `event_${(Math.random()).toString(36)}${idCount += 1}`; | ||
this.heart = heart; | ||
|
||
this.countTo = countTo || 0; | ||
this.executionCount = 0; | ||
this.countTo = countTo || 0; | ||
this.executionCount = 0; | ||
|
||
this.done = false; | ||
this.count = 0; | ||
this.schedule = 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 += 1; | ||
execute() { | ||
this.count += 1; | ||
|
||
if (this.count === this.schedule) { | ||
this.executionCount += 1; | ||
if (this.countTo !== 0 && this.executionCount >= this.countTo) { | ||
this.done = true; | ||
this.fn(this.executionCount, true); | ||
} else { | ||
this.fn(this.executionCount, false); | ||
if (this.count === this.schedule) { | ||
this.executionCount += 1; | ||
if (this.countTo !== 0 && this.executionCount >= this.countTo) { | ||
this.done = true; | ||
this.fn(this.executionCount, true); | ||
} else { | ||
this.fn(this.executionCount, false); | ||
} | ||
this.count = 0; | ||
} | ||
this.count = 0; | ||
} | ||
}; | ||
|
||
BeatEvent.prototype.kill = function () { | ||
this.heart.killEvent(this.id, this); | ||
}; | ||
kill() { | ||
this.heart.killEvent(this.id, this); | ||
} | ||
} | ||
|
||
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
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,39 +1,36 @@ | ||
let idCount = 0; | ||
|
||
function Pulse(homeheart, name) { | ||
this.id = name || `pulse_${(Math.random()).toString(36)}${idCount += 1}`; | ||
this.heart = homeheart; | ||
this.heartbeat = homeheart.heartbeat; | ||
} | ||
class Pulse { | ||
constructor(homeheart, name) { | ||
this.id = name || `pulse_${(Math.random()).toString(36)}${idCount += 1}`; | ||
this.heart = homeheart; | ||
this.heartbeat = homeheart.heartbeat; | ||
} | ||
|
||
Object.defineProperty(Pulse.prototype, 'missedBeats', { | ||
enumerable: true, | ||
get() { | ||
get missedBeats() { | ||
return this.heart.heartbeat - this.heartbeat; | ||
}, | ||
}); | ||
} | ||
|
||
Object.defineProperty(Pulse.prototype, 'lag', { | ||
enumerable: true, | ||
get() { | ||
get lag() { | ||
return this.missedBeats * this.heart.heartrate; | ||
}, | ||
}); | ||
} | ||
|
||
Pulse.prototype.beat = function () { | ||
this.heartbeat = this.heart.heartbeat; | ||
return this; | ||
}; | ||
beat() { | ||
this.heartbeat = this.heart.heartbeat; | ||
return this; | ||
} | ||
|
||
over(threshold) { | ||
if (this.heart.heartbeat - this.heartbeat > threshold) { | ||
return true; | ||
} | ||
|
||
Pulse.prototype.over = function (threshold) { | ||
if (this.heart.heartbeat - this.heartbeat > threshold) { | ||
return true; | ||
return false; | ||
} | ||
return false; | ||
}; | ||
|
||
Pulse.prototype.kill = Pulse.prototype.destroy = function () { | ||
this.heart.killPulse(this.id); | ||
}; | ||
kill() { | ||
this.heart.killPulse(this.id); | ||
} | ||
} | ||
|
||
module.exports = Pulse; |