最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

haskell - GHCJS: How do I import a high order javascript function using FFI? - Stack Overflow

programmeradmin7浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 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

发布评论

评论列表(0)

  1. 暂无评论