Skip to content

Commit

Permalink
fix: use its-fine to bridge context across the application boundary
Browse files Browse the repository at this point in the history
Signed-off-by: Trezy <tre@trezy.com>
  • Loading branch information
trezy committed Dec 31, 2024
1 parent 3817702 commit a827ee6
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 41 deletions.
45 changes: 13 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
]
},
"dependencies": {
"its-fine": "^1.2.5",
"react-reconciler": "0.31.0"
},
"devDependencies": {
Expand Down
32 changes: 25 additions & 7 deletions src/components/Application.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import {
FiberProvider,
useContextBridge,
} from 'its-fine'
import {
type Application as PixiApplication,
extensions as PixiExtensions,
TextStyle,
} from 'pixi.js';
import {
createElement,
forwardRef,
type RefObject,
useCallback,
Expand All @@ -23,7 +26,7 @@ import { type ApplicationRef } from '../typedefs/ApplicationRef';

const originalDefaultTextStyle = { ...TextStyle.defaultTextStyle };

export const Application = forwardRef<ApplicationRef, ApplicationProps>(function Application(
const ApplicationImplementation = forwardRef<ApplicationRef, ApplicationProps>(function Application(
props,
forwardedRef,
)
Expand All @@ -38,6 +41,8 @@ export const Application = forwardRef<ApplicationRef, ApplicationProps>(function
...applicationProps
} = props;

const Bridge = useContextBridge();

const applicationRef: RefObject<PixiApplication | null> = useRef(null);
const canvasRef = useRef<HTMLCanvasElement>(null);
const extensionsRef = useRef<Set<any>>(new Set());
Expand Down Expand Up @@ -135,7 +140,8 @@ export const Application = forwardRef<ApplicationRef, ApplicationProps>(function
root = createRoot(canvasElement, {}, handleInit);
}

root.render(children, applicationProps);
// @ts-expect-error The value of `children` is fine, but `PixiReactChildNode` doesn't strictly adhere to the `ReactNode` structure.
root.render((<Bridge>{children}</Bridge>), applicationProps);
}
}, [
applicationProps,
Expand Down Expand Up @@ -177,8 +183,20 @@ export const Application = forwardRef<ApplicationRef, ApplicationProps>(function
}
}, []);

return createElement('canvas', {
className,
ref: canvasRef,
});
return (
<canvas
ref={canvasRef}
className={className} />
);
});

export const Application = forwardRef<ApplicationRef, ApplicationProps>(function ApplicationWrapper(props, ref)
{
return (
<FiberProvider>
<ApplicationImplementation
ref={ref}
{...props} />
</FiberProvider>
);
});
7 changes: 5 additions & 2 deletions src/core/createRoot.ts → src/core/createRoot.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Application } from 'pixi.js';
import { type ApplicationOptions } from 'pixi.js';
import { createElement } from 'react';
import { type ReactNode } from 'react';
import { ConcurrentRoot } from 'react-reconciler/constants.js';
import { ContextProvider } from '../components/Context';
Expand Down Expand Up @@ -115,7 +114,11 @@ export function createRoot(

// Update fiber and expose Pixi.js state to children
reconciler.updateContainer(
createElement(ContextProvider, { value: applicationState }, children),
(
<ContextProvider value={applicationState}>
{children}
</ContextProvider>
),
fiber,
null,
() => undefined,
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export { useAssets } from './hooks/useAssets';
export { useExtend } from './hooks/useExtend';
export { useSuspenseAssets } from './hooks/useSuspenseAssets';
export { useTick } from './hooks/useTick';
export { type ApplicationRef } from './typedefs/ApplicationRef';
export { type PixiReactElementProps } from './typedefs/PixiReactNode';

0 comments on commit a827ee6

Please sign in to comment.