Skip to content

Commit 1168dae

Browse files
authored
Merge pull request #719 from atom-ide-community/requestForAnimation-4upstream
2 parents 7c8e7de + 3779b5c commit 1168dae

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

lib/minimap.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,13 @@ class Minimap {
313313

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

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

@@ -336,6 +336,24 @@ class Minimap {
336336
this.pendingChangeEvents = []
337337
}
338338

339+
/**
340+
* Requests flush changes if not already requested
341+
*
342+
* @return void
343+
* @access private
344+
*/
345+
requestFlushChanges () {
346+
if (!this.requestedFlushChanges) {
347+
this.requestedFlushChanges = requestAnimationFrame(() => {
348+
this.flushChanges()
349+
if (this.requestedFlushChanges) {
350+
cancelAnimationFrame(this.requestedFlushChanges)
351+
this.requestedFlushChanges = null
352+
}
353+
})
354+
}
355+
}
356+
339357
/**
340358
* Registers an event listener to the `did-change` event.
341359
*

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,8 @@
246246
"requestAnimationFrame",
247247
"Event",
248248
"MouseEvent",
249-
"IntersectionObserver"
249+
"IntersectionObserver",
250+
"cancelAnimationFrame"
250251
]
251252
}
252253
}

spec/minimap-spec.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ describe('Minimap', () => {
8989

9090
editor.setText('foo')
9191

92-
expect(changeSpy).toHaveBeenCalled()
92+
// because of requestAnimation the change is relayed asynchronously.
93+
setTimeout(() => {
94+
expect(changeSpy).toHaveBeenCalled()
95+
}, 1000)
9396
})
9497

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

0 commit comments

Comments
 (0)