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

Throwing exception when a non-function callback is passed to elm.ports[port].subscribe #952

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

emilianobovetti
Copy link

@emilianobovetti emilianobovetti commented Mar 27, 2018

Currently calling elm.ports[port].subscribe(undefined) correctly adds undefined to subscribers list. This will produce the following exception when subscribers get called:

TypeError: currentSubs[i] is not a function

I think this behaviour should be changed because that exception message can be confusing, the stacktrace contains only Elm code, but most important the error could be raised before pushing the callback in subscribers list.

Following the same logic as send, that throws a runtime exception immediately when it receives invalid data, the subscribe with this PR will raise this error:

Error: Trying to subscribe an invalid callback on port `<port name>`

Why I think typeof callback !== 'function' is safe:
According to ECMAScript language specification the function call expression currentSubs[i](value) will throw an error if IsCallable is false, which returns true if its argument has the [[Call]] internal method. typeof will return "function" if an object implements [[Call]].

If I'm not wrong, it's impossible that typeof doesn't return 'function' and currentSubs[i](value) doesn't raise a TypeError, so if this is true I don't see any downside in throwing an error immediately!

Currently calling `elm.ports[portName].subscribe(undefined)` will cause an exception only when subscribers get called, this commit adds a type check before pushing the callback on subscription list
@process-bot
Copy link

Thanks for the pull request! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

rupertlssmith pushed a commit to elm-janitor/core that referenced this pull request Mar 4, 2022
Checking callback type on port subscribe

Currently calling `elm.ports[portName].subscribe(undefined)` will cause an exception only when subscribers get called, this commit adds a type check before pushing the callback on subscription list
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.

2 participants