简体   繁体   English

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

[英]GHCJS: How do I import a high order javascript function using FFI?

How do I import in GHCJS a Javascript function like the following ? 如何在GHCJS中导入如下Javascript函数?

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. 感谢任何帮助,并链接到GHCJS FFI的文档。

Thanks 谢谢

Thanks to the guys on the GHCJS IRC Channel I got the answer: 感谢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

Thank You 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM