Skip to content

Commit

Permalink
fix(): handle very tall last item in reverse mode
Browse files Browse the repository at this point in the history
  • Loading branch information
petyosi committed Aug 6, 2021
1 parent 82beaba commit e64d207
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
21 changes: 20 additions & 1 deletion src/scrollToIndexSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export const scrollToIndexSystem = u.system(
cleanup()
if (listChanged) {
u.publish(scrollToIndex, location)
} else {
}
}

Expand All @@ -103,7 +104,7 @@ export const scrollToIndexSystem = u.system(
retry(listChanged)
})
} else {
unsubscribeNextListRefresh = u.handleNext(listRefresh, retry)
unsubscribeNextListRefresh = u.handleNext(u.pipe(listRefresh, accumulateChange(0)), retry)
}

// if the scroll jump is too small, the list won't get rerendered.
Expand All @@ -127,3 +128,21 @@ export const scrollToIndexSystem = u.system(
u.tup(sizeSystem, domIOSystem),
{ singleton: true }
)

function accumulateChange<T>(interval: number): u.Operator<T> {
let currentValue: T | undefined
let timeout: any

return (done) => (value) => {
currentValue = currentValue || value

if (timeout) {
return
}

timeout = setTimeout(() => {
timeout = undefined
done(currentValue!)
}, interval)
}
}
13 changes: 8 additions & 5 deletions test/listSystem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ describe('list engine', () => {
})
})

it('readjusts once when new sizes are reported', () => {
it('readjusts once when new sizes are reported', (done) => {
const DEVIATION = 20
publish(sti, { index: INDEX, align: 'end' })

Expand All @@ -185,10 +185,13 @@ describe('list engine', () => {

publish(sr, [{ startIndex: INDEX - 1, endIndex: INDEX - 1, size: SIZE + DEVIATION }])

expect(sub).toHaveBeenCalledWith({
top: INDEX * SIZE - VIEWPORT + SIZE + DEVIATION,
behavior: 'auto',
})
setTimeout(() => {
expect(sub).toHaveBeenCalledWith({
top: INDEX * SIZE - VIEWPORT + SIZE + DEVIATION,
behavior: 'auto',
})
done()
}, 20)
})
})

Expand Down

0 comments on commit e64d207

Please sign in to comment.