Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Commit

Permalink
refactor: use Map for cache over Object
Browse files Browse the repository at this point in the history
  • Loading branch information
keithamus committed May 10, 2019
1 parent 774c954 commit d786f8d
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/index.js
Expand Up @@ -12,17 +12,17 @@ const DEFAULT_EXTS = [ '.mjs', '.js', '.json', '.node' ];
const readFileAsync = file => new Promise((fulfil, reject) => fs.readFile(file, (err, contents) => err ? reject(err) : fulfil(contents)));
const statAsync = file => new Promise((fulfil, reject) => fs.stat(file, (err, contents) => err ? reject(err) : fulfil(contents)));
const cache = fn => {
let cache = Object.create(null);
const wrapped = (s, cb = false) => {
if (s in cache === false) {
cache[s] = fn(s).catch(err => {
delete cache[s];
const cache = new Map();
const wrapped = (fileName, cb) => {
if (cache.has(fileName) === false) {
cache.set(fileName, fn(fileName).catch(err => {
cache.delete(fileName);
throw err;
});
}));
}
return cb ? cache[s].then(v => cb(null, v), cb) : cache[s];
return cache.get(fileName).then(v => cb(null, v), cb);
};
wrapped.clear = () => { cache = {}; };
wrapped.clear = () => cache.clear();
return wrapped;
};
const ignoreENOENT = err => {
Expand Down

0 comments on commit d786f8d

Please sign in to comment.