Skip to content

Commit e8b5ca6

Browse files
committed
fix: fix memory leaks
1 parent 3cd4dcc commit e8b5ca6

1 file changed

Lines changed: 21 additions & 10 deletions

File tree

apps/package-manager/packages/generic/src/packageManager.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -832,15 +832,15 @@ class ExpectationManagerCallbacksHandler implements ExpectationManagerCallbacks
832832
)
833833
}
834834

835-
private async reportStatus<Status, Ids>(
835+
private async reportStatus<Status extends { [key: string]: any } | null, Ids>(
836836
toReportStatus: ReportStatuses<Status | null, Ids>,
837837
reportedStatuses: ReportStatuses<Status, Ids>,
838838
sendChanges: (changesToSend: ChangesTosend<Status, Ids>) => Promise<void>
839839
): Promise<void> {
840840
const changesTosend: ChangesTosend<Status, Ids> = []
841841

842842
for (const [key, o] of Object.entries(toReportStatus)) {
843-
if (o.hash) {
843+
if (o.hash !== null) {
844844
if (!o.status) {
845845
// Removed
846846
if (reportedStatuses[key]) {
@@ -884,9 +884,18 @@ class ExpectationManagerCallbacksHandler implements ExpectationManagerCallbacks
884884
}
885885

886886
// Now, check if the toReportStatus hash is still the same:
887-
if (toReportStatus[change.key].hash === change.hash) {
887+
const orgToReportStaus = toReportStatus[change.key] as ReportStatus<Status, Ids> | undefined
888+
if (orgToReportStaus && orgToReportStaus.hash === change.hash) {
888889
// Ok, this means that we have sent the latest update to Core.
889-
toReportStatus[change.key].hash = null
890+
891+
if (orgToReportStaus.status === null) {
892+
// The original data was deleted, so we can delete it, to prevent mamory leaks:
893+
delete toReportStatus[change.key]
894+
delete reportedStatuses[change.key]
895+
} else {
896+
// Set the hash to null
897+
orgToReportStaus.hash = null
898+
}
890899
}
891900
}
892901
} catch (err) {
@@ -942,6 +951,7 @@ class ExpectationManagerCallbacksHandler implements ExpectationManagerCallbacks
942951
this.packageManager.triggerUpdatedExpectedPackages()
943952
}
944953
private getIncrement(): number {
954+
if (this.increment >= Number.MAX_SAFE_INTEGER) this.increment = 0
945955
return this.increment++
946956
}
947957
}
@@ -1071,12 +1081,13 @@ export interface PackageManagerSettings {
10711081
}
10721082

10731083
type ReportStatuses<Status, Ids> = {
1074-
[key: string]: {
1075-
status: Status
1076-
ids: Ids
1077-
/** A unique value updated whenever the value is updated */
1078-
hash: number | null
1079-
}
1084+
[key: string]: ReportStatus<Status, Ids>
1085+
}
1086+
type ReportStatus<Status, Ids> = {
1087+
status: Status
1088+
ids: Ids
1089+
/** A unique value updated whenever the status is updated, or set to null if the status has already been reported. */
1090+
hash: number | null
10801091
}
10811092
type ChangesTosend<Status, Ids> = (
10821093
| {

0 commit comments

Comments
 (0)