-
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
Merged
+72
−61
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
ext.deps = [ | ||
kotlin: [ | ||
stdlib: "org.jetbrains.kotlin:kotlin-stdlib", | ||
coroutines: "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7", | ||
coroutines: [ | ||
core: "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7", | ||
debug: "org.jetbrains.kotlinx:kotlinx-coroutines-debug:1.3.7", | ||
], | ||
junit: "org.jetbrains.kotlin:kotlin-test-junit", | ||
], | ||
] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
(notMutableMap
):tuulbox/collections/src/main/kotlin/FlowMutableMap.kt
Line 18 in 0f2e7e8
MutableMap
is just a facade though, so unfortunately everyMap
is also mutable if casted toMutableMap
(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 theonChanged
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 toMutableMap
, 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 underlyingMap
.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 underlyingMap
was changing; so doesn't properly represent anonChanged
"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.
Ohhh ya. Very good point.
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.