Skip to content

Commit

Permalink
fix: test open handles
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Mar 27, 2023
1 parent 72de39c commit 083f4b7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
2 changes: 1 addition & 1 deletion docs/src/guide/storages.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ setupCache(axios, {
// You don't need to to that, as it is the default option.
storage: buildMemoryStorage(
/* cloneData default=*/ false,
/* cleanupInterval default=*/ 1000 * 60 * 60 /* 1 hour */
/* cleanupInterval default=*/ false
)
});
```
Expand Down
57 changes: 31 additions & 26 deletions src/storage/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ declare const structuredClone: (<T>(value: T) => T) | undefined;
* @param {boolean} cloneData If the data returned by `find()` should be cloned to avoid
* mutating the original data outside the `set()` method.
*
* @param {number} cleanupInterval The interval in milliseconds to run a
* setInterval job of cleaning old entries. If false, the job will not be created. Defaults to 1 hour
* @param {number | false} cleanupInterval The interval in milliseconds to run a
* setInterval job of cleaning old entries. If false, the job will not be created. Disabled is default
*/
export function buildMemoryStorage(cloneData = false, cleanupInterval = 1000 * 60 * 60) {
export function buildMemoryStorage(
cloneData = false,
cleanupInterval: number | false = false
) {
const storage = buildStorage({
set: (key, value) => {
storage.data[key] = value;
Expand Down Expand Up @@ -67,34 +70,36 @@ export function buildMemoryStorage(cloneData = false, cleanupInterval = 1000 * 6
// When this program gets running for more than the specified interval, there's a good
// chance of it being a long-running process or at least have a lot of entries. Therefore,
// "faster" loop is more important than code readability.
storage.cleaner = setInterval(() => {
const keys = Object.keys(storage.data);
if (cleanupInterval) {
storage.cleaner = setInterval(() => {
const keys = Object.keys(storage.data);

let i = -1,
value: StorageValue,
key: string;
let i = -1,
value: StorageValue,
key: string;

// Looping forward, as older entries are more likely to be expired
// than newer ones.
while (++i < keys.length) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
(key = keys[i]!), (value = storage.data[key]!);
// Looping forward, as older entries are more likely to be expired
// than newer ones.
while (++i < keys.length) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
(key = keys[i]!), (value = storage.data[key]!);

if (value.state === 'empty') {
// this storage returns void.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
storage.remove(key);
continue;
}
if (value.state === 'empty') {
// this storage returns void.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
storage.remove(key);
continue;
}

// If the value is expired and can't be stale, remove it
if (value.state === 'cached' && isExpired(value) && !canStale(value)) {
// this storage returns void.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
storage.remove(key);
// If the value is expired and can't be stale, remove it
if (value.state === 'cached' && isExpired(value) && !canStale(value)) {
// this storage returns void.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
storage.remove(key);
}
}
}
}, cleanupInterval);
}, cleanupInterval);
}

return storage;
}
Expand Down
3 changes: 3 additions & 0 deletions test/storage/memory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,8 @@ describe('tests memory storage', () => {
await expect(storage.get('loading')).resolves.toMatchObject({ state: 'loading' });
await expect(storage.get('cached')).resolves.toMatchObject({ state: 'cached' });
await expect(storage.get('expiredCache')).resolves.toMatchObject({ state: 'empty' });

// Clears handle
clearTimeout(storage.cleaner)
});
});

0 comments on commit 083f4b7

Please sign in to comment.