Comments (5)
So I guess the DIY way is something like:
const rules = {...};
di.addRules(rules);
di.resetRules = function() {
di.addRules(rules);
};
from di-ninja.
You can do this way, but you have to disable the ruleCache first
di.config('ruleCache', false)
ruleCache is enabled by default for optimization because the main expected usage is only one main call to addRules by application and the processing of rules is the main computing cost of Di-Ninja.
There is another point, the rules are apllied in cascade in a deep merging process (a little like CSS), and in your first call to di.addRules
, the override will be on rulesDefault, and on the second call, from the other rules that you have defined, there will be no difference if you explicit all options of a rule but else this can lead to unexpected results.
A workaround is to completely override the rules object like this
di.resetRules = function() {
di.rules = {} // hard reset
di.addRules(rules)
}
it's a bit hacky and if you call resetRules
when there is some async rule procressing, you'll make a knot bag.
Another approach would be to explicit all options of a rule on reset
di.resetRules = function() {
const explicitRules = {}
Object.keys(rules).forEach(key=>{
const explicitRule = {}
di.mergeRule(explicitRule, di.rulesDefault)
di.mergeRule(explicitRule, rules[key])
explicitRules[key] = explicitRule
})
di.addRules(explicitRules)
}
But I think the better way is to instantiate another di container, after all, this is not a damn singleton ;), so you can just
import container from 'di-ninja'
const di = container({
rules,
})
/*
exactly the same as
const di = container()
di.addRules(rules)
*/
const di2 = container({
rules2,
})
from di-ninja.
The separate di container seems the most promising. Thanks for the tip!
Other than that, the caching behavior seems like a gotcha. But not undocumented :)
from di-ninja.
You're right for the gotcha of caching, this desserve a more explicit warning in documentation, maybe in rules section, feel free to make a pull request for this.
I have to add precision that you have this problem only if you call di.addRules
after di.get
on the rule
// no cache no problem
di.addRules({
'A': { /*...*/ }
})
di.addRules({
'A': { /*...*/ }
})
di.get('A')
// cache problem
di.addRules({
'A': { /*...*/ }
})
// this call build rule and cache it
di.get('A')
// this is no more effective
di.addRules({
'A': { /*...*/ }
})
/*
maybe this should send a warning
I'll investigate into this, and I'll add a console warning
PR is welcome too
*/
from di-ninja.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
from di-ninja.
Related Issues (7)
- Question: di.get the only way to instantiate? HOT 5
- add full code examples of composition root with dependencies HOT 4
- Doubt about using singleton HOT 4
- add documentation on interface-prototype
- How to check whether dependency exists HOT 2
- Error `Invalid attempt to spread non-iterable instance` on server side when use `calls` option HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from di-ninja.