-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cache previously transformed request of dev server (#74)
* refactor: rename `transform` on cache service with `hash` Because it misleads user defined transformation. * refactor: split the logic that testing cache considering deps * feat: cache previously transformed request of dev server * test: add test case for dev server cache update * test: add test case for additional data of cache * fix: register nested deps' cache of target source * chore: add comment * chore: tweak comment
- Loading branch information
Showing
8 changed files
with
359 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
'use strict' | ||
|
||
/** | ||
* Similar with Cache but considering files dependencies. | ||
*/ | ||
class DepCache { | ||
constructor (cache, depResolver, readFile) { | ||
this.cache = cache | ||
this.depResolver = depResolver | ||
this.readFile = readFile | ||
} | ||
|
||
get (filename) { | ||
return this.cache.get(filename) | ||
} | ||
|
||
register (filename, source, data) { | ||
const footprints = new Set() | ||
|
||
const loop = (filename, source) => { | ||
if (footprints.has(filename)) { | ||
return | ||
} | ||
footprints.add(filename) | ||
|
||
// #26 | ||
// When `source` is empty value, | ||
// the file is not exists so we must clear the cache. | ||
if (source == null) { | ||
this.clear(filename) | ||
return | ||
} | ||
|
||
this.cache.register(filename, source, data) | ||
this.depResolver.register(filename, source) | ||
|
||
this.depResolver.getOutDeps(filename).forEach(dep => { | ||
const depSource = this.readFile(dep) | ||
if (this.cache.test(dep, depSource)) { | ||
return | ||
} | ||
|
||
loop(dep, depSource) | ||
}) | ||
} | ||
|
||
loop(filename, source, data) | ||
} | ||
|
||
clear (filename) { | ||
// Only clear the target file cache because we cannot be sure | ||
// the deps are really no longer useless for now. | ||
// i.e. The user may refer them directly via `test` method. | ||
this.cache.clear(filename) | ||
this.depResolver.clear(filename) | ||
} | ||
|
||
/** | ||
* Test the given file has the same contents. | ||
* It also considers the dependencies. That means it is treated as | ||
* updated when one of the dependenies is changed even if the target | ||
* file itself is not. | ||
*/ | ||
test (fileName, source) { | ||
// If original source is updated, it should not be the same. | ||
if (!this.cache.test(fileName, source)) { | ||
return false | ||
} | ||
|
||
const deps = this.depResolver.getOutDeps(fileName) | ||
|
||
// Loop through deps to compare its contents | ||
let isUpdate = false | ||
for (const fileName of deps) { | ||
const contents = this.readFile(fileName) | ||
|
||
// What should we do if possible deps are not found? | ||
// For now, treat it as updated contents | ||
// so that transformers can handle the error | ||
if (contents == null) { | ||
isUpdate = true | ||
} else { | ||
isUpdate = isUpdate || !this.cache.test(fileName, contents) | ||
} | ||
} | ||
|
||
return !isUpdate | ||
} | ||
} | ||
module.exports = DepCache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.