-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata-den-fetch-service.ts
More file actions
108 lines (97 loc) · 3.72 KB
/
data-den-fetch-service.ts
File metadata and controls
108 lines (97 loc) · 3.72 KB
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
import { Context } from '../../context';
import { DataDenEvent } from '../../data-den-event';
import { DataDenPubSub } from '../../data-den-pub-sub';
import { DataDenFetchDoneEvent } from './data-den-fetch-done.event.interface';
import { DataDenFetchOptions } from './data-den-fetch-options.interface';
import { DataDenDataLoaderStrategy } from './strategy/data-den-data-loader-strategy';
import { DataDenEventEmitter } from '../../data-den-event-emitter';
export class DataDenFetchService {
#loader: DataDenDataLoaderStrategy;
#fetchOptions: DataDenFetchOptions;
constructor(loader: DataDenDataLoaderStrategy, private PubSub: DataDenPubSub) {
this.#loader = loader;
this.#fetchOptions = {};
this.#subscribeFetchStart();
this.#subscribeSortingDone();
this.#subscribeQuickFilterChanged();
this.#subscribeFilterChange();
this.#subscribePaginationChange();
this.#subscribeGroupChange();
}
#getData(fetchOptions: DataDenFetchOptions): Promise<any[]> {
return this.#loader.getData(fetchOptions);
}
#publishFetchDone(context: Context, data: any[]): void {
const EventData: DataDenFetchDoneEvent = {
caller: this,
context,
rows: data,
};
this.PubSub.publish('info:fetch:done', EventData);
}
#subscribeFetchStart(): void {
this.PubSub.subscribe('command:fetch:start', (event: DataDenEvent) => {
this.#getData(this.#fetchOptions).then((data: any[]) => {
this.#publishFetchDone(event.context, data);
});
});
}
#subscribeSortingDone(): void {
this.PubSub.subscribe('command:fetch:sort-start', (event: DataDenEvent) => {
this.#fetchOptions.sortingOptions = {
field: event.data.field,
order: event.data.order,
comparator: event.data.comparator,
sortFn: event.data.sortFn,
activeSorters: event.data.activeSorters,
};
this.#getData(this.#fetchOptions).then((data: any[]) => {
this.#publishFetchDone(event.context, data);
DataDenEventEmitter.triggerEvent('sortingDone', this.#fetchOptions.sortingOptions);
});
});
}
#subscribeQuickFilterChanged(): void {
this.PubSub.subscribe('info:filtering:active-quick-filter-changed', (event: DataDenEvent) => {
this.#fetchOptions.quickFilterOptions = {
searchTerm: event.data.filter.searchTerm,
columns: event.data.filter.columns,
filterFn: event.data.filter.filterFn,
};
this.#getData(this.#fetchOptions).then((data: any[]) => {
this.#publishFetchDone(event.context, data);
});
});
}
#subscribeFilterChange(): void {
this.PubSub.subscribe('info:filtering:active-filters-changed', (event: DataDenEvent) => {
this.#fetchOptions.filtersOptions = {
filters: event.data.filters,
};
this.#getData(this.#fetchOptions).then((data: any[]) => {
this.#publishFetchDone(event.context, data);
});
});
}
#subscribePaginationChange(): void {
this.PubSub.subscribe('info:pagination:info-change:done', (event: DataDenEvent) => {
this.#fetchOptions.paginationOptions = {
firstRowIndex: event.data.firstRowIndex,
lastRowIndex: event.data.lastRowIndex,
};
this.#getData(this.#fetchOptions).then((data: any[]) => {
this.#publishFetchDone(event.context, data);
});
});
}
#subscribeGroupChange(): void {
this.PubSub.subscribe('command:group:update', (event: DataDenEvent) => {
this.#fetchOptions.groupedOptions = event.data.groupedColumns.length === 0 ? undefined : {
groupedColumns: event.data.groupedColumns,
};
this.#getData({ groupedOptions: this.#fetchOptions.groupedOptions }).then((data: any[]) => {
this.#publishFetchDone(event.context, data);
});
});
}
}