Skip to content

Commit

Permalink
defineEventNoDispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarfgp committed Apr 16, 2024
1 parent 062f9e8 commit 8de43d9
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Fabulous/Attributes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,26 @@ module Attributes =
|> AttributeDefinitionStore.registerScalar

{ Key = key; Name = name }

let inline defineEventNoDispatch<'args>
name
([<InlineIfLambda>] getEvent: obj -> IEvent<EventHandler<'args>, 'args>)
: SimpleScalarAttributeDefinition<'args -> unit> =
let key =
SimpleScalarAttributeDefinition.CreateAttributeData(
ScalarAttributeComparers.noCompare,
(fun _ (newValueOpt: ('args -> unit) voption) node ->
match node.TryGetHandler(name) with
| ValueNone -> ()
| ValueSome handler -> handler.Dispose()

match newValueOpt with
| ValueNone -> node.RemoveHandler(name)
| ValueSome(fn) ->
let event = getEvent node.Target
node.SetHandler(name, event.Subscribe(fun args -> fn args)))
)

|> AttributeDefinitionStore.registerScalar

{ Key = key; Name = name }

0 comments on commit 8de43d9

Please sign in to comment.