-
Notifications
You must be signed in to change notification settings - Fork 7.5k
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
Don't mutate the source #2418
Don't mutate the source #2418
Conversation
@@ -2,47 +2,46 @@ | |||
* @file merge-options.js | |||
*/ | |||
import merge from 'lodash-compat/object/merge'; | |||
import isArray from 'lodash-compat/lang/isArray'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This appears to be extraneous.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch.
Tests failed. Automated cross-browser testing via Sauce Labs and Travis CI shows that the JavaScript changes in this pull request are: BUSTED Commit: 86ddb70 (Please note that this is a fully automated comment.) |
mergeOptions did not mutate its first argument in video.js 4.x. Restore this behavior.
isArray isn't used in this module.
86ddb70
to
0d90544
Compare
Tests failed. Automated cross-browser testing via Sauce Labs and Travis CI shows that the JavaScript changes in this pull request are: BUSTED Commit: 0d90544 (Please note that this is a fully automated comment.) |
@@ -3,46 +3,44 @@ | |||
*/ | |||
import merge from 'lodash-compat/object/merge'; | |||
|
|||
function isPlain(obj) { | |||
const isPlain = function(obj) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why change this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We had been using var f = function() {}
bindings most everywhere else. I was just reverting this to what I thought was the current recommendation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok no problem, but if we're going by the Airbnb guid then we should move towards declarations.
https://github.com/airbnb/javascript#7.1
I've been using them for the smaller module functions at least.
https://github.com/videojs/video.js/blob/master/src/js/utils/format-time.js
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still pro-Airbnb-guide. I'll update this.
This was on purpose, to match how lodash merge works. See #2182 point 3. As is, it'll break this amazing one-liner: https://github.com/videojs/video.js/blob/master/src/js/video.js#L379 |
This is another place where documentation would help - namely, that people should do
|
@heff that one liner is pretty cool but breaking all the existing users doesn't seem worth it to me. This is probably the main utility function that plugins were using in 4.x |
return object; | ||
export default function mergeOptions() { | ||
let args = Array.prototype.slice.call(arguments); | ||
args.unshift({}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we add some comments to these lines? Takes a bit to grok as is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure thing
Ok if that's the case then I'm ok with reverting that change. Would love some code comments and we'll need to fix the addLanguage piece, but otherwise looks good to me. |
The global options setter was returning a new object after mergeOptions() was converted to be non-mutating. Use lodash's merge() so that the setting changes persist. Add comments to mergeOptions().
@@ -59,6 +59,15 @@ test('should add the value to the languages object with lower case lang code', f | |||
deepEqual(result, globalOptions['languages'][code.toLowerCase()], 'should also match'); | |||
}); | |||
|
|||
test('should allow setting global options', function() { | |||
videojs.setGlobalOptions({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fact this does a merge and not an overwrite strikes me as really odd but that's a separate discussion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree it feels a little weird, but if it did an overwrite you'd have to include every option every time instead of being able to just set one or a few.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The converse of that is you can no longer remove an option that has been set.
@heff updated. The language stuff actually works correctly because it's using lodash's mutating merge() instead of mergeOptions(). I grepped for other uses of merge() or mergeOptions() as a sanity check and found a problem with setGlobalOptions. That is fixed in the update commit. |
Tests failed. Automated cross-browser testing via Sauce Labs and Travis CI shows that the JavaScript changes in this pull request are: BUSTED Commit: 1ca358b (Please note that this is a fully automated comment.) |
I don't think we can use just |
Yeah, it looks like I did a quick sweep for uses of With Other things I noticed:
|
Yeah, all of those would just be using it as a deepCopy now (but only copying plain objects). Leaving the first object in might make it more clear what's happening since the function name doesn't match what it's doing, but I could go either way. Or we could make another function just for copying, that shares some logic with mergeOptions. The last example would be redundant now. The important thing is that the |
I think the transition to a getter/setter model for global options is creating more problems than it is solving. If we go back to using |
I think if we switched to mergeOptions for global options this could be merged in now, if the goal is to get this merged in quickly. I could also be ok with switching back to a normal object for options, since the setting process is not as intuitive with a function. |
Also, we could still have set and get globalOptions while still having |
Proposed changes to @dmlap PR above: a9b4221495fc874e22c03d8f6fb778e3f8ee2542\ There is value in a mutating version of merge that has the same semantics as the non-mutating version (namely array handling.) The secondary issue of not being able to remove properties from global options could be side-stepped by simply setting properties you don't want around any more to |
Can this be closed now that #2461 is in? |
mergeOptions did not mutate its first argument in video.js 4.x. Restore this behavior.