1/**
2@license
3Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
4This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7Code distributed by Google as part of the polymer project is also
8subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
9*/
10
11'use strict';
12
13/** @type {Promise<void>} */
14let readyPromise = null;
15
16/** @type {?function(?function())} */
17let whenReady = window['HTMLImports'] && window['HTMLImports']['whenReady'] || null;
18
19/** @type {function()} */
20let resolveFn;
21
22/**
23 * @param {?function()} callback
24 */
25export default function documentWait(callback) {
26  requestAnimationFrame(function() {
27    if (whenReady) {
28      whenReady(callback)
29    } else {
30      if (!readyPromise) {
31        readyPromise = new Promise((resolve) => {resolveFn = resolve});
32        if (document.readyState === 'complete') {
33          resolveFn();
34        } else {
35          document.addEventListener('readystatechange', () => {
36            if (document.readyState === 'complete') {
37              resolveFn();
38            }
39          });
40        }
41      }
42      readyPromise.then(function(){ callback && callback(); });
43    }
44  });
45}
46