Refactors state management code #5
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "cleanup"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Motivation
In the previous pull request, there was a fair amount of obvious boilerplate code that would be repeated. Plus a few variable names and function names that could be improved. The project is at a stage where fixing these are a quick win, which might not be the case in future.
Solution
I changed makeStateProperty from taking an arg list to taking an object. There were too many arguments and it was quite confusing to read without the names.
By splitting everything out into separate files, the state file is quite small now which is good because it's probably going to get quite large and quite repetitive, so we definitely don't want to mix up a lot of imperative code with a lot of declarative code.
I'm fairly confident that there will be a lot of properties that can make use of
makeRefresherandmakeSetterwhen creating state properties. The more declaratively these state properties can be defined, the better! These two functions make that much easier, as shown in this PR.I also renamed the result functions to singular verbs. These functions are going to show up in a lot of places, and adding extra words to the function name will only marginally reduce the cognitive load when someone first encounters them, but significantly increase the cognitive load when someone is familiar with code base. It won't take them long to realise what
succeededandfailedmean as past participle verbs. A very fair trade off in my opinion!Since some managed properties will likely have very complicated setters and refreshers, I think it's reasonable to put them in a separate
propertiesfolder with each property in a separate file.@ -0,0 +5,4 @@const state = {hostname,} as const satisfies {[key in keyof ServerConfig]: StateProperty<unknown, unknown>;This ensures that
ServerConfigandstatedo not fall out of sync in terms of their properties. This is the coupling/cohesion point between the translation layer and the logic layer.