Skip to content

Commit

Permalink
Merge pull request #259 from JordanMarr/elmish-hook-optional-overloads
Browse files Browse the repository at this point in the history
Elmish hook optional overloads
  • Loading branch information
JordanMarr committed Jan 26, 2023
2 parents 978cf13 + 060e498 commit ec9bcbc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
29 changes: 12 additions & 17 deletions src/Avalonia.FuncUI.Elmish/ElmishHook.fs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ let ignoreView = (fun _ _ -> ())

type IComponentContext with

/// Starts an Elmish loop using an existing IWritable.
/// Starts an Elmish loop with an existing IWritable.
member this.useElmish<'model, 'msg>
(
writableModel: IWritable<'model>,
update: 'msg -> 'model -> 'model * Cmd<'msg>,
mapProgram: Program<unit, 'model, 'msg, unit> -> Program<unit, 'model, 'msg, unit>
?mapProgram: Program<unit, 'model, 'msg, unit> -> Program<unit, 'model, 'msg, unit>
) =

let mapProgram = defaultArg mapProgram id
let elmishState = this.useState(None, false)

// Start Elmish loop
Expand All @@ -57,25 +58,18 @@ type IComponentContext with
|> Option.iter (fun es -> es.Dispatch map)

writableModel.Current, dispatch

/// Starts an Elmish loop using an existing IWritable.
member this.useElmish<'model, 'msg>
(
writableModel: IWritable<'model>,
update: 'msg -> 'model -> 'model * Cmd<'msg>
) =

this.useElmish(writableModel, update, id)

/// Starts an Elmish loop.
/// Starts an Elmish loop with an init arg.
member this.useElmish<'arg, 'model, 'msg>
(
init : 'arg -> 'model * Cmd<'msg>,
update: 'msg -> 'model -> 'model * Cmd<'msg>,
initArg: 'arg,
mapProgram: Program<'arg, 'model, 'msg, unit> -> Program<'arg, 'model, 'msg, unit>
?mapProgram: Program<'arg, 'model, 'msg, unit> -> Program<'arg, 'model, 'msg, unit>
) =

let mapProgram = mapProgram |> Option.defaultValue id

let elmishState = this.useState(None, false)
let writableModel = this.useState(init initArg |> fst, true)

Expand All @@ -100,11 +94,12 @@ type IComponentContext with
writableModel.Current, dispatch

/// Starts an Elmish loop.
member this.useElmish<'arg, 'model, 'msg>
member this.useElmish<'model, 'msg>
(
init : 'arg -> 'model * Cmd<'msg>,
init : unit -> 'model * Cmd<'msg>,
update: 'msg -> 'model -> 'model * Cmd<'msg>,
initArg: 'arg
?mapProgram: Program<unit, 'model, 'msg, unit> -> Program<unit, 'model, 'msg, unit>
) =

this.useElmish(init, update, initArg, id)
let mapProgram = defaultArg mapProgram id
this.useElmish(init, update, (), mapProgram)
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<AvaloniaVersion>11.0.0-preview4</AvaloniaVersion>
<FuncUIVersion>0.6.0-preview6.1</FuncUIVersion>
<FuncUIVersion>0.6.0-preview7</FuncUIVersion>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ let private subscriptions (model: Model) : Sub<Msg> =
]

let view () = Component (fun ctx ->
let model, dispatch = ctx.useElmish(init, update, (), Program.withSubscription subscriptions)
//let model, dispatch = ctx.useElmish(init, update, ()) // if no subscriptions are needed
let model, dispatch = ctx.useElmish(init, update, Program.withSubscription subscriptions)
//let model, dispatch = ctx.useElmish(init, update) // if no subscriptions are needed

Grid.create [
Grid.rowDefinitions "20, *"
Expand Down

0 comments on commit ec9bcbc

Please sign in to comment.