| 6 | const NEWS_URL = 'https://hyper-news.now.sh'; |
| 7 | |
| 8 | export default function fetchNotifications(win: BrowserWindow) { |
| 9 | const {rpc} = win; |
| 10 | const retry = (err?: Error) => { |
| 11 | setTimeout(() => fetchNotifications(win), ms('30m')); |
| 12 | if (err) { |
| 13 | console.error('Notification messages fetch error', err.stack); |
| 14 | } |
| 15 | }; |
| 16 | console.log('Checking for notification messages'); |
| 17 | fetch(NEWS_URL, { |
| 18 | headers: { |
| 19 | 'X-Hyper-Version': version, |
| 20 | 'X-Hyper-Platform': process.platform |
| 21 | } |
| 22 | }) |
| 23 | .then((res) => res.json()) |
| 24 | .then((data) => { |
| 25 | const {message} = data || {}; |
| 26 | if (typeof message !== 'object' && message !== '') { |
| 27 | throw new Error('Bad response'); |
| 28 | } |
| 29 | if (message === '') { |
| 30 | console.log('No matching notification messages'); |
| 31 | } else { |
| 32 | rpc.emit('add notification', message); |
| 33 | } |
| 34 | |
| 35 | retry(); |
| 36 | }) |
| 37 | .catch(retry); |
| 38 | } |