From 21a2c20469a3665cfad836801850dac5a987e155 Mon Sep 17 00:00:00 2001 From: benStre Date: Sun, 10 Dec 2023 04:17:03 +0100 Subject: [PATCH] fix memory leak in IterableHandler --- runtime/runtime.ts | 1 - utils/iterable-handler.ts | 27 +++++++++++++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/runtime/runtime.ts b/runtime/runtime.ts index aaeeced7..67f1652a 100644 --- a/runtime/runtime.ts +++ b/runtime/runtime.ts @@ -12,7 +12,6 @@ ╚═════════════════════════════════════════╩════════════════════════════════════════════╝ */ - //logger.info("initializing ..."); // displayInit(); diff --git a/utils/iterable-handler.ts b/utils/iterable-handler.ts index 69c36ec3..d2326f02 100644 --- a/utils/iterable-handler.ts +++ b/utils/iterable-handler.ts @@ -1,5 +1,6 @@ import { Datex } from "../mod.ts"; import { ValueError } from "../datex_all.ts"; +import { weakAction } from "./weak-action.ts"; export class IterableHandler { @@ -20,7 +21,7 @@ export class IterableHandler { 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; @@ -30,9 +31,27 @@ export class IterableHandler { } 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>) { + 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;