-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathproxy.js
40 lines (37 loc) · 820 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
class DatabaseBase {
query() {
}
}
class Database extends DatabaseBase {
query(query) {
return "response" + query;
}
}
// Cached DB obje
class CachedDatabase extends DatabaseBase {
constructor() {
super();
this.cacheQuery = {};
}
query(query) {
if(this.cacheQuery[query]) {
return this.cacheQuery[query];
}
this.cacheQuery[query] = this.getDatabase().query("quer1");
return this.cacheQuery[query];
}
// Lazy initiallization of heavy obejct
getDatabase() {
if(typeof this._database === 'object')
return this._database;
this._database = new Database();
return this._database;
}
}
/**
* CachedDatabase is proxy object for original db
* Lazy initialization
* Access control
*/
const db = new CachedDatabase();
console.log(db.query("query1"));