I took a look at the code and I have a few suggestions 🙂
Maybe the code should be wrapped in a try
/catch
block so an owner doesn't accidentally crash their forum with an invalid expression? It could become impossible to access the login modal when the eval
call throws an error since it's inside a component view
method.
It also seems like if the exp expression returns Infinity
(division by zero) the code will be stuck in an infinite loop (the while
block) and might hang the browser tab.
I also wonder about the performance impact. The way the eval
runs in a loop for each possible exp value and this happens on every redraw is probably not optimal for lower-power devices if the exp values are big. The value could be computed only once in oninit
and stored on an attribute of the LevelBar
component. Or even one step further, make it a computed attribute on the javascript user model, Flarum already has a helper to create such attributes. I think this will never be an issue inside of a discussion because the user profile is within the Post
component whose redraws are limited by the SubtreeRetainer
already, but when the component is used elsewhere and if another extension causes a lot of unnecessary redraws the performance hits will add up.