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

selection.join() improvements #264

Closed
pavelschon opened this issue Sep 30, 2020 · 5 comments
Closed

selection.join() improvements #264

pavelschon opened this issue Sep 30, 2020 · 5 comments

Comments

@pavelschon
Copy link

pavelschon commented Sep 30, 2020

First of all, I'd like to thank for the selection.join(), it's a great improvement over traditional enter-update-exit pattern.
However, I feel there's some space for improvements.

Using arrow functions are good only for the simple case like this:

selection.join(
    enter => enter.append('div').classed('enter', true),
    update => update.classed('enter', false).classed('update', true),
    exit => exit.classed('update', false).classed('exit', true).transition().remove()
)

When you need to do something more complex, you will probably have to use regular functions:

selection.join(
    function(enter) {

    },
    function(update) {

    },
    function(exit) {
    
    }
)

or even factor-out functions for readability:

function doEnter(enter) {
   
}

function doUpdate(update) {

}

function doExit(exit) {
    
}

selection.join(doEnter, doUpdate, doExit)

One may not need doUpdate() and will write:

selection.join(doEnter, doExit)  // bug!

I propose the following syntax: pass functions in the object:

selection.join({
    enter: doEnter,
    update: doUpdate,
    exit: doExit
})

Passing functions in the object will prevent bugs and improve the readability.

@mbostock
Copy link
Member

Thanks for the feedback, but I don’t want to change the API.

@pavelschon
Copy link
Author

So you're happy with the current API? Could you elaborate more?

@curran
Copy link
Contributor

curran commented Oct 1, 2020

@pavelschon The motivations for the proposal are not clear.

When you need to do something more complex, you will probably have to use regular functions:

Why? The only reason to change would be if you need to access this.

Passing functions in the object will prevent bugs and improve the readability.

How so? What is the benefit?

Also keep in mind that changing this API would be a breaking change, and keeping the old API as well would introduce more surface area to the library. This proposal does not seem to hold water IMO.

@pavelschon
Copy link
Author

How so? What is the benefit?

It's already mentioned in the first post - if you pass doExit as 2nd positional argument, it will most likely cause a bug:

selection.join(doEnter, doExit)  // bug!

SInce JS does not support named arguments, then the only way to do it is to pass an object.

@pavelschon
Copy link
Author

And it is not a breaking change: you can call join() in 2 ways already:

selection.join(element) // as a shorthand
selection.join(doEnter, doUpdate, doExit)

What's the problem to add another API like:

selection.join(obj)

?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants