Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions src/core/createRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { type ApplicationOptions } from 'pixi.js';
import { type ReactNode } from 'react';
import { ConcurrentRoot } from 'react-reconciler/constants.js';
import { ContextProvider } from '../components/Context';
import { isReadOnlyProperty } from '../helpers/isReadOnlyProperty';
import { safeAssign } from '../helpers/isReadOnlyProperty';
import { log } from '../helpers/log';
import { prepareInstance } from '../helpers/prepareInstance';
import { type ApplicationState } from '../typedefs/ApplicationState';
Expand Down Expand Up @@ -92,18 +92,7 @@ export function createRoot(

Object.entries(applicationOptions).forEach(([key, value]) =>
{
const typedKey = key as keyof ApplicationOptions;

if (isReadOnlyProperty(
applicationOptions as unknown as Record<string, unknown>,
typedKey,
))
{
return;
}

// @ts-expect-error Typescript doesn't realise it, but we're already verifying that this isn't a readonly key.
applicationState.app[typedKey] = value;
safeAssign(applicationState.app, key as keyof typeof applicationState.app, value);
});

// Update fiber and expose Pixi.js state to children
Expand Down
7 changes: 3 additions & 4 deletions src/helpers/applyProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
} from './compare';
import { diffProps } from './diffProps';
import { isDiffSet } from './isDiffSet';
import { isReadOnlyProperty } from './isReadOnlyProperty';
import { safeAssign } from './isReadOnlyProperty';
import { log } from './log';

const DEFAULT = '__default';
Expand Down Expand Up @@ -174,10 +174,9 @@ export function applyProps(
delete currentInstance[pixiKey];
}
}
else if (!isReadOnlyProperty(currentInstance as Record<string, unknown>, key))
else
{
// @ts-expect-error Typescript is grumpy because this could be setting a readonly key, but we're already handling that in the conditional above. 🤷🏻‍♂️
currentInstance[key] = value;
safeAssign(currentInstance, key, value as typeof currentInstance[keyof typeof currentInstance]);
}
}

Expand Down
17 changes: 13 additions & 4 deletions src/helpers/isReadOnlyProperty.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
export function isReadOnlyProperty(
objectInstance: Record<string, unknown>,
propertyKey: string,
)
export function isReadOnlyProperty<T>(
objectInstance: T,
propertyKey: keyof T,
): boolean
{
const prototype = Object.getPrototypeOf(objectInstance);
const propertyDescriptor = Object.getOwnPropertyDescriptor(prototype, propertyKey);

return !(typeof propertyDescriptor === 'undefined' || propertyDescriptor.writable || propertyDescriptor.set);
}

/** Attempts to assign the `value` to the `key` within `obj`, but only when `key` is *not* a readonly prop */
export function safeAssign<T>(obj: T, key: keyof T, value: T[keyof T])
{
if (!isReadOnlyProperty(obj, key))
{
obj[key] = value;
}
}
Loading