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

removeEventListener does not work because raw function is not passed #23921

Open
hamidb80 opened this issue Aug 3, 2024 · 1 comment
Open

Comments

@hamidb80
Copy link
Contributor

hamidb80 commented Aug 3, 2024

Description

import std/[dom]

proc onkeydownEventHandler(e: Event) =
  echo "hey"

window.addEventListener    "keydown", onkeydownEventHandler
window.removeEventListener "keydown", onkeydownEventHandler
<script src="./play.js"></script>

Nim Version

Nim Compiler Version 2.1.9 [Windows: amd64]
Compiled at 2024-07-30
Copyright (c) 2006-2024 by Andreas Rumpf

active boot switches: -d:release

Current Output

`hey` is printed after every key press

Expected Output

it should not not log anything when pressing keys

Possible Solution

here's the produced code when -d:danger:

Temporary1 = onkeydownEventHandler__play_u2.bind(null); Temporary1.ClP_0 = onkeydownEventHandler__play_u2; Temporary1.ClE_0 = null;
window.addEventListener("keydown", Temporary1, false);
Temporary2 = onkeydownEventHandler__play_u2.bind(null); Temporary2.ClP_0 = onkeydownEventHandler__play_u2; Temporary2.ClE_0 = null;
window.removeEventListener("keydown", Temporary2);

it would work simply if the following code would be generated:

window.addEventListener("keydown", onkeydownEventHandler__play_u2, false);
window.removeEventListener("keydown", onkeydownEventHandler__play_u2);

perhaps it is good to allow addr someFunction in js backend for workaround. i.e. the following code should produce the above code:

window.addEventListener    "keydown", addr onkeydownEventHandler
window.removeEventListener "keydown", addr onkeydownEventHandler

Additional Information

No response

@ringabout ringabout self-assigned this Aug 6, 2024
@ringabout
Copy link
Member

workaround

import std/[dom]

proc onkeydownEventHandler(e: Event) =
  echo "hey"

let e1: proc (event: Event) {.closure.} = onkeydownEventHandler
window.addEventListener    "keydown", e1
window.removeEventListener "keydown", e1

@ringabout ringabout removed their assignment Aug 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants