Skip to content

Commit

Permalink
reworked event handler management (#47)
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Aardvark.UI/Updater.fs
  • Loading branch information
luithefirst committed Apr 27, 2023
1 parent c7ca492 commit 57a04e5
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions src/Aardvark.UI/Updater.fs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ module Updaters =

type AttributeUpdater<'msg>(attributes : AttributeMap<'msg>) =
let mutable reader = attributes.GetReader()
let registeredHandlers = Dictionary<string * string, IDisposable>()
let registeredHandlers = System.Collections.Generic.HashSet<string * string>()

member x.TryGetAttrbute(name : string) =
HashMap.tryFind name reader.State
Expand All @@ -277,34 +277,25 @@ 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
registeredHandlers.Add(key) |> ignore
Event.toString id name evt

yield JSExpr.SetAttribute(self, name, value)

| Remove ->
let key = (id, name)
match registeredHandlers.TryGetValue key with
| (true, d) ->
d.Dispose()
registeredHandlers.Remove(key) |> ignore
| _ -> ()
registeredHandlers.Remove(key) |> ignore
state.handlers.Remove(key) |> ignore
yield JSExpr.RemoveAttribute(self, name)
]

member x.Dispose() =
for kv in registeredHandlers do
kv.Value.Dispose()
member x.Destroy(state : UpdateState<'msg>) =
for k in registeredHandlers do
state.handlers.Remove(k) |> ignore
registeredHandlers.Clear()
reader <- Unchecked.defaultof<_>

interface IDisposable with
member x.Dispose() = x.Dispose()

type EmptyUpdater<'msg>(e : EmptyNode<'msg>) =
inherit AbstractUpdater<'msg>(e)
Expand All @@ -328,7 +319,7 @@ module Updaters =
att.Update(token, state, id, self)

override x.PerformDestroy(state : UpdateState<'msg>, self : JSExpr) =
att.Dispose()
att.Destroy(state)
JSExpr.Nop

and InnerUpdater<'msg>(e : InnerNode<'msg>, request : Request) =
Expand Down Expand Up @@ -461,7 +452,7 @@ module Updaters =
|> JSExpr.Sequential

elemReader <- Unchecked.defaultof<_>
att.Dispose()
att.Destroy(state)
inner

and SceneUpdater<'msg>(e : SceneNode<'msg>) as this =
Expand Down Expand Up @@ -491,7 +482,7 @@ module Updaters =

override x.PerformDestroy(state : UpdateState<'msg>, self : JSExpr) =
state.scenes.Remove(x.Id.Value) |> ignore
att.Dispose()
att.Destroy(state)
initial <- true
JSExpr.Nop

Expand All @@ -511,7 +502,7 @@ module Updaters =
]

override x.PerformDestroy(state : UpdateState<'msg>, self : JSExpr) =
att.Dispose()
att.Destroy(state)
JSExpr.Nop

and MapUpdater<'inner, 'outer>(m : MapNode<'inner, 'outer>, inner : IUpdater<'inner>) =
Expand Down

0 comments on commit 57a04e5

Please sign in to comment.