From c7ca49240d34a09197d17dd77a246dd6afb85e61 Mon Sep 17 00:00:00 2001 From: Christian Luksch Date: Thu, 27 Apr 2023 14:44:27 +0200 Subject: [PATCH] proper registration and cleanup of event handlers (#47) # Conflicts: # src/Aardvark.UI/Updater.fs --- src/Aardvark.UI/Updater.fs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/Aardvark.UI/Updater.fs b/src/Aardvark.UI/Updater.fs index 6c0b1ad3..db1b6890 100644 --- a/src/Aardvark.UI/Updater.fs +++ b/src/Aardvark.UI/Updater.fs @@ -260,13 +260,13 @@ module Updaters = type AttributeUpdater<'msg>(attributes : AttributeMap<'msg>) = let mutable reader = attributes.GetReader() + let registeredHandlers = Dictionary() member x.TryGetAttrbute(name : string) = HashMap.tryFind name reader.State member x.Update(token : AdaptiveToken, state : UpdateState<'msg>, id : string, self : JSExpr) = JSExpr.Sequential [ - let old = reader.State let atts = reader.GetChanges token for (name, op) in atts do match op with @@ -277,21 +277,31 @@ module Updaters = str | AttributeValue.Event evt -> let key = (id, name) + match registeredHandlers.TryGetValue key with + | (true, d) -> d.Dispose() + | _ -> () state.handlers.[key] <- evt.serverSide + let handlerDispose = { new IDisposable with member x.Dispose() = state.handlers.Remove(key) |> ignore } + registeredHandlers.[key] <- handlerDispose // add or overwrite Event.toString id name evt yield JSExpr.SetAttribute(self, name, value) - //yield JSExpr.SetAttribute(self, name, value) - | Remove -> + let key = (id, name) + match registeredHandlers.TryGetValue key with + | (true, d) -> + d.Dispose() + registeredHandlers.Remove(key) |> ignore + | _ -> () yield JSExpr.RemoveAttribute(self, name) ] member x.Dispose() = - () + for kv in registeredHandlers do + kv.Value.Dispose() + registeredHandlers.Clear() reader <- Unchecked.defaultof<_> - //reader.Dispose() interface IDisposable with member x.Dispose() = x.Dispose()