react-ts5-remote-app-api
Version:
React hook/api for the TeamSpeak5 remote app feature
177 lines (176 loc) • 9.17 kB
JavaScript
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
/**
* Handles data received from TS5 client (list of connections, channels and clients)
* Updates the states of App.tsx
*/
var TS5DataHandler = /** @class */ (function () {
function TS5DataHandler(
// State setters for App.tsx
setConnections, setChannels, setClients, logger) {
this.setConnections = setConnections;
this.setChannels = setChannels;
this.setClients = setClients;
this.localConnections = [];
this.localChannels = [];
this.localClients = [];
this.logger = logger;
}
// Update App.tsx states
TS5DataHandler.prototype.updateConnectionsState = function () {
this.setConnections(__spreadArray([], this.localConnections, true));
};
TS5DataHandler.prototype.updateChannelsState = function () {
this.setChannels(__spreadArray([], this.localChannels, true));
};
TS5DataHandler.prototype.updateClientsState = function () {
this.setClients(__spreadArray([], this.localClients, true));
};
// Clear all data
TS5DataHandler.prototype.clearAll = function () {
this.localConnections = [];
this.localChannels = [];
this.localClients = [];
this.updateConnectionsState();
this.updateChannelsState();
this.updateClientsState();
};
// Add data to local lists and update states
TS5DataHandler.prototype.addConnection = function (connection) {
this.logger.log("Adding connection...", connection);
var existingConnection = this.localConnections.find(function (localConnection) { return localConnection.id === connection.id; });
if (existingConnection == undefined) {
this.localConnections.push(connection);
this.updateConnectionsState();
this.logger.log("Connection added");
}
else {
this.logger.log("Connection already exists");
}
};
TS5DataHandler.prototype.addChannel = function (channel) {
this.logger.log("Adding channel...", channel);
var existingChannel = this.localChannels.find(function (localChannel) { return localChannel.id === channel.id && localChannel.connection.id === channel.connection.id; });
if (existingChannel == undefined) {
this.localChannels.push(channel);
this.updateChannelsState();
this.logger.log("Channel added");
}
else {
this.logger.log("Channel already exists");
}
};
TS5DataHandler.prototype.addClient = function (client) {
this.logger.log("Adding client...", client);
var existingClient = this.localClients.find(function (localClient) { var _a, _b; return localClient.id === client.id && ((_a = localClient.channel) === null || _a === void 0 ? void 0 : _a.connection.id) === ((_b = client.channel) === null || _b === void 0 ? void 0 : _b.connection.id); });
if (existingClient == undefined) {
this.localClients.push(client);
this.updateClientsState();
this.logger.log("Client added");
}
else {
this.logger.log("Client already exists");
}
};
// Update data in local lists and update states
TS5DataHandler.prototype.updateConnection = function (connection) {
this.logger.log("Updating connection...", connection);
var existingConnection = this.localConnections.find(function (localConnection) { return localConnection.id === connection.id; });
if (existingConnection !== undefined) {
this.localConnections[this.localConnections.indexOf(existingConnection)] = connection;
this.updateConnectionsState();
this.logger.log("Connection updated");
}
else {
this.logger.log("Connection does not exist");
}
};
TS5DataHandler.prototype.updateChannel = function (channel) {
this.logger.log("Updating channel...", channel);
var existingChannel = this.localChannels.find(function (localChannel) { return localChannel.id === channel.id && localChannel.connection.id === channel.connection.id; });
if (existingChannel !== undefined) {
this.localChannels[this.localChannels.indexOf(existingChannel)] = channel;
this.updateChannelsState();
this.logger.log("Channel updated");
}
else {
this.logger.log("Channel does not exist");
}
};
TS5DataHandler.prototype.updateClient = function (client) {
this.logger.log("Updating client...", client);
var existingClient = this.localClients.find(function (localClient) { var _a, _b; return localClient.id === client.id && ((_a = localClient.channel) === null || _a === void 0 ? void 0 : _a.connection.id) === ((_b = client.channel) === null || _b === void 0 ? void 0 : _b.connection.id); });
if (existingClient !== undefined) {
this.localClients[this.localClients.indexOf(existingClient)] = client;
this.updateClientsState();
this.logger.log("Client updated");
}
else {
this.logger.log("Client does not exist");
}
};
// Remove data from local lists and update states
TS5DataHandler.prototype.removeConnection = function (connection) {
this.logger.log("Removing connection...", connection);
var existingConnection = this.localConnections.find(function (localConnection) { return localConnection.id === connection.id; });
if (existingConnection !== undefined) {
this.localConnections.splice(this.localConnections.indexOf(existingConnection), 1);
// Remove all channels and clients associated with the connection
this.localChannels = this.localChannels.filter(function (localChannel) { return localChannel.connection.id !== connection.id; });
this.localClients = this.localClients.filter(function (localClient) { var _a; return ((_a = localClient.channel) === null || _a === void 0 ? void 0 : _a.connection.id) !== connection.id; });
this.updateChannelsState();
this.updateClientsState();
this.updateConnectionsState();
this.logger.log("Connection removed");
}
else {
this.logger.log("Connection does not exist");
}
};
TS5DataHandler.prototype.removeChannel = function (channel) {
this.logger.log("Removing channel...", channel);
var existingChannel = this.localChannels.find(function (localChannel) { return localChannel.id === channel.id && localChannel.connection.id === channel.connection.id; });
if (existingChannel !== undefined) {
this.localChannels.splice(this.localChannels.indexOf(existingChannel), 1);
// Remove all clients associated with the channel
this.localClients = this.localClients.filter(function (localClient) { var _a; return ((_a = localClient.channel) === null || _a === void 0 ? void 0 : _a.id) !== channel.id; });
this.updateClientsState();
this.updateChannelsState();
this.logger.log("Channel removed");
}
else {
this.logger.log("Channel does not exist");
}
};
TS5DataHandler.prototype.removeClient = function (client) {
this.logger.log("Removing client...", client);
var existingClient = this.localClients.find(function (localClient) { var _a, _b; return localClient.id === client.id && ((_a = localClient.channel) === null || _a === void 0 ? void 0 : _a.connection.id) === ((_b = client.channel) === null || _b === void 0 ? void 0 : _b.connection.id); });
if (existingClient !== undefined) {
this.localClients.splice(this.localClients.indexOf(existingClient), 1);
this.updateClientsState();
this.logger.log("Client removed");
}
else {
this.logger.log("Client does not exist");
}
};
// Helper functions
TS5DataHandler.prototype.getConnectionById = function (id) {
return this.localConnections.find(function (connection) { return connection.id === id; });
};
TS5DataHandler.prototype.getChannelById = function (id, connectionId) {
return this.localChannels.find(function (channel) { var _a; return channel.id === id && ((_a = channel.connection) === null || _a === void 0 ? void 0 : _a.id) === connectionId; });
};
TS5DataHandler.prototype.getClientById = function (id, connectionId) {
return this.localClients.find(function (client) { var _a; return client.id === id && ((_a = client.channel) === null || _a === void 0 ? void 0 : _a.connection.id) === connectionId; });
};
return TS5DataHandler;
}());
export { TS5DataHandler };