forked from nas5w/javascript-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.js
45 lines (38 loc) · 843 Bytes
/
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
class Repository {
getContributorById(id) {
const contributors = [
{
id: 1,
name: 'Sebastian Vipin',
url: 'https://github.com/svipin',
},
{
id: 2,
name: 'Johanna Kenzo',
url: 'https://github.com/jkenzo',
},
{
id: 3,
name: 'Von San Antonio',
url: 'https://github.com/vsanantonio',
}
];
return contributors.find(contributor => contributor.id === id);
}
}
class RepositoryProxy {
constructor() {
this.repository = new Repository();
this.cache = {};
}
getContributorById(id) {
if (!this.cache[id]) {
this.cache[id] = this.repository.getContributorById(id);
}
return this.cache[id];
}
getCount() {
return Object.keys(this.cache).length;
}
}
module.exports = RepositoryProxy;