Skip to content

Instantly share code, notes, and snippets.

@ctaggart
Last active August 29, 2015 14:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctaggart/8f182e632512f649d2df to your computer and use it in GitHub Desktop.
Save ctaggart/8f182e632512f649d2df to your computer and use it in GitHub Desktop.
edge.js
[<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
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
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
@hswick
Copy link

hswick commented Jul 23, 2015

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment