Skip to content

Browser JS: One library might use the WindowDispatcher from another library and fail #3874

Open
@odzhychko

Description

Describe the bug

When using multiple independent libraries in JS Browser, that each bundle Kotlinx Coroutines, one of them might fail because it tries to use the WindowDispatcher created by the other.

In the browser, the WindowDispatcher is created once and stored on the global window object as window.coroutineDispatcher

public fun Window.asCoroutineDispatcher(): CoroutineDispatcher =
@Suppress("UnsafeCastFromDynamic")
asDynamic().coroutineDispatcher ?: WindowDispatcher(this).also {
asDynamic().coroutineDispatcher = it

But the WindowDispatcher from one library might not work with the WindowDispatcher from the other because the function names got minified differently.

Provide a Reproducer

A sample project with instructions can be found in https://github.com/odzhychko/multiple-js-libs-with-coroutines

Thoughts

What would be the downsides to storing the WindowDispatcher on a static field instead, like in a classical singleton?
The property window.coroutineAnimationQueue might have the same issues as window.coroutineDispatcher.

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions