I am quite an experienced developer but I am quite new to JS/frontend/functional programming and I am working with react-native in my day-job now.
I'm trying to do my own flux implementation to better understand redux. My goal is to have a performant data store that will behave predictably. This solution seems to be working for me, but will become slower the longer the app runs.
I've looked at using immutable.js and lodash but I'm not sure it's necessary. I've also considered using a mutable currentState
variable and only merging the changes since the last call to getState()
and then resetting i.e changes = []
and updating currentState
. I'd also like some opinions on my use of a promise in the getState
function.
export default class Session {
static changes = []
static getState() {
return new Promise((resolve, reject) => {
resolve(this.changes.reduce((a, x) => Object.assign(a,x),{}))
})
}
static update(value) {
this.changes.push(value)
}
static showChanges() {
this.changes.forEach((x)=> console.log(x))
}
}
Object.defineProperty(Session, 'changes',{
writable: false,
enumerable: true
})
getState()
when it's a synchronous operation? That will just make it slower than it needs to be and more complicated to use. \$\endgroup\$reduce
function takes too long it will block the UI animations. \$\endgroup\$.reduce()
from blocking anything..reduce()
is synchronous and JS is single thread. The promise could keep whatever code comes next in the caller from blocking the repaint, but it's still really odd to force a synchronous object to use a promise for that purpose. You could probably improve the speed of your.reduce()
loop a lot. Also single letter variable names likea
andx
do not contribute to the readability of your code by someone who doesn't know what you're trying to do. \$\endgroup\$changes
array with the later ones overwriting the older ones? It might be simpler to just maintain that summary as you add each new item tochanges
. Then, you don't have a time consuming process to build it from scratch when requested. \$\endgroup\$