Skip to content

Commit

Permalink
refactor: merge internal and external node types
Browse files Browse the repository at this point in the history
  • Loading branch information
trezy committed Jul 25, 2024
1 parent 83a6dc5 commit 9cf1fdb
Show file tree
Hide file tree
Showing 48 changed files with 219 additions and 270 deletions.
6 changes: 2 additions & 4 deletions src/components/Application.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
type Application as PixiApplication,
TextStyle,
} from 'pixi.js';
import { TextStyle } from 'pixi.js';
import {
createElement,
forwardRef,
Expand All @@ -11,6 +8,7 @@ import {
import { createRoot } from '../core/createRoot.ts';
import { useIsomorphicLayoutEffect } from '../hooks/useIsomorphicLayoutEffect.ts';

import type { Application as PixiApplication } from 'pixi.js';
import type {
ForwardRefRenderFunction,
MutableRefObject,
Expand Down
9 changes: 6 additions & 3 deletions src/core/createRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { roots } from './roots.ts';

import type { ApplicationOptions } from 'pixi.js';
import type { ReactNode } from 'react';
import type { Instance } from '../typedefs/Instance.ts';
import type { HostConfig } from '../typedefs/HostConfig.ts';
import type { InternalState } from '../typedefs/InternalState.ts';

/** Creates a new root for a Pixi React app. */
Expand All @@ -36,7 +36,7 @@ export function createRoot(
}

const fiber = root?.fiber ?? reconciler.createContainer(
state.rootContainer as Instance,
state.rootContainer as HostConfig['containerInstance'],
ConcurrentRoot,
null,
false,
Expand Down Expand Up @@ -82,7 +82,10 @@ export function createRoot(
{
const typedKey = /** @type {keyof ApplicationOptions} */ (key);

if (isReadOnlyProperty(applicationOptions, typedKey))
if (isReadOnlyProperty(
applicationOptions as unknown as Record<string, unknown>,
typedKey,
))
{
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/reconciler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import type { HostConfig } from '../typedefs/HostConfig.ts';
const reconcilerConfig: Reconciler.HostConfig<
HostConfig['type'],
HostConfig['props'],
HostConfig['container'],
HostConfig['containerInstance'],
HostConfig['instance'],
HostConfig['textInstance'],
HostConfig['suspenseInstance'],
Expand Down
2 changes: 0 additions & 2 deletions src/global.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { NamespacedPixiElements } from './typedefs/NamespacedPixiElements.ts';
import type { PixiElements } from './typedefs/PixiElements.ts';

export type { PixiReactNode } from './typedefs/PixiReactNode.ts';

declare global
{
// eslint-disable-next-line @typescript-eslint/no-namespace
Expand Down
18 changes: 10 additions & 8 deletions src/helpers/appendChild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,27 @@ import {
import { attach } from './attach.ts';
import { log } from './log.ts';

import type { ContainerElement } from '../typedefs/ContainerElement.ts';
import type { Instance } from '../typedefs/Instance.ts';
import type { HostConfig } from '../typedefs/HostConfig.ts';

/** Adds elements to our application. */
export function appendChild(parentInstance: Instance<ContainerElement>, childInstance: Instance | null)
export function appendChild(
parentNode: HostConfig['containerInstance'],
childNode: HostConfig['instance'] | null,
)
{
log('info', 'lifecycle::appendChild');

if (!childInstance)
if (!childNode)
{
return;
}

if (childInstance instanceof Container)
if (childNode instanceof Container)
{
parentInstance.addChild(childInstance);
parentNode.addChild(childNode);
}
else if (childInstance instanceof Filter)
else if (childNode instanceof Filter)
{
attach(parentInstance, childInstance);
attach(parentNode, childNode);
}
}
48 changes: 28 additions & 20 deletions src/helpers/applyProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,34 @@ import type {
FederatedWheelEvent,
} from 'pixi.js';
import type { DiffSet } from '../typedefs/DiffSet.ts';
import type { Instance } from '../typedefs/Instance.ts';
import type { InstanceProps } from '../typedefs/InstanceProps.ts';
import type { MaybeInstance } from '../typedefs/MaybeInstance.ts';
import type { HostConfig } from '../typedefs/HostConfig.ts';
import type { MaybePixiReactNode } from '../typedefs/MaybePixiReactNode.ts';
import type { NodeState } from '../typedefs/NodeState.ts';

const DEFAULT = '__default';
const DEFAULTS_CONTAINERS = new Map();

const PIXI_EVENT_PROP_NAME_ERROR_HAS_BEEN_SHOWN: Record<string, boolean> = {};

/**
* Apply properties to Pixi.js instance.
*
* @param {MaybeInstance} instance An instance?
* @param {InstanceProps | DiffSet} data New props.
*/
export function applyProps(instance: MaybeInstance, data: InstanceProps | DiffSet)
function targetKeyReducer(accumulator: MaybePixiReactNode, key: string)
{
if (key in accumulator)
{
return accumulator[key as keyof MaybePixiReactNode];
}

return accumulator;
}

/** Apply properties to Pixi.js instance. */
export function applyProps(
instance: MaybePixiReactNode,
data: HostConfig['props'] | DiffSet,
)
{
const {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
__pixireact: instanceState,
__pixireact: instanceState = {} as NodeState,
...instanceProps
} = instance;

Expand All @@ -47,7 +55,7 @@ export function applyProps(instance: MaybeInstance, data: InstanceProps | DiffSe
}
else
{
typedData = diffProps(data, instanceProps as InstanceProps);
typedData = diffProps(data, instanceProps as HostConfig['props']);
}

const { changes } = typedData;
Expand All @@ -58,11 +66,11 @@ export function applyProps(instance: MaybeInstance, data: InstanceProps | DiffSe
{
const change = changes[changeIndex];
let hasError = false;
let key = change[0] as keyof Instance;
let key = change[0] as keyof HostConfig['instance'];
let value = change[1];
const isEvent = change[2];

const keys = change[3] as (keyof Instance)[];
const keys = change[3];

let currentInstance = instance;
let targetProp = currentInstance[key];
Expand All @@ -76,7 +84,7 @@ export function applyProps(instance: MaybeInstance, data: InstanceProps | DiffSe
else
{
hasError = true;
log('warn', `The \`draw\` prop was used on a \`${instance.type}\` component, but it's only valid on \`graphics\` components.`);
log('warn', `The \`draw\` prop was used on a \`${instanceState.type}\` component, but it's only valid on \`graphics\` components.`);
}
}

Expand All @@ -99,17 +107,16 @@ export function applyProps(instance: MaybeInstance, data: InstanceProps | DiffSe
// Resolve dashed props
if (keys.length)
{
targetProp = keys.reduce((accumulator, key) => accumulator[key], currentInstance);
targetProp = keys.reduce(targetKeyReducer, currentInstance);

// If the target is atomic, it forces us to switch the root
if (!(targetProp && targetProp.set))
{
const [name, ...reverseEntries] = keys.reverse();

currentInstance = reverseEntries.reverse().reduce((accumulator, key) =>
accumulator[key], currentInstance);
currentInstance = reverseEntries.reverse().reduce(targetKeyReducer, currentInstance);

key = name;
key = name as keyof MaybePixiReactNode;
}
}

Expand Down Expand Up @@ -160,8 +167,9 @@ export function applyProps(instance: MaybeInstance, data: InstanceProps | DiffSe
delete currentInstance[pixiKey];
}
}
else if (!isReadOnlyProperty(currentInstance, key))
else if (!isReadOnlyProperty(currentInstance as Record<string, unknown>, key))
{
// @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;
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/helpers/attach.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { Filter } from 'pixi.js';

import type { ContainerElement } from '../typedefs/ContainerElement.ts';
import type { Instance } from '../typedefs/Instance.ts';
import type { HostConfig } from '../typedefs/HostConfig.ts';

export function attach(
parentInstance: Instance<ContainerElement>,
childInstance: Instance,
parentInstance: HostConfig['containerInstance'],
childInstance: HostConfig['instance'],
targetIndex?: number
)
{
if (childInstance instanceof Filter)
{
childInstance.__pixireact.parent = parentInstance;
(childInstance as HostConfig['filterInstance']).__pixireact.parent = parentInstance;

if (typeof targetIndex === 'number')
{
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/catalogue.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Instance } from '../typedefs/Instance';
import type { HostConfig } from '../typedefs/HostConfig.ts';

export const catalogue: {
[name: string]: {
new (...args: any): Instance
new (...args: any): HostConfig['instance'],
}
} = {};
8 changes: 3 additions & 5 deletions src/helpers/commitUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ import { switchInstance } from './switchInstance.ts';

import type { Fiber } from 'react-reconciler';
import type { HostConfig } from '../typedefs/HostConfig.ts';
import type { Instance } from '../typedefs/Instance.ts';
import type { InstanceProps } from '../typedefs/InstanceProps.ts';
import type { UpdatePayload } from '../typedefs/UpdatePayload.ts';

export function commitUpdate(
instance: Instance,
instance: HostConfig['instance'],
updatePayload: UpdatePayload,
type: HostConfig['type'],
_oldProps: InstanceProps,
newProps: InstanceProps,
_oldProps: HostConfig['props'],
newProps: HostConfig['props'],
fiber: Fiber,
)
{
Expand Down
6 changes: 2 additions & 4 deletions src/helpers/createInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ import { parseComponentType } from './parseComponentType.ts';
import { prepareInstance } from './prepareInstance.ts';

import type { HostConfig } from '../typedefs/HostConfig.ts';
import type { Instance } from '../typedefs/Instance.ts';
import type { InstanceProps } from '../typedefs/InstanceProps.ts';

export function createInstance(
type: HostConfig['type'],
props: InstanceProps,
root: Instance,
props: HostConfig['props'],
root: HostConfig['containerInstance'],
)
{
log('info', 'lifecycle::createInstance');
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/createTextInstance.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { log } from './log.ts';

import type { Instance } from '../typedefs/Instance.ts';
import type { HostConfig } from '../typedefs/HostConfig.ts';

/** Always throws, because we don't support this (yet). */
export function createTextInstance(
_text: string,
_rootContainer: Instance,
_rootContainer: HostConfig['containerInstance'],
_hostContext: null,
_internalHandle: any,
)
Expand Down
9 changes: 5 additions & 4 deletions src/helpers/detach.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Filter } from 'pixi.js';

import type { ContainerElement } from 'typedefs/ContainerElement.ts';
import type { Instance } from '../typedefs/Instance.ts';
import type { HostConfig } from '../typedefs/HostConfig.ts';

export function detach(childInstance: Instance)
export function detach(
childInstance: HostConfig['instance'],
)
{
if (childInstance instanceof Filter)
{
const parentInstance = childInstance.__pixireact.parent as Instance<ContainerElement>;
const parentInstance = childInstance.__pixireact.parent as HostConfig['instance'];

if (parentInstance)
{
Expand Down
6 changes: 3 additions & 3 deletions src/helpers/diffProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { isEqual } from './compare.ts';
import { gentleCloneProps } from './gentleCloneProps.ts';

import type { Change } from '../typedefs/Change.ts';
import type { InstanceProps } from '../typedefs/InstanceProps.ts';
import type { HostConfig } from '../typedefs/HostConfig.ts';

const DEFAULT = '__default';

export function diffProps(
newProps: InstanceProps,
oldProps: InstanceProps = {},
newProps: HostConfig['props'],
oldProps: HostConfig['props'] = {},
remove = false,
)
{
Expand Down
26 changes: 12 additions & 14 deletions src/helpers/hideInstance.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import { catalogue } from './catalogue.ts';
import {
Container,
Filter,
} from 'pixi.js';

import type { ContainerElement } from '../typedefs/ContainerElement.ts';
import type { FilterElement } from '../typedefs/FilterElement.ts';
import type { Instance } from '../typedefs/Instance.ts';
import type { HostConfig } from '../typedefs/HostConfig.ts';

export function hideInstance(instance: Instance)
export function hideInstance(
instance: HostConfig['instance']
)
{
const {
Container,
Filter,
} = catalogue;

if (Container && instance instanceof Container)
if (instance instanceof Container)
{
(instance as ContainerElement).visible = false;
(instance as HostConfig['containerInstance']).visible = false;
}
else if (Filter && instance instanceof Filter)
else if (instance instanceof Filter)
{
(instance as FilterElement).enabled = false;
(instance as HostConfig['filterInstance']).enabled = false;
}
}
Loading

0 comments on commit 9cf1fdb

Please sign in to comment.