mariocasciaro / scatter

IoC container and out-of-the-box extensibility for Node.js applications
MIT License
154 stars 14 forks source link

assemble and registerModuleInstance not working as expected #38

Open cludden opened 7 years ago

cludden commented 7 years ago

first off, awesome project!

ok, heres my issue. In a service, I load an entire namespace (using the scatter-plugin-all plugin), iterate over the modules and register new modules in a new namespace depending on certain configuration. this part works as expected

// psuedo-code
scatter.load('config')
.then(config => {
    scatter.load('all!workers')
    .then(workers => {
        return Object.keys(workers).map(name => [name, workers[name]]
    })
    .each(pair => {
        const [name, worker] = pair
        const data = { /* ... */ }
        if (config.enabled[name]) {
            const consumer = new Consumer(name, worker, data)
            container.registerModuleInstance(`consumers/${name}`, consumer)
        }
    })
})

the issue is, when I then try to load all of the new namespace in another module, i receive an empty object, unless i load twice. my guess is this has something to do with the caching mechanism and the fact that the new namespace is registered after startup

container.load('all!consumers')
.then(console.log) // {}
.then(() => container.load('all!consumers'))
.then(console.log) // { 'consumers/a': Consumer, 'consumers/b': Consumer }

any ideas on what I'm doing wrong?