axe-core
axe-core copied to clipboard
Add generator method to cache.get()
There are a good bunch of places in axe where we have a pattern like this:
let x;
if (cache.get('x') === undefined) {
x = 12345
cache.set('x', x);
} else {
x = cache.get('x')
}
I think a cleaner way that express this would be with a method that either returns the value from cache, or generates it if the value isn't cached. I would like to see us replace those patterns with a slightly higher-level solution like this:
const x = cache.pull('x', () => 12345);
In other words. If x
is defined, get it from the cache. If it's not, run the callback, store that as x
, and then return it. This has a few advantages. Firstly, it's more expressive. It's also shorter, and I think it discourages using set
and get
in different places, using the cache as some kind of global scope, which is not a good practice.
tech debt.. No QA required
Moved it to "In Progress" as nothing merged against this ticket yet.
Completed in https://github.com/dequelabs/axe-core/pull/3608