-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcacheService.ts
54 lines (51 loc) · 1.6 KB
/
cacheService.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
import { Injectable, EventEmitter } from '@angular/core';
import { TableColumn } from '../../models/table-column';
@Injectable({
providedIn: 'root',
})
export class CacheCellService {
public dict: { [key: string]: { [key: string]: any } } = {};
constructor() {}
getCache(item: any, col: TableColumn<any>, fieldName: string) {
return new Promise<any>((resolve, rej) => {
let mySubscribe = () => {
let s = this.dict[col.field]?.[fieldId].pipe().subscribe((res: any) => {
s.unsubscribe();
this.dict[col.field][fieldId] = res;
resolve(this.dict?.[col.field]?.[fieldId]);
return;
});
return s;
};
let createEventEmitterAndSubscribe = () => {
this.dict[col.field][fieldId] = new EventEmitter();
let s = mySubscribe();
this.getFunc(item)?.then((res) => {
if (!!res) {
this.dict[col.field][fieldId].next(res);
} else {
s.unsubscribe();
resolve(null);
}
});
};
let fieldId = item?.[fieldName];
if (!fieldId) {
resolve(null);
return;
}
if (col.field && col.field in this.dict && fieldId in this.dict?.[col.field]) {
if (this.dict[col.field][fieldId] instanceof EventEmitter) {
mySubscribe();
} else {
resolve(this.dict?.[col.field]?.[fieldId]);
return;
}
} else {
if (col.field && !(col.field in this.dict)) this.dict[col.field] = {};
createEventEmitterAndSubscribe();
}
});
}
getFunc: (item: any) => Promise<any>;
}