-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.js
57 lines (47 loc) · 1.21 KB
/
state.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
const hackrf = require('hackrf');
const initialState = () => ({
radio: {
isConnected: false
}
});
let device = null;
let stateObservers = [];
let state = initialState();
function changeState() {
let args = Array.prototype.slice.call(arguments);
let keys = args;
let value = args.pop();
let set = (keys, obj, val) => {
if (keys.length === 1) {
obj[keys[0]] = val;
return true;
} else {
return set(keys.slice(1), obj[keys[0]], val);
}
}
set(keys, state, value);
stateObservers.forEach(o => o(state));
}
function observe(callback) {
stateObservers.push(callback);
callback(state); // initial call
}
function statusLoop() {
if (!device) {
let hrf = hackrf();
if (hrf.length === 0) {
changeState('radio', 'isConnected', false);
} else if (!state.radio.isConnected) {
changeState('radio', 'isConnected', true);
device = hrf.open(0);
changeState('radio', 'version', device.getVersion());
}
}
setTimeout(statusLoop, 500);
}
statusLoop();
//console.log(hrf);
//window.radio = hrf.open(0);
module.exports = {
observe: observe
};