mirror of
https://github.com/quine-global/hyper.git
synced 2026-01-13 04:28:41 -09:00
39 lines
1,007 B
JavaScript
39 lines
1,007 B
JavaScript
const ms = require('ms');
|
|
const fetch = require('node-fetch');
|
|
const {satisfies} = require('semver');
|
|
|
|
const {version} = './package';
|
|
|
|
const NEWS_URL = 'https://hyper-news.now.sh';
|
|
const matchVersion = versions => (
|
|
versions.some(v => v === '*' || satisfies(version, v))
|
|
);
|
|
|
|
module.exports = function fetchNotifications(win) {
|
|
const {rpc} = win;
|
|
const retry = err => {
|
|
setTimeout(() => fetchNotifications(win), ms(err ? '10s' : '5m'));
|
|
if (err) {
|
|
console.error('Notification messages fetch error', err.stack);
|
|
}
|
|
};
|
|
|
|
console.log('Checking for notification messages');
|
|
fetch(NEWS_URL)
|
|
.then(res => res.json())
|
|
.then(data => {
|
|
const {messages} = data || {};
|
|
if (!messages) {
|
|
throw new Error('Bad response');
|
|
}
|
|
const message = messages.find(msg => matchVersion(msg.versions));
|
|
if (message) {
|
|
rpc.emit('add notification', message);
|
|
} else {
|
|
console.log('No matching notification messages');
|
|
}
|
|
|
|
retry();
|
|
})
|
|
.catch(retry);
|
|
};
|