Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use signal API to simplify using the smooth slider #5077

Merged
merged 3 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 5 additions & 20 deletions webview/src/plots/components/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2853,10 +2853,7 @@ describe('App', () => {
const smoothPlot = screen.getByTestId(`plot_${smoothId}`)
await waitForVega(smoothPlot)

// eslint-disable-next-line testing-library/no-node-access
const slider = smoothPlot.querySelector(
'.vega-bindings input[name="smooth"]'
)
const slider = within(smoothPlot).getByRole('slider')
expect(slider).toBeInTheDocument()

fireEvent.change(slider as HTMLInputElement, { target: { value: 0.4 } })
Expand All @@ -2876,10 +2873,7 @@ describe('App', () => {
const popup = screen.getByTestId('zoomed-in-plot')
await waitForVega(popup)

// eslint-disable-next-line testing-library/no-node-access
const slider = popup.querySelector(
'.vega-bindings input[name="smooth"]'
)
const slider = within(popup).getByRole('slider')
expect(slider).toBeInTheDocument()

fireEvent.change(slider as HTMLInputElement, { target: { value: 0.4 } })
Expand All @@ -2895,10 +2889,7 @@ describe('App', () => {
const smoothPlot = screen.getByTestId(`plot_${smoothId}`)
await waitForVega(smoothPlot)

// eslint-disable-next-line testing-library/no-node-access
const slider = smoothPlot.querySelector(
'.vega-bindings input[name="smooth"]'
)
const slider = within(smoothPlot).getByRole('slider')
expect(slider).toBeInTheDocument()

expect(slider).toHaveValue('0.6')
Expand All @@ -2918,10 +2909,7 @@ describe('App', () => {
const popup = screen.getByTestId('zoomed-in-plot')
await waitForVega(popup)

// eslint-disable-next-line testing-library/no-node-access
const slider = popup.querySelector(
'.vega-bindings input[name="smooth"]'
)
const slider = within(popup).getByRole('slider')
expect(slider).toBeInTheDocument()

expect(slider).toHaveValue('0.6')
Expand All @@ -2936,10 +2924,7 @@ describe('App', () => {

await waitForVega(smoothPlot)

// eslint-disable-next-line testing-library/no-node-access
const slider = smoothPlot.querySelector(
'.vega-bindings input[name="smooth"]'
)
const slider = within(smoothPlot).getByRole('slider')

expect(slider).toBeInTheDocument()
expect(slider).toHaveValue('0.2')
Expand Down
68 changes: 24 additions & 44 deletions webview/src/plots/components/vegaLite/ExtendedVegaLite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ import { setSmoothPlotValues } from '../../util/messages'
import { config } from '../constants'
import { useGetPlot } from '../../hooks/useGetPlot'

interface VegaState {
signals?: { [name: string]: number | undefined }
data?: unknown
}

export const ExtendedVegaLite = ({
actions,
id,
Expand Down Expand Up @@ -44,68 +39,53 @@ export const ExtendedVegaLite = ({
} as VegaLiteProps

const vegaView = useRef<View>()
const plotWrapperEl = useRef<HTMLSpanElement>(null)
const smoothPlotValues = useSelector(
(state: PlotsState) => state.template.smoothPlotValues
)
const changeDebounceTimer = useRef(0)
const currentValue = smoothPlotValues[id]

useEffect(() => {
const newValue = smoothPlotValues[id]
if (!newValue || !vegaView.current) {
return
return () => {
vegaView.current?.finalize()
}
}, [])

const currentState: VegaState = vegaView.current.getState()
const currentValue: number | undefined = currentState?.signals?.smooth
if (newValue !== currentValue) {
vegaView.current.setState({
...currentState,
signals: { ...currentState.signals, smooth: newValue }
})
useEffect(() => {
if (!currentValue || !vegaView.current) {
return
}
}, [smoothPlotValues, id])

const addRangeEventListener = () => {
const smoothRange = plotWrapperEl.current?.querySelector(
'input[name="smooth"]'
)

smoothRange?.addEventListener('change', (event: Event) => {
if (event.target) {
window.clearTimeout(changeDebounceTimer.current)
changeDebounceTimer.current = window.setTimeout(() => {
setSmoothPlotValues(
id,
Number((event.target as HTMLInputElement).value)
)
}, 500)
}
})
}
if (vegaView.current.signal('smooth') !== currentValue) {
vegaView.current.signal('smooth', currentValue)
vegaView.current.run()
}
}, [currentValue])

return (
<span ref={plotWrapperEl}>
<span>
<VegaLite
{...vegaLiteProps}
onNewView={view => {
onNewView(view)
vegaView.current = view
const defaultValue = smoothPlotValues[id]
const state = view.getState() as VegaState

if (!state?.signals?.smooth) {
if (!view.signal('smooth')) {
return
}

if (defaultValue) {
view.setState({
...state,
signals: { ...state.signals, smooth: defaultValue }
})
if (currentValue) {
view.signal('smooth', currentValue)
view.run()
}

addRangeEventListener()
view.addSignalListener('smooth', (_, value) => {
window.clearTimeout(changeDebounceTimer.current)
changeDebounceTimer.current = window.setTimeout(
() => setSmoothPlotValues(id, Number(value)),
500
)
})
}}
/>
</span>
Expand Down
Loading