@pixi/react
Version:
Write PixiJS applications using React declarative style.
1 lines • 6.82 kB
Source Map (JSON)
{"version":3,"file":"createRoot.mjs","sources":["../../src/core/createRoot.tsx"],"sourcesContent":["import { Application } from 'pixi.js';\nimport { type ApplicationOptions } from 'pixi.js';\nimport { type ReactNode } from 'react';\nimport { ConcurrentRoot } from 'react-reconciler/constants.js';\nimport { ContextProvider } from '../components/Context';\nimport { isReadOnlyProperty } from '../helpers/isReadOnlyProperty';\nimport { log } from '../helpers/log';\nimport { prepareInstance } from '../helpers/prepareInstance';\nimport { type ApplicationState } from '../typedefs/ApplicationState';\nimport { type CreateRootOptions } from '../typedefs/CreateRootOptions';\nimport { type HostConfig } from '../typedefs/HostConfig';\nimport { type InternalState } from '../typedefs/InternalState';\nimport { reconciler } from './reconciler';\nimport { roots } from './roots';\n\n/** Creates a new root for a Pixi React app. */\nexport function createRoot(\n /** @description The DOM node which will serve as the root for this tree. */\n target: HTMLElement | HTMLCanvasElement,\n\n /** @description Options to configure the tree. */\n options: CreateRootOptions = {},\n)\n{\n // Check against mistaken use of createRoot\n let root = roots.get(target);\n let applicationState = (root?.applicationState ?? {\n isInitialised: false,\n isInitialising: false,\n }) as ApplicationState;\n\n const internalState = root?.internalState ?? {} as InternalState;\n\n if (root)\n {\n log('warn', 'createRoot should only be called once!');\n }\n else\n {\n applicationState.app = new Application();\n internalState.rootContainer = prepareInstance(applicationState.app.stage) as HostConfig['containerInstance'];\n }\n\n const fiber = root?.fiber ?? (reconciler as any).createContainer(\n internalState.rootContainer, // container\n ConcurrentRoot, // tag\n null, // hydration callbacks\n false, // isStrictMode\n null, // concurrentUpdatesByDefaultOverride\n '', // identifierPrefix\n console.error, // onUncaughtError\n console.error, // onCaughtError\n console.error, // onRecoverableError\n null, // transitionCallbacks\n );\n\n if (!root)\n {\n let canvas;\n\n if (target instanceof HTMLCanvasElement)\n {\n canvas = target;\n }\n\n if (!canvas)\n {\n canvas = document.createElement('canvas');\n target.innerHTML = '';\n target.appendChild(canvas);\n }\n\n internalState.canvas = canvas;\n\n const render = async (\n children: ReactNode,\n applicationOptions: ApplicationOptions,\n ) =>\n {\n if (!applicationState.app.renderer && !applicationState.isInitialised && !applicationState.isInitialising)\n {\n applicationState.isInitialising = true;\n await applicationState.app.init({\n ...applicationOptions,\n canvas,\n });\n applicationState.isInitialising = false;\n applicationState.isInitialised = true;\n applicationState = { ...applicationState };\n options.onInit?.(applicationState.app);\n }\n\n Object.entries(applicationOptions).forEach(([key, value]) =>\n {\n const typedKey = key as keyof ApplicationOptions;\n\n if (isReadOnlyProperty(\n applicationOptions as unknown as Record<string, unknown>,\n typedKey,\n ))\n {\n return;\n }\n\n // @ts-expect-error Typescript doesn't realise it, but we're already verifying that this isn't a readonly key.\n applicationState.app[typedKey] = value;\n });\n\n // Update fiber and expose Pixi.js state to children\n reconciler.updateContainer(\n (\n <ContextProvider value={applicationState}>\n {children}\n </ContextProvider>\n ),\n fiber,\n null,\n () => undefined,\n );\n\n return applicationState.app;\n };\n\n root = {\n applicationState,\n fiber,\n internalState,\n render,\n };\n\n roots.set(canvas, root);\n }\n\n return root;\n}\n"],"names":[],"mappings":";;;;;;;;;;;AAgBO,SAAS,UAEZ,CAAA,MAAA,EAGA,OAA6B,GAAA,EAEjC,EAAA;AAEI,EAAI,IAAA,IAAA,GAAO,KAAM,CAAA,GAAA,CAAI,MAAM,CAAA;AAC3B,EAAI,IAAA,gBAAA,GAAoB,MAAM,gBAAoB,IAAA;AAAA,IAC9C,aAAe,EAAA,KAAA;AAAA,IACf,cAAgB,EAAA;AAAA,GACpB;AAEA,EAAM,MAAA,aAAA,GAAgB,IAAM,EAAA,aAAA,IAAiB,EAAC;AAE9C,EAAA,IAAI,IACJ,EAAA;AACI,IAAA,GAAA,CAAI,QAAQ,wCAAwC,CAAA;AAAA,GAGxD,MAAA;AACI,IAAiB,gBAAA,CAAA,GAAA,GAAM,IAAI,WAAY,EAAA;AACvC,IAAA,aAAA,CAAc,aAAgB,GAAA,eAAA,CAAgB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