|
| 1 | +/* |
| 2 | + - Copyright 2022 Sven Loesekann |
| 3 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + you may not use this file except in compliance with the License. |
| 5 | + You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + Unless required by applicable law or agreed to in writing, software |
| 8 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + See the License for the specific language governing permissions and |
| 11 | + limitations under the License. |
| 12 | +*/ |
| 13 | +/// <reference lib="webworker" /> |
| 14 | +/* eslint-disable-next-line no-restricted-globals */ |
| 15 | +declare var self: DedicatedWorkerGlobalScope; |
| 16 | +export { }; |
| 17 | + |
| 18 | +interface MsgData { |
| 19 | + jwtToken: string; |
| 20 | + newNotificationUrl: string; |
| 21 | +} |
| 22 | + |
| 23 | +interface UserResponse { |
| 24 | + Token?: string |
| 25 | + Message?: string |
| 26 | +} |
| 27 | + |
| 28 | +let jwtToken = ''; |
| 29 | +let tokenIntervalRef: ReturnType<typeof setInterval>; |
| 30 | +const refreshToken = (myToken: string) => { |
| 31 | + if (!!tokenIntervalRef) { |
| 32 | + clearInterval(tokenIntervalRef); |
| 33 | + } |
| 34 | + jwtToken = myToken; |
| 35 | + if (!!jwtToken && jwtToken.length > 10) { |
| 36 | + tokenIntervalRef = setInterval(() => { |
| 37 | + const requestOptions = { |
| 38 | + method: 'GET', |
| 39 | + headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${jwtToken}` }, |
| 40 | + }; |
| 41 | + fetch('/appuser/refreshtoken', requestOptions).then(response => response.json() as UserResponse).then(result => { |
| 42 | + if ((!result.Message && !!result.Token && result.Token.length > 10)) { |
| 43 | + //console.log('Token refreshed.'); |
| 44 | + jwtToken = result.Token; |
| 45 | + /* eslint-disable-next-line no-restricted-globals */ |
| 46 | + self.postMessage(result); |
| 47 | + } else { |
| 48 | + jwtToken = ''; |
| 49 | + clearInterval(tokenIntervalRef); |
| 50 | + } |
| 51 | + }); |
| 52 | + }, 45000); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +let firstNotRequest = true; |
| 57 | +let notificationIntervalRef: ReturnType<typeof setInterval>; |
| 58 | +/* eslint-disable-next-line no-restricted-globals */ |
| 59 | +self.addEventListener('message', (event: MessageEvent) => { |
| 60 | + const msgData = event.data as MsgData; |
| 61 | + refreshToken(msgData.jwtToken); |
| 62 | + if (!!notificationIntervalRef) { |
| 63 | + clearInterval(notificationIntervalRef); |
| 64 | + } |
| 65 | + notificationIntervalRef = setInterval(() => { |
| 66 | + if (!jwtToken) { |
| 67 | + clearInterval(notificationIntervalRef); |
| 68 | + firstNotRequest = true; |
| 69 | + } |
| 70 | + const requestOptions = { |
| 71 | + method: 'GET', |
| 72 | + headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${jwtToken}` }, |
| 73 | + }; |
| 74 | + /* eslint-disable-next-line no-restricted-globals */ |
| 75 | + self.fetch(msgData.newNotificationUrl, requestOptions).then(result => result.json()).then(resultJson => { |
| 76 | + if (!!resultJson && resultJson?.length > 0) { |
| 77 | + /* eslint-disable-next-line no-restricted-globals */ |
| 78 | + self.postMessage(resultJson); |
| 79 | + //Notification |
| 80 | + //console.log(Notification.permission); |
| 81 | + if (Notification.permission === 'granted' && !firstNotRequest) { |
| 82 | + if(resultJson?.length > 0 && resultJson[0]?.Message?.length > 1 && resultJson[0]?.Title?.length > 1) { |
| 83 | + for(let value of resultJson) { |
| 84 | + new Notification(value?.Title, {body: value?.Message}); |
| 85 | + } |
| 86 | + } |
| 87 | + } else if(!!firstNotRequest) { |
| 88 | + firstNotRequest = false; |
| 89 | + } |
| 90 | + } |
| 91 | + }); |
| 92 | + }, 60000); |
| 93 | +}); |
0 commit comments