Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1384 from blackflux/dev
[Gally]: master <- dev
- Loading branch information
Showing
8 changed files
with
128 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test/util/cache-clearer-disabled.spec.js |
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,57 @@ | ||
import assert from 'assert'; | ||
import LRU from 'lru-cache-ext'; | ||
|
||
const getFns = (obj) => { | ||
const result = []; | ||
const properties = []; | ||
let o = obj; | ||
while (o instanceof Object) { | ||
properties.push(...Object.getOwnPropertyNames(o)); | ||
o = Object.getPrototypeOf(o); | ||
} | ||
for (let i = 0; i < properties.length; i += 1) { | ||
const key = properties[i]; | ||
const value = LRU.prototype[key]; | ||
if (typeof value !== 'function') { | ||
// eslint-disable-next-line no-continue | ||
continue; | ||
} | ||
result.push({ obj, key, value }); | ||
} | ||
return result; | ||
}; | ||
|
||
export default () => { | ||
let injected = false; | ||
const fns = [ | ||
...getFns(LRU.prototype) | ||
]; | ||
const caches = []; | ||
|
||
return { | ||
inject: () => { | ||
assert(injected === false); | ||
fns.forEach(({ obj, key, value }) => { | ||
try { | ||
// eslint-disable-next-line no-param-reassign,func-names | ||
obj[key] = function (...args) { | ||
caches.push(this); | ||
return value.call(this, ...args); | ||
}; | ||
} catch (e) { /* ignored */ } | ||
}); | ||
injected = true; | ||
}, | ||
release: () => { | ||
assert(injected === true); | ||
fns.forEach(({ obj, key, value }) => { | ||
try { | ||
// eslint-disable-next-line no-param-reassign | ||
obj[key] = value; | ||
} catch (e) { /* ignored */ } | ||
}); | ||
caches.splice(0).forEach((c) => c.clear()); | ||
injected = false; | ||
} | ||
}; | ||
}; |
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,19 @@ | ||
import LRU from 'lru-cache-ext'; | ||
import { expect } from 'chai'; | ||
import describe from '../../src/util/desc.js'; | ||
|
||
describe('Testing Clear Cache Disabled', { clearCache: false }, () => { | ||
let cache; | ||
before(() => { | ||
cache = new LRU({ ttl: 100, max: 100 }); | ||
}); | ||
|
||
it('First', () => { | ||
expect(cache.has('a')).to.equal(false); | ||
cache.set('a', 1); | ||
}); | ||
|
||
it('Second', () => { | ||
expect(cache.has('a')).to.equal(true); | ||
}); | ||
}); |
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