Skip to content

Commit 4228bc1

Browse files
committed
Fix resultEqualityCheck behavior in weakMapMemoize
- Previously `resultEqualityCheck` would be called with empty objects as arguments due to how `inputStabilityCheck` works. This update Fixes `resultEqualityCheck` in `weakMapMemoize` so that when input selectors are stable it does not get called on the first run of the output selector.
1 parent 3fb8c98 commit 4228bc1

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

src/weakMapMemoize.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -227,25 +227,29 @@ export function weakMapMemoize<Func extends AnyFunction>(
227227
// Allow errors to propagate
228228
result = func.apply(null, arguments as unknown as any[])
229229
resultsCount++
230-
}
231230

232-
terminatedNode.s = TERMINATED
231+
if (resultEqualityCheck) {
232+
const lastResultValue = lastResult?.deref?.() ?? lastResult
233233

234-
if (resultEqualityCheck) {
235-
const lastResultValue = lastResult?.deref?.() ?? lastResult
236-
if (
237-
lastResultValue != null &&
238-
resultEqualityCheck(lastResultValue as ReturnType<Func>, result)
239-
) {
240-
result = lastResultValue
241-
resultsCount !== 0 && resultsCount--
242-
}
234+
if (
235+
lastResultValue != null &&
236+
resultEqualityCheck(lastResultValue as ReturnType<Func>, result)
237+
) {
238+
result = lastResultValue
239+
240+
resultsCount !== 0 && resultsCount--
241+
}
242+
243+
const needsWeakRef =
244+
(typeof result === 'object' && result !== null) ||
245+
typeof result === 'function'
243246

244-
const needsWeakRef =
245-
(typeof result === 'object' && result !== null) ||
246-
typeof result === 'function'
247-
lastResult = needsWeakRef ? new Ref(result) : result
247+
lastResult = needsWeakRef ? new Ref(result) : result
248+
}
248249
}
250+
251+
terminatedNode.s = TERMINATED
252+
249253
terminatedNode.v = result
250254
return result
251255
}

0 commit comments

Comments
 (0)