@@ -23,7 +23,7 @@ import { isEventType } from "./create-events.ts"
2323import { useThree } from "./hooks.ts"
2424import { addToEventListeners , useCanvasProps } from "./internal-context.ts"
2525import type { Instance } from "./types.ts"
26- import { when } from "./utils/conditionals.ts"
26+ import { check } from "./utils/conditionals.ts"
2727import { hasColorSpace } from "./utils/has-colorspace.ts"
2828import { isInstance } from "./utils/is-instance.ts"
2929import { resolve } from "./utils/resolve.ts"
@@ -44,7 +44,7 @@ import { resolve } from "./utils/resolve.ts"
4444 * @param props - An object containing the props to apply. This includes both direct properties
4545 * and special properties like `ref` and `children`.
4646 */
47- export function manageProps < T > ( object : Accessor < Instance < T > > , props : any ) {
47+ export function manageProps < T extends object > ( object : Accessor < T > , props : any ) {
4848 const [ local , instanceProps ] = splitProps ( props , [ "ref" , "args" , "object" , "attach" , "children" ] )
4949
5050 // Assign ref
@@ -53,12 +53,14 @@ export function manageProps<T>(object: Accessor<Instance<T>>, props: any) {
5353 else local . ref = object ( )
5454 } )
5555
56- // Connect or attach children to THREE-instance
57- const childrenAccessor = children ( ( ) => props . children )
58- createRenderEffect ( ( ) =>
59- // @ts -expect-error TODO: fix type-error
60- manageSceneGraph ( object ( ) , childrenAccessor as unknown as Accessor < Instance > ) ,
61- )
56+ createRenderEffect ( ( ) => {
57+ if ( "children" in props ) {
58+ // Connect or attach children to THREE-instance
59+ const childrenAccessor = children ( ( ) => props . children )
60+ // @ts -expect-error TODO: fix type-error
61+ manageSceneGraph ( object ( ) , childrenAccessor as unknown as Accessor < Instance > )
62+ }
63+ } )
6264
6365 // Apply the props to THREE-instance
6466 createRenderEffect ( ( ) => {
@@ -69,7 +71,7 @@ export function manageProps<T>(object: Accessor<Instance<T>>, props: any) {
6971
7072 // Automatically dispose
7173 onCleanup ( ( ) =>
72- when ( object , object => {
74+ check ( object , object => {
7375 if ( "dispose" in object && typeof object . dispose === "function" ) {
7476 object . dispose ( )
7577 }
@@ -83,20 +85,22 @@ export function manageProps<T>(object: Accessor<Instance<T>>, props: any) {
8385/* */
8486/**********************************************************************************/
8587
86- export function applyProps < T > ( object : Instance < T > , props : any ) {
87- const keys = Object . keys ( props )
88- for ( const key of keys ) {
89- // An array of sub-property-keys:
90- // p.ex in <T.Mesh position={} position-x={}/> position's subKeys will be ['position-x']
91- const subKeys = keys . filter ( _key => key !== _key && _key . includes ( key ) )
92- createRenderEffect ( ( ) => {
93- applyProp ( object , key , props [ key ] )
94- // If property updates, apply its sub-properties immediately after.
95- for ( const subKey of subKeys ) {
96- applyProp ( object , subKey , props [ subKey ] )
97- }
98- } )
99- }
88+ export function applyProps < T > ( object : T , props : any ) {
89+ createRenderEffect ( ( ) => {
90+ const keys = Object . keys ( props )
91+ for ( const key of keys ) {
92+ // An array of sub-property-keys:
93+ // p.ex in <T.Mesh position={} position-x={}/> position's subKeys will be ['position-x']
94+ const subKeys = keys . filter ( _key => key !== _key && _key . includes ( key ) )
95+ createRenderEffect ( ( ) => {
96+ applyProp ( object , key , props [ key ] )
97+ // If property updates, apply its sub-properties immediately after.
98+ for ( const subKey of subKeys ) {
99+ applyProp ( object , subKey , props [ subKey ] )
100+ }
101+ } )
102+ }
103+ } )
100104}
101105
102106/**********************************************************************************/
@@ -127,7 +131,7 @@ const NEEDS_UPDATE = [
127131 * @param type - The property name, which can include nested paths indicated by hyphens.
128132 * @param value - The value to be assigned to the property; can be of any appropriate type.
129133 */
130- export function applyProp < T > ( source : Instance < T > , type : string , value : any ) {
134+ export function applyProp < T > ( source : T , type : string , value : any ) {
131135 if ( ! source ) {
132136 console . error ( "error while applying prop" , source , type , value )
133137 return
@@ -140,7 +144,7 @@ export function applyProp<T>(source: Instance<T>, type: string, value: any) {
140144 if ( type . indexOf ( "-" ) > - 1 ) {
141145 const [ property , ...rest ] = type . split ( "-" )
142146 // @ts -expect-error TODO: fix type-error
143- applyProp ( source [ property as string ] , rest . join ( "-" ) , value )
147+ applyProp ( source [ property ] , rest . join ( "-" ) , value )
144148 return
145149 }
146150
0 commit comments