-
Notifications
You must be signed in to change notification settings - Fork 8
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
Make scope parameter of MapState #8
base: master
Are you sure you want to change the base?
Make scope parameter of MapState #8
Conversation
Thanks, I'll have a look soon. Hopefully tomorrow evening. |
Thanks for looking into this. However, I believe that having |
Imo forcing new dependency is bad practice, plus its a dependency on swing ui library. Fine if you are using swing ui, but bad otherwise. |
Does |
I have never used swing coroutines in any multiplatform project before. Doesn't mean you are wrong, just saying that library you want to use shouldn't force it's ways on you especially when it's not necessary. Also, firs thing in Google documentation on coroutines, is don't hardcode dispatchers, not sure how much more clear can that be. https://developer.android.com/kotlin/coroutines/coroutines-best-practices Btw, in regards to forcing dependencies onto library users, I noticed another one, kotlinx-io which seems to be required for tile loading. Requiring RawSource in parameters to TileStreamProvider forces the user to add kotlinx-io just to transform InputStream into RawSource. I didnt really explore the code too much to be honest, but I'd suggest moving to simple Input/Output streams, thats much cleaner imho. Anyway, I'll update the PR with additional change I needed to make it work, if you don't agree with it, feel free to close this :) I'll probably send over at least one more PR, I found one little bug in tile rendering but I dont want to spend more time on this. |
Regarding dispatchers and not hardcoding them, I totally agree. However, there is one very important thing to understand: The library is designed to perform some operation in the same thread as the one used by the ui framework. In case of Android, it's Some design change could be done (that is, reconsider this design), but this would require careful thinking and testing. By experience, this kind of change isn't trivial. |
Regarding |
I'd like to make another point. Not hardcoding dispatchers is a good pratice for apps, for testatbility notably. However, imagine letting your users tweak around your library multithreading design by exposing your dispatchers. For example, a core compoent is Now, back to the temporary fix. The documentation of A coroutine dispatcher that is confined to the Main thread operating with UI objects.
Usually such dispatchers are single-threaded.
Access to this property may throw an [IllegalStateException] if no main dispatchers are present in the classpath.
Depending on platform and classpath, it can be mapped to different dispatchers:
- On JVM it is either the Android main thread dispatcher, JavaFx, or Swing EDT dispatcher. It is chosen by the [`ServiceLoader`](https://docs.oracle.com/javase/8/docs/api/java/util/ServiceLoader.html).
- On JS it is equivalent to the [Default] dispatcher with [immediate][MainCoroutineDispatcher.immediate] support.
- On Native Darwin-based targets, it is a dispatcher backed by Darwin's main queue.
- On other Native targets, it is not available.
- `Dispatchers.setMain` from the `kotlinx-coroutines-test` artifact can replace the main dispatcher with a mock one for testing.
In order to work with the `Main` dispatcher on the JVM, the following artifact should be added to the project runtime dependencies:
- `kotlinx-coroutines-android` — for Android Main thread dispatcher
- `kotlinx-coroutines-javafx` — for JavaFx Application thread dispatcher
- `kotlinx-coroutines-swing` — for Swing EDT dispatcher |
The more I think about the proposed changes, the more I'm convinced this isn't the way to go. Imagine we let user choose which dispatcher the MapState works with for tasks formely dispatched to the "main" thread. Potentially, we can inject |
Regarding the kotlinx-io, I know nothing about that so I can't cast any judgement. With the dispatcher, I still think passing dispatcher in parameter is the best way but after I actually implemented the library in my app I ended up using |
Closes #7
Making CoroutineScope parameter of MapState and removing hardcoded Dispatchers.Main from it so it should be platform independant.