hyper/app/notifications.js

41 lines
1.1 KiB
JavaScript
Raw Normal View History

2019-11-28 05:17:01 -09:00
import ms from 'ms';
import fetch from 'electron-fetch';
import {version} from './package';
const NEWS_URL = 'https://hyper-news.now.sh';
2019-11-28 05:17:01 -09:00
export default function fetchNotifications(win) {
const {rpc} = win;
const retry = err => {
setTimeout(() => fetchNotifications(win), ms('30m'));
if (err) {
//eslint-disable-next-line no-console
console.error('Notification messages fetch error', err.stack);
}
};
//eslint-disable-next-line no-console
console.log('Checking for notification messages');
fetch(NEWS_URL, {
headers: {
'X-Hyper-Version': version,
'X-Hyper-Platform': process.platform
}
})
.then(res => res.json())
.then(data => {
const {message} = data || {};
if (typeof message !== 'object' && message !== '') {
throw new Error('Bad response');
}
if (message === '') {
//eslint-disable-next-line no-console
console.log('No matching notification messages');
} else {
rpc.emit('add notification', message);
}
retry();
})
.catch(retry);
2019-11-28 05:17:01 -09:00
}