diff options
author | Yamagishi Kazutoshi <ykzts@desire.sh> | 2022-10-04 01:15:47 +0900 |
---|---|---|
committer | Claire <claire.github-309c@sitedethib.com> | 2022-10-09 18:23:15 +0200 |
commit | cc93cd5d6cf302140d9df56e64dc6e902ce56708 (patch) | |
tree | 86d6d0aa5784167f85657dfe797cc3674252a217 | |
parent | d86cb4cab89e99afae8ebf49e4276b40d444c498 (diff) |
[Glitch] Disable push notification when not logged in
Port 216dbaedaf587db834cfdd53b896e9c7e1284d9c to glitch-soc Signed-off-by: Claire <claire.github-309c@sitedethib.com>
-rw-r--r-- | app/javascript/flavours/glitch/actions/push_notifications/index.js | 26 | ||||
-rw-r--r-- | app/javascript/flavours/glitch/packs/home.js | 6 | ||||
-rw-r--r-- | app/javascript/flavours/glitch/util/main.js | 41 | ||||
-rw-r--r-- | app/javascript/flavours/glitch/util/ready.js | 37 |
4 files changed, 71 insertions, 39 deletions
diff --git a/app/javascript/flavours/glitch/actions/push_notifications/index.js b/app/javascript/flavours/glitch/actions/push_notifications/index.js index 2ffec500a..9dcc4bd4b 100644 --- a/app/javascript/flavours/glitch/actions/push_notifications/index.js +++ b/app/javascript/flavours/glitch/actions/push_notifications/index.js @@ -1,19 +1,5 @@ -import { - SET_BROWSER_SUPPORT, - SET_SUBSCRIPTION, - CLEAR_SUBSCRIPTION, - SET_ALERTS, - setAlerts, -} from './setter'; -import { register, saveSettings } from './registerer'; - -export { - SET_BROWSER_SUPPORT, - SET_SUBSCRIPTION, - CLEAR_SUBSCRIPTION, - SET_ALERTS, - register, -}; +import { setAlerts } from './setter'; +import { saveSettings } from './registerer'; export function changeAlerts(path, value) { return dispatch => { @@ -21,3 +7,11 @@ export function changeAlerts(path, value) { dispatch(saveSettings()); }; } + +export { + CLEAR_SUBSCRIPTION, + SET_BROWSER_SUPPORT, + SET_SUBSCRIPTION, + SET_ALERTS, +} from './setter'; +export { register } from './registerer'; diff --git a/app/javascript/flavours/glitch/packs/home.js b/app/javascript/flavours/glitch/packs/home.js index d06688985..076370b72 100644 --- a/app/javascript/flavours/glitch/packs/home.js +++ b/app/javascript/flavours/glitch/packs/home.js @@ -1,8 +1,10 @@ import 'packs/public-path'; import loadPolyfills from 'flavours/glitch/util/load_polyfills'; -loadPolyfills().then(() => { - require('flavours/glitch/util/main').default(); +loadPolyfills().then(async () => { + const { default: main } = import('flavours/glitch/util/main'); + + return main(); }).catch(e => { console.error(e); }); diff --git a/app/javascript/flavours/glitch/util/main.js b/app/javascript/flavours/glitch/util/main.js index 9e76774b6..b4e6bc151 100644 --- a/app/javascript/flavours/glitch/util/main.js +++ b/app/javascript/flavours/glitch/util/main.js @@ -1,12 +1,14 @@ import React from 'react'; import ReactDOM from 'react-dom'; -import * as registerPushNotifications from 'flavours/glitch/actions/push_notifications'; import { setupBrowserNotifications } from 'flavours/glitch/actions/notifications'; import Mastodon, { store } from 'flavours/glitch/containers/mastodon'; import ready from 'flavours/glitch/util/ready'; -const perf = require('./performance'); +const perf = require('flavours/glitch/util/performance'); +/** + * @returns {Promise<void>} + */ function main() { perf.start('main()'); @@ -18,7 +20,7 @@ function main() { } } - ready(() => { + return ready(async () => { const mountNode = document.getElementById('mastodon'); const props = JSON.parse(mountNode.getAttribute('data-props')); @@ -26,19 +28,28 @@ function main() { store.dispatch(setupBrowserNotifications()); if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { - import('workbox-window') - .then(({ Workbox }) => { - const wb = new Workbox('/sw.js'); - - return wb.register(); - }) - .then(() => { - store.dispatch(registerPushNotifications.register()); - }) - .catch(err => { - console.error(err); - }); + const [{ Workbox }, { me }] = await Promise.all([ + import('workbox-window'), + import('mastodon/initial_state'), + ]); + + const wb = new Workbox('/sw.js'); + + try { + await wb.register(); + } catch (err) { + console.error(err); + + return; + } + + if (me) { + const registerPushNotifications = await import('flavours/glitch/actions/push_notifications'); + + store.dispatch(registerPushNotifications.register()); + } } + perf.stop('main()'); }); } diff --git a/app/javascript/flavours/glitch/util/ready.js b/app/javascript/flavours/glitch/util/ready.js index dd543910b..e769cc756 100644 --- a/app/javascript/flavours/glitch/util/ready.js +++ b/app/javascript/flavours/glitch/util/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); + } + }); } |