Skip to content

Commit

Permalink
fix memory leak in IterableHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
benStre committed Dec 10, 2023
1 parent b8e7a38 commit 21a2c20
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
1 change: 0 additions & 1 deletion runtime/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
╚═════════════════════════════════════════╩════════════════════════════════════════════╝
*/


//logger.info("initializing ...");

// displayInit();
Expand Down
27 changes: 23 additions & 4 deletions utils/iterable-handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Datex } from "../mod.ts";
import { ValueError } from "../datex_all.ts";
import { weakAction } from "./weak-action.ts";

export class IterableHandler<T, U = T> {

Expand All @@ -20,7 +21,7 @@ export class IterableHandler<T, U = T> {
onEmpty?: () => void
}) {
this.map = callbacks.map;
this.filter = callbacks.filter;
// this.filter = callbacks.filter;
this.onNewEntry = callbacks.onNewEntry;
this.onEntryRemoved = callbacks.onEntryRemoved;
this.onEmpty = callbacks.onEmpty;
Expand All @@ -30,9 +31,27 @@ export class IterableHandler<T, U = T> {
}

observe() {
Datex.Ref.observeAndInit(this.iterable, (v, k, t)=>{
this.onValueChanged(v, k, t)
});
// deno-lint-ignore no-this-alias
const self = this;
const iterable = this.iterable;
weakAction(
{self},
({self}) => {
const handler = this.workaroundGetHandler(self)
Datex.Ref.observeAndInit(iterable, handler);
}
);
}

workaroundGetHandler(handler: WeakRef<IterableHandler<any>>) {
return (v:any, k:any, t:any) => {
const deref = handler.deref();
if (!deref) {
console.warn("Undetected garbage collection (datex-w0001)");
return;
}
deref.onValueChanged(v, k, t)
}
}

#entries?: Map<number, U>;
Expand Down

0 comments on commit 21a2c20

Please sign in to comment.