hyper/app/notifications.ts

39 lines
1.1 KiB
TypeScript
Raw Normal View History

2019-11-28 05:17:01 -09:00
import ms from 'ms';
import fetch from 'electron-fetch';
2020-01-02 05:44:11 -09:00
import {version} from './package.json';
import {BrowserWindow} from 'electron';
const NEWS_URL = 'https://hyper-news.now.sh';
2020-01-02 05:44:11 -09:00
export default function fetchNotifications(win: BrowserWindow) {
const {rpc} = win;
2020-06-19 04:51:34 -08:00
const retry = (err?: Error) => {
setTimeout(() => fetchNotifications(win), ms('30m'));
if (err) {
console.error('Notification messages fetch error', err.stack);
}
};
console.log('Checking for notification messages');
fetch(NEWS_URL, {
headers: {
'X-Hyper-Version': version,
'X-Hyper-Platform': process.platform
}
})
2020-03-25 02:15:08 -08:00
.then((res) => res.json())
.then((data) => {
2023-06-26 01:23:37 -08:00
const message: {text: string; url: string; dismissable: boolean} | '' = data.message || '';
if (typeof message !== 'object' && message !== '') {
throw new Error('Bad response');
}
if (message === '') {
console.log('No matching notification messages');
} else {
rpc.emit('add notification', message);
}
retry();
})
.catch(retry);
2019-11-28 05:17:01 -09:00
}