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

execution order of plugins initialization & player initialize callback function #2505

Closed
Vuuo opened this issue Aug 24, 2015 · 3 comments
Closed

Comments

@Vuuo
Copy link

Vuuo commented Aug 24, 2015

videojs version: 4.11

I wanna make a plugin. The plugin will attach a function to the player which get initialized. Then I wanna be able to call the attached function in the player initialize callback function.

// my_plugin.js
(function(window, videojs) {
  my_plugin = function(init_options) {
    return console.log('my_plugin initialized');
    this.my_func = function() {
      return console.log('my_func invoked');
    }
  };
  return videojs.plugin('my_plugin', my_plugin);
})(window, window.videojs);
// index.html
...
<script>
  player = videojs('player', {
    techOrder: ["flash", "html5"]
    ...
    plugins: {
      my_plugin: {}
    }
  }, function(){
    console.log('player initialize callback function invoked');
    this.my_func();
  });
</script>

If the tech is flash, this.my_func() will get executed well. However if the tech is html5, it throws error saying that the function is not defined.

Further investigation tells me that in flash mode, the plugin get initialized before the callback function runs. While in html5 mode, the callback function get executed before the plugin initialization.

Looks like the behaviour is inconsistent across flash and html5. Or am I doing something wrong?
Any workaround would also be great.

Thanks

@mmcc
Copy link
Member

mmcc commented Aug 24, 2015

This looks like it is a bug in 4.x, but there is very little chance we're going to put out a patch to fix this in 4.12. The easiest workaround would me to call your function in your ready block instead of using options to initialize:

var player = videojs('player', {}, function() {
  this.my_plugin();
  this.my_func();
});

I was hoping this had been fixed in 5.0 already, but it looks like we've got a new issue to fix regarding plugin initialization via an options object. So basically, same workaround required in master right now, but we're working on it.

All of this being said, if you're planning on releasing this plugin into the wild, please add things to the videojs prototype with extreme caution. There's the simple case of two plugins potentially overwriting each other, but we also try to subscribe to the "don't modify objects you don't own" philosophy, so generally we suggest adding new methods under your plugin rather than directly to vjs. Feel free to totally ignore all of that if you plan on keeping this plugin to yourself; this is more applicable if you plan on releasing the plugin publicly.

@mmcc mmcc closed this as completed Aug 24, 2015
@mmcc mmcc reopened this Aug 24, 2015
@mmcc
Copy link
Member

mmcc commented Aug 24, 2015

I initially accidentally closed this, but I think #2510 probably covers this in terms of tracking. Closing this in lieu of that issue, but feel free to continue the conversation here or there.

@mmcc mmcc closed this as completed Aug 24, 2015
@Vuuo
Copy link
Author

Vuuo commented Aug 24, 2015

I see. Thank you very much.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants