@@ -2,9 +2,12 @@ import { type ButtonConfig } from '../../config/ButtonConfig';
22import { getUUID } from '../../utils/Utils' ;
33import { ErrorCollection } from '../../utils/errors/ErrorCollection' ;
44import { ErrorLevel , MetaBindButtonError } from '../../utils/errors/MetaBindErrors' ;
5+ import { RefCounter } from '../../utils/RefCounter' ;
56
67export class ButtonManager {
7- buttons : Map < string , Map < string , ButtonConfig > > ;
8+ // filePath -> buttonId -> ref counter of buttonConfig
9+ buttons : Map < string , Map < string , RefCounter < ButtonConfig > > > ;
10+ // filePath -> buttonId -> listenerId -> callback
811 buttonLoadListeners : Map < string , Map < string , Map < string , ( config : ButtonConfig ) => void > > > ;
912 buttonTemplates : Map < string , ButtonConfig > ;
1013
@@ -89,34 +92,37 @@ export class ButtonManager {
8992 throw new Error ( `ButtonManager | button with id ${ buttonId } does not exist` ) ;
9093 }
9194
92- const fileButtonLoadListeners = this . buttonLoadListeners . get ( filePath ) ;
93- if ( ! fileButtonLoadListeners ) {
95+ const fileLoadListeners = this . buttonLoadListeners . get ( filePath ) ;
96+ if ( ! fileLoadListeners ) {
9497 return ;
9598 }
9699
97- const buttonLoadListeners = fileButtonLoadListeners . get ( buttonId ) ;
98- if ( ! buttonLoadListeners ) {
100+ const loadListeners = fileLoadListeners . get ( buttonId ) ;
101+ if ( ! loadListeners ) {
99102 return ;
100103 }
101104
102- for ( const [ _ , fileButtonLoadListener ] of buttonLoadListeners ) {
105+ for ( const [ _ , fileButtonLoadListener ] of loadListeners ) {
103106 fileButtonLoadListener ( config ) ;
104107 }
105108 }
106109
107110 private removeButtonLoadListener ( filePath : string , buttonId : string , listenerId : string ) : void {
108- const fileButtonLoadListeners = this . buttonLoadListeners . get ( filePath ) ;
109- if ( ! fileButtonLoadListeners ) {
111+ const fileLoadListeners = this . buttonLoadListeners . get ( filePath ) ;
112+ if ( ! fileLoadListeners ) {
110113 return ;
111114 }
112115
113- const buttonLoadListeners = fileButtonLoadListeners . get ( buttonId ) ;
114- if ( ! buttonLoadListeners ) {
116+ const loadListeners = fileLoadListeners . get ( buttonId ) ;
117+ if ( ! loadListeners ) {
115118 return ;
116119 }
117120
118- buttonLoadListeners . delete ( listenerId ) ;
119- if ( fileButtonLoadListeners . size === 0 ) {
121+ loadListeners . delete ( listenerId ) ;
122+ if ( loadListeners . size === 0 ) {
123+ fileLoadListeners . delete ( buttonId ) ;
124+ }
125+ if ( fileLoadListeners . size === 0 ) {
120126 this . buttonLoadListeners . delete ( filePath ) ;
121127 }
122128 }
@@ -137,33 +143,37 @@ export class ButtonManager {
137143 const fileButtons = this . buttons . get ( filePath ) ! ;
138144
139145 if ( fileButtons . has ( button . id ) ) {
140- throw new Error ( `ButtonManager | button with id "${ button . id } " already exists` ) ;
146+ if ( JSON . stringify ( fileButtons . get ( button . id ) ?. getValue ( ) ) === JSON . stringify ( button ) ) {
147+ fileButtons . get ( button . id ) ?. increment ( ) ;
148+ return ;
149+ } else {
150+ throw new Error ( `ButtonManager | button with id "${ button . id } " already exists` ) ;
151+ }
141152 }
142153
143- fileButtons . set ( button . id , button ) ;
154+ fileButtons . set ( button . id , new RefCounter ( button ) ) ;
144155 this . notifyButtonLoadListeners ( filePath , button . id ) ;
145156 }
146157
147- public getButtons ( filePath : string ) : Map < string , ButtonConfig > | undefined {
148- return this . buttons . get ( filePath ) ;
149- }
150-
151158 public getButton ( filePath : string , buttonId : string ) : ButtonConfig | undefined {
152159 if ( this . buttonTemplates . has ( buttonId ) ) {
153160 return this . buttonTemplates . get ( buttonId ) ;
154161 }
155162
156163 const fileButtons = this . buttons . get ( filePath ) ;
157164 if ( fileButtons ) {
158- return fileButtons . get ( buttonId ) ;
165+ return fileButtons . get ( buttonId ) ?. getValue ( ) ;
159166 }
160167 return undefined ;
161168 }
162169
163170 public removeButton ( filePath : string , buttonId : string ) : void {
164171 const fileButtons = this . buttons . get ( filePath ) ;
165172 if ( fileButtons ) {
166- fileButtons . delete ( buttonId ) ;
173+ fileButtons . get ( buttonId ) ?. decrement ( ) ;
174+ if ( fileButtons . get ( buttonId ) ?. isEmpty ( ) ) {
175+ fileButtons . delete ( buttonId ) ;
176+ }
167177
168178 if ( fileButtons . size === 0 ) {
169179 this . buttons . delete ( filePath ) ;
0 commit comments