@@ -11,6 +11,7 @@ import {
11
11
EditorEvent ,
12
12
} from 'mo/model' ;
13
13
import { searchById } from '../helper' ;
14
+ import { editor as monacoEditor , Uri } from 'mo/monaco' ;
14
15
15
16
export interface IEditorService extends Component < IEditor > {
16
17
/**
@@ -72,6 +73,13 @@ export class EditorService
72
73
this . state = container . resolve ( EditorModel ) ;
73
74
}
74
75
76
+ private disposeModel ( tabs : IEditorTab | IEditorTab [ ] ) {
77
+ const arr = Array . isArray ( tabs ) ? tabs : [ tabs ] ;
78
+ arr . forEach ( ( tab ) => {
79
+ monacoEditor . getModel ( Uri . parse ( tab . id ! ) ) ?. dispose ( ) ;
80
+ } ) ;
81
+ }
82
+
75
83
public setEntry ( component : React . ReactNode ) {
76
84
this . setState ( {
77
85
entry : component ,
@@ -126,6 +134,9 @@ export class EditorService
126
134
// so delete group and choose last or former group as current one
127
135
const activeGroup =
128
136
nextGroups [ groupIndex + 1 ] || nextGroups [ groupIndex - 1 ] ;
137
+
138
+ // the model of closed tab should be disposed after closing
139
+ this . disposeModel ( nextGroup . data ! [ tabIndex ] ) ;
129
140
nextGroups . splice ( groupIndex , 1 ) ;
130
141
131
142
this . setState ( {
@@ -144,6 +155,8 @@ export class EditorService
144
155
nextGroup . activeTab = nextTab ?. id ;
145
156
}
146
157
158
+ this . disposeModel ( nextGroup . data ! [ tabIndex ] ) ;
159
+
147
160
nextGroup . data ! . splice ( tabIndex , 1 ) ;
148
161
nextGroups [ groupIndex ] = nextGroup ;
149
162
@@ -164,6 +177,10 @@ export class EditorService
164
177
const nextTabData = nextGroup . data ! ;
165
178
166
179
const updateTabs = nextTabData ! . filter ( searchById ( tabId ) ) ;
180
+ // tab data is unlikely to be large enough to affect exec time, so we filter twice for maintainability
181
+ const removedTabs = nextTabData ! . filter ( ( item ) => item . id !== tabId ) ;
182
+
183
+ this . disposeModel ( removedTabs ) ;
167
184
168
185
this . updateGroup ( groupId , {
169
186
data : updateTabs ,
@@ -185,6 +202,9 @@ export class EditorService
185
202
if ( tabIndex <= - 1 ) return ;
186
203
187
204
const updateTabs = nextTabData ?. slice ( 0 , tabIndex + 1 ) ;
205
+ const removedTabs = nextTabData ?. slice ( tabIndex + 1 ) ;
206
+
207
+ removedTabs && this . disposeModel ( removedTabs ) ;
188
208
189
209
this . updateGroup ( groupId , {
190
210
data : updateTabs ,
@@ -206,6 +226,9 @@ export class EditorService
206
226
if ( tabIndex <= - 1 ) return ;
207
227
208
228
const updateTabs = nextTabData ?. slice ( tabIndex , nextTabData . length ) ;
229
+ const removedTabs = nextTabData ?. slice ( 0 , tabIndex ) ;
230
+
231
+ this . disposeModel ( removedTabs || [ ] ) ;
209
232
210
233
this . updateGroup ( groupId , {
211
234
data : updateTabs ,
@@ -318,6 +341,9 @@ export class EditorService
318
341
const nextGroups = [ ...groups ] ;
319
342
let nextCurrentGroup = current ;
320
343
344
+ // dispose all models in specific group
345
+ this . disposeModel ( nextGroups [ groupIndex ] . data || [ ] ) ;
346
+
321
347
nextGroups . splice ( groupIndex , 1 ) ;
322
348
323
349
if ( current && current . id === groupId ) {
0 commit comments