From 1aefb22789793cbfe3f51ede79a42aa63eccf8df Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Fri, 23 Oct 2015 18:13:30 -0600 Subject: [PATCH] Add ViewRegistry.prototype.getNextUpdatePromise Signed-off-by: Max Brunsfeld --- spec/view-registry-spec.coffee | 18 ++++++++++++++++++ src/view-registry.coffee | 7 +++++++ 2 files changed, 25 insertions(+) diff --git a/spec/view-registry-spec.coffee b/spec/view-registry-spec.coffee index fcddd325a..a2b4965a5 100644 --- a/spec/view-registry-spec.coffee +++ b/spec/view-registry-spec.coffee @@ -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() diff --git a/src/view-registry.coffee b/src/view-registry.coffee index 3a46aa87a..c21622c04 100644 --- a/src/view-registry.coffee +++ b/src/view-registry.coffee @@ -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})