-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDataStore.js
More file actions
executable file
·65 lines (53 loc) · 1.28 KB
/
DataStore.js
File metadata and controls
executable file
·65 lines (53 loc) · 1.28 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
'use strict';
const Store = require('electron-store');
class DataStore extends Store {
constructor(settings) {
super(settings);
this.name = settings.name;
// initialize with items or empty array
this.items = this.get(this.name) || [];
}
getall() {
return this.items;
}
clear() {
this.items = []
this.set(this.name, this.items);
}
saveitems() {
// save items to JSON file
this.set(this.name, this.items.slice(this.items.length-1001,this.items.length-1));
// returning 'this' allows method chaining
return this;
}
getitems(key) {
// set object's items to items in JSON file
return this.items.find((t) => t.hash == key);
}
additem(item) {
// merge the existing items with the new item
this.items.push(item);
return this.saveitems();
}
deleteitem(item) {
// filter out the target item
this.items = this.items.filter((t) => t !== item);
return this.saveitems();
}
}
var Singleton = (function () {
var instance;
function createInstance() {
var object = new DataStore({ name: 'Items Main' });
return object;
}
return {
getInstance: function () {
if (!instance) {
instance = createInstance();
}
return instance;
},
};
})();
module.exports = Singleton;