-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStore.js
More file actions
67 lines (54 loc) · 1.56 KB
/
Store.js
File metadata and controls
67 lines (54 loc) · 1.56 KB
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
const User = require('./User.js')
const ObjectId = require('mongoose').Types.ObjectId
class Store {
constructor () {
this.users = []
this.promises = []
this.waitingIds = []
}
async getOne (id) {
if (!ObjectId.isValid(id)) {
throw new Error('id is not an objectid !!')
}
const index = this.waitingIds.indexOf(id.toString())
if (index >= 0) {
return this.promises[index]
}
for (let i = 0; i < this.users.length; i++) {
if (this.users[i]._id.toString() === id.toString()) {
return this.users[i]
}
}
const promise = User.findById(id)
.then((user) => {
this.users.push(user)
const index = this.waitingIds.indexOf(user._id.toString())
this.waitingIds.splice(index, 1)
this.promises.splice(index, 1)
return user
})
this.waitingIds.push(id.toString())
this.promises.push(promise)
return promise
}
async get (ids) {
if (!(ids instanceof Array)) {
throw new Error('ids is not an array')
}
for (let i = 0; i < ids.length; i++) {
if (!ObjectId.isValid(ids[i])) {
throw new Error(`index ${i} is not an objectid !!`)
}
}
const promise = User.find({_id: {$in: ids}})
.then((users) => {
this.users = this.users.concat(users)
return users
})
// If we wanted to to this completely, we should add the promise (ids.length) times
// to this.promises & this.waitingIds so that even if the operation is done with get,
// we stay consistent
return promise
}
}
module.exports = Store