Skip to content

Commit 8de43d9

Browse files
committed
defineEventNoDispatch
1 parent 062f9e8 commit 8de43d9

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

src/Fabulous/Attributes.fs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,3 +355,26 @@ module Attributes =
355355
|> AttributeDefinitionStore.registerScalar
356356

357357
{ Key = key; Name = name }
358+
359+
let inline defineEventNoDispatch<'args>
360+
name
361+
([<InlineIfLambda>] getEvent: obj -> IEvent<EventHandler<'args>, 'args>)
362+
: SimpleScalarAttributeDefinition<'args -> unit> =
363+
let key =
364+
SimpleScalarAttributeDefinition.CreateAttributeData(
365+
ScalarAttributeComparers.noCompare,
366+
(fun _ (newValueOpt: ('args -> unit) voption) node ->
367+
match node.TryGetHandler(name) with
368+
| ValueNone -> ()
369+
| ValueSome handler -> handler.Dispose()
370+
371+
match newValueOpt with
372+
| ValueNone -> node.RemoveHandler(name)
373+
| ValueSome(fn) ->
374+
let event = getEvent node.Target
375+
node.SetHandler(name, event.Subscribe(fun args -> fn args)))
376+
)
377+
378+
|> AttributeDefinitionStore.registerScalar
379+
380+
{ Key = key; Name = name }

0 commit comments

Comments
 (0)