-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path11.1-pre-init-proxy.js
94 lines (70 loc) · 2.22 KB
/
11.1-pre-init-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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import EventEmitter from 'events';
function createProxyWithPreInitQueues(target, options) {
const { isInitProp, initEventName, augmentMethods } = options;
let queue = [];
const proxy = new Proxy(target, {
get: (target, property) => {
// if no need to queue or target initilized
if (!augmentMethods.includes(property) || target[isInitProp]) {
return target[property];
}
return (...args) => new Promise((resolve, reject) => {
console.log('Queued', property);
queue.push(() => target[property](...args).then(resolve, reject));
})
}
});
target.on(initEventName, async () => {
console.log(`${initEventName} event fired. Executing queued operations....`);
for (const op of queue) {
await op();
}
console.log('Done executing queued operations');
queue = [];
});
return proxy;
}
class DB extends EventEmitter {
connect(url) {
return new Promise((resolve, reject) => {
setTimeout(() => {
this.emit('connect');
this.connected = true;
resolve();
}, 2000);
})
}
disconnect() {
this.connected = false;
}
async find(filter, select) {
return Promise.resolve({ filter, select });
}
async findById(id, select) {
return Promise.resolve({ id, select });
}
async updateOne(id, doc) {
return Promise.resolve({ id, doc });
}
async checkStatus() {
return Promise.resolve(this.connected ? 'CONNECTED' : 'NOT CONNECTED');
}
}
// TESTING
const db = new DB();
const dbPreInitQueue = createProxyWithPreInitQueues(
db,
{
isInitProp: 'connected',
initEventName: 'connect',
augmentMethods: ['find', 'findById', 'updateOne'],
});
dbPreInitQueue.find({ name: 1 }, { _id: 0 }).then(console.log);
dbPreInitQueue.findById('ID2', { _id: 0 }).then(console.log);
dbPreInitQueue.find({ name: 3 }, { _id: 0 }).then(console.log);
dbPreInitQueue.updateOne('ID4', { prop: 0 }).then(console.log);
dbPreInitQueue.checkStatus().then(console.log);
await dbPreInitQueue.connect('url');
dbPreInitQueue.find({ name: 6 }, { _id: 2 }).then(console.log);
dbPreInitQueue.updateOne('ID8', { prop: 3 }).then(console.log);
dbPreInitQueue.checkStatus().then(console.log);