-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
init options bug #1761
base: develop
Are you sure you want to change the base?
init options bug #1761
Conversation
get dataPlayerConfig() { | ||
try { | ||
return JSON.parse(this.media.getAttribute('data-plyr-config')); | ||
} catch (e) { | ||
return {}; | ||
} | ||
} | ||
|
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 do we need this as a getter?
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.
my bad we don't need a getter, but the current implementation is unreadable as well, it confuses the editor that the code below this line is not reachable
I guess we can have a method say parseJSON(jsonString)
to handle 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.
Yeah, it could just be a util really.
|
||
Object.keys(defaults) | ||
.forEach(key => { | ||
if (Object.prototype.hasOwnProperty.call(options, key)) { |
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 not use Object.keys(options).includes(key)
?
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.
hasOwnProperty
is faster with fewer properties say 100, I don't think there's gonna be more than 100 keys here (as of now)
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.
Readability > a probably very minor speed increase. You'll notice the rest of the code we use that style so let's stick with that. It's the sort of thing browsers will optimise anyway.
@@ -0,0 +1,18 @@ | |||
export default function(options, defaults) { |
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.
Is this not just merging the two objects? In which case we can just use the extend
helper in the object utils?
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.
yep, it is! We have two options with multiple properties quality
and speed
the current implementation will break if the initialization is like
const player = new Plyr('#player', {
speed: {
selected: 2
}
});
it will break here since the user has not provided the options
Line 1057 in 39558cb
this.options.speed = this.options.speed.filter(o => o >= this.minimumSpeed && o <= this.maximumSpeed); |
the above code just makes sure that incorrect options don't break the code, it wasn't breaking previously since we were overriding user's config
if (!Object.keys(target).includes(key)) { | ||
Object.assign(target, { [key]: {} }); | ||
} | ||
|
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.
What's the reason behind this change? You could potentially overwrite an existing value rather than merge.
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 felt like extending an object should extend it unconditionally, rather than checking for the existence of keys in loop
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.
Did you test it? From looking at it, happens with your change will be that if the target
already includes key
, you're going to wipe it to be an empty object.
i.e.
const obj1 = { a: { b: 2, c: 3 }};
const obj2 = { a: { d: 4, e: 5 }};
const obj3 = extend(obj1, obj2);
You would expect obj3 to be { b: 2, c: 3, d: 4, e: 5 }
but with your change it'll be { d: 4, e: 5 }
because it wipes out the previous value.
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.
let me know your thoughts on this @sampotts
@@ -0,0 +1,18 @@ | |||
export default function(options, defaults) { |
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.
yep, it is! We have two options with multiple properties quality
and speed
the current implementation will break if the initialization is like
const player = new Plyr('#player', {
speed: {
selected: 2
}
});
it will break here since the user has not provided the options
Line 1057 in 39558cb
this.options.speed = this.options.speed.filter(o => o >= this.minimumSpeed && o <= this.maximumSpeed); |
the above code just makes sure that incorrect options don't break the code, it wasn't breaking previously since we were overriding user's config
get dataPlayerConfig() { | ||
try { | ||
return JSON.parse(this.media.getAttribute('data-plyr-config')); | ||
} catch (e) { | ||
return {}; | ||
} | ||
} | ||
|
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.
my bad we don't need a getter, but the current implementation is unreadable as well, it confuses the editor that the code below this line is not reachable
I guess we can have a method say parseJSON(jsonString)
to handle this
|
||
Object.keys(defaults) | ||
.forEach(key => { | ||
if (Object.prototype.hasOwnProperty.call(options, key)) { |
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.
hasOwnProperty
is faster with fewer properties say 100, I don't think there's gonna be more than 100 keys here (as of now)
if (!Object.keys(target).includes(key)) { | ||
Object.assign(target, { [key]: {} }); | ||
} | ||
|
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 felt like extending an object should extend it unconditionally, rather than checking for the existence of keys in loop
Link to related issue (if applicable)
#1737
Summary of proposed changes
this PR helps in prioritizing the config provided by the User, rather than applying values from local storage, the high priority is given to
user-config
local-storage
app's default config
it also normalizes options such as quality and speed such that if the user forgets to input one of the required fields it uses app's default config for that field
this code has been tested with
HTML5
,Youtube
andVimeo
and works without issuesChecklist
develop
as the base branch/dist
changes) from the PR