Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary ValueOptions #1095

Merged
merged 3 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/Fabulous/AttributeDefinitions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,6 @@ module AttributeHelpers =
open ScalarAttributeDefinitions

let tryFindSimpleScalarAttribute (definition: SimpleScalarAttributeDefinition<'T>) (widget: Widget) =
match widget.ScalarAttributes with
| ValueNone -> ValueNone
| ValueSome attrs ->
match attrs |> Array.tryFind(fun attr -> attr.Key = definition.Key) with
| None -> ValueNone
| Some attr -> ValueSome(unbox<'T> attr.Value)
match widget.ScalarAttributes |> Array.tryFind(fun attr -> attr.Key = definition.Key) with
| None -> ValueNone
| Some attr -> ValueSome(unbox<'T> attr.Value)
61 changes: 35 additions & 26 deletions src/Fabulous/Builders.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ open Fabulous.StackAllocatedCollections
open Fabulous.StackAllocatedCollections.StackList
open Microsoft.FSharp.Core

type AttributesBundle = (struct (StackList<ScalarAttribute> * WidgetAttribute[] voption * WidgetCollectionAttribute[] voption * EnvironmentAttribute[] voption))
TimLariviere marked this conversation as resolved.
Show resolved Hide resolved
type AttributesBundle = (struct (StackList<ScalarAttribute> * WidgetAttribute[] * WidgetCollectionAttribute[] * EnvironmentAttribute[]))

[<Struct; NoComparison; NoEquality>]
type WidgetBuilder<'msg, 'marker when 'msg: equality> =
Expand All @@ -17,25 +17,25 @@ type WidgetBuilder<'msg, 'marker when 'msg: equality> =

new(key: WidgetKey) =
{ Key = key
Attributes = AttributesBundle(StackList.empty(), ValueNone, ValueNone, ValueNone) }
Attributes = AttributesBundle(StackList.empty(), [||], [||], [||]) }

new(key: WidgetKey, attributes: AttributesBundle) = { Key = key; Attributes = attributes }

new(key: WidgetKey, scalar: ScalarAttribute) =
{ Key = key
Attributes = AttributesBundle(StackList.one scalar, ValueNone, ValueNone, ValueNone) }
Attributes = AttributesBundle(StackList.one scalar, [||], [||], [||]) }

new(key: WidgetKey, scalarA: ScalarAttribute, scalarB: ScalarAttribute) =
{ Key = key
Attributes = AttributesBundle(StackList.two(scalarA, scalarB), ValueNone, ValueNone, ValueNone) }
Attributes = AttributesBundle(StackList.two(scalarA, scalarB), [||], [||], [||]) }

new(key: WidgetKey, scalar1: ScalarAttribute, scalar2: ScalarAttribute, scalar3: ScalarAttribute) =
{ Key = key
Attributes = AttributesBundle(StackList.three(scalar1, scalar2, scalar3), ValueNone, ValueNone, ValueNone) }
Attributes = AttributesBundle(StackList.three(scalar1, scalar2, scalar3), [||], [||], [||]) }

new(key: WidgetKey, widget: WidgetAttribute) =
{ Key = key
Attributes = AttributesBundle(StackList.empty(), ValueSome [| widget |], ValueNone, ValueNone) }
Attributes = AttributesBundle(StackList.empty(), [| widget |], [||], [||]) }

[<EditorBrowsable(EditorBrowsableState.Never)>]
member x.Compile() : Widget =
Expand All @@ -48,14 +48,23 @@ type WidgetBuilder<'msg, 'marker when 'msg: equality> =
#endif
ScalarAttributes =
match StackList.length &scalarAttributes with
| 0us -> ValueNone
| _ -> ValueSome(Array.sortInPlace _.Key (StackList.toArray &scalarAttributes))
| 0us -> [||]
| _ -> Array.sortInPlace _.Key (StackList.toArray &scalarAttributes)

WidgetAttributes = ValueOption.map (Array.sortInPlace(_.Key)) widgetAttributes
WidgetAttributes =
match widgetAttributes with
| [||] -> [||]
| _ -> Array.sortInPlace _.Key widgetAttributes

WidgetCollectionAttributes = widgetCollectionAttributes |> ValueOption.map(Array.sortInPlace(_.Key))
WidgetCollectionAttributes =
match widgetCollectionAttributes with
| [||] -> [||]
| _ -> Array.sortInPlace _.Key widgetCollectionAttributes

EnvironmentAttributes = environmentAttributes |> ValueOption.map(Array.sortInPlace(_.Key)) }
EnvironmentAttributes =
match environmentAttributes with
| [||] -> [||]
| _ -> Array.sortInPlace _.Key environmentAttributes }

[<EditorBrowsable(EditorBrowsableState.Never)>]
member inline x.AddScalar(attr: ScalarAttribute) =
Expand Down Expand Up @@ -103,14 +112,14 @@ type WidgetBuilder<'msg, 'marker when 'msg: equality> =

let res =
match attribs with
| ValueNone -> [| attr |]
| ValueSome attribs ->
| [||] -> [| attr |]
| attribs ->
let attribs2 = Array.zeroCreate(attribs.Length + 1)
Array.blit attribs 0 attribs2 0 attribs.Length
attribs2[attribs.Length] <- attr
attribs2

WidgetBuilder<'msg, 'marker>(x.Key, struct (scalarAttributes, ValueSome res, widgetCollectionAttributes, environmentAttributes))
WidgetBuilder<'msg, 'marker>(x.Key, struct (scalarAttributes, res, widgetCollectionAttributes, environmentAttributes))

[<EditorBrowsable(EditorBrowsableState.Never)>]
member x.AddWidgetCollection(attr: WidgetCollectionAttribute) =
Expand All @@ -121,14 +130,14 @@ type WidgetBuilder<'msg, 'marker when 'msg: equality> =

let res =
match attribs with
| ValueNone -> [| attr |]
| ValueSome attribs ->
| [||] -> [| attr |]
| attribs ->
let attribs2 = Array.zeroCreate(attribs.Length + 1)
Array.blit attribs 0 attribs2 0 attribs.Length
attribs2[attribs.Length] <- attr
attribs2

WidgetBuilder<'msg, 'marker>(x.Key, struct (scalarAttributes, widgetAttributes, ValueSome res, environmentAttributes))
WidgetBuilder<'msg, 'marker>(x.Key, struct (scalarAttributes, widgetAttributes, res, environmentAttributes))

[<EditorBrowsable(EditorBrowsableState.Never)>]
member inline x.AddEnvironment(key: EnvironmentAttributeKey, value: obj) =
Expand All @@ -146,14 +155,14 @@ type WidgetBuilder<'msg, 'marker when 'msg: equality> =

let res =
match attribs with
| ValueNone -> [| attr |]
| ValueSome attribs ->
| [||] -> [| attr |]
| attribs ->
let attribs2 = Array.zeroCreate(attribs.Length + 1)
Array.blit attribs 0 attribs2 0 attribs.Length
attribs2[attribs.Length] <- attr
attribs2

WidgetBuilder<'msg, 'marker>(x.Key, struct (scalarAttributes, widgetAttributes, widgetCollectionAttributes, ValueSome res))
WidgetBuilder<'msg, 'marker>(x.Key, struct (scalarAttributes, widgetAttributes, widgetCollectionAttributes, res))
end


Expand All @@ -170,12 +179,12 @@ type CollectionBuilder<'msg, 'marker, 'itemMarker when 'msg: equality> =

new(widgetKey: WidgetKey, scalars: StackList<ScalarAttribute>, attr: WidgetCollectionAttributeDefinition) =
{ WidgetKey = widgetKey
Attributes = AttributesBundle(scalars, ValueNone, ValueNone, ValueNone)
Attributes = AttributesBundle(scalars, [||], [||], [||])
Attr = attr }

new(widgetKey: WidgetKey, attr: WidgetCollectionAttributeDefinition) =
{ WidgetKey = widgetKey
Attributes = AttributesBundle(StackList.empty(), ValueNone, ValueNone, ValueNone)
Attributes = AttributesBundle(StackList.empty(), [||], [||], [||])
Attr = attr }

new(widgetKey: WidgetKey, attr: WidgetCollectionAttributeDefinition, attributes: AttributesBundle) =
Expand All @@ -185,12 +194,12 @@ type CollectionBuilder<'msg, 'marker, 'itemMarker when 'msg: equality> =

new(widgetKey: WidgetKey, attr: WidgetCollectionAttributeDefinition, scalar: ScalarAttribute) =
{ WidgetKey = widgetKey
Attributes = AttributesBundle(StackList.one scalar, ValueNone, ValueNone, ValueNone)
Attributes = AttributesBundle(StackList.one scalar, [||], [||], [||])
Attr = attr }

new(widgetKey: WidgetKey, attr: WidgetCollectionAttributeDefinition, scalarA: ScalarAttribute, scalarB: ScalarAttribute) =
{ WidgetKey = widgetKey
Attributes = AttributesBundle(StackList.two(scalarA, scalarB), ValueNone, ValueNone, ValueNone)
Attributes = AttributesBundle(StackList.two(scalarA, scalarB), [||], [||], [||])
Attr = attr }

member inline x.Run(c: Content<'msg>) =
Expand All @@ -205,8 +214,8 @@ type CollectionBuilder<'msg, 'marker, 'itemMarker when 'msg: equality> =

let widgetCollections =
match widgetCollections with
| ValueNone -> ValueSome([| widgetCollAttr |])
| ValueSome widgetCollectionAttributes -> ValueSome(Array.appendOne widgetCollAttr widgetCollectionAttributes)
| [||] -> [| widgetCollAttr |]
| widgetCollectionAttributes -> Array.appendOne widgetCollAttr widgetCollectionAttributes

WidgetBuilder<'msg, 'marker>(x.WidgetKey, AttributesBundle(scalars, widgets, widgetCollections, environments))

Expand Down
38 changes: 19 additions & 19 deletions src/Fabulous/Components/Component.fs
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,40 @@ type Component
| ValueSome componentWidget ->
let componentScalars =
match componentWidget.ScalarAttributes with
| ValueNone -> ValueNone
| ValueSome attrs ->
| [||] -> [||]
| attrs ->
let filteredAttrs =
attrs |> Array.filter(fun scalarAttr -> scalarAttr.Key <> componentDataKey)

ValueSome(filteredAttrs) // skip the component data
filteredAttrs // skip the component data

let scalars =
match rootWidget.ScalarAttributes, componentScalars with
| ValueNone, ValueNone -> ValueNone
| ValueSome attrs, ValueNone
| ValueNone, ValueSome attrs -> ValueSome attrs
| ValueSome widgetAttrs, ValueSome componentAttrs -> ValueSome(Array.append componentAttrs widgetAttrs)
| [||], [||] -> [||]
| attrs, [||]
| [||], attrs -> attrs
| widgetAttrs, componentAttrs -> Array.append componentAttrs widgetAttrs

let widgets =
match rootWidget.WidgetAttributes, componentWidget.WidgetAttributes with
| ValueNone, ValueNone -> ValueNone
| ValueSome attrs, ValueNone
| ValueNone, ValueSome attrs -> ValueSome attrs
| ValueSome widgetAttrs, ValueSome componentAttrs -> ValueSome(Array.append componentAttrs widgetAttrs)
| [||], [||] -> [||]
| attrs, [||]
| [||], attrs -> attrs
| widgetAttrs, componentAttrs -> Array.append componentAttrs widgetAttrs

let widgetColls =
match rootWidget.WidgetCollectionAttributes, componentWidget.WidgetCollectionAttributes with
| ValueNone, ValueNone -> ValueNone
| ValueSome attrs, ValueNone
| ValueNone, ValueSome attrs -> ValueSome attrs
| ValueSome widgetAttrs, ValueSome componentAttrs -> ValueSome(Array.append componentAttrs widgetAttrs)
| [||], [||] -> [||]
| attrs, [||]
| [||], attrs -> attrs
| widgetAttrs, componentAttrs -> Array.append componentAttrs widgetAttrs

let environments =
match rootWidget.EnvironmentAttributes, componentWidget.EnvironmentAttributes with
| ValueNone, ValueNone -> ValueNone
| ValueSome attrs, ValueNone
| ValueNone, ValueSome attrs -> ValueSome attrs
| ValueSome widgetAttrs, ValueSome componentAttrs -> ValueSome(Array.append componentAttrs widgetAttrs)
| [||], [||] -> [||]
| attrs, [||]
| [||], attrs -> attrs
| widgetAttrs, componentAttrs -> Array.append componentAttrs widgetAttrs

struct (scalars, widgets, widgetColls, environments)

Expand Down
17 changes: 8 additions & 9 deletions src/Fabulous/Components/Widget.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ module Component' =
CreateView =
fun (widget, envContext, treeContext, _) ->
match widget.ScalarAttributes with
| ValueNone -> failwith "Component widget must have a body"
| ValueSome attrs ->
| [||] -> failwith "Component widget must have a body"
| attrs ->
let data =
let scalarAttrsOpt =
attrs |> Array.tryFind(fun scalarAttr -> scalarAttr.Key = Data.Key)
Expand All @@ -37,8 +37,8 @@ module Component' =
AttachView =
fun (widget, envContext, treeContext, _, view) ->
match widget.ScalarAttributes with
| ValueNone -> failwith "Component widget must have a body"
| ValueSome attrs ->
| [||] -> failwith "Component widget must have a body"
| attrs ->
let data =
let scalarAttrsOpt =
attrs |> Array.tryFind(fun scalarAttr -> scalarAttr.Key = Data.Key)
Expand All @@ -62,26 +62,25 @@ module Component' =
let canReuseComponent (prev: Widget) (curr: Widget) =
let prevData =
match prev.ScalarAttributes with
| ValueSome attrs ->
| [||] -> failwith "Component widget must have a body"
| attrs ->
let scalarAttrsOpt =
attrs |> Array.tryFind(fun scalarAttr -> scalarAttr.Key = Data.Key)

match scalarAttrsOpt with
| None -> failwithf "Component widget must have a body"
| Some value -> value.Value :?> ComponentData

| _ -> failwith "Component widget must have a body"

let currData =
match curr.ScalarAttributes with
| ValueSome attrs ->
| [||] -> failwith "Component widget must have a body"
| attrs ->
let scalarAttrsOpt =
attrs |> Array.tryFind(fun scalarAttr -> scalarAttr.Key = Data.Key)

match scalarAttrsOpt with
| None -> failwithf "Component widget must have a body"
| Some value -> value.Value :?> ComponentData
| _ -> failwith "Component widget must have a body"

// NOTE: Somehow using = here crashes the app and prevents debugging...
Object.Equals(prevData.Key, currData.Key)
8 changes: 4 additions & 4 deletions src/Fabulous/Dispatch.fs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ module Dispatcher =
| ValueSome msg -> dispatch msg

match widget.WidgetAttributes with
| ValueNone -> ()
| ValueSome widgetAttrs ->
| [||] -> ()
| widgetAttrs ->
for childAttr in widgetAttrs do
dispatchAndVisitChildren false dispatch childAttr.Value

match widget.WidgetCollectionAttributes with
| ValueNone -> ()
| ValueSome widgetCollAttrs ->
| [||] -> ()
| widgetCollAttrs ->
for widgetCollAttr in widgetCollAttrs do
for childWidget in ArraySlice.toSpan widgetCollAttr.Value do
dispatchAndVisitChildren false dispatch childWidget
Expand Down
2 changes: 1 addition & 1 deletion src/Fabulous/Memo.fs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module Memo =

let inline private getMemoData (widget: Widget) : MemoData =
match widget.ScalarAttributes with
| ValueSome attrs when attrs.Length = 1 -> attrs[0].Value :?> MemoData
| [| attr |] -> attr.Value :?> MemoData
| _ -> failwith "Memo widget cannot have extra attributes"

let internal canReuseMemoizedWidget prev next =
Expand Down
8 changes: 4 additions & 4 deletions src/Fabulous/Primitives.fs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ and [<Struct>] Widget =
#if DEBUG
DebugName: string
#endif
ScalarAttributes: ScalarAttribute[] voption
WidgetAttributes: WidgetAttribute[] voption
WidgetCollectionAttributes: WidgetCollectionAttribute[] voption
EnvironmentAttributes: EnvironmentAttribute[] voption }
ScalarAttributes: ScalarAttribute[]
WidgetAttributes: WidgetAttribute[]
WidgetCollectionAttributes: WidgetCollectionAttribute[]
EnvironmentAttributes: EnvironmentAttribute[] }
Loading
Loading