module UnityPeripherals.System.Threading.Tasks.Task
#nowarn "3370"
open System.Threading.Tasks
open Stdlib
open Stdlib.Syntax
open Stdlib.Asyncseq
open Stdlib.Asyncseq.Syntax
open UnityEngine
let reportError x = task {
try return! x
with e ->
Debug.Log @@ sprintf "[warning - background error] \"\"\"%A\"\"\"" e
}
type objnode = System.Text.Json.Nodes.JsonNode
type objexception = System.Text.Json.JsonException
let dev_ttyACM0 = "/dev/ttyACM0"
let arduino_avr_uno = "arduino:avr:uno"
type start'<
'Impl, 'T, 'TParam, 'TBuilder when 'Impl :> UnityPeripherals.editor_os<'T, 'TParam, 'TBuilder> and 'TBuilder :> Stdlib.Evaluate<'T>
and 'TBuilder :> Stdlib.Serializable<'T, 'TParam>
and 'TBuilder : (new : unit -> 'TBuilder)
// and 'Impl : (new : UnityPeripherals.syntax<'T, 'TParam, 'TBuilder> -> 'Impl)
>(f : UnityPeripherals.syntax<'T, 'TParam, 'TBuilder> -> 'Impl) =
let builder = new 'TBuilder ()
let x = f (new UnityPeripherals.syntax<'T, 'TParam, 'TBuilder>(builder))
let b = Frduino.Genlet.B.Make(fun () ->
(x.PullStream (new Frduino.Genlet.For_lib.Async.env ())) :?> Frduino.Genlet.Code<_, unit>)
let lock = new System.Threading.CancellationTokenSource ()
let m = Frduino.With_prgmr.session.v ()
member self.prepare with get () = reportError @@ task {
try
let code =
Frduino.Genlet.Gen.reset ()
let x = b.a
Frduino.Genlet.Gen.reset ()
x
// Debug.Log $"[editor_os system 1] code: '{code}'"
Frduino.Genlet.Gen.reset ()
do! Frduino.With_prgmr.compile m arduino_avr_uno dev_ttyACM0 code
Debug.Log "[editor_os system 1] done uploading"
do! Task.Delay 1000
with e ->
Debug.Log $"[editor_os system 1] uncaught exception: {e}"
Debug.Log "[editor_os system 1] rewatching (1s)"
do! Task.Delay 1000
do! self.prepare
}
member self.this with get() = taskseq {
try
for str in Serial.read dev_ttyACM0 lock.Token do
Debug.Log "[editor_os system 2] received"
let model = // TODO(kinten) fuck me, parse is blocking
try Option.ofObj <| objnode.Parse str
with :? objexception -> None
yield! model |> Seq.Option.bind @@ fun model ->
Debug.Log "[editor_os system 2] parsed"
let x =
try Some @@ builder.eval model
with :? System.NullReferenceException -> None
// Debug.Log$"[editor_os system 2] evaluated: '{x}'"
x |> Seq.Option.bind @@ fun x ->
taskseq { yield x }
Debug.Log "[editor_os system 2] done reading socket"
Debug.Log "[editor_os system 2] rewatching (1s)"
do! Task.Delay 1000
yield! self.this
with
| :? TaskCanceledException as u when lock.Token.Equals u.CancellationToken ->
Debug.Log "[editor_os system 2] caught a comp_depend cancellation"
Debug.Log "[editor_os system 2] retrying (1s)"
do! Task.Delay 1000
yield! self.this
| Serial.DeviceNotFound ->
Debug.Log $"[editor_os system 2] error: device not found"
Debug.Log "[editor_os system 2] retrying (1s)"
do! Task.Delay 1000
yield! self.this
| e ->
Debug.Log $"[editor_os system 2] uncaught exception: {e}"
Debug.Log "[editor_os system 2] retrying (1s)"
do! Task.Delay 1000
yield! self.this
}
let rec start<
'Impl, 'T, 'TParam, 'TBuilder when 'Impl :> UnityPeripherals.editor_os<'T, 'TParam, 'TBuilder> and 'TBuilder :> Stdlib.Evaluate<'T>
and 'TBuilder :> Stdlib.Serializable<'T, 'TParam>
and 'TBuilder : (new : unit -> 'TBuilder)
// and 'Impl : (new : UnityPeripherals.syntax<'T, 'TParam, 'TBuilder> -> 'Impl)
> (f : UnityPeripherals.syntax<'T, 'TParam, 'TBuilder> -> 'Impl) = taskseq {
let s = new start'<'Impl, 'T, 'TParam, 'TBuilder> (f)
do! s.prepare
yield! s.this
}
module Object =
type param<
'Impl, 'T, 'TParam, 'TBuilder when 'Impl :> UnityPeripherals.editor_os<'T, 'TParam, 'TBuilder> and 'TBuilder :> Stdlib.Evaluate<'T>
and 'TBuilder :> Stdlib.Serializable<'T, 'TParam>
and 'TBuilder : (new : unit -> 'TBuilder)
> = Make of (UnityPeripherals.syntax<'T, 'TParam, 'TBuilder> -> 'Impl)
let task<
'Impl, 'T, 'TParam, 'TBuilder when 'Impl :> UnityPeripherals.editor_os<'T, 'TParam, 'TBuilder> and 'TBuilder :> Stdlib.Evaluate<'T>
and 'TBuilder :> Stdlib.Serializable<'T, 'TParam>
and 'TBuilder : (new : unit -> 'TBuilder)
> (make: UnityPeripherals.syntax<'T, 'TParam, 'TBuilder> -> 'Impl) =
Make make
let finish<
'Impl, 'T, 'TParam, 'TBuilder when 'Impl :> UnityPeripherals.editor_os<'T, 'TParam, 'TBuilder> and 'TBuilder :> Stdlib.Evaluate<'T>
and 'TBuilder :> Stdlib.Serializable<'T, 'TParam>
and 'TBuilder : (new : unit -> 'TBuilder)
> (it: param<'Impl, 'T, 'TParam, 'TBuilder>) =
let (Make f) = it
start<'Impl, 'T, 'TParam, 'TBuilder> f