Skip to content

Commit

Permalink
remove default ttl from memo (#195)
Browse files Browse the repository at this point in the history
* remove default ttl from memo
  • Loading branch information
sodiray authored Dec 16, 2022
1 parent 33739ae commit 5445226
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 28 deletions.
16 changes: 10 additions & 6 deletions cdn/radash.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,23 +473,27 @@ const memoize = (cache, func, keyFunc, ttl) => {
const key = keyFunc ? keyFunc(...args) : JSON.stringify({ args });
const existing = cache[key];
if (existing !== void 0) {
if (!existing.exp)
return existing.value;
if (existing.exp > new Date().getTime()) {
return existing.value;
}
}
const result = func(...args);
cache[key] = {
exp: new Date().getTime() + ttl,
exp: ttl ? new Date().getTime() + ttl : null,
value: result
};
return result;
};
};
const memo = (func, {
key = null,
ttl = 300
} = {}) => {
return memoize({}, func, key, ttl);
const memo = (func, options = {}) => {
return memoize(
{},
func,
options.key ?? null,
options.ttl ?? null
);
};
const debounce = ({ delay }, func) => {
let timer = void 0;
Expand Down
16 changes: 10 additions & 6 deletions cdn/radash.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,23 +476,27 @@ var radash = (function (exports) {
const key = keyFunc ? keyFunc(...args) : JSON.stringify({ args });
const existing = cache[key];
if (existing !== void 0) {
if (!existing.exp)
return existing.value;
if (existing.exp > new Date().getTime()) {
return existing.value;
}
}
const result = func(...args);
cache[key] = {
exp: new Date().getTime() + ttl,
exp: ttl ? new Date().getTime() + ttl : null,
value: result
};
return result;
};
};
const memo = (func, {
key = null,
ttl = 300
} = {}) => {
return memoize({}, func, key, ttl);
const memo = (func, options = {}) => {
return memoize(
{},
func,
options.key ?? null,
options.ttl ?? null
);
};
const debounce = ({ delay }, func) => {
let timer = void 0;
Expand Down
2 changes: 1 addition & 1 deletion cdn/radash.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "radash",
"version": "10.1.0",
"version": "10.2.0",
"description": "Functional utility library - modern, simple, typed, powerful",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand Down Expand Up @@ -48,4 +48,4 @@
"engines": {
"node": ">=14.18.0"
}
}
}
30 changes: 17 additions & 13 deletions src/curry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,48 +47,52 @@ export const proxied = <T, K>(
)
}

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

const memoize = <T>(
cache: Cache<T>,
func: Func<any, T>,
keyFunc: Func<string> | null,
ttl: number
ttl: number | null
) => {
return function callWithMemo(...args: any): T {
const key = keyFunc ? keyFunc(...args) : JSON.stringify({ args })
const existing = cache[key]
if (existing !== undefined) {
if (!existing.exp) return existing.value
if (existing.exp > new Date().getTime()) {
return existing.value
}
}
const result = func(...args)
cache[key] = {
exp: new Date().getTime() + ttl,
exp: ttl ? new Date().getTime() + ttl : null,
value: result
}
return result
}
}

/**
* Creates a memoized function using the key and ttl
* options. The returned function will only execute
* the source function when no value has previously
* been computed or the previous value has expired
* Creates a memoized function. The returned function
* will only execute the source function when no value
* has previously been computed. If a ttl (milliseconds)
* is given previously computed values will be checked
* for expiration before being returned.
*/
export const memo = <TFunc extends Function>(
func: TFunc,
{
key = null,
ttl = 300
}: {
key?: Func<any, string> | null
options: {
key?: Func<any, string>
ttl?: number
} = {}
) => {
return memoize({}, func as any, key, ttl) as any as TFunc
return memoize(
{},
func as any,
options.key ?? null,
options.ttl ?? null
) as any as TFunc
}

export type DebounceFunction<TArgs extends any[]> = {
Expand Down
9 changes: 9 additions & 0 deletions src/tests/curry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ describe('curry module', () => {
const resultB = func()
assert.notEqual(resultA, resultB)
})
test('does not call function again when first value has not expired', async () => {
const func = _.memo(() => new Date().getTime(), {
ttl: 1000
})
const resultA = func()
await new Promise(res => setTimeout(res, 100))
const resultB = func()
assert.equal(resultA, resultB)
})
})

describe('debounce function', () => {
Expand Down

0 comments on commit 5445226

Please sign in to comment.