Run polling functions when windows resizes

This commit is contained in:
Jessica Lord 2015-07-21 11:10:35 -07:00 committed by Nathan Sobo
parent fb7304a3d7
commit 96874d68d8
2 changed files with 13 additions and 1 deletions

View File

@ -186,7 +186,9 @@ describe "ViewRegistry", ->
expect(events).toEqual ['write', 'read', 'poll']
describe "::pollDocument(fn)", ->
it "calls all registered polling functions after document or stylesheet changes until they are disabled via a returned disposable", ->
[testElement, testStyleSheet, disposable1, disposable2, events] = []
beforeEach ->
testElement = document.createElement('div')
testStyleSheet = document.createElement('style')
testStyleSheet.textContent = 'body {}'
@ -198,6 +200,7 @@ describe "ViewRegistry", ->
disposable1 = registry.pollDocument -> events.push('poll 1')
disposable2 = registry.pollDocument -> events.push('poll 2')
it "calls all registered polling functions after document or stylesheet changes until they are disabled via a returned disposable", ->
expect(events).toEqual []
testElement.style.height = '400px'
@ -223,3 +226,10 @@ describe "ViewRegistry", ->
runs ->
expect(events).toEqual ['poll 2']
it "calls all registered polling functions when the window resizes", ->
expect(events).toEqual []
window.dispatchEvent(new UIEvent('resize'))
expect(events).toEqual ['poll 1', 'poll 2']

View File

@ -210,9 +210,11 @@ class ViewRegistry
writer() while writer = @documentWriters.shift()
startPollingDocument: ->
window.addEventListener('resize', @performDocumentPoll)
@observer.observe(document, {subtree: true, childList: true, attributes: true})
stopPollingDocument: ->
window.removeEventListener('resize', @performDocumentPoll)
@observer.disconnect()
performDocumentPoll: =>