GHCJS: How do I import a high order javascript function using FFI?
Asked Answered
E

1

18

How do I import in GHCJS a Javascript function like the following ?

xs.subscribe(function(x) { console.log(x) })

I tried various combinations of the following without success:

data Observable_
data Disposable_

type Observable a = JSRef Observable_
type Disposable = JSRef ()

foreign import javascript unsafe "$1.subscribe($2)"
  rx_subscribe :: Observable a -> JSRef (a -> IO()) -> IO Disposable

Any help is appreciated, and links to documentation of the GHCJS FFI.

Thanks

Exultation answered 16/10, 2013 at 22:49 Comment(3)
Have you read weblog.luite.com/wordpress/?p=14 ?Moolah
I did, but I haven't found an example on how to import a javascript function that takes a Haskell lambda as argument and transforms it into a JavaScript function like the one above.Exultation
is this for a Famo.us codebase by any chance?Tradelast
E
12

Thanks to the guys on the GHCJS IRC Channel I got the answer:

foreign import javascript safe "$1.subscribe($2)"
  rx_subscribe :: Observable a -> JSFun (a -> IO()) -> IO Disposable

subscribe :: FromJSRef a => (a -> IO()) -> Observable a -> IO Disposable
subscribe f xs = syncCallback1 True True f' >>= rx_subscribe xs
                 where f' x = fromJSRef x >>= f . fromJust

Thank You

Exultation answered 17/10, 2013 at 21:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.