diff options
author | Yamagishi Kazutoshi <ykzts@desire.sh> | 2022-10-04 01:15:47 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-03 18:15:47 +0200 |
commit | 216dbaedaf587db834cfdd53b896e9c7e1284d9c (patch) | |
tree | 7b03a9e780675f0cd4a35795a0d32cabd663ec44 /app/javascript/mastodon/ready.js | |
parent | 37eaa7fc01d8e9cbd06a62cc9ac4b3bd4d835240 (diff) |
Disable push notification when not logged in (#19272)
Diffstat (limited to 'app/javascript/mastodon/ready.js')
-rw-r--r-- | app/javascript/mastodon/ready.js | 37 |
1 files changed, 31 insertions, 6 deletions
diff --git a/app/javascript/mastodon/ready.js b/app/javascript/mastodon/ready.js index dd543910b..e769cc756 100644 --- a/app/javascript/mastodon/ready.js +++ b/app/javascript/mastodon/ready.js @@ -1,7 +1,32 @@ -export default function ready(loaded) { - if (['interactive', 'complete'].includes(document.readyState)) { - loaded(); - } else { - document.addEventListener('DOMContentLoaded', loaded); - } +// @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); + } + }); } |