marcuswestin / store.js

Cross-browser storage for all use cases, used across the web.
MIT License
14.02k stars 1.33k forks source link

Allow super_fn argument passing from mixins #206

Closed w33ble closed 7 years ago

w33ble commented 7 years ago

When a mixin calls super_fn, the arguments are currently bound to the original call, which makes it impossible to do some more advanced things, like override set for example.

function compressionPlugin() {
  return {
    set: set
  }

  function set(super_fn, key, value) {
    var newValue = 'MODIFIED' + value;
    super_fn(key, newValue)
  }
}

As a plugin author, you may expect that calling set('foo', 'bar') results in MODIFIEDbar being stored. But because the original args value, bar, is bound, that's the value that gets stored.

With this PR, any arguments passed into super_fn override the original values, so calling set('foo', 'bar') with this PR results in MODIFIEDbar being stored.

Additionally, any additional arguments passed in the original call will continue to be passed through unmodified.

I'm not entirely sure how to write tests for this change, short of writing a plugin that would use this change. That said, I would like to modify #204 to use it...

marcuswestin commented 7 years ago

Looks good! This will break in legacy IE so I'll have to make some changes but I think the approach is good! Stay tuned for final code...

marcuswestin commented 7 years ago

Done! Can you please double check https://github.com/marcuswestin/store.js/blob/master/tests/tests.js#L61 to ensure that it behaves in the way you'd expect it to?

w33ble commented 7 years ago

I think that looks right. I don't see a test that is checking the arguments to the super_fn, but the use in the second addPlugin there is how I would expect to use it. Sorry, now I see how that test works. Yup, that looks right.

I'll update #204 and report back.

marcuswestin commented 7 years ago

👍