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

[DX-324] full async plugins initialisation #795

Merged
merged 3 commits into from
Dec 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/registry/domain/plugins-initialiser.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,27 +81,28 @@ module.exports.init = function(pluginsToRegister, callback) {
};

const loadPlugin = function(plugin, cb) {
const done = _.once(cb);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This allows robustness against plugins that would erroneously call the done() callback multiple times:
https://github.com/opencomponents/oc-hobknob/blob/master/index.js#L25


if (registered[plugin.name]) {
return cb();
return done();
}

if (!plugin.register.dependencies) {
plugin.register.dependencies = [];
}

if (!dependenciesRegistered(plugin.register.dependencies)) {
return defer(plugin, cb);
return defer(plugin, done);
}

const dependencies = _.pick(registered, plugin.register.dependencies);

plugin.register.register(
plugin.options || {},
dependencies,
plugin.callback || _.noop
);
registered[plugin.name] = plugin.register.execute;
cb();
plugin.register.register(plugin.options || {}, dependencies, err => {
const pluginCallback = plugin.callback || _.noop;
pluginCallback(err);
registered[plugin.name] = plugin.register.execute;
done(err);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now the callback is called actually after the registration is completed

});
};

const terminator = function(err) {
Expand Down
27 changes: 11 additions & 16 deletions test/unit/registry-domain-plugins-initialiser.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,29 +105,24 @@ describe('registry : domain : plugins-initialiser', () => {
{
name: 'getValue',
register: {
register: function(options, deps, cb) {
register: (options, deps, cb) => {
passedOptions = options;
cb();
},
execute: function(key) {
return passedOptions[key];
}
execute: key => passedOptions[key]
},
options: {
a: 123,
b: 456
}
options: { a: 123, b: 456 }
},
{
name: 'isFlagged',
register: {
register: function(options, deps, cb) {
flag = true;
cb();
register: (options, deps, cb) => {
setTimeout(() => {
flag = true;
cb();
}, 10);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this artificial timeout, I am testing that the callback is actually called after the registration (test failed before the code change)

},
execute: function() {
return flag;
}
execute: () => flag
}
}
];
Expand All @@ -148,8 +143,8 @@ describe('registry : domain : plugins-initialiser', () => {
});

it('should be make the functionality usable', () => {
const a = result.getValue('a'),
flagged = result.isFlagged();
const a = result.getValue('a');
const flagged = result.isFlagged();

expect(a).to.equal(123);
expect(flagged).to.equal(true);
Expand Down