-
Notifications
You must be signed in to change notification settings - Fork 0
/
storageContainers.js
118 lines (104 loc) · 2.89 KB
/
storageContainers.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import localForage from 'localforage';
export class List {
constructor(key) {
this._key = key;
}
push(item) {
return localForage.getItem(this._key).then(list => {
if (list === null) {
list = [];
}
list.push(item);
return localForage.setItem(this._key, list);
});
}
extend(extendList) {
return localForage.getItem(this._key).then(list => {
if (list === null) {
list = [];
}
return localForage.setItem(this._key, list.concat(extendList));
});
}
insert(item, position) {
return localForage.getItem(this._key).then(list => {
if (list === null) {
list = [];
}
list.splice(position, 0, item);
return localForage.setItem(this._key, list);
});
}
clear() {
return localForage.removeItem(this._key);
}
toArray() {
return localForage.getItem(this._key).then(list => {
if (list === null) {
list = [];
}
return Promise.resolve(list);
});
}
assign_array(array) {
return localForage.setItem(this._key, array);
}
}
export class Queue extends List {
pop() {
return localForage.getItem(this._key).then(queue => {
if (queue === null) {
return Promise.resolve(null);
}
let item = queue.shift();
return localForage.setItem(this._key, queue).then(_ => Promise.resolve(item));
});
}
peek() {
return localForage.getItem(this._key).then(queue => {
if (queue === null) {
return Promise.resolve(null);
}
return Promise.resolve(queue[0]);
});
}
}
export class Dictionary {
constructor(key) {
this._key = key;
}
set(key, value) {
return localForage.getItem(this._key).then(dict => {
if (dict === null) {
dict = {};
}
dict[key] = value;
return localForage.setItem(this._key, dict);
});
}
merge(otherDict) {
return localForage.getItem(this._key).then(dict => {
if (dict === null) {
dict = {};
}
Object.keys(otherDict).forEach(key => dict[key] = otherDict[key]);
return localForage.setItem(this._key, dict);
});
}
get(key) {
return localForage.getItem(this._key).then(dict => {
if (dict === null) {
dict = {};
}
return Promise.resolve(dict[key]);
});
}
toObject() {
return localForage.getItem(this._key).then(dict => {
if (dict === null) {
dict = {};
}
return Promise.resolve(dict);
});
}
}