-
Notifications
You must be signed in to change notification settings - Fork 3
/
RecentLibraries.js
41 lines (33 loc) · 1.29 KB
/
RecentLibraries.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
'use strict'
let RecentLibraries = function (context, workspace) {
this.context = context
this.workspace = workspace
this.key = 'recentLibraries'
this.libraries = this.context.globalState.get(this.key, [])
}
RecentLibraries.prototype.add = function (library) {
// Remove library if it already exists in array
for (let index in this.libraries) {
// Match found, remove it from the array
if (library.libraryName === this.libraries[index].libraryName && library.version === this.libraries[index].version) {
this.libraries.splice(index, 1)
break
}
}
// Add new library to the front of the array
this.libraries.unshift(library)
const config = this.workspace.getConfiguration('cdnjs')
// Limit to maxium number of recent libraries according to configuration
let max = config.get('maxRecentLibraries')
max = (Number.isInteger(max) === true && max >= 1) ? max : config.inspect('maxRecentLibraries').defaultValue
this.libraries = this.libraries.slice(0, max)
return this.context.globalState.update(this.key, this.libraries)
}
RecentLibraries.prototype.get = function () {
return this.libraries
}
RecentLibraries.prototype.clear = function () {
this.libraries = []
return this.context.globalState.update(this.key, this.libraries)
}
module.exports = RecentLibraries