mirror of
https://github.com/mastodon/mastodon.git
synced 2024-08-20 21:08:15 -07:00
32 lines
660 B
JavaScript
32 lines
660 B
JavaScript
// @ts-check
|
|
|
|
/**
|
|
* @param {(() => void) | (() => Promise<void>)} callback
|
|
* @returns {Promise<void>}
|
|
*/
|
|
export default function ready(callback) {
|
|
return new Promise((resolve, reject) => {
|
|
function loaded() {
|
|
let result;
|
|
try {
|
|
result = callback();
|
|
} catch (err) {
|
|
reject(err);
|
|
|
|
return;
|
|
}
|
|
|
|
if (typeof result?.then === 'function') {
|
|
result.then(resolve).catch(reject);
|
|
} else {
|
|
resolve();
|
|
}
|
|
}
|
|
|
|
if (['interactive', 'complete'].includes(document.readyState)) {
|
|
loaded();
|
|
} else {
|
|
document.addEventListener('DOMContentLoaded', loaded);
|
|
}
|
|
});
|
|
}
|