-
Notifications
You must be signed in to change notification settings - Fork 110
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
[WIP] Live components #247
Draft
Krzysztof-Cieslak
wants to merge
4
commits into
main
Choose a base branch
from
liveComponents
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8dcac80
Changes to channels implementation and IChannelHub
Krzysztof-Cieslak cb206d5
Initial implementation of LiveComponent
Krzysztof-Cieslak 93928cd
Live View should target only netcoreapp3.1
Krzysztof-Cieslak 0f7f298
Fix paket references in LiveView project
Krzysztof-Cieslak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
namespace Saturn | ||
|
||
open Channels | ||
open Microsoft.AspNetCore.Http | ||
open Microsoft.Extensions.DependencyInjection | ||
open System.Threading.Tasks | ||
open Giraffe.GiraffeViewEngine | ||
open Elmish | ||
open FSharp.Control.Tasks.V2 | ||
|
||
module LiveComponenet = | ||
type ILiveComponenet = | ||
abstract member InternalChannel : IChannel with get | ||
|
||
type LiveComponentMsg = {Event: string; ElementId: string; Data: string} | ||
type internal ViewUpdateMsg = {ComponentId: string; Data: string} | ||
|
||
[<AutoOpen>] | ||
module LiveComponentBuilder = | ||
open LiveComponenet | ||
|
||
type LiveComponenetBuilderState<'State, 'Msg> = { | ||
Join: (HttpContext -> ClientInfo -> Task<JoinResult>) option | ||
Init: (HttpContext -> ClientInfo -> (Cmd<'Msg> -> unit) -> Task<'State * Cmd<'Msg>>) option | ||
Update: (HttpContext -> ClientInfo -> 'Msg -> 'State -> Task<'State * Cmd<'Msg>>) option | ||
View: (HttpContext -> ClientInfo -> 'State -> XmlNode) option | ||
MessageMap: (HttpContext -> ClientInfo -> LiveComponentMsg -> 'Msg) option | ||
} | ||
|
||
type internal StateMsg<'State, 'Msg> = | ||
| Init of HttpContext * ClientInfo | ||
| SetState of 'State | ||
| Dispatch of Cmd<'Msg> | ||
| Update of 'Msg | ||
|
||
|
||
type LiveComponenetBuilder<'State, 'Msg> internal (componentId: string) = | ||
|
||
member __.Yield (_) : LiveComponenetBuilderState<'State, 'Msg> = | ||
{Join = None; Init = None; Update = None; View = None; MessageMap = None} | ||
|
||
[<CustomOperation("join")>] | ||
///Action executed when client tries to join the channel. | ||
///You can either return `Ok` if channel allows join, or reject it with `Rejected` | ||
///Typical cases for rejection may include authorization/authentication, | ||
///not being able to handle more connections or other business logic reasons. | ||
/// | ||
/// As arguments, `join` action gets: | ||
/// * current `HttpContext` for the request | ||
/// * `ClientInfo` instance representing additional information about client sending request | ||
member __.Join (state, handler) : LiveComponenetBuilderState<'State, 'Msg> = | ||
{state with Join = handler} | ||
|
||
[<CustomOperation("init")>] | ||
///Action executed after client succesfully join the channel. Used to set initial state of the compnent. | ||
/// | ||
/// As arguments, `init` action gets: | ||
/// * current `HttpContext` for the request | ||
/// * `ClientInfo` instance representing additional information about client sending request | ||
/// * `(Cmd<'Msg> -> unit)` function that can be used to dispatch additional messages (for example used when in `init` you can subscribe to external events) | ||
/// | ||
/// Returns: `Task<'State * Cmd<'Msg>>` | ||
member __.Init (state, handler) : LiveComponenetBuilderState<'State, 'Msg> = | ||
{state with Init = handler} | ||
|
||
[<CustomOperation("update")>] | ||
///Action executed after client performs some event in the component | ||
/// | ||
/// As arguments, `update` action gets: | ||
/// * current `HttpContext` for the request | ||
/// * `ClientInfo` instance representing additional information about client sending request | ||
/// * message `'Msg` that represetns event that happened | ||
/// | ||
/// Returns: `Task<'State * Cmd<'Msg>>` | ||
member __.Update (state, handler) : LiveComponenetBuilderState<'State, 'Msg> = | ||
{state with Update = handler} | ||
|
||
[<CustomOperation("view")>] | ||
///Function responsible for mapping current state to the view | ||
/// | ||
/// As arguments, `view` action gets: | ||
/// * current `HttpContext` for the request | ||
/// * `ClientInfo` instance representing additional information about client sending request | ||
/// * current state `'State` | ||
/// | ||
/// Returns: `XmlNode` (Giraffe.ViewEngine) | ||
member __.View (state, handler) : LiveComponenetBuilderState<'State, 'Msg> = | ||
{state with View = handler} | ||
|
||
[<CustomOperation("message_map")>] | ||
///Function responsible for mapping raw messages into component domain messages | ||
/// | ||
/// As arguments, `message_map` action gets: | ||
/// * current `HttpContext` for the request | ||
/// * `ClientInfo` instance representing additional information about client sending request | ||
/// * instance of `LiveComponentMsg` representing raw message | ||
/// | ||
/// Returns: `'Msg` representing domain message | ||
member __.MessageMap (state, handler) : LiveComponenetBuilderState<'State, 'Msg> = | ||
{state with MessageMap = handler} | ||
|
||
member __.Run (state : LiveComponenetBuilderState<'State, 'Msg>) : ILiveComponenet = | ||
if state.Join.IsNone then failwith "Join is required operation for any Live Component. Please use `join` operation in your `liveComponent` CE to define it." | ||
if state.Init.IsNone then failwith "Init is required operation for any Live Component. Please use `init` operation in your `liveComponent` CE to define it." | ||
if state.View.IsNone then failwith "View is required operation for any Live Component. Please use `view` operation in your `liveComponent` CE to define it." | ||
if state.Update.IsNone then failwith "Update is required operation for any Live Component. Please use `update` operation in your `liveComponent` CE to define it." | ||
if state.MessageMap.IsNone then failwith "MessageMap is required operation for any Live Component. Please use `message_map` operation in your `liveComponent` CE to define it." | ||
|
||
|
||
let joinH = state.Join.Value | ||
let initH = state.Init.Value | ||
let viewH = state.View.Value | ||
let updateH = state.Update.Value | ||
let mmH = state.MessageMap.Value | ||
|
||
let c = | ||
let rec stateMP = MailboxProcessor.Start(fun inbox -> | ||
|
||
let rec messageLoop(state: 'State, (ctx: HttpContext), ci) = async { | ||
let! msg = inbox.Receive() | ||
let! newState, ctx, ci = | ||
match msg with | ||
| Init (ctx, ci) -> | ||
async { return state, ctx, ci} | ||
| SetState (state) -> | ||
async { | ||
let clientHub = ctx.RequestServices.GetService<ISocketHub> () | ||
let viewTemplate = viewH ctx ci state | ||
let viewStr = Giraffe.GiraffeViewEngine.renderHtmlDocument viewTemplate | ||
let viewMsg = {ComponentId = componentId; Data = viewStr} | ||
do! clientHub.SendMessageToClient ci "liveComponent" viewMsg |> Async.AwaitTask | ||
|
||
return state, ctx, ci | ||
} | ||
| Update msg -> | ||
async { | ||
let! (state, cmd) = (updateH ctx ci msg state |> Async.AwaitTask) | ||
|
||
let clientHub = ctx.RequestServices.GetService<ISocketHub> () | ||
let viewTemplate = viewH ctx ci state | ||
let viewStr = Giraffe.GiraffeViewEngine.renderHtmlDocument viewTemplate | ||
let viewMsg = {ComponentId = componentId; Data = viewStr} | ||
do! clientHub.SendMessageToClient ci "liveComponent" viewMsg |> Async.AwaitTask | ||
|
||
inbox.Post (Dispatch cmd) | ||
return state, ctx, ci | ||
} | ||
| Dispatch (cmd: Cmd<'Msg>) -> | ||
async { | ||
cmd |> List.iter (fun n -> n (Update >> inbox.Post) ) | ||
return state, ctx, ci | ||
} | ||
return! messageLoop (newState, ctx, ci) } | ||
|
||
let inState = Unchecked.defaultof<'State> | ||
let inCtx = Unchecked.defaultof<HttpContext> | ||
let inCi = Unchecked.defaultof<ClientInfo> | ||
messageLoop (inState, inCtx, inCi) | ||
) | ||
|
||
channel { | ||
join (fun ctx si -> task { | ||
let! res = joinH ctx si | ||
match res with | ||
| JoinResult.Ok -> | ||
stateMP.Post (Init (ctx, si)) | ||
let! (s,cmd) = initH ctx si (Dispatch >> stateMP.Post) | ||
stateMP.Post (SetState s) | ||
stateMP.Post (Dispatch cmd) | ||
| _ -> | ||
() | ||
return res | ||
}) | ||
|
||
handle "liveComponent" (fun ctx si (msg: Message<LiveComponentMsg>) -> task { | ||
let m = mmH ctx si msg.Payload | ||
stateMP.Post (Update m) | ||
return () | ||
}) | ||
|
||
terminate (fun ctx si -> task { | ||
(stateMP :> System.IDisposable).Dispose() | ||
return () | ||
}) | ||
} | ||
|
||
{ new ILiveComponenet with | ||
member __.InternalChannel with get () = c | ||
} | ||
|
||
let liveComponent<'State, 'Msg> id = LiveComponenetBuilder<'State, 'Msg>(id) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Library</OutputType> | ||
<TargetFrameworks>netcoreapp3.1</TargetFrameworks> | ||
<DebugType>portable</DebugType> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<Description>Saturn LiveView - rich, real-time user experience with server-rendered HTML.</Description> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\Saturn\Saturn.fsproj"> | ||
<Name>Saturn.fsproj</Name> | ||
</ProjectReference> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="LiveView.fs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1' "> | ||
<FrameworkReference Include="Microsoft.AspNetCore.App" /> | ||
</ItemGroup> | ||
|
||
<Import Project="..\..\.paket\Paket.Restore.targets" /> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Elmish | ||
FSharp.Core |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the concrete type
XmlNode
ties this implementation of live components with the view engine of Giraffe. It won't be possible to use Feliz.ViewEngine which uses a different type calledReactElement
.Would it be possible to abstract the view output into the type declaration? i.e.