Skip to content

Commit

Permalink
Finish implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothé Larivière committed Jan 20, 2024
1 parent eeed9f9 commit 953d53f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
24 changes: 20 additions & 4 deletions src/Fabulous/Component.fs
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,13 @@ type Component(treeContext: ViewTreeContext, body: ComponentBody, context: Compo

struct (node, view)

member this.AttachView(componentWidget: Widget voption, view: obj) =
member this.AttachView(componentWidget: Widget, view: obj) =
let struct (context, rootWidget) = _body.Invoke(_context)
_widget <- rootWidget
_context <- context

let struct (scalars, widgets, widgetColls) =
this.MergeAttributes(rootWidget, componentWidget)
this.MergeAttributes(rootWidget, ValueSome componentWidget)

let rootWidget: Widget =
{ Key = rootWidget.Key
Expand Down Expand Up @@ -330,7 +330,6 @@ module Component =
{ Key = key
Name = "Component"
TargetType = typeof<Component>
AttachView = fun _ -> failwith "Component widget cannot be attached"
CreateView =
fun (widget, treeContext, _) ->
match widget.ScalarAttributes with
Expand All @@ -347,7 +346,24 @@ module Component =

// TODO: Attach component to view so component is not discarded by GC

struct (node, view) }
struct (node, view)
AttachView =
fun (widget, treeContext, _, view) ->
match widget.ScalarAttributes with
| ValueNone -> failwith "Component widget must have a body"
| ValueSome attrs ->
let data =
match Array.tryHead attrs with
| Some attr -> attr.Value :?> ComponentData
| None -> failwith "Component widget must have a body"

let ctx = ComponentContext()
let comp = new Component(treeContext, data.Body, ctx)
let node = comp.AttachView(widget, view)

// TODO: Attach component to view so component is not discarded by GC

node }

WidgetDefinitionStore.set key definition
key
Expand Down
31 changes: 29 additions & 2 deletions src/Fabulous/MvuComponent.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ module MvuComponent =
{ Key = key
Name = "MvuComponent"
TargetType = typeof<Component>
AttachView = fun _ -> failwith "Component widget cannot be attached"
CreateView =
fun (widget, treeContext, _) ->
match widget.ScalarAttributes with
Expand Down Expand Up @@ -44,7 +43,35 @@ module MvuComponent =

// TODO: Attach component to view so component is not discarded by GC

struct (node, view) }
struct (node, view)
AttachView =
fun (widget, treeContext, _, view) ->
match widget.ScalarAttributes with
| ValueNone -> failwith "Component widget must have a body"
| ValueSome attrs ->
let data =
match Array.tryHead attrs with
| Some attr -> attr.Value :?> MvuComponentData
| None -> failwith "Component widget must have a body"

let ctx = ComponentContext(1)

let runner =
Runner((fun () -> ctx.TryGetValue(0).Value), (fun v -> ctx.SetValue(0, v)), data.Program)

runner.Start(data.Arg)

// Redirect messages to runner
let treeContext =
{ treeContext with
Dispatch = runner.Dispatch }

let comp = new Component(treeContext, data.Body, ctx)
let node = comp.AttachView(widget, view)

// TODO: Attach component to view so component is not discarded by GC

node }

WidgetDefinitionStore.set key definition
key
Expand Down

0 comments on commit 953d53f

Please sign in to comment.