Skip to content

Commit 5ff09c7

Browse files
authored
types: fix makeChildrenNodes and childNodeLoader types #2097
* Make node types generic
1 parent 439103f commit 5ff09c7

File tree

19 files changed

+59
-77
lines changed

19 files changed

+59
-77
lines changed

src/apigateway/explorer/apiGatewayNodes.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,13 @@ export class ApiGatewayNode extends AWSTreeNodeBase {
3535

3636
return [...this.apiNodes.values()]
3737
},
38-
getErrorNode: async (error: Error, logID: number) =>
39-
new ErrorNode(this, error, logID),
38+
getErrorNode: async (error: Error, logID: number) => new ErrorNode(this, error, logID),
4039
getNoChildrenPlaceholderNode: async () =>
4140
new PlaceholderNode(
4241
this,
4342
localize('AWS.explorerNode.apigateway.noApis', '[No API Gateway REST APIs found]')
4443
),
45-
sort: (nodeA: RestApiNode, nodeB: RestApiNode) => nodeA.label!.localeCompare(nodeB.label!),
44+
sort: (nodeA, nodeB) => nodeA.label!.localeCompare(nodeB.label!),
4645
})
4746
}
4847

src/apprunner/explorer/apprunnerNode.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ export class AppRunnerNode extends AWSTreeNodeBase {
3939
this,
4040
localize('AWS.explorerNode.apprunner.noServices', '[No App Runner services found]')
4141
),
42-
sort: (nodeA: AppRunnerServiceNode, nodeB: AppRunnerServiceNode) =>
43-
nodeA.label!.localeCompare(nodeB.label!),
42+
sort: (nodeA, nodeB) => nodeA.label!.localeCompare(nodeB.label!),
4443
})
4544
}
4645

src/awsexplorer/awsExplorer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export class AwsExplorer implements vscode.TreeDataProvider<AWSTreeNodeBase>, Re
143143
throw error
144144
},
145145
getNoChildrenPlaceholderNode: async () => this.ROOT_NODE_ADD_REGION,
146-
sort: (nodeA: RegionNode, nodeB: RegionNode) => nodeA.regionName.localeCompare(nodeB.regionName),
146+
sort: (nodeA, nodeB) => nodeA.regionName.localeCompare(nodeB.regionName),
147147
})
148148
}
149149
}

src/awsexplorer/childNodeCache.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import { ChildNodePage } from './childNodeLoader'
1111
*
1212
* Allows for easier appending to the node's children and less error-prone (resetting) of the node's internal state.
1313
*/
14-
export class ChildNodeCache {
15-
private _children: AWSTreeNodeBase[] = []
14+
export class ChildNodeCache<T extends AWSTreeNodeBase = AWSTreeNodeBase> {
15+
private _children: T[] = []
1616
private _continuationToken: string | undefined = undefined
1717
private _isPristine: boolean = true
1818

@@ -22,7 +22,7 @@ export class ChildNodeCache {
2222
* Once this has been called, the cache is no longer considered pristine ({@link isPristine} will return false).
2323
* This is true even if the original state of the cache remains unchanged (all items appended are empty/undefined).
2424
*/
25-
public appendPage(page: ChildNodePage): void {
25+
public appendPage(page: ChildNodePage<T>): void {
2626
this._children = [...this._children, ...page.newChildren]
2727
this._continuationToken = page.newContinuationToken
2828
this._isPristine = false
@@ -31,7 +31,7 @@ export class ChildNodeCache {
3131
/**
3232
* The list of children nodes previously appended to the cache.
3333
*/
34-
public get children(): AWSTreeNodeBase[] {
34+
public get children(): T[] {
3535
return this._children
3636
}
3737

src/awsexplorer/childNodeLoader.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,34 @@ import * as AsyncLock from 'async-lock'
1111

1212
const LOCK_KEY = 'ChildNodeLoader'
1313

14-
export interface ChildNodePage {
15-
newChildren: AWSTreeNodeBase[]
14+
export interface ChildNodePage<T extends AWSTreeNodeBase = AWSTreeNodeBase> {
15+
newChildren: T[]
1616
newContinuationToken: string | undefined
1717
}
1818

1919
/**
2020
* Controls loading paginated children for LoadMore nodes.
2121
*/
22-
export class ChildNodeLoader {
23-
private readonly loadPage: (continuationToken: string | undefined) => Promise<ChildNodePage>
22+
export class ChildNodeLoader<T extends AWSTreeNodeBase = AWSTreeNodeBase> {
23+
private readonly loadPage: (continuationToken: string | undefined) => Promise<ChildNodePage<T>>
2424
private readonly moreResults: MoreResultsNode
2525
private readonly loadChildrenLock: AsyncLock
26-
private cache: ChildNodeCache
26+
private cache: ChildNodeCache<T>
2727

2828
public constructor(
2929
parent: LoadMoreNode,
30-
loadPage: (continuationToken: string | undefined) => Promise<ChildNodePage>
30+
loadPage: (continuationToken: string | undefined) => Promise<ChildNodePage<T>>
3131
) {
3232
this.loadPage = loadPage
3333
this.moreResults = new MoreResultsNode(parent)
3434
this.loadChildrenLock = new AsyncLock()
35-
this.cache = new ChildNodeCache()
35+
this.cache = new ChildNodeCache<T>()
3636
}
3737

3838
/**
3939
* Gets the initial or previously-loaded children.
4040
*/
41-
public async getChildren(): Promise<AWSTreeNodeBase[]> {
41+
public async getChildren(): Promise<(T | MoreResultsNode)[]> {
4242
await this.loadMoreChildrenIf(() => !this.initialChildrenLoaded())
4343
return this.getExistingChildren()
4444
}
@@ -74,7 +74,7 @@ export class ChildNodeLoader {
7474
return this.initialChildrenLoaded() && this.cache.continuationToken === undefined
7575
}
7676

77-
private getExistingChildren(): AWSTreeNodeBase[] {
77+
private getExistingChildren(): (T | MoreResultsNode)[] {
7878
if (this.cache.continuationToken !== undefined) {
7979
return [...this.cache.children, this.moreResults]
8080
}

src/cloudWatchLogs/explorer/cloudWatchLogsNode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export abstract class CloudWatchLogsBase extends AWSTreeNodeBase {
4545
getErrorNode: async (error: Error, logID: number) => new ErrorNode(this, error, logID),
4646
getNoChildrenPlaceholderNode: async () =>
4747
new PlaceholderNode(this, localize('AWS.explorerNode.cloudWatchLogs.placeholder', '[No Logs found]')),
48-
sort: (nodeA: LogGroupNode, nodeB: LogGroupNode) => nodeA.name.localeCompare(nodeB.name),
48+
sort: (nodeA, nodeB) => nodeA.name.localeCompare(nodeB.name),
4949
})
5050
}
5151

src/ecr/explorer/ecrNode.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,10 @@ export class EcrNode extends AWSTreeNodeBase {
3232

3333
return response.map(item => new EcrRepositoryNode(this, this.ecr, item))
3434
},
35-
getErrorNode: async (error: Error, logID: number) =>
36-
new ErrorNode(this, error, logID),
35+
getErrorNode: async (error: Error, logID: number) => new ErrorNode(this, error, logID),
3736
getNoChildrenPlaceholderNode: async () =>
3837
new PlaceholderNode(this, localize('AWS.explorerNode.ecr.noRepositories', '[No repositories found]')),
39-
sort: (item1: EcrRepositoryNode, item2: EcrRepositoryNode) =>
40-
item1.repository.repositoryName.localeCompare(item2.repository.repositoryName),
38+
sort: (item1, item2) => item1.repository.repositoryName.localeCompare(item2.repository.repositoryName),
4139
})
4240
}
4341

src/ecr/explorer/ecrRepositoryNode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class EcrRepositoryNode extends AWSTreeNodeBase implements AWSResourceNod
4545
getErrorNode: async (error: Error, logID: number) => new ErrorNode(this, error, logID),
4646
getNoChildrenPlaceholderNode: async () =>
4747
new PlaceholderNode(this, localize('AWS.explorerNode.ecr.noTags', '[No tags found]')),
48-
sort: (item1: EcrTagNode, item2: EcrTagNode) => item1.tag.localeCompare(item2.tag),
48+
sort: (item1, item2) => item1.tag.localeCompare(item2.tag),
4949
})
5050
}
5151

src/eventSchemas/explorer/registryItemNode.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,10 @@ export class RegistryItemNode extends AWSTreeNodeBase {
5050

5151
return [...this.schemaNodes.values()]
5252
},
53-
getErrorNode: async (error: Error, logID: number) =>
54-
new ErrorNode(this, error, logID),
53+
getErrorNode: async (error: Error, logID: number) => new ErrorNode(this, error, logID),
5554
getNoChildrenPlaceholderNode: async () =>
5655
new PlaceholderNode(this, localize('AWS.explorerNode.registry.noSchemas', '[No Registry Schemas]')),
57-
sort: (nodeA: SchemaItemNode, nodeB: SchemaItemNode) => nodeA.schemaName.localeCompare(nodeB.schemaName),
56+
sort: (nodeA, nodeB) => nodeA.schemaName.localeCompare(nodeB.schemaName),
5857
})
5958
}
6059

src/eventSchemas/explorer/schemasNode.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,10 @@ export class SchemasNode extends AWSTreeNodeBase {
3535

3636
return [...this.registryNodes.values()]
3737
},
38-
getErrorNode: async (error: Error, logID: number) =>
39-
new ErrorNode(this, error, logID),
38+
getErrorNode: async (error: Error, logID: number) => new ErrorNode(this, error, logID),
4039
getNoChildrenPlaceholderNode: async () =>
4140
new PlaceholderNode(this, localize('AWS.explorerNode.schemas.noRegistry', '[No Schema Registries]')),
42-
sort: (nodeA: RegistryItemNode, nodeB: RegistryItemNode) =>
43-
nodeA.registryName.localeCompare(nodeB.registryName),
41+
sort: (nodeA, nodeB) => nodeA.registryName.localeCompare(nodeB.registryName),
4442
})
4543
}
4644

0 commit comments

Comments
 (0)