-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
79 lines (68 loc) · 2.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const electron = require('electron');
const React = require('react');
const ReactDOM = require('react-dom');
const {Store} = require('./lib/store');
const App = require('./components/app');
const store = new Store();
let lastUnplayedCount = 0;
let sessionAccount;
store.on('update', () => {
localStorage.data = JSON.stringify(store);
ReactDOM.render(React.createElement(App, {store}, null), document.getElementById('root'));
// Let the main process know about changes to the logged in account.
let newSessionAccount = store.session ? store.session.account : null;
if (newSessionAccount !== sessionAccount) {
sessionAccount = newSessionAccount;
electron.ipcRenderer.send('status', 'sessionAccount', sessionAccount);
}
// Show the app when there is new unplayed content.
// TODO: Only when autoplay is enabled?
const chunks = store.unplayedChunks;
if (chunks.length > lastUnplayedCount) {
electron.ipcRenderer.send('control', 'show');
} else if (chunks.length === 0 && !store.windowVisible) {
// Try to select another unplayed stream if current stream is played.
// Note that we only do this while window is hidden to avoid confusion.
for (let i = store.streams.length - 1; i >= 0; i--) {
const stream = store.streams[i];
if (store.getUnplayedChunksForStream(stream).length > 0) {
store.update({selectedStreamId: stream.id});
break;
}
}
}
lastUnplayedCount = chunks.length;
});
store.update('data' in localStorage ? JSON.parse(localStorage.data) : {});
if (!store.session) {
// The user is not logged in, so pop up the window.
electron.ipcRenderer.send('control', 'show');
}
// Set initial autoplay state.
if (!store.hasOwnProperty('autoplay')) store.update({autoplay: true});
electron.ipcRenderer.send('control', store.autoplay ? 'enableAutoplay' : 'disableAutoplay');
// Enable main thread to control the app state.
electron.ipcRenderer.on('control', (event, action) => {
switch (action) {
case 'logOut':
store.logOut();
break;
}
});
// Pass on app status updates to the UI.
electron.ipcRenderer.on('status', (event, status, value) => {
switch (status) {
case 'autoplay':
store.update({autoplay: value});
break;
case 'visible':
store.update({windowVisible: value});
break;
}
});
// Request streams every 30 seconds.
function requestStreams() {
if (store.session) store.getStreams();
}
requestStreams();
setInterval(requestStreams, 30000);