Skip to content

Commit

Permalink
perf(useResizeColumns): optimize dispatch count (#3231)
Browse files Browse the repository at this point in the history
* perf(useResizeColumns): optimize dispatch count for #3165

* console log demo

* Revert "console log demo"

37a69f5

* test(useResizeColumns): update tests
  • Loading branch information
theopak committed Apr 8, 2022
1 parent 05ed37c commit f2e8827
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/plugin-hooks/tests/useResizeColumns.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ const sizesAfter = [
'179.26829268292684px',
]

beforeEach(() => {
jest.spyOn(window, 'requestAnimationFrame').mockImplementation(cb => cb())
})

afterEach(() => {
window.requestAnimationFrame.mockRestore()
})

test('table can be resized by a mouse', () => {
const rtl = render(<App />)

Expand Down
26 changes: 21 additions & 5 deletions src/plugin-hooks/useResizeColumns.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,31 @@ const defaultGetResizerProps = (props, { instance, header }) => {

const clientX = isTouchEvent ? Math.round(e.touches[0].clientX) : e.clientX

const dispatchMove = clientXPos => {
dispatch({ type: actions.columnResizing, clientX: clientXPos })
let raf
let mostRecentClientX

const dispatchEnd = () => {
window.cancelAnimationFrame(raf)
raf = null
dispatch({ type: actions.columnDoneResizing })
}
const dispatchMove = () => {
window.cancelAnimationFrame(raf)
raf = null
dispatch({ type: actions.columnResizing, clientX: mostRecentClientX })
}

const scheduleDispatchMoveOnNextAnimationFrame = clientXPos => {
mostRecentClientX = clientXPos
if (!raf) {
raf = window.requestAnimationFrame(dispatchMove)
}
}
const dispatchEnd = () => dispatch({ type: actions.columnDoneResizing })

const handlersAndEvents = {
mouse: {
moveEvent: 'mousemove',
moveHandler: e => dispatchMove(e.clientX),
moveHandler: e => scheduleDispatchMoveOnNextAnimationFrame(e.clientX),
upEvent: 'mouseup',
upHandler: e => {
document.removeEventListener(
Expand All @@ -78,7 +94,7 @@ const defaultGetResizerProps = (props, { instance, header }) => {
e.preventDefault()
e.stopPropagation()
}
dispatchMove(e.touches[0].clientX)
scheduleDispatchMoveOnNextAnimationFrame(e.touches[0].clientX)
return false
},
upEvent: 'touchend',
Expand Down

0 comments on commit f2e8827

Please sign in to comment.