Skip to content

Commit 70d177f

Browse files
FNDmoonglum
authored andcommitted
switched to proper Set and Map instances
replacing poor man's versions
1 parent a709c62 commit 70d177f

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

lib/util/files/index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,26 @@ let { writeFile } = require("fs/promises");
44
let { mkdirSync } = require("fs");
55
let path = require("path");
66

7-
let KNOWN = {}; // avoids redundant `mkdirp` invocations
8-
let LOCKS = {};
7+
let KNOWN = new Set(); // avoids redundant `mkdir` invocations
8+
let LOCKS = new Map();
99

1010
// avoids concurrent write operations and creates target directory if necessary
1111
module.exports = function createFile(filepath, contents) {
12-
let lock = LOCKS[filepath];
12+
let lock = LOCKS.get(filepath);
1313
if(lock) { // defer
1414
return lock.then(_ => createFile(filepath, contents));
1515
}
1616

1717
// create directory if necessary
18-
if(!KNOWN[filepath]) {
19-
KNOWN[filepath] = true;
18+
if(!KNOWN.has(filepath)) {
19+
KNOWN.add(filepath);
2020
// NB: `sync` avoids race condition for subsequent operations
2121
mkdirSync(path.dirname(filepath), { recursive: true });
2222
}
2323

2424
let prom = writeFile(filepath, contents);
25-
LOCKS[filepath] = prom;
25+
LOCKS.set(filepath, prom);
2626
return prom.then(_ => {
27-
delete LOCKS[filepath];
27+
LOCKS.delete(filepath);
2828
});
2929
};

0 commit comments

Comments
 (0)