-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode.js
26 lines (23 loc) · 980 Bytes
/
node.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
const pubsubService = require('./src/services/pubsubService.js');
const dharmaService = require('./src/services/dharmaService.js');
let _orders = {};
pubsubService.subscribe(
(orderJson) => dharmaService
.parseJsonOrder(orderJson)
.then(order => {
_orders = Object.assign({ [order.hash]: order }, _orders);
console.log(`New order recieved: ${JSON.stringify(order)}`);
}),
() => Object.values(_orders)
);
setInterval(() => {
const ordersArray = Object.values(_orders);
const orderPromises = ordersArray.map(order => dharmaService.validateOrderAsync(order).catch(() => null));
Promise.all(orderPromises)
.then(orders => orders.filter(x => x))
.then(orders => orders.reduce((prev, curr) => Object.assign({ [curr.hash]: curr }, prev), {}))
.then(orders => {
console.log(`Filtered orders length: ${Object.keys(orders).length}`)
_orders = orders;
});
}, 3000)