altmp / altv-js-module

JS module for alt:V Multiplayer. Powered by NodeJS & v8
MIT License
55 stars 35 forks source link

Calling alt.off with invalid function reference breaks other listeners #229

Open xxshady opened 1 year ago

xxshady commented 1 year ago

Description of the problem

See reproduction code.

Reproduction steps

const handler = (key) => {
  alt.log("keydown", key)
  listenKeydown()
}

const listenKeydown = () => {
  const [listener] = alt.getEventListeners("keydown")
  alt.log("keydown listener:", listener)

  if (listener) {
    alt.log("alt.off keydown with invalid function ref")
    alt.off("keydown", () => {})
  } else {
    alt.log("alt.on keydown")
    alt.on("keydown", handler)
  }
}

listenKeydown()

Expected behaviour

Maybe we should check if passed function reference is not valid and throw error?

Additional context

No response

Operating system

Windows 11

Version

13.0-rc2

Scope

shared

Reproduction tested

LeonMrBonnie commented 1 year ago

The PR didn't actually fix it, so I will leave it open for now until we actually fix it