ghcjs / ghcjs-base

base library for GHCJS for JavaScript interaction and marshalling, used by higher level libraries like JSC
MIT License
45 stars 67 forks source link

Add multi argument callback helpers. #77

Closed crocket closed 7 years ago

crocket commented 7 years ago

This pull request does several things.

I tested syncCallbackMulti, syncCallbackMulti', and asyncCallbackMulti against the NodeJS example I included in README, and the functions worked well in my example.

This closes https://github.com/ghcjs/ghcjs-base/issues/76

It doesn't break existing functions. Thus, I want this pull request to be merged as soon as possible if maintainers don't have major objections.

hamishmack commented 7 years ago

LGTM

crocket commented 7 years ago

@hamishmack Would it be ok to replace JavaScript.Array.toListIO :: SomeJSArray m -> IO [JSVal] with GHCJS.Prim.fromJSArray :: JSVal -> IO [JSVal] in my multi-argument callback functions? I presented the definition of syncCallbackMulti below to facilitate discussion.

{- | Make a callback (JavaScript function) that runs the supplied IO function
     in a synchronous thread when called. The callback takes an arbitrary
     number of arguments that it passes as an array of JSVal values to the
     Haskell function.
     Call 'releaseCallback' on the callback when done with the callback,
     freeing data referenced by the function.
 -}
syncCallbackMulti :: OnBlocked -- ^ what to do when the thread blocks
                  -> ([JSVal] -> IO ()) -- ^ the Haskell action
                  -> IO (Callback ([JSVal] -> IO ())) -- ^ the callback
syncCallbackMulti onBlocked f = do
  js_syncCallbackMulti (onBlocked == ContinueAsync) $ unsafeCoerce $ \args ->
    Array.toListIO (unsafeCoerce args) >>= f

Update 1. the above function is now gone. The functions are now replaced with polyvariadic variants.

hamishmack commented 7 years ago

I think it would be fine.

crocket commented 7 years ago

@hamishmack @luite I think the pull request is ready for merge. I request that a tag, v0.2.1.0 be added to the git repository after merging this pull request. Is it ok for you to do so?