diff --git a/lib/beatevent.js b/lib/beatevent.js index 49120fe..5fb87ca 100644 --- a/lib/beatevent.js +++ b/lib/beatevent.js @@ -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; diff --git a/lib/heart.js b/lib/heart.js index 6a586dc..e921c6b 100644 --- a/lib/heart.js +++ b/lib/heart.js @@ -11,140 +11,136 @@ function initialize(globalHearts) { // Heart -function Heart(heartrate, name) { - this.heartbeat = 0; +class Heart { + constructor(heartrate, name) { + this.heartbeat = 0; - this.events = []; - this.eventList = {}; + this.events = []; + this.eventList = {}; - this.id = name || `heart_${(Math.random()).toString(36)}${idCount += 1}`; - this.heartrate = heartrate || 2500; + this.id = name || `heart_${(Math.random()).toString(36)}${idCount += 1}`; + this.heartrate = heartrate || 2500; - this.pulses = {}; + this.pulses = {}; - this.interval = setInterval(createInterval(this), heartrate); + this.interval = setInterval(createInterval(this), heartrate); - unreference(this); -} + unreference(this); + } -Object.defineProperty(Heart.prototype, 'age', { - enumerable: true, - get() { + get age() { return this.heartbeat; - }, -}); + } -Object.defineProperty(Heart.prototype, 'name', { - enumerable: true, - get() { + get name() { return this.id; - }, -}); + } -Heart.prototype.setHeartrate = function (heartrate) { - if (heartrate) { - this.heartrate = heartrate; + setHeartrate(heartrate) { + if (heartrate) { + this.heartrate = heartrate; - clearInterval(this.interval); - this.interval = setInterval(createInterval(this), heartrate); + clearInterval(this.interval); + this.interval = setInterval(createInterval(this), heartrate); - if (this.events.length === 0) { - unreference(this); + if (this.events.length === 0) { + unreference(this); + } } - } - return this; -}; + return this; + } -Heart.prototype.kill = Heart.prototype.destroy = function () { - clearInterval(this.interval); + kill() { + clearInterval(this.interval); - if (hearts[this.id] !== undefined) { - hearts[this.id] = undefined; + if (hearts[this.id] !== undefined) { + hearts[this.id] = undefined; + } } -}; -// Pulses + // Pulses -Heart.prototype.newPulse = Heart.prototype.createPulse = function (name) { - const pulse = new Pulse(this, name); + createPulse(name) { + const pulse = new Pulse(this, name); - if (name) { - this.killPulse(name); - this.pulses[pulse.id] = pulse; + if (name) { + this.killPulse(name); + this.pulses[pulse.id] = pulse; + } + + return pulse; } - return pulse; -}; + killPulse(name) { + if (this.pulses[name] !== undefined) { + this.pulses[name] = undefined; + } + } -Heart.prototype.killPulse = function (name) { - if (this.pulses[name] !== undefined) { - this.pulses[name] = undefined; + pulse(name) { + return this.pulses[name]; } -}; -Heart.prototype.pulse = function (name) { - return this.pulses[name]; -}; + // Events -// Events + createEvent(modulo, options, fn) { + if (typeof options === 'function') { + fn = options; + options = {}; + } -Heart.prototype.createEvent = function (modulo, options, fn) { - if (typeof options === 'function') { - fn = options; - options = {}; - } + const { name } = options; + const countTo = options.countTo || options.repeat; // repeat to be deprecated + const event = new BeatEvent(this, modulo, name, countTo, fn); - const { name } = options; - const countTo = options.countTo || options.repeat; // repeat to be deprecated - const event = new BeatEvent(this, modulo, name, countTo, fn); + if (name !== undefined) { + this.killEvent(name); + this.eventList[name] = event; + } - if (name !== undefined) { - this.killEvent(name); - this.eventList[name] = event; - } + this.events.push(event); - this.events.push(event); + if (this.interval.ref) { + this.interval.ref(); + } - if (this.interval.ref) { - this.interval.ref(); + return event; } - return event; -}; + event(name) { + return this.eventList[name]; + } -Heart.prototype.event = function (name) { - return this.eventList[name]; -}; + killEvent(name, beatevent) { + let idx; + + if (this.eventList[name] !== undefined) { + idx = this.events.indexOf(this.eventList[name]); + this.eventList[name] = undefined; + } else if (beatevent !== undefined) { + if (beatevent.id) { + this.eventList[beatevent.id] = undefined; + } + idx = this.events.indexOf(beatevent); + } -Heart.prototype.killEvent = function (name, beatevent) { - let idx; + if (idx > -1) { + this.events.splice(idx, 1); + } - if (this.eventList[name] !== undefined) { - idx = this.events.indexOf(this.eventList[name]); - this.eventList[name] = undefined; - } else if (beatevent !== undefined) { - if (beatevent.id) { - this.eventList[beatevent.id] = undefined; + if (this.events.length === 0) { + unreference(this); } - idx = this.events.indexOf(beatevent); } - if (idx > -1) { - this.events.splice(idx, 1); - } + killAllEvents() { + this.events = []; + this.eventList = {}; - if (this.events.length === 0) { unreference(this); } -}; - -Heart.prototype.killAllEvents = function () { - this.events = []; - this.eventList = {}; - - unreference(this); -}; +} function createInterval(heart) { return function () { diff --git a/lib/pulse.js b/lib/pulse.js index 2ae5160..3f601c3 100644 --- a/lib/pulse.js +++ b/lib/pulse.js @@ -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; diff --git a/test/all.test.js b/test/all.test.js index d6e7f4c..db10887 100644 --- a/test/all.test.js +++ b/test/all.test.js @@ -20,15 +20,6 @@ describe('module functions', () => { }); describe('heart basics', () => { - test('unreference heart', (done) => { - const unrefTest = spawn('node', ['./test/spawn.js'], { stdio: 'inherit' }); - - unrefTest.on('exit', (code) => { - expect(code).toBe(0); - done(); - }); - }); - test('create heart', (done) => { const heart = heartbeats.createHeart(1000, 'globalBeat'); @@ -47,35 +38,6 @@ describe('heart basics', () => { }); }); -test('pulses', (done) => { - const heart = heartbeats.createHeart(1000, 'globalBeat'); - - const pulses = { - pulseA: heart.createPulse(), - pulseB: heart.createPulse(), - }; - - const iA = setInterval(() => { - pulses.pulseA.beat(); - }, 500); - - const iB = setInterval(() => { - pulses.pulseB.beat(); - }, 5000); - - setTimeout(() => { - clearInterval(iA); - clearInterval(iB); - - expect(pulses.pulseA.missedBeats).toBe(0); - expect(pulses.pulseB.missedBeats).toBe(3); - expect(pulses.pulseA.lag).toBe(0); - expect(pulses.pulseB.lag).toBe(3000); - - done(); - }, 3250); -}); - describe('events', () => { test('add events', (done) => { let totalTriggers = 0; @@ -129,8 +91,51 @@ describe('events', () => { }); }); -test('remove heart', (done) => { - heartbeats.killHeart('globalBeat'); - expect(heartbeats.heart('globalBeat')).toBe(undefined); - done(); +describe('teardown and unreferencing', () => { + test('remove heart', (done) => { + heartbeats.killHeart('globalBeat'); + expect(heartbeats.heart('globalBeat')).toBe(undefined); + done(); + }); + + test('unreference heart', (done) => { + const unrefTest = spawn('node', ['./test/spawn.js'], { stdio: 'inherit' }); + + unrefTest.on('exit', (code) => { + expect(code).toBe(0); + done(); + }); + }); +}); + +describe('pulses', () => { + test('pulse properties', (done) => { + const heart = heartbeats.createHeart(1000, 'pulseChecker'); + + const pulses = { + pulseA: heart.createPulse(), + pulseB: heart.createPulse(), + }; + + const iA = setInterval(() => { + pulses.pulseA.beat(); + }, 500); + + const iB = setInterval(() => { + pulses.pulseB.beat(); + }, 5000); + + setTimeout(() => { + clearInterval(iA); + clearInterval(iB); + + expect(pulses.pulseA.missedBeats).toBe(0); + expect(pulses.pulseB.missedBeats).toBe(3); + expect(pulses.pulseA.lag).toBe(0); + expect(pulses.pulseB.lag).toBe(3000); + + heartbeats.killHeart('pulseChecker'); + done(); + }, 3250); + }); });