This repository has been archived by the owner on Jan 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(demos): Add global
demoReady()
function (#1919)
For this PR, I only updated the button demo page to use the new `demoReady()` function. #1920 updates the remaining demo pages. Also note that our packaged JS is now being loaded with `async`, which speeds up initial rendering time over slow connections. Using "Mid-tier mobile" network emulation in Chrome devtools, the [TTFMP](https://developers.google.com/web/tools/lighthouse/audits/first-meaningful-paint) of the button demo page was reduced from ~10.5s to ~9.5s, and the time to render the Material icon font dropped from ~12s to ~10.5s.
- Loading branch information
Showing
3 changed files
with
115 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/** | ||
* @license | ||
* Copyright 2017 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// eslint-disable no-unused-vars no-var | ||
|
||
/** | ||
* Adds the given event handler to the queue. It will be executed asynchronously after all external JS and CSS resources | ||
* have finished loading (as determined by continuous long-polling with a timeout). If this function is called after all | ||
* resources have finished loading, the given handler function will be invoked synchronously (in the same call stack). | ||
* Handlers are invoked in FIFO order. | ||
* @param {function() : undefined} handler | ||
*/ | ||
window.demoReady = (function() { | ||
var TIMEOUT_MS = 60 * 1000; | ||
var DELAY_MS = 100; | ||
|
||
var isReadyCached = false; | ||
var handlers = []; | ||
var testDom = null; | ||
var startTimeMs = null; | ||
var timer = null; | ||
|
||
function isReady() { | ||
if (isReadyCached) { | ||
return true; | ||
} | ||
ensureDetectionDom(); | ||
isReadyCached = Boolean(window.mdc) && getComputedStyle(testDom).position === 'relative'; | ||
return isReadyCached; | ||
} | ||
|
||
function ensureDetectionDom() { | ||
if (testDom) { | ||
return; | ||
} | ||
testDom = document.createElement('div'); | ||
testDom.classList.add('demo-ready-detect'); | ||
document.body.appendChild(testDom); | ||
} | ||
|
||
function removeDetectionDom() { | ||
if (!testDom) { | ||
return; | ||
} | ||
document.body.removeChild(testDom); | ||
testDom = null; | ||
} | ||
|
||
function startTimer() { | ||
if (timer) { | ||
return; | ||
} | ||
startTimeMs = Date.now(); | ||
timer = setInterval(tick, DELAY_MS); | ||
} | ||
|
||
function tick() { | ||
if (isReady()) { | ||
clearInterval(timer); | ||
removeDetectionDom(); | ||
invokeHandlers(); | ||
return; | ||
} | ||
|
||
const elapsedTimeMs = Date.now() - startTimeMs; | ||
if (elapsedTimeMs > TIMEOUT_MS) { | ||
clearInterval(timer); | ||
removeDetectionDom(); | ||
console.error('Timed out waiting for JS and CSS to load'); | ||
return; | ||
} | ||
} | ||
|
||
function invokeHandlers() { | ||
handlers.forEach(function(handler) { | ||
handler(); | ||
}); | ||
} | ||
|
||
return function addHandler(handler) { | ||
if (isReady()) { | ||
handler(); | ||
return; | ||
} | ||
|
||
handlers.push(handler); | ||
startTimer(); | ||
}; | ||
})(); |