Skip to content

Commit 5af3050

Browse files
authored
Merge pull request #651 from aryaemami59/typed-result-equality-check
2 parents 6a03653 + d7632a6 commit 5af3050

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

src/defaultMemoize.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export function createCacheKeyComparator(equalityCheck: EqualityFn) {
126126
/**
127127
* @public
128128
*/
129-
export interface DefaultMemoizeOptions {
129+
export interface DefaultMemoizeOptions<T = any> {
130130
/**
131131
* Used to compare the individual arguments of the provided calculation function.
132132
*
@@ -142,7 +142,7 @@ export interface DefaultMemoizeOptions {
142142
* use case, where an update to another field in the original data causes a recalculation
143143
* due to changed references, but the output is still effectively the same.
144144
*/
145-
resultEqualityCheck?: EqualityFn
145+
resultEqualityCheck?: EqualityFn<T>
146146
/**
147147
* The cache size for the selector. If greater than 1, the selector will use an LRU cache internally.
148148
*
@@ -167,7 +167,7 @@ export interface DefaultMemoizeOptions {
167167
*/
168168
export function defaultMemoize<Func extends AnyFunction>(
169169
func: Func,
170-
equalityCheckOrOptions?: EqualityFn | DefaultMemoizeOptions
170+
equalityCheckOrOptions?: EqualityFn | DefaultMemoizeOptions<ReturnType<Func>>
171171
) {
172172
const providedOptions =
173173
typeof equalityCheckOrOptions === 'object'
@@ -191,20 +191,20 @@ export function defaultMemoize<Func extends AnyFunction>(
191191

192192
// we reference arguments instead of spreading them for performance reasons
193193
function memoized() {
194-
let value = cache.get(arguments)
194+
let value = cache.get(arguments) as ReturnType<Func>
195195
if (value === NOT_FOUND) {
196196
// @ts-ignore
197-
value = func.apply(null, arguments)
197+
value = func.apply(null, arguments) as ReturnType<Func>
198198
resultsCount++
199199

200200
if (resultEqualityCheck) {
201201
const entries = cache.getEntries()
202202
const matchingEntry = entries.find(entry =>
203-
resultEqualityCheck(entry.value, value)
203+
resultEqualityCheck(entry.value as ReturnType<Func>, value)
204204
)
205205

206206
if (matchingEntry) {
207-
value = matchingEntry.value
207+
value = matchingEntry.value as ReturnType<Func>
208208
resultsCount--
209209
}
210210
}

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ export type Combiner<InputSelectors extends SelectorArray, Result> = Distribute<
295295
*
296296
* @public
297297
*/
298-
export type EqualityFn = (a: any, b: any) => boolean
298+
export type EqualityFn<T = any> = (a: T, b: T) => boolean
299299

300300
/**
301301
* The frequency of input stability checks.

src/weakMapMemoize.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function createCacheNode<T>(): CacheNode<T> {
7272
/**
7373
* @public
7474
*/
75-
export interface WeakMapMemoizeOptions {
75+
export interface WeakMapMemoizeOptions<T = any> {
7676
/**
7777
* If provided, used to compare a newly generated output value against previous values in the cache.
7878
* If a match is found, the old value is returned. This addresses the common
@@ -82,7 +82,7 @@ export interface WeakMapMemoizeOptions {
8282
* use case, where an update to another field in the original data causes a recalculation
8383
* due to changed references, but the output is still effectively the same.
8484
*/
85-
resultEqualityCheck?: EqualityFn
85+
resultEqualityCheck?: EqualityFn<T>
8686
}
8787

8888
/**
@@ -160,7 +160,7 @@ export interface WeakMapMemoizeOptions {
160160
*/
161161
export function weakMapMemoize<Func extends AnyFunction>(
162162
func: Func,
163-
options: WeakMapMemoizeOptions = {}
163+
options: WeakMapMemoizeOptions<ReturnType<Func>> = {}
164164
) {
165165
let fnNode = createCacheNode()
166166
const { resultEqualityCheck } = options
@@ -222,7 +222,10 @@ export function weakMapMemoize<Func extends AnyFunction>(
222222

223223
if (resultEqualityCheck) {
224224
const lastResultValue = lastResult?.deref() ?? lastResult
225-
if (lastResultValue != null && resultEqualityCheck(lastResultValue, result)) {
225+
if (
226+
lastResultValue != null &&
227+
resultEqualityCheck(lastResultValue as ReturnType<Func>, result)
228+
) {
226229
result = lastResultValue
227230
resultsCount !== 0 && resultsCount--
228231
}

0 commit comments

Comments
 (0)