-
Notifications
You must be signed in to change notification settings - Fork 1.2k
JS: Introduce helper functions for event listeners #5495
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
Open
osipxd
wants to merge
1
commit into
release/3.x
Choose a base branch
from
osipxd/typesafe-js-event
base: release/3.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+94
−66
Open
Changes from all commits
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
64 changes: 64 additions & 0 deletions
64
ktor-client/ktor-client-core/web/src/io/ktor/client/utils/JsUtils.web.kt
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * Copyright 2014-2026 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
| */ | ||
|
|
||
| package io.ktor.client.utils | ||
|
|
||
| import kotlinx.coroutines.DisposableHandle | ||
| import org.w3c.dom.events.Event | ||
| import org.w3c.dom.events.EventTarget | ||
| import kotlin.js.JsAny | ||
| import kotlin.js.unsafeCast | ||
|
|
||
| /** | ||
| * Registers event listeners for the given [events] and returns a [DisposableHandle] to remove them. | ||
| * | ||
| * The event is unsafe-cast to [T] before invoking [listener]. The cast is unchecked because JS | ||
| * event objects from some runtimes (e.g. the `ws` npm package) are plain objects that do not | ||
| * satisfy Kotlin `instanceof` checks for DOM event subclasses. | ||
| * | ||
| * @param events event names to listen for | ||
| * @param listener callback invoked when any of the events fire | ||
| * @return a handle to remove the listeners | ||
| */ | ||
| internal inline fun <reified T : Event> EventTarget.addEventListener( | ||
| vararg events: String, | ||
| crossinline listener: (T) -> Unit | ||
| ): DisposableHandle { | ||
| // The callback parameter is JsAny, not Event, by design. | ||
| // Kotlin/WasmJS generates a JS adapter for each lambda based on its declared parameter type: | ||
| // a `(Event) -> Unit` adapter checks `x instanceof globalThis.Event` before invoking the lambda. | ||
| // The `ws` npm package defines its own `Event` hierarchy that does not extend `globalThis.Event` | ||
| // (see https://github.com/websockets/ws/issues/1818), so that check fails for every event fired | ||
| // by a Node.js WebSocket. | ||
| // Using `(JsAny) -> Unit` produces an adapter with no instanceof check, accepting any JS value. | ||
| // This is valid Kotlin: `(JsAny) -> Unit` is a subtype of `(Event) -> Unit` by contravariance. | ||
| // `unsafeCast<T>()` inside the body is a no-op — it changes only the compile-time type with no | ||
| // runtime check — so it never fails regardless of the actual JS object type. | ||
| @Suppress("RemoveExplicitTypeArguments") // Compiler fails to infer type arguments otherwise | ||
| val callback = { event: JsAny -> listener(event.unsafeCast<T>()) } | ||
| events.forEach { addEventListener(it, callback) } | ||
| return DisposableHandle { events.forEach { removeEventListener(it, callback) } } | ||
| } | ||
|
|
||
| /** | ||
| * Registers a one-time event listener that removes itself after the first event fires. | ||
| * The listener is registered for all specified [events] and fires once for whichever event occurs first. | ||
| * | ||
| * The event is unsafe-cast to [T] before invoking [listener]. See [addEventListener] for why. | ||
| * | ||
| * @param events event names to listen for | ||
| * @param listener callback invoked when one of the events fires | ||
| * @return a handle to remove the listener before it fires | ||
| */ | ||
| internal inline fun <reified T : Event> EventTarget.addOneTimeEventListener( | ||
| vararg events: String, | ||
| crossinline listener: (T) -> Unit | ||
| ): DisposableHandle { | ||
| lateinit var disposable: DisposableHandle | ||
| disposable = addEventListener<T>(*events) { event -> | ||
| disposable.dispose() | ||
| listener(event) | ||
| } | ||
| return disposable | ||
| } | ||
Oops, something went wrong.
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.
It turned out to be not so simple 😅
But at least now this code is isolated and documented here
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.
Have you tried this with the "once" option? https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#once
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.
Thanks! This is much better for event listeners listening a single event. I will try.
However, for the same listener registered to listen two events we will still need this utility function