mirror of
https://github.com/atom/atom.git
synced 2026-01-23 13:58:08 -05:00
Throttle document polling
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
{find} = require 'underscore-plus'
|
||||
Grim = require 'grim'
|
||||
{Disposable} = require 'event-kit'
|
||||
_ = require 'underscore-plus'
|
||||
|
||||
# Essential: `ViewRegistry` handles the association between model and view
|
||||
# types in Atom. We call this association a View Provider. As in, for a given
|
||||
@@ -45,6 +46,8 @@ class ViewRegistry
|
||||
documentUpdateRequested: false
|
||||
documentReadInProgress: false
|
||||
performDocumentPollAfterUpdate: false
|
||||
debouncedPerformDocumentPoll: null
|
||||
minimumPollInterval: 200
|
||||
|
||||
constructor: ->
|
||||
@views = new WeakMap
|
||||
@@ -53,7 +56,8 @@ class ViewRegistry
|
||||
@documentReaders = []
|
||||
@documentPollers = []
|
||||
|
||||
@observer = new MutationObserver(@performDocumentPoll)
|
||||
@observer = new MutationObserver(@requestDocumentPoll)
|
||||
@debouncedPerformDocumentPoll = _.throttle(@performDocumentPoll, @minimumPollInterval).bind(this)
|
||||
|
||||
# Essential: Add a provider that will be used to construct views in the
|
||||
# workspace's view layer based on model objects in its model layer.
|
||||
@@ -208,16 +212,19 @@ class ViewRegistry
|
||||
writer() while writer = @documentWriters.shift()
|
||||
|
||||
startPollingDocument: ->
|
||||
window.addEventListener('resize', @performDocumentPoll)
|
||||
window.addEventListener('resize', @requestDocumentPoll)
|
||||
@observer.observe(document, {subtree: true, childList: true, attributes: true})
|
||||
|
||||
stopPollingDocument: ->
|
||||
window.removeEventListener('resize', @performDocumentPoll)
|
||||
window.removeEventListener('resize', @requestDocumentPoll)
|
||||
@observer.disconnect()
|
||||
|
||||
performDocumentPoll: =>
|
||||
requestDocumentPoll: =>
|
||||
if @documentUpdateRequested
|
||||
@performDocumentPollAfterUpdate = true
|
||||
else
|
||||
poller() for poller in @documentPollers
|
||||
return
|
||||
@debouncedPerformDocumentPoll()
|
||||
|
||||
performDocumentPoll: ->
|
||||
poller() for poller in @documentPollers
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user