Skip to content

Commit

Permalink
Merge pull request #719 from atom-ide-community/requestForAnimation-4…
Browse files Browse the repository at this point in the history
…upstream
  • Loading branch information
aminya committed Nov 21, 2020
2 parents 7c8e7de + 3779b5c commit 1168dae
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
22 changes: 20 additions & 2 deletions lib/minimap.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,13 @@ class Minimap {

// Optimisation: If the redraw delay is set to 0, do not even schedule a timer
if (!this.redrawDelay) {
return this.flushChanges()
this.requestFlushChanges()
}

if (!this.flushChangesTimer) {
// If any changes happened within the timeout's delay, a timeout will already have been
// scheduled -> no need to schedule again
this.flushChangesTimer = setTimeout(() => { this.flushChanges() }, this.redrawDelay)
this.flushChangesTimer = setTimeout(() => { this.requestFlushChanges() }, this.redrawDelay)
}
}

Expand All @@ -336,6 +336,24 @@ class Minimap {
this.pendingChangeEvents = []
}

/**
* Requests flush changes if not already requested
*
* @return void
* @access private
*/
requestFlushChanges () {
if (!this.requestedFlushChanges) {
this.requestedFlushChanges = requestAnimationFrame(() => {
this.flushChanges()
if (this.requestedFlushChanges) {
cancelAnimationFrame(this.requestedFlushChanges)
this.requestedFlushChanges = null
}
})
}
}

/**
* Registers an event listener to the `did-change` event.
*
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@
"requestAnimationFrame",
"Event",
"MouseEvent",
"IntersectionObserver"
"IntersectionObserver",
"cancelAnimationFrame"
]
}
}
5 changes: 4 additions & 1 deletion spec/minimap-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ describe('Minimap', () => {

editor.setText('foo')

expect(changeSpy).toHaveBeenCalled()
// because of requestAnimation the change is relayed asynchronously.
setTimeout(() => {
expect(changeSpy).toHaveBeenCalled()
}, 1000)
})

it('relays scroll top events from the editor', () => {
Expand Down

0 comments on commit 1168dae

Please sign in to comment.