Skip to content

fix(client): actually unregister the requested plugin channel - #1536

Open
GoatTech-42 wants to merge 2 commits into
PrismarineJS:masterfrom
GoatTech-42:fix/unregister-channel
Open

GoatTech-42 wants to merge 2 commits into
PrismarineJS:masterfrom
GoatTech-42:fix/unregister-channel

Conversation

@GoatTech-42

Copy link
Copy Markdown

Fixes #1534.

unregisterChannel() used Array.find() and stored the matching channel name in a variable called index, then passed that string to splice():

const index = channels.find(function (name) {
  return channel === name
})
if (index) {
  proto.types[channel] = undefined
  channels.splice(index, 1)

JavaScript coerces a non-numeric channel string to 0, so unregistering any custom channel removed the first registered channel (usually the built-in minecraft:register) and left the requested channel active. The truthiness check would also have skipped a legitimate match at index 0 once findIndex was used.

The fix uses findIndex() and tests for -1:

const index = channels.findIndex(function (name) {
  return channel === name
})
if (index !== -1) {

Regression tests in test/pluginChannelsTest.js cover both failure modes: the requested channel no longer receives custom_payload events after being unregistered (previously the splice removed the wrong entry, so it stayed active), and a channel sitting at index 0 can be unregistered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

unregisterChannel() never unregisters a channel

1 participant