base/base-task

Cannot reuse plugin, tasks get reinitialized

maxwellb opened this issue · 2 comments

Base plugins which have app.use(tasks()) overwrite each other's tasks.

Suppose I am writing two base plugins, each of which export something like the following.

const tasks = require("base-task");
const pluginId = "my-id"; // generated
module.exports = function (app, ..._rest) {
    if (app.isRegistered(pluginId)) {
        return;
    }
    
    app.use(tasks());
    app.task("foo", function(done) {
        console.log("I am a task");
        done();
    });
}

If the first plugin defines task "foo", and the second "bar", then app will only have task "bar" because the tasks got reinitialized.

I can workaround this by using the following in place of app.use(tasks()).

    if (typeof app.task !== "function") {
        app.use(tasks());
    }

Should base-task use the isRegistered pattern, or is there a use case for reinitialization?

doowb commented

In the latest version of base the .use method can take a name as the first argument. If app is from the new base, then try the following:

app.use('task', tasks());

You can see this done in the tests here

Thanks, I'll give it a try.