tim
04/10/2020, 9:49 AMstate.copy(levelOne = levelOne.copy(levelTwo = levelTwo.copy( value = 0 )))
which may not seem too bad here, but does get quiet verbose when I'm trying to update several values at two different 'paths'. Additionally I need to get/set values dynamically (think lodash's set/get functionality) i.e., get<T>(state, "levelOne.levelTwo.value)
and set(state, "levelOne.levelTwo.value", 1)
. I should note my state is being serialised/deserialised to/from Firestore as a Map.
To accomplish the above, I'm converting my hierarchy of data classes to a map (with nested maps) using jackson, and then implementing the set/get functionality on those maps, before converting back to the original State data class hierachy. But this seems needlessly complex. For example, I have to convert everything in the specified update path to a mutable structure, transverse the maps, and do lots of casting along the way. But, the benefit is that nested access is straightforward and my reducers are simple:
reducer(state: State, action: FSA) {
val payload = action.payload
// I actually do this in my root reducer so i just mutate state directly here
val fresh = state.copy()
val oldValue = get<Int>("levelOne.levelTwo.value")
set(fresh, "levelOne.levelTwo.value", 3 * oldValue)
return fresh
}
I'm thinking one improvement I could do is implement something like immerjs where state is 'patched' in reducers and the patches are applied all at once before the root reducer finally returns the next state to minimise my converting to/from maps.
The friction here stopping me from going to Maps altogether, is with data classes I get type safety when reading most values from state normally ... val some = tate.levelOne.levelTwo.value
.
Grateful for your thoughts/objections to the approaches above 🙏tim
04/10/2020, 11:05 AMPatrick Jackson
04/10/2020, 1:38 PMPatrick Jackson
04/10/2020, 1:39 PMtim
04/10/2020, 1:44 PMtim
04/10/2020, 1:48 PMval next = state.patch {
id = "new-id"
state.attributes.add(2)
set("some.nested.path", listOf(1, 2, 3))
}
The Immutable version is again a data class but the variables are backed by a map under the hood which allows for dynamically accessing/setting valuesPatrick Jackson
04/10/2020, 1:48 PMtim
04/10/2020, 1:49 PMPatrick Jackson
04/10/2020, 1:51 PMtim
04/10/2020, 1:53 PMtim
04/10/2020, 1:54 PMsteveme
05/30/2020, 6:17 PMtim
06/01/2020, 4:09 PM