Skip to content

Commit 93b96c3

Browse files
attempt at memory cleanup
1 parent 3cc3dab commit 93b96c3

File tree

1 file changed

+49
-17
lines changed

1 file changed

+49
-17
lines changed

webview-ui/src/components/package-manager/PackageManagerViewStateManager.ts

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,21 @@ export class PackageManagerViewStateManager {
8686
}
8787

8888
public getState(): ViewState {
89-
return { ...this.state }
89+
return {
90+
...this.state,
91+
allItems: [...this.state.allItems],
92+
displayItems: this.state.displayItems ? [...this.state.displayItems] : undefined,
93+
refreshingUrls: [...this.state.refreshingUrls],
94+
sources: [...this.state.sources],
95+
filters: {
96+
...this.state.filters,
97+
tags: [...this.state.filters.tags],
98+
},
99+
}
90100
}
91101

92102
private notifyStateChange(): void {
93-
const newState = { ...this.state }
94-
103+
const newState = this.getState() // Use getState to ensure proper copying
95104
this.stateChangeHandlers.forEach((handler) => {
96105
handler(newState)
97106
})
@@ -235,14 +244,23 @@ export class PackageManagerViewStateManager {
235244

236245
case "UPDATE_SORT": {
237246
const { sortConfig } = transition.payload as TransitionPayloads["UPDATE_SORT"]
238-
this.state.sortConfig = {
239-
...this.state.sortConfig,
240-
...sortConfig,
247+
// Create new state with updated sort config
248+
this.state = {
249+
...this.state,
250+
sortConfig: {
251+
...this.state.sortConfig,
252+
...sortConfig,
253+
},
241254
}
242255
// Apply sorting to both allItems and displayItems
243-
this.state.allItems = this.sortItems(this.state.allItems)
244-
if (this.state.displayItems) {
245-
this.state.displayItems = this.sortItems(this.state.displayItems)
256+
// Sort items immutably
257+
const sortedAllItems = this.sortItems(this.state.allItems)
258+
const sortedDisplayItems = this.state.displayItems ? this.sortItems(this.state.displayItems) : undefined
259+
260+
this.state = {
261+
...this.state,
262+
allItems: sortedAllItems,
263+
displayItems: sortedDisplayItems,
246264
}
247265
this.notifyStateChange()
248266
break
@@ -251,7 +269,10 @@ export class PackageManagerViewStateManager {
251269
case "REFRESH_SOURCE": {
252270
const { url } = transition.payload as TransitionPayloads["REFRESH_SOURCE"]
253271
if (!this.state.refreshingUrls.includes(url)) {
254-
this.state.refreshingUrls = [...this.state.refreshingUrls, url]
272+
this.state = {
273+
...this.state,
274+
refreshingUrls: [...this.state.refreshingUrls, url],
275+
}
255276
this.notifyStateChange()
256277
vscode.postMessage({
257278
type: "refreshPackageManagerSource",
@@ -263,20 +284,25 @@ export class PackageManagerViewStateManager {
263284

264285
case "REFRESH_SOURCE_COMPLETE": {
265286
const { url } = transition.payload as TransitionPayloads["REFRESH_SOURCE_COMPLETE"]
266-
this.state.refreshingUrls = this.state.refreshingUrls.filter((existingUrl) => existingUrl !== url)
287+
this.state = {
288+
...this.state,
289+
refreshingUrls: this.state.refreshingUrls.filter((existingUrl) => existingUrl !== url),
290+
}
267291
this.notifyStateChange()
268292
break
269293
}
270294

271295
case "UPDATE_SOURCES": {
272296
const { sources } = transition.payload as TransitionPayloads["UPDATE_SOURCES"]
273297
// If all sources are removed, add the default source
274-
const updatedSources = sources.length === 0 ? [DEFAULT_PACKAGE_MANAGER_SOURCE] : sources
275-
this.state.sources = updatedSources
298+
const updatedSources = sources.length === 0 ? [DEFAULT_PACKAGE_MANAGER_SOURCE] : [...sources]
299+
this.state = {
300+
...this.state,
301+
sources: updatedSources,
302+
isFetching: false, // Reset fetching state first
303+
}
276304
this.sourcesModified = true // Set the flag when sources are modified
277305

278-
// Reset fetching state first
279-
this.state.isFetching = false
280306
this.notifyStateChange()
281307

282308
// Send sources update to extension
@@ -288,7 +314,10 @@ export class PackageManagerViewStateManager {
288314
// Only start fetching if we have sources
289315
if (updatedSources.length > 0) {
290316
// Set fetching state and notify
291-
this.state.isFetching = true
317+
this.state = {
318+
...this.state,
319+
isFetching: true,
320+
}
292321
this.notifyStateChange()
293322

294323
// Send fetch request
@@ -369,7 +398,10 @@ export class PackageManagerViewStateManager {
369398
// Update sources from either sources or packageManagerSources in state
370399
if (message.state?.sources || message.state?.packageManagerSources) {
371400
const sources = message.state.packageManagerSources || message.state.sources
372-
this.state.sources = sources?.length > 0 ? sources : [DEFAULT_PACKAGE_MANAGER_SOURCE]
401+
this.state = {
402+
...this.state,
403+
sources: sources?.length > 0 ? [...sources] : [DEFAULT_PACKAGE_MANAGER_SOURCE],
404+
}
373405
this.notifyStateChange()
374406
}
375407

0 commit comments

Comments
 (0)