【问题标题】:GHCJS: How do I import a high order javascript function using FFI?GHCJS:如何使用 FFI 导入高阶 javascript 函数?
【发布时间】:2013-10-16 22:49:41
【问题描述】:

如何在 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 文档的链接。

谢谢

【问题讨论】:

  • 我做到了,但我还没有找到一个示例,说明如何导入一个将 Haskell lambda 作为参数并将其转换为上述 JavaScript 函数的 javascript 函数。
  • 这是为了 Famo.us 代码库吗?

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


【解决方案1】:

感谢 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

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-06
    • 2019-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多