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' ;
67import { ResourceModelBase } from 'api/src/resources/base' ;
8+ import { BranchDataItemWrapper } from './BranchDataItemWrapper' ;
79import { ResourceGroupsItem } from './ResourceGroupsItem' ;
810
911export 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}
0 commit comments