-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow "on changed" Flow emission for more general MutableMap #13
Conversation
c7ee114
to
e3e1dca
Compare
e3e1dca
to
0f2e7e8
Compare
Codecov Report
@@ Coverage Diff @@
## master #13 +/- ##
============================================
+ Coverage 67.85% 71.42% +3.57%
+ Complexity 18 17 -1
============================================
Files 2 2
Lines 28 28
Branches 4 4
============================================
+ Hits 19 20 +1
+ Misses 9 8 -1
Continue to review full report at Codecov.
|
* Wraps a [MutableMap] to provide a [Flow] of [onChanged] events. The [onChanged] events emit a | ||
* copy of the [MutableMap] at the time of the change event. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might be forgetting something, but what if the onChanged
emitted a non-mutable copy?
If a listener wanted a mutable copy of the map, they could just make a local mutable copy, right?
I guess my question is: why emit a mutable copy of the map?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's actually emitting a Map
(not MutableMap
):
val onChanged: Flow<Map<K, V>> = _onChanged.filterNotNull() |
MutableMap
is just a facade though, so unfortunately every Map
is also mutable if casted to MutableMap
(thanks Java). So enforcement is more at the level of just making it more difficult to modify.
...but, by copying the original MutableMap
ensures that any changes done to the emitted map (from the onChanged
event) via casting means developer will be modifying the copy, not the original. Thereby protecting the expected dataset in the original map.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah thanks for the explanation. Flow<Map<K, V>>
🤦♂️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@twyatt if we're willing to JVM-lock, there is always Collections.unmodifiableMap(yourMap)
to return a runtime-enforced read-only wrapper (not a copy, reads through) map. Because it's a Java function you can still cast it to MutableMap
, but mutate attempts will at least fail at runtime.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cedrickcooke ohh, I do like that better; would presumably be more efficient than a copy.
I'll make that change.
I'm fine with the JVM-lock; as going multiplatform wouldn't be difficult and just entail a basic expect
/ actual
function to provide the platform specific "immutable collection" for the change event.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh wait, Collections.unmodifiableMap(yourMap)
won't adhere to the desired functionality. It's just a "view" of the underlying Map
.
I need the emitted Map
to reflect the collection at a point in time.
I believe Collections.unmodifiableMap(yourMap)
would produce different values over time if the underlying Map
was changing; so doesn't properly represent an onChanged
"event".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the same map is shared by all observers of the flow it might make sense to copy it and then wrap it in the unmodifiable view. I'm not sure if that's too defensive or if it's still reasonable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the same map is shared by all observers of the flow it might make sense to copy it and then wrap it in the unmodifiable view.
Ohhh ya. Very good point.
I'm not sure if that's too defensive or if it's still reasonable.
I think it's definitely reasonable; I'd rather start too defensive and later lift restriction if needed (e.g. performance reasons or something). Almost always easier to lift restrictions than to impose them later.
ConcurrentHashMap
MutableMap
insteadinternal
to force usage of extension function and simplify similar changes in the future