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';
|
import type { TeamViewSnapshot } from '@shared/types';
|
||||||
|
|
||||||
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
function arePlainObjectPair(previous: object, next: object): boolean {
|
||||||
if (value == null || typeof value !== 'object') {
|
const previousPrototype = Object.getPrototypeOf(previous);
|
||||||
|
if (previousPrototype !== Object.prototype && previousPrototype !== null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const prototype = Object.getPrototypeOf(value);
|
const nextPrototype = Object.getPrototypeOf(next);
|
||||||
return prototype === Object.prototype || prototype === null;
|
return nextPrototype === Object.prototype || nextPrototype === null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function structurallySharePlainValue<T>(previous: T, next: T): T {
|
export function structurallySharePlainValue<T>(previous: T, next: T): T {
|
||||||
|
|
@ -13,6 +14,15 @@ export function structurallySharePlainValue<T>(previous: T, next: T): T {
|
||||||
return previous;
|
return previous;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
previous == null ||
|
||||||
|
next == null ||
|
||||||
|
typeof previous !== 'object' ||
|
||||||
|
typeof next !== 'object'
|
||||||
|
) {
|
||||||
|
return next;
|
||||||
|
}
|
||||||
|
|
||||||
if (Array.isArray(previous) && Array.isArray(next)) {
|
if (Array.isArray(previous) && Array.isArray(next)) {
|
||||||
const hasLengthChange = previous.length !== next.length;
|
const hasLengthChange = previous.length !== next.length;
|
||||||
let result: unknown[] | null = hasLengthChange ? new Array(next.length) : null;
|
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;
|
return result ? (result as T) : previous;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isPlainObject(previous) && isPlainObject(next)) {
|
if (arePlainObjectPair(previous, next)) {
|
||||||
const previousRecord = previous as Record<string, unknown>;
|
const previousRecord = previous as Record<string, unknown>;
|
||||||
const nextRecord = next as Record<string, unknown>;
|
const nextRecord = next as Record<string, unknown>;
|
||||||
const previousKeys = Object.keys(previousRecord);
|
const previousKeys = Object.keys(previousRecord);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue