-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathindex.js
124 lines (105 loc) · 3.68 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
let currentPrice = {};
const formatCurrency = require('format-currency')
const axios = require('axios')
const _ = require('underscore')
let socket;
// HTTP API is used as the primary source of prices
const HTTP = async(data, Config) => {
const requests = data.map(async(x) => {
let url = `https://min-api.cryptocompare.com/data/price?fsym=${x.from}&tsyms=${x.to}&e=${x.exchange}`
let res = await axios.get(url)
let exchange = x.exchange
let exchangeFallback = false
if (res.data[x.to] == undefined) {
url = `https://min-api.cryptocompare.com/data/price?fsym=${x.from}&tsyms=${x.to}`
res = await axios.get(url)
exchangeFallback = 'CCCAGG'
}
let uniqueId = x.from + x.to + x.exchange
let from = x.from
let to = x.to
return {
uniqueId,
exchange,
exchangeFallback,
from,
to,
data: res.data
}
});
const responses = await axios.all(requests)
return responses.reduce((accum, res) => {
let prefix = Config.currencies.filter(x => x.label === res.to)[0].prefix
accum[res.uniqueId] = {
exchange: res.exchange,
exchangeFallback: res.exchangeFallback,
from: res.from,
to: res.to,
flag: 4,
price: res.data[res.to],
volume24h: 0,
prefix: prefix
}
return accum
}, {})
}
let refreshIntervalId;
let Socket = {
connect: (store, tray, getImage, Config, state) => {
socket = require('socket.io-client')('https://streamer.cryptocompare.com/');
let selectedCurrencies = store.get('preferences').currencies
let data = {}
let dataBkp = {}
let dataFallback = {}
let subscription = []
for (let i of selectedCurrencies) {
subscription.push(`2~${i.exchange}~${i.from}~${i.to}`)
}
socket.emit('SubAdd', {
subs: subscription
});
// throttle state updates to prevent performance degradation
let throttle = _.throttle(state,5000)
HTTP(selectedCurrencies, Config).then(result => {
dataBkp = result
throttle(Object.assign(dataBkp, data))
})
// remove previous interval if existing
if(refreshIntervalId){
clearInterval(refreshIntervalId);
}
// create new interval for pooling
refreshIntervalId = setInterval(() => {
HTTP(selectedCurrencies, Config).then(result => {
dataBkp = result
throttle(Object.assign(dataBkp, data))
})
}, 30000);
socket.on("m", message => {
let messageArray = message.split('~')
subscription.map(x => {
let xArray = x.split('~')
if (xArray[2] === messageArray[2] && xArray[3] === messageArray[3]) {
let prefix = Config.currencies.filter(x => x.label === messageArray[3])[0].prefix
let concatData = messageArray.concat(prefix)
if (concatData.length === 14) {
data[concatData[2] + concatData[3] + concatData[1]] = {
exchange: concatData[1],
from: concatData[2],
to: concatData[3],
flag: concatData[4],
price: concatData[5],
volume24h: concatData[10],
prefix: concatData[13]
}
}
}
})
throttle(Object.assign(dataBkp, data))
});
},
disconnect: () => {
socket.disconnect()
}
}
module.exports = Socket