-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.js
56 lines (49 loc) · 1.32 KB
/
proxy.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
// external API
function CryptoCurrencyApi() {
this.getValue = (coin) => {
console.log('calling external Api ...');
switch (coin) {
case 'bitcoin':
return 3500;
case 'litecoin':
return 500;
case 'ethereum':
return 1500;
case 'somecoin':
return 50;
}
};
}
let res;
// ====================OLD WAY========================================
const api = new CryptoCurrencyApi();
res = api.getValue('bitcoin');
// console.info(res);
// ====================NEW WAY========================================
// adding the proxy pattern
function CryptoCurrencyProxyPattern() {
this.api = new CryptoCurrencyApi();
this.cache = new Map();
this.getValue = (coin) => {
if (this.cache.get(coin)) return this.cache.get(coin);
else {
let value = api.getValue(coin);
this.cache.set(coin, value);
console.log('cache ... ', this.cache);
return value;
}
};
}
const proxy_api = new CryptoCurrencyProxyPattern();
[1, 2, 3, 4, 5, 6].forEach((element) => {
res = proxy_api.getValue('bitcoin');
console.log(res);
res = proxy_api.getValue('litecoin');
console.log(res);
res = proxy_api.getValue('ethereum');
console.log(res);
res = proxy_api.getValue('somecoin');
console.log(res);
res = proxy_api.getValue('bitcoin');
console.log(res);
});