Skip to content

Commit

Permalink
Merge pull request #1272 from erwinmombay/when-doc-ready
Browse files Browse the repository at this point in the history
feature(document-state): add `whenDocumentReady` function that returns a promise
  • Loading branch information
erwinmombay committed Dec 30, 2015
2 parents 3a28d82 + 74273ac commit e0edec6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 10 deletions.
11 changes: 2 additions & 9 deletions extensions/amp-user-notification/0.1/amp-user-notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {cidFor} from '../../../src/cid';
import {getService} from '../../../src/service';
import {isExperimentOn} from '../../../src/experiments';
import {log} from '../../../src/log';
import {onDocumentReady} from '../../../src/document-state';
import {whenDocumentReady} from '../../../src/document-state';
import {viewerFor} from '../../../src/viewer';
import {xhrFor} from '../../../src/xhr';

Expand Down Expand Up @@ -258,17 +258,10 @@ export class UserNotificationManager {
/** @private @const {!Viewer} */
this.viewer_ = viewerFor(this.win_);

// TODO(erwinm, #1226)
// Add `whenDocumentReady` function in document-state
// that returns a promise.
const documentReadyPromise_ = new Promise(resolve => {
onDocumentReady(this.win_.document, resolve);
});

/** @private {!Promise} */
this.managerReadyPromise_ = all([
this.viewer_.whenVisible(),
documentReadyPromise_
whenDocumentReady(this.win_.document)
]);

/** @private {!Promise} */
Expand Down
11 changes: 11 additions & 0 deletions src/document-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ export function onDocumentReady(doc, callback) {
}
}

/**
* Returns a promise that is resolved when document is ready.
* @param {!Document} doc
* @return {!Promise}
*/
export function whenDocumentReady(doc) {
return new Promise(resolve => {
onDocumentReady(doc, resolve);
});
}


/**
*/
Expand Down
56 changes: 55 additions & 1 deletion test/functional/test-document-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
* limitations under the License.
*/

import {DocumentState, isDocumentReady, onDocumentReady} from
import {DocumentState, isDocumentReady, onDocumentReady, whenDocumentReady} from
'../../src/document-state';
import {timer} from '../../src/timer';
import * as sinon from 'sinon';


Expand Down Expand Up @@ -88,6 +89,59 @@ describe('documentReady', () => {
expect(callback.callCount).to.equal(1);
expect(eventListeners['readystatechange']).to.equal(undefined);
});

describe('whenDocumentReady', () => {

it('should call callback immediately when ready', () => {
testDoc.readyState = 'complete';
const spy = sinon.spy();
const spy2 = sinon.spy();
const spy3 = sinon.spy();

whenDocumentReady(testDoc).then(spy).then(spy2);

whenDocumentReady(testDoc).then(spy3);

expect(spy.callCount).to.equal(0);
expect(spy2.callCount).to.equal(0);
expect(spy3.callCount).to.equal(0);

return timer.promise().then(() => {
expect(spy.callCount).to.equal(1);
expect(spy2.callCount).to.equal(1);
expect(spy3.callCount).to.equal(1);
});
});

it('should not call callback', () => {
const spy = sinon.spy();
whenDocumentReady(testDoc).then(spy);
expect(spy.callCount).to.equal(0);
return timer.promise().then(() => {
expect(spy.callCount).to.equal(0);
});
});

it('should wait to call callback until ready', () => {
testDoc.readyState = 'loading';
const callback = sinon.spy();
whenDocumentReady(testDoc).then(callback);

return timer.promise().then(() => {
expect(callback.callCount).to.equal(0);
expect(eventListeners['readystatechange']).to.not.equal(undefined);

// Complete
testDoc.readyState = 'complete';
eventListeners['readystatechange']();

return timer.promise().then(() => {
expect(callback.callCount).to.equal(1);
expect(eventListeners['readystatechange']).to.equal(undefined);
});
});
});
});
});


Expand Down

0 comments on commit e0edec6

Please sign in to comment.