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

Placeholder doesn't work for converted functions unless explicitly recurried #3440

Closed
daedalus28 opened this issue Oct 20, 2017 · 24 comments · Fixed by oisinf/ModernReactWithRedux#1
Labels

Comments

@daedalus28
Copy link
Contributor

daedalus28 commented Oct 20, 2017

When using convert on functions from lodash fp, they seem to lose their support for using _ as a placeholder. If I explicitly call curry on the result, they regain their placeholder support. Here's a full test case showing a failing test:

import _ from 'lodash/fp'

describe('lodash test', () => {
  it('placeholder for converted functions', () => {
    let mutableUnset = _.unset.convert({ immutable: false })
    let x = {
      a: 1,
      b: 1
    }
    console.log(mutableUnset.placeholder) // <-- Seems to be lodash
    console.log(mutableUnset.placeholder == _) // <-- false

    let unsetInX = mutableUnset(_, x)
    unsetInX('a') // <--- throws `TypeError: unsetInX is not a function`
    console.log(x)
  })
  it('placeholder for explicitly curried converted functions', () => {
    let mutableUnset = _.curryN(2, _.unset.convert({ immutable: false }))
    let x = {
      a: 1,
      b: 1
    }
    console.log(mutableUnset.placeholder) // <-- Seems to be lodash
    console.log(mutableUnset.placeholder == _) // <-- true

    let unsetInX = mutableUnset(_, x)
    unsetInX('a') // <--- works because of the explicit curry above
    console.log(x) // { b: 1}
  })
})
@jdalton
Copy link
Member

jdalton commented Oct 20, 2017

Hi @daedalus28!

Is there a .placeholder property on the mutableUnset function?

@daedalus28
Copy link
Contributor Author

daedalus28 commented Oct 20, 2017

Yes, there is. It appears to be a lodash instance, but it is not the same one as what's imported. I'm updating my original post to include the lines I added to test for placeholder:

console.log(mutableUnset.placeholder) // <-- Seems to be lodash
console.log(mutableUnset.placeholder == _) // <-- false

@daedalus28
Copy link
Contributor Author

In the one with the explicit curry, mutableUnset.placeholder == _ is true

@jdalton
Copy link
Member

jdalton commented Oct 20, 2017

Can you try using the mutableUnset.placeholder placeholder for the unsetInX assignment.

@daedalus28
Copy link
Contributor Author

Yup, using mutableUnset.placeholder instead of _ works. It seems like something in the conversion is creating a clone of lodash or something like that - it doesn't seem to be an issue with the function of placeholder itself

@jdalton
Copy link
Member

jdalton commented Oct 20, 2017

Nice!

This is by design and also why we bolt on the .placeholder prop to methods. Lodash allows creating its world again to avoid cross contamination of state and settings. This is part of that.

@daedalus28
Copy link
Contributor Author

I don't think anyone would expect that behavior. Why does simply calling convert create an entirely new instance of lodash?

@daedalus28
Copy link
Contributor Author

In practice, this means every method I convert I'll also have to manually re-curry. If nothing else, converting should optionally allow preserving the placeholder instance.

Also, does that mean every time I call convert on a method, I'm creating a whole new instance of lodash in memory? That seems less than ideal from a performance standpoint.

@jdalton
Copy link
Member

jdalton commented Oct 20, 2017

lodash itself is converted to lodash/fp so converting methods is a natural fit.
You might be able to avoid currying by simply replacing the .placeholder value.

mutableUnset.placeholder = _

If that works I'll patch fp to automatically do that.

@daedalus28
Copy link
Contributor Author

Yup, reassigning works:

it('placeholder for converted functions', () => {
    let mutableUnset = _.unset.convert({ immutable: false })
    let x = {
      a: 1,
      b: 1
    }
    mutableUnset.placeholder = _
    console.log(mutableUnset.placeholder == _) // <-- true

    let unsetInX = mutableUnset(_, x)
    unsetInX('a') // <--- Works :D
    console.log(x)
  })

@daedalus28
Copy link
Contributor Author

I would happily do the PR myself but I'm not sure where the code for convert is - it doesn't seem to be in this repo

@jdalton
Copy link
Member

jdalton commented Oct 20, 2017

Rock 😋!

Don't worry, our master branch is a mess with v5 prep, I'll patch in an isolated branch.

@daedalus28
Copy link
Contributor Author

@jdalton not to be annoying, but any idea on an ETA for that fix being published?

@jdalton
Copy link
Member

jdalton commented Oct 28, 2017

@daedalus28

not to be annoying, but any idea on an ETA for that fix being published?

No ETA yet. Thanks for keeping it on my radar though.

@daedalus28
Copy link
Contributor Author

@jdalton is it possible to push a 4.17.5 that just adds this in?

@daedalus28
Copy link
Contributor Author

@jdalton Not to be annoying, but it's been almost 2 months. Any chance of getting the patch released as 4.17.5?

@jdalton
Copy link
Member

jdalton commented Dec 18, 2017

When we do a backport of webpack's sideEffects flag this will likely get swept up with that. I'm not actively working on v4 right now. My attention is on v5.

@daedalus28
Copy link
Contributor Author

@jdalton There's been a couple of fix revision releases, but I haven't seen this in yet. Was it just missing in the changelog, or is this still open?

@jdalton
Copy link
Member

jdalton commented May 23, 2018

Ah @daedalus28!

This totally did not get fixed. I missed it 😱
I can prepare a patch to do a minor release by end of day Friday.

@daedalus28
Copy link
Contributor Author

@jdalton did that happen?

@jdalton
Copy link
Member

jdalton commented May 29, 2018

Annnnd I missed it. (the three day weekend got me).
It's in my email marked as important so I should keep seeing it :)

@daedalus28
Copy link
Contributor Author

Checking in... any update?

@jdalton
Copy link
Member

jdalton commented Aug 31, 2018

Patched 2de676f.

@lock
Copy link

lock bot commented Aug 31, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Aug 31, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

2 participants