|
3 | 3 | const cacheSymbol = Symbol();
|
4 | 4 |
|
5 | 5 | function probe(file, fs, callback) {
|
6 |
| - const cachedPrecision = fs[cacheSymbol]; |
| 6 | + let cachedPrecisions = fs[cacheSymbol]; |
| 7 | + if (cachedPrecisions === undefined) { |
| 8 | + cachedPrecisions = new Map(); |
7 | 9 |
|
8 |
| - if (cachedPrecision) { |
9 |
| - return fs.stat(file, (err, stat) => { |
10 |
| - /* istanbul ignore if */ |
11 |
| - if (err) { |
12 |
| - return callback(err); |
13 |
| - } |
14 |
| - |
15 |
| - callback(null, stat.mtime, cachedPrecision); |
16 |
| - }); |
| 10 | + // Cache the precisions in a non-enumerable way |
| 11 | + Object.defineProperty(fs, cacheSymbol, { value: cachedPrecisions }); |
17 | 12 | }
|
18 | 13 |
|
19 |
| - // Set mtime by ceiling Date.now() to seconds + 5ms so that it's "not on the second" |
20 |
| - const mtime = new Date((Math.ceil(Date.now() / 1000) * 1000) + 5); |
21 |
| - |
22 |
| - fs.utimes(file, mtime, mtime, (err) => { |
| 14 | + return fs.stat(file, (err, stat) => { |
23 | 15 | /* istanbul ignore if */
|
24 | 16 | if (err) {
|
25 | 17 | return callback(err);
|
26 | 18 | }
|
27 | 19 |
|
28 |
| - fs.stat(file, (err, stat) => { |
| 20 | + const dev = stat.dev; |
| 21 | + |
| 22 | + // Precisions are cached by device, see #103 |
| 23 | + const precision = cachedPrecisions.get(dev); |
| 24 | + if (precision !== undefined) { |
| 25 | + return callback(null, stat.mtime, precision); |
| 26 | + } |
| 27 | + |
| 28 | + // Set mtime by ceiling Date.now() to seconds + 5ms so that it's "not on the second" |
| 29 | + const mtime = new Date((Math.ceil(Date.now() / 1000) * 1000) + 5); |
| 30 | + |
| 31 | + fs.utimes(file, mtime, mtime, (err) => { |
29 | 32 | /* istanbul ignore if */
|
30 | 33 | if (err) {
|
31 | 34 | return callback(err);
|
32 | 35 | }
|
33 | 36 |
|
34 |
| - const precision = stat.mtime.getTime() % 1000 === 0 ? 's' : 'ms'; |
| 37 | + fs.stat(file, (err, stat) => { |
| 38 | + /* istanbul ignore if */ |
| 39 | + if (err) { |
| 40 | + return callback(err); |
| 41 | + } |
35 | 42 |
|
36 |
| - // Cache the precision in a non-enumerable way |
37 |
| - Object.defineProperty(fs, cacheSymbol, { value: precision }); |
| 43 | + const precision = stat.mtime.getTime() % 1000 === 0 ? 's' : 'ms'; |
| 44 | + cachedPrecisions.set(dev, precision); |
38 | 45 |
|
39 |
| - callback(null, stat.mtime, precision); |
| 46 | + callback(null, stat.mtime, precision); |
| 47 | + }); |
40 | 48 | });
|
41 | 49 | });
|
42 | 50 | }
|
|
0 commit comments