Add ViewRegistry.prototype.getNextUpdatePromise

Signed-off-by: Max Brunsfeld <maxbrunsfeld@github.com>
This commit is contained in:
Nathan Sobo
2015-10-23 18:13:30 -06:00
parent cdaac9dfcc
commit 1aefb22789
2 changed files with 25 additions and 0 deletions

View File

@@ -209,3 +209,21 @@ describe "ViewRegistry", ->
window.dispatchEvent(new UIEvent('resize'))
expect(events).toEqual ['poll 1', 'poll 2']
describe "::getNextUpdatePromise()", ->
it "returns a promise that resolves at the end of the next update cycle", ->
updateCalled = false
readCalled = false
pollCalled = false
waitsFor 'getNextUpdatePromise to resolve', (done) ->
registry.getNextUpdatePromise().then ->
expect(updateCalled).toBe true
expect(readCalled).toBe true
expect(pollCalled).toBe true
done()
registry.updateDocument -> updateCalled = true
registry.readDocument -> readCalled = true
registry.pollDocument -> pollCalled = true
registry.pollAfterNextUpdate()

View File

@@ -195,6 +195,10 @@ class ViewRegistry
pollAfterNextUpdate: ->
@performDocumentPollAfterUpdate = true
getNextUpdatePromise: ->
@nextUpdatePromise ?= new Promise (resolve) =>
@resolveNextUpdatePromise = resolve
clearDocumentRequests: ->
@documentReaders = []
@documentWriters = []
@@ -220,6 +224,9 @@ class ViewRegistry
# process updates requested as a result of reads
writer() while writer = @documentWriters.shift()
@nextUpdatePromise = null
@resolveNextUpdatePromise?()
startPollingDocument: ->
window.addEventListener('resize', @requestDocumentPoll)
@observer.observe(document, {subtree: true, childList: true, attributes: true})