-
Notifications
You must be signed in to change notification settings - Fork 345
/
Cache.ts
106 lines (95 loc) 路 2.5 KB
/
Cache.ts
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
/**
* Copyright (c) 2017 ~ present NAVER Corp.
* billboard.js project is licensed under the MIT license
*/
import {isValue, toArray} from "./util";
import type {DataRow} from "../../types/types";
/**
* Constant for cache key
* - NOTE: Prefixed with '$', will be resetted when .load() is called
* @private
*/
export const KEY = {
bubbleBaseLength: "$baseLength",
colorPattern: "__colorPattern__",
dataMinMax: "$dataMinMax",
dataTotalSum: "$dataTotalSum",
dataTotalPerIndex: "$totalPerIndex",
legendItemTextBox: "legendItemTextBox",
radarPoints: "$radarPoints",
setOverOut: "setOverOut",
callOverOutForTouch: "callOverOutForTouch",
textRect: "textRect"
};
export default class Cache {
private cache = {};
/**
* Add cache
* @param {string} key Cache key
* @param {*} value Value to be stored
* @param {boolean} isDataType Weather the cache is data typed '{id:'data', id_org: 'data', values: [{x:0, index:0,...}, ...]}'
* @returns {*} Added data value
* @private
*/
add(key: string, value, isDataType = false) {
this.cache[key] = isDataType ? this.cloneTarget(value) : value;
return this.cache[key];
}
/**
* Remove cache
* @param {string|Array} key Cache key
* @private
*/
remove(key: string | string[]) {
toArray(key).forEach(v => delete this.cache[v]);
}
/**
* Get cahce
* @param {string|Array} key Cache key
* @param {boolean} isDataType Weather the cache is data typed '{id:'data', id_org: 'data', values: [{x:0, index:0,...}, ...]}'
* @returns {*}
* @private
*/
get(key: string, isDataType = false): any | null {
if (isDataType) {
const targets: any[] = [];
for (let i = 0, id; (id = key[i]); i++) {
if (id in this.cache) {
targets.push(this.cloneTarget(this.cache[id]));
}
}
return targets;
} else {
const value = this.cache[key];
return isValue(value) ? value : null;
}
}
/**
* Reset cached data
* @param {boolean} all true: reset all data, false: reset only '$' prefixed key data
* @private
*/
reset(all?: boolean): void {
const $$ = this;
for (const x in $$.cache) {
// reset the prefixed '$' key(which is internal use data) only.
if (all || /^\$/.test(x)) {
$$.cache[x] = null;
}
}
}
/**
* Clone data target object
* @param {object} target Data object
* @returns {object}
* @private
*/
// eslint-disable-next-line camelcase
cloneTarget(target: DataRow): DataRow {
return {
id: target.id,
id_org: target.id_org,
values: target.values.map(d => ({x: d.x, value: d.value, id: d.id}))
};
}
}