perf(renderer): trim snapshot sharing type checks
This commit is contained in:
parent
dbc5b5bd32
commit
bfabc57652
1 changed files with 15 additions and 5 deletions
|
|
@ -1,11 +1,12 @@
|
|||
import type { TeamViewSnapshot } from '@shared/types';
|
||||
|
||||
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
||||
if (value == null || typeof value !== 'object') {
|
||||
function arePlainObjectPair(previous: object, next: object): boolean {
|
||||
const previousPrototype = Object.getPrototypeOf(previous);
|
||||
if (previousPrototype !== Object.prototype && previousPrototype !== null) {
|
||||
return false;
|
||||
}
|
||||
const prototype = Object.getPrototypeOf(value);
|
||||
return prototype === Object.prototype || prototype === null;
|
||||
const nextPrototype = Object.getPrototypeOf(next);
|
||||
return nextPrototype === Object.prototype || nextPrototype === null;
|
||||
}
|
||||
|
||||
export function structurallySharePlainValue<T>(previous: T, next: T): T {
|
||||
|
|
@ -13,6 +14,15 @@ export function structurallySharePlainValue<T>(previous: T, next: T): T {
|
|||
return previous;
|
||||
}
|
||||
|
||||
if (
|
||||
previous == null ||
|
||||
next == null ||
|
||||
typeof previous !== 'object' ||
|
||||
typeof next !== 'object'
|
||||
) {
|
||||
return next;
|
||||
}
|
||||
|
||||
if (Array.isArray(previous) && Array.isArray(next)) {
|
||||
const hasLengthChange = previous.length !== next.length;
|
||||
let result: unknown[] | null = hasLengthChange ? new Array(next.length) : null;
|
||||
|
|
@ -35,7 +45,7 @@ export function structurallySharePlainValue<T>(previous: T, next: T): T {
|
|||
return result ? (result as T) : previous;
|
||||
}
|
||||
|
||||
if (isPlainObject(previous) && isPlainObject(next)) {
|
||||
if (arePlainObjectPair(previous, next)) {
|
||||
const previousRecord = previous as Record<string, unknown>;
|
||||
const nextRecord = next as Record<string, unknown>;
|
||||
const previousKeys = Object.keys(previousRecord);
|
||||
|
|
|
|||
Loading…
Reference in a new issue