Skip to content
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

Merged
merged 3 commits into from
Aug 14, 2020

Conversation

twyatt
Copy link
Member

@twyatt twyatt commented Aug 12, 2020

  • Previously wrapped ConcurrentHashMap
    • Changed to allow more general usage by wrapping MutableMap instead
  • Binary incompatible change due to type of constructor default value changed
    • Marked constructor internal to force usage of extension function and simplify similar changes in the future

@twyatt twyatt force-pushed the twyatt/FlowMutableMap branch from c7ee114 to e3e1dca Compare August 12, 2020 19:14
@twyatt twyatt added this to the 2.0.0 milestone Aug 12, 2020
@twyatt twyatt force-pushed the twyatt/FlowMutableMap branch from e3e1dca to 0f2e7e8 Compare August 12, 2020 19:16
@codecov
Copy link

codecov bot commented Aug 12, 2020

Codecov Report

Merging #13 into master will increase coverage by 3.57%.
The diff coverage is 87.50%.

Impacted file tree graph

@@             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     
Impacted Files Coverage Δ Complexity Δ
collections/src/main/kotlin/FlowMutableMap.kt 86.95% <87.50%> (ø) 17.00 <1.00> (?)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7e9b8ff...0871d4f. Read the comment docs.

@twyatt twyatt marked this pull request as ready for review August 12, 2020 19:18
Comment on lines +10 to +11
* 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.

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?

Copy link
Member Author

@twyatt twyatt Aug 12, 2020

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.

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>> 🤦‍♂️

Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Member Author

@twyatt twyatt Aug 13, 2020

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".

Copy link
Contributor

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.

Copy link
Member Author

@twyatt twyatt Aug 13, 2020

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.

@twyatt twyatt requested a review from Chris-Corea August 12, 2020 21:17
@twyatt twyatt merged commit 3705cac into master Aug 14, 2020
@twyatt twyatt deleted the twyatt/FlowMutableMap branch August 14, 2020 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants