GithubHelp home page GithubHelp logo

Comments (5)

ernsheong avatar ernsheong commented on May 27, 2024

So I guess the DIY way is something like:

const rules = {...};

di.addRules(rules);

di.resetRules = function() {
  di.addRules(rules);
};

from di-ninja.

devthejo avatar devthejo commented on May 27, 2024

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.

ernsheong avatar ernsheong commented on May 27, 2024

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.

devthejo avatar devthejo commented on May 27, 2024

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.

lock avatar lock commented on May 27, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.