-
Notifications
You must be signed in to change notification settings - Fork 20
/
Targets.service.tsx
81 lines (76 loc) · 2.92 KB
/
Targets.service.tsx
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
/*
* Copyright The Cryostat Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import _ from 'lodash';
import { Observable, BehaviorSubject, of, EMPTY } from 'rxjs';
import { catchError, concatMap, first, map, tap } from 'rxjs/operators';
import { ApiService } from './Api.service';
import { Target, NotificationCategory, TargetDiscoveryEvent } from './api.types';
import { LoginService } from './Login.service';
import { NotificationChannel } from './NotificationChannel.service';
import { NotificationService } from './Notifications.service';
import { SessionState } from './service.types';
export class TargetsService {
private readonly _targets$: BehaviorSubject<Target[]> = new BehaviorSubject<Target[]>([]);
constructor(
private readonly api: ApiService,
private readonly notifications: NotificationService,
login: LoginService,
notificationChannel: NotificationChannel,
) {
login
.getSessionState()
.pipe(concatMap((sessionState) => (sessionState === SessionState.USER_SESSION ? this.queryForTargets() : EMPTY)))
.subscribe(() => {
// just trigger a startup query
});
notificationChannel.messages(NotificationCategory.TargetJvmDiscovery).subscribe((v) => {
const evt: TargetDiscoveryEvent = v.message.event;
switch (evt.kind) {
case 'FOUND':
this._targets$.next(_.unionBy(this._targets$.getValue(), [evt.serviceRef], (t) => t.connectUrl));
break;
case 'LOST':
this._targets$.next(_.filter(this._targets$.getValue(), (t) => t.connectUrl !== evt.serviceRef.connectUrl));
break;
case 'MODIFIED':
{
const idx = _.findIndex(this._targets$.getValue(), (t) => t.connectUrl === evt.serviceRef.connectUrl);
if (idx >= 0) {
this._targets$.getValue().splice(idx, 1, evt.serviceRef);
this._targets$.next([...this._targets$.getValue()]);
}
}
break;
default:
break;
}
});
}
queryForTargets(): Observable<void> {
return this.api.doGet<Target[]>(`targets`).pipe(
first(),
tap((targets) => this._targets$.next(targets)),
map(() => undefined),
catchError((err) => {
this.notifications.danger('Target List Update Failed', JSON.stringify(err));
return of(undefined);
}),
);
}
targets(): Observable<Target[]> {
return this._targets$.asObservable();
}
}