Skip to content

Commit 243c22c

Browse files
committed
Added timeflies example with AsyncRx
1 parent b3b3095 commit 243c22c

File tree

5 files changed

+106
-26
lines changed

5 files changed

+106
-26
lines changed

examples/timeflies/Program.fs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Learn more about F# at http://docs.microsoft.com/dotnet/fsharp
2+
module Program
3+
4+
open System
5+
open FSharp.Control
6+
open Fable.Python
7+
open Fable.Python.TkInter
8+
open Fable.Python.Queue
9+
10+
type Msg =
11+
| Place of label: Label * x: int * y: int
12+
//| Place of x: int * y: int
13+
| Empty
14+
15+
let root = Tk()
16+
root.title("Fable Python Rocks on Tkinter!")
17+
let queue = Queue<Msg> ()
18+
19+
let source, mouseMoves: IAsyncObserver<int*int>*IAsyncObservable<int*int> = AsyncRx.subject()
20+
21+
let workerAsync (mb: MailboxProcessor<Event>) =
22+
let rec messageLoop () =
23+
async {
24+
let! event = mb.Receive ()
25+
do! source.OnNextAsync((event.x, event.y))
26+
27+
return! messageLoop ()
28+
}
29+
messageLoop ()
30+
31+
let agent = MailboxProcessor<TkInter.Event>.Start(workerAsync)
32+
33+
let frame = Frame(root, width=800, height=600, bg="white")
34+
frame.bind("<Motion>", agent.Post) |> ignore
35+
frame.pack()
36+
37+
let stream =
38+
Seq.toList "TIME FLIES LIKE AN ARROW"
39+
|> Seq.mapi (fun i c -> i, Label(frame, text=(string c), fg="black", bg="white"))
40+
|> AsyncRx.ofSeq
41+
|> AsyncRx.flatMap (fun (i, label) ->
42+
mouseMoves
43+
|> AsyncRx.delay (100 * i)
44+
|> AsyncRx.map (fun (x, y) -> label, x + i * 12 + 15, y))
45+
46+
let sink (ev: Notification<Label*int*int>) =
47+
async {
48+
match ev with
49+
| OnNext (label, x, y) -> queue.put(Place(label, x, y))
50+
| OnError(err) -> printfn $"Stream Error: {err}"
51+
| _ -> printfn "Stream Completed"
52+
return ()
53+
}
54+
55+
let mainAsync =
56+
async {
57+
use! disposable = stream.SubscribeAsync(sink)
58+
59+
let rec update () =
60+
let size = queue.qsize()
61+
for _ in 1..size do
62+
let msg = queue.get(false)
63+
64+
match msg with
65+
| Place (label, x, y) -> label.place(x, y)
66+
| _ -> ()
67+
68+
match size with
69+
| n when n > 0 -> root.after(1, update)
70+
| _ -> root.after(10, update)
71+
72+
root.after(1, update)
73+
root.mainloop()
74+
75+
return ()
76+
}
77+
78+
[<EntryPoint>]
79+
let main argv =
80+
printfn "Started ..."
81+
Async.Start mainAsync
82+
83+
0 // return an integer exit code
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
<WarnOn>3390;$(WarnOn)</WarnOn>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<Compile Include="Program.fs" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="Fable.Python" Version="0.5.0" />
15+
<PackageReference Include="FSharp.Control.AsyncRx" Version="1.6.0" />
16+
</ItemGroup>
17+
<ItemGroup>
18+
<ProjectReference Include="../GitHub/Fable/src/Fable.Core/Fable.Core.fsproj" />
19+
</ItemGroup>
20+
21+
</Project>

flask/Fable.Python.Flask.fsproj

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/Fable.Python.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<Author>Dag Brattli</Author>
77
<Copyright>Dag Brattli</Copyright>
88
<PackageLicenseFile>LICENSE</PackageLicenseFile>
9-
<Version>0.5.0</Version>
9+
<Version>0.6.0</Version>
1010
<WarnOn>3390;$(WarnOn)</WarnOn>
1111
</PropertyGroup>
1212
<ItemGroup>

src/stdlib/TkInter.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type Tk (screenName: string option) =
2929

3030
member _.update() = nativeOnly
3131
member _.mainloop() = nativeOnly
32-
member _.after(second: float, callback: unit -> unit) = nativeOnly
32+
member _.after(msecs: int, callback: unit -> unit) = nativeOnly
3333

3434
[<Import("Frame", "tkinter")>]
3535
type Frame(master: Misc) =

0 commit comments

Comments
 (0)