mirror of https://github.com/vizality/vizality
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.1 KiB
55 lines
1.1 KiB
import { API } from '@vizality/entities';
|
|
|
|
export default class Connections extends API {
|
|
constructor () {
|
|
super();
|
|
this.connections = [];
|
|
this._module = 'API';
|
|
this._submodule = 'Connections';
|
|
}
|
|
|
|
get filter () {
|
|
return this.connections.filter.bind(this.connections);
|
|
}
|
|
|
|
stop () {
|
|
this.removeAllListeners();
|
|
delete vizality.api.connections;
|
|
}
|
|
|
|
get map () {
|
|
return this.connections.map.bind(this.connections);
|
|
}
|
|
|
|
get filter () {
|
|
return this.connections.filter.bind(this.connections);
|
|
}
|
|
|
|
registerConnection (connection) {
|
|
if (this.get(connection.type)) {
|
|
/* @todo: Use logger. */
|
|
throw new Error('This type of connection already exists!');
|
|
}
|
|
this.connections.push(connection);
|
|
}
|
|
|
|
unregisterConnection (type) {
|
|
this.connections = this.connections.filter(c => c.type !== type);
|
|
}
|
|
|
|
fetchAccounts (id) {
|
|
return Promise.all(
|
|
this.filter(c => c.enabled).map(c => c.fetchAccount(id))
|
|
);
|
|
}
|
|
|
|
get (type) {
|
|
const connections = {};
|
|
for (const element of this.connections) {
|
|
connections[element.type] = element;
|
|
}
|
|
|
|
return connections[type] || null;
|
|
}
|
|
}
|