I'm working on a simple Javascript module and I'd like to make it follow the functional programming style.
This module basically allows you to measure how much time has passed when you started and finished an event and sends the data in chunks of 2 measurements:
const register = {
marks: {},
marksCount: 0,
restart: function () {
this.marks = {}
this.marksCount = 0
},
startMark: function (id) {
performance.mark(`${id}/start`)
},
finishMark: function (id) {
performance.mark(`${id}/end`)
performance.measure(id, `${id}/start`, `${id}/end`)
this.marksCount++
this.marks[id] = performance.getEntriesByName(id, 'measure')[0]
if (this.marksCount === 2) {
console.log(this.marks)
this.restart()
}
}
}
// then you can use it like this
register.startMark('event1')
register.startMark('event2')
register.finishMark('event1')
register.finishMark('event2')
I've been reading some posts regarding how FP manages the state and I'd really like to see how this simple module can be written using pure FP principles, specially how can we prevent mutating the properties.