Cache some volatile conversation properties

This commit is contained in:
Fedor Indutny 2022-12-22 16:13:23 -08:00 committed by GitHub
parent ba79595563
commit f92f81dfd6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 157 additions and 92 deletions

20
ts/util/memoizeByThis.ts Normal file
View file

@ -0,0 +1,20 @@
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { isEqual } from 'lodash';
export function memoizeByThis<Owner extends Record<string, unknown>, Result>(
fn: () => Result
): () => Result {
const lastValueMap = new WeakMap<Owner, Result>();
return function memoizedFn(this: Owner): Result {
const lastValue = lastValueMap.get(this);
const newValue = fn();
if (lastValue !== undefined && isEqual(lastValue, newValue)) {
return lastValue;
}
lastValueMap.set(this, newValue);
return newValue;
};
}