1 | import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
|
2 |
|
3 | var Cache = function () {
|
4 | function Cache() {
|
5 | _classCallCheck(this, Cache);
|
6 |
|
7 | this._root = null;
|
8 | this._store = new Map();
|
9 | }
|
10 |
|
11 | Cache.prototype.empty = function empty() {
|
12 | return this._store.size === 0;
|
13 | };
|
14 |
|
15 | Cache.prototype.has = function has(key) {
|
16 | return this._store.has(key);
|
17 | };
|
18 |
|
19 | Cache.prototype.get = function get(key, defaultValue) {
|
20 | var res = this.has(key) ? this._store.get(key) : this.root();
|
21 | return typeof res === 'undefined' || res === null ? defaultValue : res;
|
22 | };
|
23 |
|
24 | Cache.prototype.add = function add(key, value) {
|
25 | if (this.empty()) {
|
26 | this._root = key;
|
27 | }
|
28 | this._store.set(key, value);
|
29 | };
|
30 |
|
31 | Cache.prototype.update = function update(key, value) {
|
32 | if (this.has(key)) {
|
33 | this._store.set(key, value);
|
34 | }
|
35 | };
|
36 |
|
37 | Cache.prototype.remove = function remove(key) {
|
38 | this._store.delete(key);
|
39 |
|
40 | if (key === this._root) {
|
41 | var maps = this._store.keys();
|
42 |
|
43 | var nextkey = maps.next().value;
|
44 | this._root = nextkey;
|
45 | }
|
46 | };
|
47 |
|
48 | Cache.prototype.clear = function clear() {
|
49 | this._store.clear();
|
50 | };
|
51 |
|
52 | Cache.prototype.root = function root() {
|
53 | return this._store.get(this._root);
|
54 | };
|
55 |
|
56 | return Cache;
|
57 | }();
|
58 |
|
59 | export default Cache; |
\ | No newline at end of file |