Cleaning up some code in Async Server Tree. (#22732)

This commit is contained in:
Aasim Khan
2023-04-17 23:23:41 -07:00
committed by GitHub
parent 938a8bffbe
commit b7d24dcecd
2 changed files with 56 additions and 24 deletions

View File

@@ -157,6 +157,51 @@ export class ConnectionProfileGroup implements IConnectionProfileGroup {
});
}
/**
* Remove the given connections from the group.
*/
public removeConnections(connections: ConnectionProfile[]): void {
const connectionIdsToRemove = connections.map(conn => conn.id);
this._childConnections = this._childConnections.filter((conn) => { return !connectionIdsToRemove.includes(conn.id); });
}
/**
* Gets the matching connection from the group if it exists
*/
public getMatchingConnection(connection: ConnectionProfile): ConnectionProfile | undefined {
return this._childConnections.find((conn) => connection.matches(conn));
}
/**
* Adds the given connection to the group if it doesn't already exist, otherwise replaces the matching connection.
*/
public addOrReplaceConnection(connection: ConnectionProfile): void {
const matchingConnection = this.getMatchingConnection(connection);
connection.parent = this;
connection.groupId = this.id;
if (matchingConnection) {
this.replaceConnection(connection, matchingConnection.id);
} else {
this._childConnections.push(connection);
}
}
/**
* Replaces the connection with the given id with the given connection
* @param connection The connection to replace with
* @param oldConnectionId The id of the existing connection to replace
*/
public replaceConnection(connection: ConnectionProfile, oldConnectionId: string): void {
const oldConnectionIndex = this._childConnections.findIndex((conn) => conn.id === oldConnectionId);
if (oldConnectionIndex !== -1) {
this._childConnections[oldConnectionIndex] = connection;
connection.parent = this;
connection.groupId = this.id;
} else {
throw new Error(`Could not find connection with id ${oldConnectionId} in group ${this.name}`);
}
}
public getParent(): ConnectionProfileGroup | undefined {
return this.parent;
}