How do I import in GHCJS a Javascript function like the following ?
xs.subscribe(function(x) { console.log(x) })
I tried various binations 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
How do I import in GHCJS a Javascript function like the following ?
xs.subscribe(function(x) { console.log(x) })
I tried various binations 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
Share Improve this question edited Oct 17, 2013 at 7:33 Jonke 6,5432 gold badges28 silver badges40 bronze badges asked Oct 16, 2013 at 22:49 HoloedHoloed 6434 silver badges15 bronze badges 3- 3 Have you read weblog.luite./wordpress/?p=14 ? – Jonke Commented Oct 17, 2013 at 7:38
- 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. – Holoed Commented Oct 17, 2013 at 8:57
- is this for a Famo.us codebase by any chance? – Erik Kaplun Commented May 4, 2015 at 16:46
1 Answer
Reset to default 12Thanks 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