Skip to content

Commit 5445226

Browse files
authored
remove default ttl from memo (#195)
* remove default ttl from memo
1 parent 33739ae commit 5445226

File tree

6 files changed

+49
-28
lines changed

6 files changed

+49
-28
lines changed

Diff for: cdn/radash.esm.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -473,23 +473,27 @@ const memoize = (cache, func, keyFunc, ttl) => {
473473
const key = keyFunc ? keyFunc(...args) : JSON.stringify({ args });
474474
const existing = cache[key];
475475
if (existing !== void 0) {
476+
if (!existing.exp)
477+
return existing.value;
476478
if (existing.exp > new Date().getTime()) {
477479
return existing.value;
478480
}
479481
}
480482
const result = func(...args);
481483
cache[key] = {
482-
exp: new Date().getTime() + ttl,
484+
exp: ttl ? new Date().getTime() + ttl : null,
483485
value: result
484486
};
485487
return result;
486488
};
487489
};
488-
const memo = (func, {
489-
key = null,
490-
ttl = 300
491-
} = {}) => {
492-
return memoize({}, func, key, ttl);
490+
const memo = (func, options = {}) => {
491+
return memoize(
492+
{},
493+
func,
494+
options.key ?? null,
495+
options.ttl ?? null
496+
);
493497
};
494498
const debounce = ({ delay }, func) => {
495499
let timer = void 0;

Diff for: cdn/radash.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -476,23 +476,27 @@ var radash = (function (exports) {
476476
const key = keyFunc ? keyFunc(...args) : JSON.stringify({ args });
477477
const existing = cache[key];
478478
if (existing !== void 0) {
479+
if (!existing.exp)
480+
return existing.value;
479481
if (existing.exp > new Date().getTime()) {
480482
return existing.value;
481483
}
482484
}
483485
const result = func(...args);
484486
cache[key] = {
485-
exp: new Date().getTime() + ttl,
487+
exp: ttl ? new Date().getTime() + ttl : null,
486488
value: result
487489
};
488490
return result;
489491
};
490492
};
491-
const memo = (func, {
492-
key = null,
493-
ttl = 300
494-
} = {}) => {
495-
return memoize({}, func, key, ttl);
493+
const memo = (func, options = {}) => {
494+
return memoize(
495+
{},
496+
func,
497+
options.key ?? null,
498+
options.ttl ?? null
499+
);
496500
};
497501
const debounce = ({ delay }, func) => {
498502
let timer = void 0;

Diff for: cdn/radash.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "radash",
3-
"version": "10.1.0",
3+
"version": "10.2.0",
44
"description": "Functional utility library - modern, simple, typed, powerful",
55
"main": "dist/cjs/index.cjs",
66
"module": "dist/esm/index.mjs",
@@ -48,4 +48,4 @@
4848
"engines": {
4949
"node": ">=14.18.0"
5050
}
51-
}
51+
}

Diff for: src/curry.ts

+17-13
Original file line numberDiff line numberDiff line change
@@ -47,48 +47,52 @@ export const proxied = <T, K>(
4747
)
4848
}
4949

50-
type Cache<T> = Record<string, { exp: number; value: T }>
50+
type Cache<T> = Record<string, { exp: number | null; value: T }>
5151

5252
const memoize = <T>(
5353
cache: Cache<T>,
5454
func: Func<any, T>,
5555
keyFunc: Func<string> | null,
56-
ttl: number
56+
ttl: number | null
5757
) => {
5858
return function callWithMemo(...args: any): T {
5959
const key = keyFunc ? keyFunc(...args) : JSON.stringify({ args })
6060
const existing = cache[key]
6161
if (existing !== undefined) {
62+
if (!existing.exp) return existing.value
6263
if (existing.exp > new Date().getTime()) {
6364
return existing.value
6465
}
6566
}
6667
const result = func(...args)
6768
cache[key] = {
68-
exp: new Date().getTime() + ttl,
69+
exp: ttl ? new Date().getTime() + ttl : null,
6970
value: result
7071
}
7172
return result
7273
}
7374
}
7475

7576
/**
76-
* Creates a memoized function using the key and ttl
77-
* options. The returned function will only execute
78-
* the source function when no value has previously
79-
* been computed or the previous value has expired
77+
* Creates a memoized function. The returned function
78+
* will only execute the source function when no value
79+
* has previously been computed. If a ttl (milliseconds)
80+
* is given previously computed values will be checked
81+
* for expiration before being returned.
8082
*/
8183
export const memo = <TFunc extends Function>(
8284
func: TFunc,
83-
{
84-
key = null,
85-
ttl = 300
86-
}: {
87-
key?: Func<any, string> | null
85+
options: {
86+
key?: Func<any, string>
8887
ttl?: number
8988
} = {}
9089
) => {
91-
return memoize({}, func as any, key, ttl) as any as TFunc
90+
return memoize(
91+
{},
92+
func as any,
93+
options.key ?? null,
94+
options.ttl ?? null
95+
) as any as TFunc
9296
}
9397

9498
export type DebounceFunction<TArgs extends any[]> = {

Diff for: src/tests/curry.test.ts

+9
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,15 @@ describe('curry module', () => {
145145
const resultB = func()
146146
assert.notEqual(resultA, resultB)
147147
})
148+
test('does not call function again when first value has not expired', async () => {
149+
const func = _.memo(() => new Date().getTime(), {
150+
ttl: 1000
151+
})
152+
const resultA = func()
153+
await new Promise(res => setTimeout(res, 100))
154+
const resultB = func()
155+
assert.equal(resultA, resultB)
156+
})
148157
})
149158

150159
describe('debounce function', () => {

0 commit comments

Comments
 (0)