This repository has been archived by the owner on Sep 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsockets.js
71 lines (58 loc) · 1.44 KB
/
sockets.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
var async = require('async');
module.exports = function(io ,db, stock) {
var stocks = io.of('/api/io');
function getStocks(cb) {
db.findStocks(function(err,data) {
var tasks = [];
data.map(function(d) {
tasks.push(function(callback) {
stock.fetchStock(d.name, function(stock) {
callback(null, stock);
});
});
});
async.parallel(tasks, function(err, data) {
if (err) return cb(err);
cb(null, data);
});
});
}
function addStock(name, cb) {
stock.fetchStock(name, function(data) {
if (data.name === name) {
db.addStock(name, function(err,d) {
if (err) return cb(err);
cb(null, d);
});
} else {
cb('not found');
}
});
}
stocks.on('connection', function(client) {
client.on('rm', function(name) {
db.removeStock(name, function(err) {
if (!err) {
getStocks(function(err, data) {
if (err) return;
stocks.emit('stocks', data);
});
}
});
});
client.on('add', function(name) {
addStock(name, function(err) {
if (!err) {
getStocks(function(err, data) {
if (err) return;
stocks.emit('stocks', data);
});
}
})
});
getStocks(function(err, data) {
if (err) return;
client.emit('stocks', data);
});
});
};