axe-core icon indicating copy to clipboard operation
axe-core copied to clipboard

Add generator method to cache.get()

Open WilcoFiers opened this issue 2 years ago • 2 comments

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.

WilcoFiers avatar Nov 12 '21 12:11 WilcoFiers

tech debt.. No QA required

padmavemulapati avatar Sep 01 '22 14:09 padmavemulapati

Moved it to "In Progress" as nothing merged against this ticket yet.

padmavemulapati avatar Sep 01 '22 14:09 padmavemulapati

Completed in https://github.com/dequelabs/axe-core/pull/3608

straker avatar Oct 17 '22 19:10 straker