diff --git a/packages/shared/utils/pipe.ts b/packages/shared/utils/pipe.ts new file mode 100644 index 000000000..25d846609 --- /dev/null +++ b/packages/shared/utils/pipe.ts @@ -0,0 +1,15 @@ +/** + * pipe takes an array of unary functions as an argument. It returns a function that accepts a value + * that's going to be passed through the supplied functions. + * + * @example + * // Without pipe. + * const add1ThenDouble = (x) => double(add1(x)); + * + * // With pipe. + * const add1ThenDouble = pipe(add1, double); + */ +export const pipe = + (...fns: Array<(pipeSubject: PipeSubject) => PipeSubject>) => + (x: PipeSubject) => + fns.reduce((v, f) => f(v), x); diff --git a/packages/teleterm/src/ui/services/clusters/clustersService.ts b/packages/teleterm/src/ui/services/clusters/clustersService.ts index 8ccc41dbc..d82b868d6 100644 --- a/packages/teleterm/src/ui/services/clusters/clustersService.ts +++ b/packages/teleterm/src/ui/services/clusters/clustersService.ts @@ -5,6 +5,7 @@ import { makeLabelTag } from 'teleport/components/formatters'; import { Label } from 'teleport/types'; import { formatDatabaseInfo } from 'teleport/services/databases/makeDatabase'; import { DbProtocol, DbType } from 'teleport/services/databases'; +import { pipe } from 'shared/utils/pipe'; import { routing } from 'teleterm/ui/uri'; import { NotificationsService } from 'teleterm/ui/services/notifications'; @@ -713,16 +714,20 @@ export class ClustersService extends ImmutableStore { clusterUri ) : undefined; + const mergeAssumedRequests = (cluster: Cluster) => ({ + ...cluster, + loggedInUser: cluster.loggedInUser && { + ...cluster.loggedInUser, + assumedRequests, + }, + }); + const processCluster = pipe( + this.removeInternalLoginsFromCluster, + mergeAssumedRequests + ); + this.setState(draft => { - draft.clusters.set(clusterUri, { - ...this.removeInternalLoginsFromCluster(cluster), - loggedInUser: cluster.loggedInUser - ? { - ...cluster.loggedInUser, - assumedRequests, - } - : undefined, - }); + draft.clusters.set(clusterUri, processCluster(cluster)); }); }