Skip to content

Commit cea19b9

Browse files
committed
Add weakMapMemoize maybeDeref
1 parent 92af1a0 commit cea19b9

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/weakMapMemoize.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,20 @@ export interface WeakMapMemoizeOptions<Result = any> {
9696
resultEqualityCheck?: EqualityFn<Result>
9797
}
9898

99+
/**
100+
* Derefences the argument if it is a Ref. Else if it is a value already, return it.
101+
*
102+
* @param r - the object to maybe deref
103+
* @returns The derefenced value if the argument is a Ref, else the argument value itself.
104+
*/
105+
function maybeDeref(r: any) {
106+
if (r instanceof Ref) {
107+
return r.deref()
108+
}
109+
110+
return r
111+
}
112+
99113
/**
100114
* Creates a tree of `WeakMap`-based cache nodes based on the identity of the
101115
* arguments it's been called with (in this case, the extracted values from your input selectors).
@@ -229,7 +243,8 @@ export function weakMapMemoize<Func extends AnyFunction>(
229243
resultsCount++
230244

231245
if (resultEqualityCheck) {
232-
const lastResultValue = lastResult?.deref?.() ?? lastResult
246+
// Deref lastResult if it is a Ref
247+
const lastResultValue = maybeDeref(lastResult)
233248

234249
if (
235250
lastResultValue != null &&

0 commit comments

Comments
 (0)