Skip to content

Commit 3bb907f

Browse files
MarcinSzalomski3m5. Marcin Szalomski
andauthored
feat(RB-3031): ADD mobx watcher back eg. for storybook (#486)
(cherry picked from commit c6503e3d963f2e06ad438baa533c73affa30844a) (cherry picked from commit 1addc59) # Conflicts: # src/components/robots/RobotAnimator.tsx [Storybook Link](https://wandelbotsgmbh.github.io/wandelbots-js-react-components/overview.html) --------- Co-authored-by: 3m5. Marcin Szalomski <marcin.szalomski@partner.wandelbots.com>
1 parent c30b616 commit 3bb907f

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

src/components/robots/RobotAnimator.tsx

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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"
77
import type { Group, Object3D } from "three"
88
import { useAutorun } from "../utils/hooks"
99
import { 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

Comments
 (0)