Last active
August 29, 2015 14:02
-
-
Save ctaggart/8f182e632512f649d2df to your computer and use it in GitHub Desktop.
edge.js
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
[<AutoOpen>] | |
module EdgeJs.FSharp | |
open System | |
open System.Threading.Tasks | |
type Async with | |
static member Box (r:Async<'TResult>) = | |
async { | |
let! o = r | |
return box o | |
} | |
static member Unbox<'TResult> (r:Async<obj>) : Async<'TResult> = | |
async { | |
let! o = r | |
return unbox o | |
} | |
type Edge with | |
static member Func f = | |
Func<obj,Task<obj>>(fun o -> f(unbox o) |> Async.Box |> Async.StartAsTask) | |
static member Async js = | |
fun arg -> | |
let func = Edge.Func js | |
func.Invoke arg |> Async.AwaitTask |
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
open EdgeJs | |
let func = | |
Edge.Func | |
""" | |
return function(data, callback) { | |
callback(null, 'Node js ' + process.version + ' welcomes ' + data); | |
} | |
""" | |
[<EntryPoint>] | |
let main argv = | |
let msg = func.Invoke(".NET").Result | |
printfn "%A" msg | |
0 |
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
module Program | |
open EdgeJs | |
let appendNetVersion (s:string) = | |
async { return sprintf "%s embedded in a .NET %s app" s (System.Environment.Version.ToString()) } | |
type Funcs() = | |
member x.appendNetVersion = Edge.Func appendNetVersion | |
let getVersions = | |
Edge.Async | |
""" | |
return function(funcs, cb) { | |
funcs.appendNetVersion('Node.js ' + process.version, cb); | |
} | |
""" | |
[<EntryPoint>] | |
let main argv = | |
let s = getVersions(Funcs()) |> Async.Unbox |> Async.RunSynchronously | |
printfn "This has %s written in F#." s | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you show more examples using EdgeJs.FSharp module and how it works? Seems like a good start to porting Edge to F#, and also really useful for a current project I'm working on! :)
I can see you are using it in edgejs2.fs, but I'm a little confused
Thanks!