Skip to content

Commit

Permalink
Add method to invalidate the entire cache
Browse files Browse the repository at this point in the history
  • Loading branch information
jantimon committed Jun 28, 2016
1 parent d47cedf commit 2612ef1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.09

- Add method to invalidate the entire cache

## 0.08

- Support instantiation without options
Expand Down
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,16 @@ HotFileCache.prototype.isFileWatched = function (file) {
*/
HotFileCache.prototype.invalidateCache = function (reason, filepath) {
if (this.cache[filepath]) {
this.emit('cache-revoked', filepath);
this.emit('cache-revoked', filepath, reason);
}
delete this.cache[filepath];
};

/**
* Invalidate entire cache
*/
HotFileCache.prototype.invalidateEntireCache = function (reason) {
Object.keys(this.cache).forEach(this.invalidateCache.bind(this, reason));
};

module.exports = HotFileCache;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hot-file-cache",
"version": "0.0.8",
"version": "0.0.9",
"description": "A watched file cache which will invalidate automatically if the source changes",
"main": "index.js",
"scripts": {
Expand Down
10 changes: 10 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ test('get files', async t => {
t.pass();
});

test('invalidate entire cache', async t => {
const dir = await createTestEnvironment();
const cache = new HotFileCache(['*.md', '**/*.json'], { cwd: dir });
await cache.readFile(path.join(dir, 'subdir', 'subsubdir', 'file3.json'));
t.is(Object.keys(cache.cache).length, 1);
cache.invalidateEntireCache();
t.is(Object.keys(cache.cache).length, 0);
t.pass();
});

test('file processor', async t => {
const dir = await createTestEnvironment();
const processor = (file, fileContent) => JSON.parse(fileContent);
Expand Down

0 comments on commit 2612ef1

Please sign in to comment.