-
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(timed-memoize): additional memoize decorator with timeout
- Loading branch information
Showing
3 changed files
with
63 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { TimedMemoize } from './timed-memoize'; | ||
|
||
class TestTimedMemoize { | ||
counter = 0; | ||
|
||
@TimedMemoize(2000) | ||
count(...args: any[]) { | ||
return ++this.counter; | ||
} | ||
} | ||
|
||
describe('TimedMemoize', () => { | ||
it('should really invalidate cache',() => { | ||
const past = Date.now() + 50000; | ||
const test = new TestTimedMemoize(); | ||
expect(test.count(1)).toEqual(1); | ||
|
||
jest.spyOn(Date, 'now').mockImplementation(() => past); | ||
|
||
expect(test.count(1)).toEqual(2); | ||
}); | ||
}); |
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,34 @@ | ||
import { generateFunctionDecorator, isUndefined } from '../../helpers/general'; | ||
|
||
interface ITimedMemoizeProps { | ||
hashFn?: (...args: any[]) => string, | ||
invalidateOnTimeout?: boolean | ||
} | ||
|
||
export function TimedMemoize(timeoutMs: number, props: ITimedMemoizeProps = {}) { | ||
return generateFunctionDecorator('TimedMemoize', decorator, timeoutMs, props); | ||
} | ||
|
||
function decorator<T>(fn: (...args: any[]) => any, timeoutMs: number, { hashFn, invalidateOnTimeout }: ITimedMemoizeProps) { | ||
const cache: { [key: string]: { value: string, timeoutTimestamp: number } } = {}; | ||
|
||
return function(...args: any[]) { | ||
const now = Date.now(); | ||
const key = hashFn ? hashFn.apply(this, args) : JSON.stringify(args); | ||
|
||
if (!isUndefined(cache[key]) && cache[key].timeoutTimestamp > now) { | ||
return cache[key].value; | ||
} | ||
|
||
cache[key] = { | ||
value: fn.apply(this, args), | ||
timeoutTimestamp: now + timeoutMs | ||
}; | ||
|
||
if (invalidateOnTimeout) { | ||
setTimeout(() => delete cache[key], timeoutMs); | ||
} | ||
|
||
return cache[key].value; | ||
}; | ||
} |