Skip to content

Commit 68bc3b4

Browse files
Fix BranchDataItemCache (#678)
1 parent e6102d2 commit 68bc3b4

File tree

3 files changed

+37
-23
lines changed

3 files changed

+37
-23
lines changed

src/tree/BranchDataItemCache.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6+
import { isAzExtTreeItem } from '@microsoft/vscode-azext-utils';
67
import { ResourceModelBase } from 'api/src/resources/base';
8+
import { BranchDataItemWrapper } from './BranchDataItemWrapper';
79
import { ResourceGroupsItem } from './ResourceGroupsItem';
810

911
export class BranchDataItemCache {
@@ -12,8 +14,9 @@ export class BranchDataItemCache {
1214

1315
addBranchItem(branchItem: ResourceModelBase, item: ResourceGroupsItem): void {
1416
this.branchItemToResourceGroupsItemCache.set(branchItem, item);
15-
if (branchItem.id) {
16-
this.idToBranchItemCache.set(branchItem.id, branchItem);
17+
const id = this.getIdForBranchItem(branchItem);
18+
if (id) {
19+
this.idToBranchItemCache.set(id, branchItem);
1720
}
1821
}
1922

@@ -26,11 +29,30 @@ export class BranchDataItemCache {
2629
return this.branchItemToResourceGroupsItemCache.get(branchItem);
2730
}
2831

29-
getItemForId(id?: string): ResourceGroupsItem | undefined {
32+
getItemForBranchItemById(branchItem: ResourceModelBase): ResourceGroupsItem | undefined {
33+
const id = this.getIdForBranchItem(branchItem);
3034
if (!id) {
3135
return undefined;
3236
}
33-
const branchItem = this.idToBranchItemCache.get(id);
34-
return branchItem ? this.branchItemToResourceGroupsItemCache.get(branchItem) : undefined;
37+
const cachedBranchItem = this.idToBranchItemCache.get(id);
38+
return cachedBranchItem ? this.branchItemToResourceGroupsItemCache.get(cachedBranchItem) : undefined;
39+
}
40+
41+
createOrGetItem<T extends BranchDataItemWrapper>(branchItem: ResourceModelBase, createItem: () => T): T {
42+
const cachedItem = this.getItemForBranchItemById(branchItem) as T | undefined;
43+
if (cachedItem) {
44+
cachedItem.branchItem = branchItem;
45+
this.addBranchItem(branchItem, cachedItem);
46+
return cachedItem;
47+
}
48+
return createItem();
49+
}
50+
51+
private getIdForBranchItem(branchItem: ResourceModelBase): string | undefined {
52+
if (isAzExtTreeItem(branchItem)) {
53+
return branchItem.fullId;
54+
}
55+
56+
return branchItem.id;
3557
}
3658
}

src/tree/BranchDataItemWrapper.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,9 @@ export class BranchDataItemWrapper implements ResourceGroupsItem, Wrapper {
115115
export type BranchDataItemFactory = (branchItem: ResourceModelBase, branchDataProvider: BranchDataProvider<ResourceBase, ResourceModelBase>, options?: BranchDataItemOptions) => BranchDataItemWrapper;
116116

117117
export function createBranchDataItemFactory(itemCache: BranchDataItemCache): BranchDataItemFactory {
118-
return (branchItem, branchDataProvider, options) => {
119-
const cachedItem = itemCache.getItemForId(branchItem.id) as BranchDataItemWrapper | undefined;
120-
if (cachedItem) {
121-
cachedItem.branchItem = branchItem;
122-
itemCache.addBranchItem(branchItem, cachedItem);
123-
return cachedItem;
124-
}
125-
return new BranchDataItemWrapper(branchItem, branchDataProvider, itemCache, options);
126-
}
118+
return (branchItem, branchDataProvider, options) =>
119+
itemCache.createOrGetItem(
120+
branchItem,
121+
() => new BranchDataItemWrapper(branchItem, branchDataProvider, itemCache, options),
122+
)
127123
}

src/tree/azure/AzureResourceItem.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,9 @@ export class AzureResourceItem<T extends AzureResource> extends BranchDataItemWr
5454
export type ResourceItemFactory<T extends AzureResource> = (resource: T, branchItem: ResourceModelBase, branchDataProvider: BranchDataProvider<ResourceBase, ResourceModelBase>, parent?: ResourceGroupsItem, options?: BranchDataItemOptions) => AzureResourceItem<T>;
5555

5656
export function createResourceItemFactory<T extends AzureResource>(itemCache: BranchDataItemCache): ResourceItemFactory<T> {
57-
return (resource, branchItem, branchDataProvider, parent, options) => {
58-
const cachedItem = itemCache.getItemForId(branchItem.id) as AzureResourceItem<T> | undefined;
59-
if (cachedItem) {
60-
cachedItem.branchItem = branchItem;
61-
itemCache.addBranchItem(branchItem, cachedItem);
62-
return cachedItem;
63-
}
64-
return new AzureResourceItem(resource, branchItem, branchDataProvider, itemCache, parent, options);
65-
}
57+
return (resource, branchItem, branchDataProvider, parent, options) =>
58+
itemCache.createOrGetItem(
59+
branchItem,
60+
() => new AzureResourceItem(resource, branchItem, branchDataProvider, itemCache, parent, options)
61+
);
6662
}

0 commit comments

Comments
 (0)