iitc-project / ingress-intel-total-conversion

ingress.com/intel total conversion user script with some new features. Should allow easier extension of the intel map.
http://iitc.jonatkins.com/
ISC License
991 stars 552 forks source link

plugins calling pluginCreateHook for other plugins' hooks [code cleanup] #1248

Open nhamer opened 6 years ago

nhamer commented 6 years ago

Example here, but it's literally every plugin which depends on another plugin which isn't fragile:

https://github.com/iitc-project/ingress-intel-total-conversion/blob/0fabfb68943fc833153d89454220fa638a8b9fb6/plugins/cross_link.user.js#L296

The problem is, at boot time, a plugin may want to listen to a plugin hook for a plugin which is loaded, but hasn't yet run its own setup, so addHook() will fail. The correct way to add callbacks for plugin hooks is something along the lines of the following:

function setup() {
  function addMyHooks() {
    window.addHook('pluginDrawTools', function(e) {
      // actual hook code here
    });
  }
  if(window.iitcLoaded) {
    addMyHooks();
  } else {
    window.addHook('iitcLoaded', addMyHooks);
  }
}

but that's (a) ugly and (b) unobvious, so every plugin author just calls pluginCreateHook.

I think the right fix is just to not check for VALID_HOOKS in addHook.