@@ -3,7 +3,7 @@ import type {
33 DHParameter ,
44 MotionGroupStateResponse ,
55} from "@wandelbots/nova-api/v1"
6- import React , { useEffect , useRef } from "react"
6+ import React , { useCallback , useEffect , useRef } from "react"
77import type { Group , Object3D } from "three"
88import { useAutorun } from "../utils/hooks"
99import { ValueInterpolator } from "../utils/interpolation"
@@ -68,11 +68,6 @@ export default function RobotAnimator({
6868 invalidate ( )
6969 }
7070
71- function updateJoints ( newJointValues : number [ ] ) {
72- jointValues . current = newJointValues
73- interpolatorRef . current ?. setTarget ( newJointValues )
74- }
75-
7671 function setRotation ( ) {
7772 const updatedJointValues = interpolatorRef . current ?. getCurrentValues ( ) || [ ]
7873
@@ -90,13 +85,33 @@ export default function RobotAnimator({
9085 }
9186 }
9287
93- useAutorun ( ( ) => {
88+ const updateJoints = useCallback ( ( ) => {
9489 const newJointValues =
9590 rapidlyChangingMotionState . state . joint_position . joints . filter (
9691 ( item ) => item !== undefined ,
9792 )
9893
99- requestAnimationFrame ( ( ) => updateJoints ( newJointValues ) )
94+ requestAnimationFrame ( ( ) => {
95+ jointValues . current = newJointValues
96+ interpolatorRef . current ?. setTarget ( newJointValues )
97+ } )
98+ } , [ rapidlyChangingMotionState ] )
99+
100+ /**
101+ * Fire an update joints call on every motion state change.
102+ * requestAnimationFrame used to avoid blocking main thread
103+ */
104+ useEffect ( ( ) => {
105+ updateJoints ( )
106+ } , [ rapidlyChangingMotionState , updateJoints ] )
107+
108+ /**
109+ * As some consumer applications (eg. storybook) deliver
110+ * mobx observable for rapidlyChangingMotionState, we need to
111+ * register the watcher to get the newest value updates
112+ */
113+ useAutorun ( ( ) => {
114+ updateJoints ( )
100115 } )
101116
102117 return < group ref = { setGroupRef } > { children } </ group >
0 commit comments