GHCJS:如何使用FFI导入高阶javascript函数?

时间:2013-10-16 22:49:42

标签: javascript haskell reactive-extensions-js rxjs ghcjs

如何在GHCJS中导入如下的Javascript函数?

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

我尝试了以下各种组合而没有成功:

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

感谢任何帮助,并链接到GHCJS FFI的文档。

由于

1 个答案:

答案 0 :(得分:12)

感谢GHCJS IRC频道的人们,我得到了答案:

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

谢谢