mirror of
https://github.com/block/buzz.git
synced 2026-10-30 14:01:37 +00:00
Signed-off-by: Wes <wesbillman@users.noreply.github.com> Co-authored-by: Brain <21994759fc7a6fa6b965551d35cfd7897d262f2495467f2d78694ddcfa6a5c7e@sprout-oss.stage.blox.sqprod.co>
55 lines
1.6 KiB
TypeScript
55 lines
1.6 KiB
TypeScript
import * as React from "react";
|
|
|
|
/**
|
|
* Returns `next` but preserves the previous reference whenever the two Maps
|
|
* have identical entries (same size, same key→value pairs). Lets a value that
|
|
* is recomputed into a fresh Map on an unrelated invalidation (e.g. a version
|
|
* bump) keep a stable identity so downstream `React.memo` boundaries can bail.
|
|
*/
|
|
export function useStableMap<K, V>(next: Map<K, V>): Map<K, V> {
|
|
const ref = React.useRef(next);
|
|
const prev = ref.current;
|
|
if (prev !== next && mapsEqual(prev, next)) {
|
|
return prev;
|
|
}
|
|
ref.current = next;
|
|
return next;
|
|
}
|
|
|
|
function mapsEqual<K, V>(a: Map<K, V>, b: Map<K, V>): boolean {
|
|
if (a.size !== b.size) return false;
|
|
for (const [key, value] of a) {
|
|
if (!b.has(key) || !Object.is(b.get(key), value)) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Returns `next` but preserves the previous reference when the two arrays are
|
|
* shallow-equal (same length, `Object.is` on each element). Same purpose as
|
|
* `useStableMap` for array-valued derived state. Preserves the caller's exact
|
|
* array type (mutable or readonly).
|
|
*/
|
|
export function useStableArrayShallow<T extends readonly unknown[]>(
|
|
next: T,
|
|
): T {
|
|
const ref = React.useRef(next);
|
|
const prev = ref.current;
|
|
if (prev !== next && arraysShallowEqual(prev, next)) {
|
|
return prev;
|
|
}
|
|
ref.current = next;
|
|
return next;
|
|
}
|
|
|
|
function arraysShallowEqual(
|
|
a: readonly unknown[],
|
|
b: readonly unknown[],
|
|
): boolean {
|
|
if (a.length !== b.length) return false;
|
|
for (let i = 0; i < a.length; i += 1) {
|
|
if (!Object.is(a[i], b[i])) return false;
|
|
}
|
|
return true;
|
|
}
|