Skip to content

Commit

Permalink
add [names] option to resetProviders
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas O'Brien committed Feb 15, 2018
1 parent 889cc52 commit 5d2dc55
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
19 changes: 12 additions & 7 deletions src/Bottle/reset-providers.js
Expand Up @@ -11,18 +11,23 @@ var removeProviderMap = function resetProvider(name) {
};

/**
* Resets all providers on a bottle instance.
* Resets providers on a bottle instance. If 'names' array is provided, only the named providers will be reset.
*
* @param Array names
* @return void
*/
var resetProviders = function resetProviders() {
var providers = this.originalProviders;
Object.keys(this.originalProviders).forEach(function resetPrvider(provider) {
var parts = provider.split(DELIMITER);
var resetProviders = function resetProviders(names) {
var tempProviders = this.originalProviders;

Object.keys(this.originalProviders).forEach(function resetProvider(originalProviderName) {
if (Array.isArray(names) && names.indexOf(originalProviderName) === -1) {
return;
}
var parts = originalProviderName.split(DELIMITER);
if (parts.length > 1) {
parts.forEach(removeProviderMap, getNestedBottle.call(this, parts[0]));
}
removeProviderMap.call(this, provider);
this.provider(provider, providers[provider]);
removeProviderMap.call(this, originalProviderName);
this.provider(originalProviderName, tempProviders[originalProviderName]);
}, this);
};
20 changes: 19 additions & 1 deletion test/spec/provider.spec.js
Expand Up @@ -123,7 +123,7 @@
expect(b.container.Util.ThingProvider).toBeDefined();
expect(b.container.Util.Thing).toBeDefined();
});

it('does not log an error if a service is added to a nested bottle with initialized services', function() {
var b = new Bottle();
var Thing = function() {};
Expand Down Expand Up @@ -171,6 +171,24 @@
expect(b.container.Thing instanceof ThingProvider).toBe(true);
expect(i).toEqual(2);
});
it('allows for selectively resetting providers by name', function() {
var i = 0;
var j = 0;
var b = new Bottle();
var FirstProvider = function() { i = ++i; this.$get = function() { return this; }; };
var SecondProvider = function() { j = ++j; this.$get = function() { return this; }; };
b.provider('First', FirstProvider);
b.provider('Second', SecondProvider);
expect(b.container.First instanceof FirstProvider).toBe(true);
expect(b.container.Second instanceof SecondProvider).toBe(true);
expect(i).toEqual(1);
expect(j).toEqual(1);
b.resetProviders(['First']);
expect(b.container.First instanceof FirstProvider).toBe(true);
expect(b.container.Second instanceof SecondProvider).toBe(true);
expect(i).toEqual(2);
expect(j).toEqual(1);
});
it('allows for sub containers to re-initiate as well', function() {
var i = 0;
var b = new Bottle();
Expand Down

0 comments on commit 5d2dc55

Please sign in to comment.