Skip to content

Commit

Permalink
Use ES6 classes (#20)
Browse files Browse the repository at this point in the history
* Use ES6 classes

* Fix flakey test
  • Loading branch information
arjunmehta committed Jul 12, 2023
1 parent 5c5d2e3 commit a17b922
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 185 deletions.
50 changes: 26 additions & 24 deletions lib/beatevent.js
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;
180 changes: 88 additions & 92 deletions lib/heart.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
51 changes: 24 additions & 27 deletions lib/pulse.js
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;
Loading

0 comments on commit a17b922

Please sign in to comment.