EvolveLabs / electron-plugins

Plugin loader for electron applications.
MIT License
58 stars 11 forks source link

why are the modules not return in the callback? #4

Closed alvarolorentedev closed 8 years ago

alvarolorentedev commented 8 years ago

so i have my use case where i need to call some functions common to my modules during execution. I am finding that this seems not actually possible as in the callback the return values are a list of the 'dependencies'.

function loadPlugin(context, results, callback) {
    var modules = []
    var dependencies = []
    try {
            ...
            var Plugin = require(file)
            var mod = new Plugin(context.appContext)
            modules.push(mod)
            dependencies.push(depName)
        }
    } catch (err) {
        return callback(err)
    }

    callback(null, dependencies)
}

is there any why the modules collection should not be returned in the callback?

justinmchase commented 8 years ago

That would probably be ok but you can also add things onto the context.appContext.

var appContext = {
  common: function (dep) { console.log(dep); }
}

loadPlugins({ appContext: appContext }, results, callback);

Then in your plugin:

module.exports = function (context) {

   context.common(this); // do your common stuff here...

};
alvarolorentedev commented 8 years ago

yes but i am trying to do a crossplatform social network manager, so i need access to a plugin for example to post. That in my brain feels like a direct request from the app side towards the plugin itself. It feels not natural to do a double indirection to achieve it if i know all my plugins have the same signature. Thanks for the fast reply :+1:

justinmchase commented 8 years ago

Can you give a little code snippet with what you want to do with it?

alvarolorentedev commented 8 years ago

yes of course, first of course i will load the plugins and keep a reference to the collection

view.document.addEventListener('DOMContentLoaded', function () {
plugins.load(context, (err, ids, plugins) => {
                if(err)
                    return;
                this.pluginsHolder = plugins;
            });
});

each plugin contains a social network, each of the plugins export a class with the same signature. So i can do something like:

function postData()
{
       for (var i = 0; i < this.pluginsHolder.lenght; ++i) {
                this.pluginsHolder[i].post('Hello World');
       }
}
justinmchase commented 8 years ago

Oh ok! I'm glad you provided that snippet. You're just saying that you want the modules to also be added as a parameter to the callback?

e.g.

callback(null, dependencies, modules)

That would be pretty easy and backward compatible if you say yes I can publish this change pretty fast.

alvarolorentedev commented 8 years ago

yes that us exactly what i was thinking about :). if you publish that change it will be really helpful for me.

justinmchase commented 8 years ago

Published in v0.1.0

alvarolorentedev commented 8 years ago

thanks a lot :)