-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[lib] Introduce useDerivedObject for caching whole object when no val…
…ues change Summary: We only need a subset of a `KeyserverInfoPartial` for constructing a keyserver call. Let's call that subset `KeyserverCallInfo`. We can introduce a selector that will cache a `KeyserverCallInfo` for a given `KeyserverInfoPartial`. But we still have a problem, because we still need a way to make sure the whole object of `KeyserverCallInfo`s doesn't change if a single `KeyserverCallInfo` changes. This `useDerivedObject` does that. However, we still have an issue owing to the fact that the `useDerivedObject` will cache on a per-hook-invocation level, whereas `bindCallKeyserverEndpointSelector` is caching globally. The following diff will resolve that. Depends on D10464 Test Plan: Before this stack, I was able to reproduce [ENG-3612](https://linear.app/comm/issue/ENG-3612/[native]-getting-huge-number-of-unhandled-promise-rejection-when) by going to the `ThreadSettings` screen in `native` while my local `keyserver` was down. After this stack, the issue no longer repros. I also compiled a release build of the iOS app to my phone to confirm that there were no regressions in TTI, or the time it takes to open a `MessageList` and go back to the `ChatThreadList`. Reviewers: inka, rohan Reviewed By: inka Subscribers: tomek Differential Revision: https://phab.comm.dev/D10465
- Loading branch information
Showing
2 changed files
with
137 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// @flow | ||
|
||
import * as React from 'react'; | ||
|
||
type CacheEntry<I, O> = { | ||
+inVal: I, | ||
+outVal: O, | ||
+derivationSelector: I => O, | ||
}; | ||
|
||
function useDerivedObject<I, O>( | ||
object: { +[string]: I }, | ||
createDerivationSelector: () => I => O, | ||
): { +[string]: O } { | ||
const cacheRef = React.useRef<Map<string, CacheEntry<I, O>>>(new Map()); | ||
const prevCreateDerivationSelector = React.useRef<() => I => O>( | ||
createDerivationSelector, | ||
); | ||
const prevResultRef = React.useRef<?{ +[string]: O }>(); | ||
|
||
return React.useMemo(() => { | ||
if (prevCreateDerivationSelector.current !== createDerivationSelector) { | ||
cacheRef.current = new Map(); | ||
prevCreateDerivationSelector.current = createDerivationSelector; | ||
} | ||
|
||
const cache = cacheRef.current; | ||
|
||
const newCache = new Map<string, CacheEntry<I, O>>(); | ||
let changeOccurred = Object.keys(object).length !== cache.size; | ||
|
||
const result: { [string]: O } = {}; | ||
for (const key in object) { | ||
const inVal = object[key]; | ||
|
||
const cacheEntry = cache.get(key); | ||
if (!cacheEntry) { | ||
changeOccurred = true; | ||
const derivationSelector = createDerivationSelector(); | ||
const outVal = derivationSelector(inVal); | ||
newCache.set(key, { inVal, outVal, derivationSelector }); | ||
result[key] = outVal; | ||
continue; | ||
} | ||
|
||
if (inVal === cacheEntry.inVal) { | ||
newCache.set(key, cacheEntry); | ||
result[key] = cacheEntry.outVal; | ||
continue; | ||
} | ||
|
||
const { derivationSelector } = cacheEntry; | ||
const outVal = derivationSelector(inVal); | ||
if (outVal !== cacheEntry.outVal) { | ||
changeOccurred = true; | ||
} | ||
newCache.set(key, { inVal, outVal, derivationSelector }); | ||
result[key] = outVal; | ||
} | ||
cacheRef.current = newCache; | ||
|
||
if (!changeOccurred && prevResultRef.current) { | ||
return prevResultRef.current; | ||
} | ||
prevResultRef.current = result; | ||
return result; | ||
}, [object, createDerivationSelector]); | ||
} | ||
|
||
export { useDerivedObject }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters