-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconnections_service.ts
77 lines (65 loc) · 2.21 KB
/
connections_service.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import { BehaviorSubject, Observable, from } from 'rxjs';
import { DataPublicPluginStart } from 'src/plugins/data/public';
import { API } from '../../../common';
import { Connection, ConnectionsServiceDeps } from '../../types';
export class ConnectionsService {
protected http!: ConnectionsServiceDeps['http'];
private uiService$ = new BehaviorSubject<DataPublicPluginStart['ui'] | undefined>(undefined);
private isDataSourceEnabled = false;
private isDataSourceEnabled$ = new BehaviorSubject<boolean>(this.isDataSourceEnabled);
private selectedConnection: Connection | undefined = undefined;
private selectedConnection$ = new BehaviorSubject<Connection | undefined>(
this.selectedConnection
);
constructor(deps: ConnectionsServiceDeps) {
deps.startServices.then(([coreStart, depsStart]) => {
this.http = deps.http;
this.uiService$.next(depsStart.data.ui);
this.setIsDataSourceEnabled$(depsStart.dataSource?.dataSourceEnabled || false);
});
}
getIsDataSourceEnabled = () => {
return this.isDataSourceEnabled;
};
setIsDataSourceEnabled$ = (isDataSourceEnabled: boolean) => {
this.isDataSourceEnabled = isDataSourceEnabled;
this.isDataSourceEnabled$.next(this.isDataSourceEnabled);
};
getIsDataSourceEnabled$ = () => {
return this.isDataSourceEnabled$.asObservable();
};
getUiService = () => {
return this.uiService$.asObservable();
};
getConnections = (): Observable<Connection[]> => {
return from(
this.http.fetch({
method: 'GET',
path: API.DATA_SOURCE.CONNECTIONS,
})
);
};
getConnectionById = (id: string): Observable<Connection> => {
const path = `${API.DATA_SOURCE.CONNECTIONS}/${id}`;
return from(
this.http.fetch({
method: 'GET',
path,
})
);
};
getSelectedConnection = () => {
return this.selectedConnection;
};
setSelectedConnection$ = (connection: Connection | undefined) => {
this.selectedConnection = connection;
this.selectedConnection$.next(this.selectedConnection);
};
getSelectedConnection$ = () => {
return this.selectedConnection$.asObservable();
};
}