UNPKG

1.46 kBPlain TextView Raw
1import {Autowired, Bean, PostConstruct} from "../context/context";
2import {GridOptionsWrapper} from "../gridOptionsWrapper";
3import {RowNode} from "../entities/rowNode";
4
5@Bean('valueCache')
6export class ValueCache {
7
8 @Autowired('gridOptionsWrapper') private gridOptionsWrapper: GridOptionsWrapper;
9
10 private cacheVersion = 0;
11 private active: boolean;
12 private neverExpires: boolean;
13
14 @PostConstruct
15 public init(): void {
16 this.active = this.gridOptionsWrapper.isValueCache();
17 this.neverExpires = this.gridOptionsWrapper.isValueCacheNeverExpires();
18 }
19
20 public onDataChanged(): void {
21 if (this.neverExpires) { return; }
22
23 this.expire();
24 }
25
26 public expire(): void {
27 this.cacheVersion++;
28 }
29
30 public setValue(rowNode: RowNode, colId: string, value: any): any {
31 if (this.active) {
32 if (rowNode.__cacheVersion !== this.cacheVersion) {
33 rowNode.__cacheVersion = this.cacheVersion;
34 rowNode.__cacheData = {};
35 }
36 rowNode.__cacheData[colId] = value;
37 }
38 }
39
40 public getValue(rowNode: RowNode, colId: string): any {
41 let valueInCache = this.active
42 && rowNode.__cacheVersion===this.cacheVersion
43 && rowNode.__cacheData[colId]!==undefined;
44 if (valueInCache) {
45 return rowNode.__cacheData[colId];
46 } else {
47 return undefined;
48 }
49 }
50
51}