1
1
import { DataSource } from '@angular/cdk/collections' ;
2
2
import { MatPaginator , MatSort } from '@angular/material' ;
3
3
import { Observable , BehaviorSubject , Subscription } from 'rxjs' ;
4
- import { SocketService } from 'src/app/api/api/socket.service' ;
5
4
import { Instance } from 'src/app/model/models/instance' ;
6
- import { DatePipe } from '@angular/common' ;
7
- import { EventTypeEnum } from 'src/app/model/models/socketMessage' ;
8
5
import { StoreService } from 'src/app/model/store.service' ;
6
+ import { EventService } from 'src/app/model/event.service' ;
9
7
10
8
export interface InfoCenterItem {
11
9
instanceId : number ;
@@ -23,16 +21,15 @@ export interface InfoCenterItem {
23
21
export class InfoCenterDataSource extends DataSource < InfoCenterItem > {
24
22
private infoCenterSubject : BehaviorSubject < InfoCenterItem [ ] > ;
25
23
26
- private instanceAddedSubscription : Subscription ;
27
- private instanceChangedSubscription : Subscription ;
28
- private instanceRemovedSubscription : Subscription ;
24
+ private eventSubscription : Subscription ;
29
25
private data : InfoCenterItem [ ] ;
30
26
public numberEvents = 0 ;
31
27
private instance : Instance ;
32
28
33
- constructor ( private storeService : StoreService , private socketService : SocketService ,
34
- private paginator : MatPaginator , private sort : MatSort , private compType : string , private instanceId : string ) {
29
+ constructor ( private storeService : StoreService , private paginator : MatPaginator ,
30
+ private sort : MatSort , private compType : string , private instanceId : string , private eventService : EventService ) {
35
31
super ( ) ;
32
+
36
33
this . data = [ ] ;
37
34
if ( this . instanceId ) {
38
35
this . instance = this . storeService . getState ( ) . instances [ this . instanceId ] ;
@@ -44,36 +41,17 @@ export class InfoCenterDataSource extends DataSource<InfoCenterItem> {
44
41
45
42
this . paginator . page . subscribe ( ( ) => { this . infoCenterSubject . next ( this . getPagedData ( ) ) ; } ) ;
46
43
this . sort . sortChange . subscribe ( ( ) => { this . data = this . getSortedData ( this . data ) ; this . infoCenterSubject . next ( this . getPagedData ( ) ) ; } ) ;
47
-
48
- this . instanceAddedSubscription = this . socketService . subscribeForEvent < Instance > ( EventTypeEnum . InstanceAddedEvent ) .
49
- subscribe ( ( newInstance : Instance ) => {
50
- if ( this . applyFilter ( newInstance ) ) {
51
- const newEntry = this . transformEventToNotificaton ( newInstance , 'new Instance added' , 'add_circle' ) ;
52
- this . applyUpdate ( newEntry ) ;
53
- }
54
- } ) ;
55
-
56
- this . instanceRemovedSubscription = this . socketService . subscribeForEvent < Instance > ( EventTypeEnum . InstanceRemovedEvent ) .
57
- subscribe ( ( removeInstance : Instance ) => {
58
- if ( this . applyFilter ( removeInstance ) ) {
59
- const newEntry = this . transformEventToNotificaton ( removeInstance , 'Instance removed' , 'delete_sweep' ) ;
60
- this . applyUpdate ( newEntry ) ;
61
- }
62
- } ) ;
63
-
64
- this . instanceChangedSubscription = this . socketService . subscribeForEvent < Instance > ( EventTypeEnum . StateChangedEvent ) .
65
- subscribe ( ( changeInstance : Instance ) => {
66
- if ( this . applyFilter ( changeInstance ) ) {
67
- const newEntry = this . transformEventToNotificaton ( changeInstance , 'Instance changed' , 'link' ) ;
68
- this . applyUpdate ( newEntry ) ;
69
- }
70
- } ) ;
44
+ this . paginator . initialized . subscribe ( ( ) => { this . infoCenterSubject . next ( this . getPagedData ( ) ) ; } ) ;
45
+ this . eventSubscription = this . eventService . getEventObservable ( ) . subscribe ( ( newNotifs : InfoCenterItem [ ] ) => {
46
+ this . applyUpdate ( newNotifs ) ;
47
+ } ) ;
71
48
}
72
49
73
- applyFilter ( instance : Instance ) : boolean {
50
+ applyFilter ( notifItem : InfoCenterItem ) : boolean {
74
51
if ( ! this . instanceId && ! this . compType ) {
75
52
return true ;
76
53
} else {
54
+ const instance = this . storeService . getState ( ) . instances [ notifItem . instanceId ] ;
77
55
if ( this . instanceId ) {
78
56
return instance . id === this . instance . id ;
79
57
} else {
@@ -91,8 +69,11 @@ export class InfoCenterDataSource extends DataSource<InfoCenterItem> {
91
69
return this . infoCenterSubject . asObservable ( ) ;
92
70
}
93
71
94
- private applyUpdate ( newEntry : InfoCenterItem ) {
95
- this . data = this . getSortedData ( [ newEntry , ...this . data ] ) ;
72
+ private applyUpdate ( newEntry : InfoCenterItem [ ] ) {
73
+
74
+ const filteredEntries = newEntry . filter ( ( entry => this . applyFilter ( entry ) ) ) ;
75
+
76
+ this . data = this . getSortedData ( [ ...filteredEntries ] ) ;
96
77
this . numberEvents = this . data . length ;
97
78
this . infoCenterSubject . next ( this . getPagedData ( ) ) ;
98
79
}
@@ -102,9 +83,9 @@ export class InfoCenterDataSource extends DataSource<InfoCenterItem> {
102
83
* any open connections or free any held resources that were set up during connect.
103
84
*/
104
85
disconnect ( ) {
105
- this . instanceAddedSubscription . unsubscribe ( ) ;
106
- this . instanceChangedSubscription . unsubscribe ( ) ;
107
- this . instanceRemovedSubscription . unsubscribe ( ) ;
86
+ this . eventSubscription . unsubscribe ( ) ;
87
+ this . paginator . page . unsubscribe ( ) ;
88
+ this . sort . sortChange . unsubscribe ( ) ;
108
89
this . infoCenterSubject . complete ( ) ;
109
90
}
110
91
@@ -113,8 +94,12 @@ export class InfoCenterDataSource extends DataSource<InfoCenterItem> {
113
94
* this would be replaced by requesting the appropriate data from the server.
114
95
*/
115
96
private getPagedData ( ) {
116
- const startIndex = this . paginator . pageIndex * this . paginator . pageSize ;
117
- return [ ...this . data ] . splice ( startIndex , this . paginator . pageSize ) ;
97
+
98
+ if ( this . paginator && this . paginator . pageIndex !== undefined && this . paginator . pageSize !== undefined ) {
99
+ const startIndex = this . paginator . pageIndex * this . paginator . pageSize ;
100
+ return [ ...this . data ] . splice ( startIndex , this . paginator . pageSize ) ;
101
+ }
102
+ return [ ...this . data ] ;
118
103
}
119
104
120
105
/**
@@ -137,13 +122,6 @@ export class InfoCenterDataSource extends DataSource<InfoCenterItem> {
137
122
} ) ;
138
123
}
139
124
140
- private transformEventToNotificaton ( instance : Instance , notifName : string , type : string ) : InfoCenterItem {
141
- const datePipe = new DatePipe ( 'en-US' ) ;
142
- const actualDate = datePipe . transform ( Date . now ( ) , 'dd/MM/yyyy hh:mm:ss:SSS' ) ;
143
- return { instanceId : instance . id , type : type ,
144
- notifName : notifName , dateTime : actualDate , details : instance . name } ;
145
- }
146
-
147
125
}
148
126
149
127
/** Simple sort comparator for example ID/Name columns (for client-side sorting). */
0 commit comments