This repository has been archived by the owner. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathactions.js
70 lines (57 loc) · 2.07 KB
/
actions.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
import { createConnection } from 'home-assistant-js-websocket';
import { getters as authGetters } from '../auth';
import { actions as syncActions } from '../sync';
import actionTypes from './action-types';
import handleRemoteEvent from './handle-remote-event';
import debounce from '../../util/debounce';
// maximum time we can go without receiving anything from the server
const MAX_INACTIVITY_TIME = 60000;
const EVENTS = ['state_changed', 'component_loaded', 'service_registered'];
const STREAMS = {};
function stopStream(reactor) {
const stream = STREAMS[reactor.hassId];
if (!stream) {
return;
}
stream.scheduleHealthCheck.clear();
stream.conn.close();
STREAMS[reactor.hassId] = false;
}
export function start(reactor, { syncOnInitialConnect = true } = {}) {
stopStream(reactor);
const authToken = reactor.evaluate(authGetters.authToken);
let url = document.location.protocol === 'https:' ? 'wss://' : 'ws://';
url += document.location.hostname;
if (document.location.port) {
url += `:${document.location.port}`;
}
url += '/api/websocket';
createConnection(url, { authToken }).then(
(conn) => {
// Websocket connection made for first time
const scheduleHealthCheck = debounce(() => conn.ping(), MAX_INACTIVITY_TIME);
scheduleHealthCheck();
conn.socket.addEventListener('message', scheduleHealthCheck);
STREAMS[reactor.hassId] = { conn, scheduleHealthCheck };
EVENTS.forEach(
eventType => conn.subscribeEvents(
handleRemoteEvent.bind(null, reactor), eventType));
reactor.batch(() => {
reactor.dispatch(actionTypes.STREAM_START);
if (syncOnInitialConnect) {
syncActions.fetchAll(reactor);
}
});
conn.addEventListener('disconnected', () => {
reactor.dispatch(actionTypes.STREAM_ERROR);
});
conn.addEventListener('ready', () => {
reactor.batch(() => {
reactor.dispatch(actionTypes.STREAM_START);
// We are streaming, fetch latest info
syncActions.fetchAll(reactor);
});
});
}
);
}