forked from HowProgrammingWorks/ReactiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4-observer.js
More file actions
48 lines (40 loc) · 836 Bytes
/
4-observer.js
File metadata and controls
48 lines (40 loc) · 836 Bytes
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
'use strict';
const { EventEmitter } = require('events');
const { max } = Math;
const ee = new EventEmitter();
const cities = [];
const variables = {
maxDensity: 0,
count: 0,
};
ee.on('city', city => {
variables.count++;
variables.maxDensity = max(variables.maxDensity, city.density);
cities.push(city);
cities.forEach(city => {
city.relative = Math.round(city.density * 100 / variables.maxDensity);
});
console.table(variables);
console.table(cities);
});
ee.emit('city', {
city: 'Shanghai',
population: 24256800,
area: 6340,
density: 3826,
country: 'China',
});
ee.emit('city', {
city: 'Beijing',
population: 21516000,
area: 16411,
density: 1311,
country: 'China',
});
ee.emit('city', {
city: 'Delhi',
population: 16787941,
area: 1484,
density: 11313,
country: 'India',
});