Test move this out of component mount

It needs to run faster
This commit is contained in:
Lim Chee Aun 2024-01-02 17:45:58 +08:00
parent e41e49884f
commit 098df0ad2c

View file

@ -15,6 +15,22 @@ import Link from './link';
import Modal from './modal'; import Modal from './modal';
import Notification from './notification'; import Notification from './notification';
{
if ('serviceWorker' in navigator) {
console.log('👂👂👂 Listen to message');
navigator.serviceWorker.addEventListener('message', (event) => {
console.log('💥💥💥 Message event', event);
const { type, id, accessToken } = event?.data || {};
if (type === 'notification') {
states.routeNotification = {
id,
accessToken,
};
}
});
}
}
export default memo(function NotificationService() { export default memo(function NotificationService() {
if (!('serviceWorker' in navigator)) return null; if (!('serviceWorker' in navigator)) return null;
@ -82,25 +98,25 @@ export default memo(function NotificationService() {
})(); })();
}, [id, accessToken]); }, [id, accessToken]);
useLayoutEffect(() => { // useLayoutEffect(() => {
// Listen to message from service worker // // Listen to message from service worker
const handleMessage = (event) => { // const handleMessage = (event) => {
console.log('💥💥💥 Message event', event); // console.log('💥💥💥 Message event', event);
const { type, id, accessToken } = event?.data || {}; // const { type, id, accessToken } = event?.data || {};
if (type === 'notification') { // if (type === 'notification') {
states.routeNotification = { // states.routeNotification = {
id, // id,
accessToken, // accessToken,
}; // };
} // }
}; // };
console.log('👂👂👂 Listen to message'); // console.log('👂👂👂 Listen to message');
navigator.serviceWorker.addEventListener('message', handleMessage); // navigator.serviceWorker.addEventListener('message', handleMessage);
return () => { // return () => {
console.log('👂👂👂 Remove listen to message'); // console.log('👂👂👂 Remove listen to message');
navigator.serviceWorker.removeEventListener('message', handleMessage); // navigator.serviceWorker.removeEventListener('message', handleMessage);
}; // };
}, []); // }, []);
useLayoutEffect(() => { useLayoutEffect(() => {
if (navigator?.clearAppBadge) { if (navigator?.clearAppBadge) {