-
-
Notifications
You must be signed in to change notification settings - Fork 7
feat(Player): add OnEvent callback #423
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -36,6 +36,8 @@ export async function init(id, invoke, method, options) { | |||||
| ...options | ||||||
| } | ||||||
| p.player = new Plyr(el, config); | ||||||
| handlerEvents(p); | ||||||
|
|
||||||
| if (source.sources.length === 0) { | ||||||
| return; | ||||||
| } | ||||||
|
|
@@ -62,10 +64,69 @@ const initHls = (p, options) => { | |||||
| setTimeout(() => hls.subtitleTrack = player.currentTrack, 50); | ||||||
| }); | ||||||
| p.player = player; | ||||||
| handlerEvents(p); | ||||||
| }); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| export function reload(id, options) { | ||||||
| const p = Data.get(id); | ||||||
| if (p === null) { | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| const { player, hls } = p; | ||||||
| const source = options.source.sources; | ||||||
| delete options.source; | ||||||
| if (hls) { | ||||||
| if (source.length > 0) { | ||||||
| const src = source[0].src; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (code-quality): Prefer object destructuring when accessing and using properties. (
Suggested change
ExplanationObject destructuring can often remove an unnecessary temporary reference, as well as making your code more succinct.From the Airbnb Javascript Style Guide |
||||||
| hls.loadSource(src); | ||||||
| } | ||||||
| } | ||||||
| else { | ||||||
| player.poster = source.poster ?? options.poster; | ||||||
| player.source = source; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| export function setPoster(id, poster) { | ||||||
| const p = Data.get(id); | ||||||
| if (p) { | ||||||
| const { player } = p; | ||||||
| player.poster = poster; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| export function dispose(id) { | ||||||
| const p = Data.get(id); | ||||||
| Data.remove(id); | ||||||
|
|
||||||
| if (p) { | ||||||
| const { player } = p; | ||||||
| if (player) { | ||||||
| player.destroy(); | ||||||
| player = null; | ||||||
|
Comment on lines
+108
to
+109
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (bug_risk): Avoid reassigning a destructured variable in dispose. Since 'player' is a destructured const, nulling it doesn’t affect p. To clear the reference, assign null to p.player. |
||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| const handlerEventName = (name, p) => { | ||||||
| const { el, invoke, method, player } = p; | ||||||
| player.on(name, () => { | ||||||
| const fire = el.getAttribute('data-bb-event') === 'true'; | ||||||
| if (fire) { | ||||||
| invoke.invokeMethodAsync(method, name); | ||||||
| } | ||||||
| }); | ||||||
| } | ||||||
|
|
||||||
| const handlerEvents = p => { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (complexity): Consider inlining the event registration logic into a single loop to simplify the code. Consider inlining the event registration logic into a single loop instead of creating two helper functions. This reduces one layer of abstraction without altering functionality. For example, instead of having separate functions: const handlerEventName = (name, p) => {
const { el, invoke, method, player } = p;
player.on(name, () => {
const fire = el.getAttribute('data-bb-event') === 'true';
if (fire) {
invoke.invokeMethodAsync(method, name);
}
});
}
const handlerEvents = p => {
['ready', 'play', 'pause', 'ended', 'enterfullscreen', 'exitfullscreen', 'languagechange']
.forEach(name => {
handlerEventName(name, p);
});
}You can inline the logic like this: const handlerEvents = (p) => {
const { el, invoke, method, player } = p;
['ready', 'play', 'pause', 'ended', 'enterfullscreen', 'exitfullscreen', 'languagechange']
.forEach(name => {
player.on(name, () => {
if (el.getAttribute('data-bb-event') === 'true') {
invoke.invokeMethodAsync(method, name);
}
});
});
}This change maintains the same behavior while simplifying the code structure. |
||||||
| ['ready', 'play', 'pause', 'ended', 'enterfullscreen', 'exitfullscreen', 'languagechange'].forEach(name => { | ||||||
| handlerEventName(name, p); | ||||||
| }); | ||||||
| } | ||||||
|
|
||||||
| const setLang = (option) => { | ||||||
| option.i18n = { | ||||||
| restart: '重启', | ||||||
|
|
@@ -112,48 +173,3 @@ const setLang = (option) => { | |||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| export function reload(id, options) { | ||||||
| const p = Data.get(id); | ||||||
| if (p === null) { | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| const { player, hls } = p; | ||||||
| const source = options.source.sources; | ||||||
| delete options.source; | ||||||
| if (hls) { | ||||||
| if (source.length > 0) { | ||||||
| const src = source[0].src; | ||||||
| hls.loadSource(src); | ||||||
| } | ||||||
| } | ||||||
| else { | ||||||
| player.poster = source.poster ?? options.poster; | ||||||
| player.source = source; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| export function setPoster(id, poster) { | ||||||
| execute(id, p => { | ||||||
| const { player } = p; | ||||||
| player.poster = poster; | ||||||
| }); | ||||||
| } | ||||||
|
|
||||||
| const execute = (id, callback) => { | ||||||
| const p = Data.get(id); | ||||||
| if (p) { | ||||||
| callback(p); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| export function dispose(id) { | ||||||
| const p = Data.get(id); | ||||||
| Data.remove(id); | ||||||
|
|
||||||
| execute(id, player => { | ||||||
| player.destroy(); | ||||||
| player = null; | ||||||
| }); | ||||||
| } | ||||||
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.
question (bug_risk): Review the logic of the EventString property.
The mapping is inverted: ‘true’ is set when OnEvent is null and cleared when a callback exists. Swap these so JS events fire only when a callback is provided.