Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Jooseppi12 committed Apr 16, 2024
1 parent 70ee5df commit 9ec8bd6
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 3 deletions.
4 changes: 3 additions & 1 deletion WebSharper.UI.Templating.Tests/Client.fs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ module Client =
let itemsSub = Submitter.Create myItems.View Seq.empty
let stitle = "Starting titlo"
let var = Var.Create ""

let varInAsync = Var.Create "Hello"
let title =
stitle
|> Seq.toList
|> List.map Var.Create

async {
do! Async.Sleep 1500
let! asyncValue = varInAsync
Console.Log(asyncValue)
Var.Set (List.item (title.Length - 1) title) 'e'
} |> Async.Start

Expand Down
31 changes: 31 additions & 0 deletions WebSharper.UI/Extensions.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// $begin{copyright}
//
// This file is part of WebSharper
//
// Copyright (c) 2008-2018 IntelliFactory
//
// Licensed under the Apache License, Version 2.0 (the "License"); you
// may not use this file except in compliance with the License. You may
// obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// $end{copyright}

namespace WebSharper.UI

open WebSharper

[<AutoOpen;JavaScript>]
module Extensions =
type AsyncBuilder with
member this.Bind(var: Var<'T>, continuation: 'T -> Async<'U>) : Async<'U> =
var.View |> View.GetAsync |> fun res -> async.Bind(res, continuation)
member this.Bind(view: View<'T>, continuation: 'T -> Async<'U>) : Async<'U> =
view |> View.GetAsync |> fun res -> async.Bind(res, continuation)
6 changes: 6 additions & 0 deletions WebSharper.UI/ReactiveExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ module VarModule =
[<Macro(typeof<Macros.LensFunction>)>]
let Lens (x: 'T) = Var.Create x

[<Extension; JavaScript>]
type VarExteions =
[<Extension>]
static member GetAsync(v: Var<'A>) : Async<'A> =
v.View |> View.GetAsync

// These methods apply to specific types of View (such as View<seq<'A>> when 'A : equality)
/// so we need to use C#-style extension methods.
[<Extension; JavaScript>]
Expand Down
5 changes: 5 additions & 0 deletions WebSharper.UI/ReactiveExtensions.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ open WebSharper
module VarModule =
val Lens : 'T -> Var<'T>

[<Extension; Class ;JavaScript>]
type VarExteions =
[<Extension>]
static member GetAsync : Var<'A> -> Async<'A>

/// More members on View.
[<Extension; Class>]
type ReactiveExtensions =
Expand Down
4 changes: 2 additions & 2 deletions WebSharper.UI/Routing.fs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ module Routing =
if not (same var.Value value) then
var.Value <- value
let onUpdate = System.Action<_>(fun (evt: Dom.Event) -> set (cur ()))
win.Onpopstate <- onUpdate
win.Onhashchange <- onUpdate
win.OnPopState <- onUpdate
win.OnHashChange <- onUpdate
var.View
|> View.Sink (fun loc ->
let ha = Route.MakeHash (Route.FromList (rt.Ser loc))
Expand Down
1 change: 1 addition & 0 deletions WebSharper.UI/WebSharper.UI.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<Content Include="SVG.fs" />
<Compile Include="Input.fsi" />
<Compile Include="Input.fs" />
<Compile Include="Extensions.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../WebSharper.UI.Templating.Common\WebSharper.UI.Templating.Common.fsproj" />
Expand Down

0 comments on commit 9ec8bd6

Please sign in to comment.