mirror of
https://github.com/quine-global/hyper.git
synced 2026-01-15 21:28:40 -09:00
Move notification fetching to the main process (#804)
* Move notification fetching to the main process * Add npm run app
This commit is contained in:
parent
db53b08002
commit
2ffc1aee3d
6 changed files with 50 additions and 48 deletions
|
|
@ -15,6 +15,7 @@ const toElectronBackgroundColor = require('./utils/to-electron-background-color'
|
||||||
const createMenu = require('./menu');
|
const createMenu = require('./menu');
|
||||||
const createRPC = require('./rpc');
|
const createRPC = require('./rpc');
|
||||||
const notify = require('./notify');
|
const notify = require('./notify');
|
||||||
|
const fetchNotifications = require('./notifications');
|
||||||
|
|
||||||
app.commandLine.appendSwitch('js-flags', '--harmony');
|
app.commandLine.appendSwitch('js-flags', '--harmony');
|
||||||
|
|
||||||
|
|
@ -165,6 +166,7 @@ app.on('ready', () => installDevExtensions(isDev).then(() => {
|
||||||
(app.windowCallback || fn)(win);
|
(app.windowCallback || fn)(win);
|
||||||
delete (app.windowCallback);
|
delete (app.windowCallback);
|
||||||
|
|
||||||
|
fetchNotifications(win);
|
||||||
// auto updates
|
// auto updates
|
||||||
if (!isDev && process.platform !== 'linux') {
|
if (!isDev && process.platform !== 'linux') {
|
||||||
AutoUpdater(win);
|
AutoUpdater(win);
|
||||||
|
|
|
||||||
39
app/notifications.js
Normal file
39
app/notifications.js
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
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);
|
||||||
|
};
|
||||||
|
|
@ -21,6 +21,7 @@
|
||||||
"git-describe": "3.0.2",
|
"git-describe": "3.0.2",
|
||||||
"mkdirp": "0.5.1",
|
"mkdirp": "0.5.1",
|
||||||
"ms": "0.7.1",
|
"ms": "0.7.1",
|
||||||
|
"node-fetch": "1.6.3",
|
||||||
"shell-env": "0.2.0",
|
"shell-env": "0.2.0",
|
||||||
"uuid": "2.0.2"
|
"uuid": "2.0.2"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,3 @@
|
||||||
import ms from 'ms';
|
|
||||||
import {satisfies} from 'semver';
|
|
||||||
import {version} from '../../package';
|
|
||||||
import {
|
import {
|
||||||
NOTIFICATION_MESSAGE,
|
NOTIFICATION_MESSAGE,
|
||||||
NOTIFICATION_DISMISS
|
NOTIFICATION_DISMISS
|
||||||
|
|
@ -21,44 +18,3 @@ export function addNotificationMessage(text, url = null, dismissable = true) {
|
||||||
dismissable
|
dismissable
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function fetchNotifications() {
|
|
||||||
return dispatch => {
|
|
||||||
const retry = err => {
|
|
||||||
setTimeout(() => dispatch(fetchNotifications()), ms(err ? '10s' : '5m'));
|
|
||||||
if (err) {
|
|
||||||
console.error('Notification messages fetch error', err.stack);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
console.log('Checking for notification messages');
|
|
||||||
fetch('https://hyper-news.now.sh')
|
|
||||||
.then(res => res.json())
|
|
||||||
.then(data => {
|
|
||||||
const {messages} = data || {};
|
|
||||||
if (!messages) {
|
|
||||||
throw new Error('Bad response');
|
|
||||||
}
|
|
||||||
const message = messages.find(msg => {
|
|
||||||
return matchVersion(msg.versions);
|
|
||||||
});
|
|
||||||
if (message) {
|
|
||||||
dispatch(addNotificationMessage(
|
|
||||||
message.text,
|
|
||||||
message.url,
|
|
||||||
message.dismissable
|
|
||||||
));
|
|
||||||
} else {
|
|
||||||
console.log('No matching notification messages');
|
|
||||||
}
|
|
||||||
retry();
|
|
||||||
})
|
|
||||||
.catch(retry);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function matchVersion(versions) {
|
|
||||||
return versions.some(v => {
|
|
||||||
return v === '*' || satisfies(version, v);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -12,9 +12,9 @@ import * as uiActions from './actions/ui';
|
||||||
import * as updaterActions from './actions/updater';
|
import * as updaterActions from './actions/updater';
|
||||||
import * as sessionActions from './actions/sessions';
|
import * as sessionActions from './actions/sessions';
|
||||||
import * as termGroupActions from './actions/term-groups';
|
import * as termGroupActions from './actions/term-groups';
|
||||||
import {fetchNotifications} from './actions/notifications';
|
import {addNotificationMessage} from './actions/notifications';
|
||||||
import HyperTermContainer from './containers/hyperterm';
|
|
||||||
import {loadConfig, reloadConfig} from './actions/config';
|
import {loadConfig, reloadConfig} from './actions/config';
|
||||||
|
import HyperTermContainer from './containers/hyperterm';
|
||||||
import configureStore from './store/configure-store';
|
import configureStore from './store/configure-store';
|
||||||
|
|
||||||
// Disable pinch zoom
|
// Disable pinch zoom
|
||||||
|
|
@ -37,7 +37,6 @@ config.subscribe(() => {
|
||||||
// and subscribe to all user intents for example from menus
|
// and subscribe to all user intents for example from menus
|
||||||
rpc.on('ready', () => {
|
rpc.on('ready', () => {
|
||||||
store_.dispatch(init());
|
store_.dispatch(init());
|
||||||
store_.dispatch(fetchNotifications());
|
|
||||||
store_.dispatch(uiActions.setFontSmoothing());
|
store_.dispatch(uiActions.setFontSmoothing());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -125,6 +124,10 @@ rpc.on('move', () => {
|
||||||
store_.dispatch(uiActions.windowMove());
|
store_.dispatch(uiActions.windowMove());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
rpc.on('add notification', ({text, url, dismissable}) => {
|
||||||
|
store_.dispatch(addNotificationMessage(text, url, dismissable));
|
||||||
|
});
|
||||||
|
|
||||||
const app = render(
|
const app = render(
|
||||||
<Provider store={store_}>
|
<Provider store={store_}>
|
||||||
<HyperTermContainer/>
|
<HyperTermContainer/>
|
||||||
|
|
|
||||||
|
|
@ -100,7 +100,8 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "concurrently --kill-others --raw \"npm run dev\" \"electron app\"",
|
"start": "concurrently --kill-others --raw \"npm run dev\" \"npm run app\"",
|
||||||
|
"app": "electron app",
|
||||||
"dev": "webpack -w",
|
"dev": "webpack -w",
|
||||||
"build": "NODE_ENV=production webpack",
|
"build": "NODE_ENV=production webpack",
|
||||||
"test": "npm run dist && xo && ava",
|
"test": "npm run dist && xo && ava",
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue