Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multi argument callback helpers. #77

Closed
wants to merge 25 commits into from
Closed

Add multi argument callback helpers. #77

wants to merge 25 commits into from

Conversation

crocket
Copy link
Contributor

@crocket crocket commented Oct 29, 2016

This pull request does several things.

  • It makes sure that line widths don't exceed 80 in GHCJS/Foreign/Callback.hs
  • It adds jsbits/callback.js which contains h$makeCallbackMulti.
  • It adds GHCJS/Foreign/Callback/Variadic.hs which enables polyvariadic callback functions.
  • It adds syncCallbackMulti, syncCallbackMulti', and asyncCallbackMulti to
    GHCJS/Foreign/Callback.hs. These functions utilize h$makeCallbackMulti and VariadicCallback for convenience of users. Refer to README for details.
  • It bumps up the version to 0.2.1.0
  • It documents OnBlocked and syncCallback' more clearly on README.
  • It documents syncCallbackMulti, syncCallbackMulti', and
    asyncCallbackMulti on README.

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

This closes #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.

This commit does several things.

* It makes sure that line widths don't exceed 80 in GHCJS/Foreign/Callback.hs
* It adds jsbits/callback.js which contains h$makeCallbackMulti.
* It adds syncCallbackMulti, syncCallbackMulti', and asyncCallbackMulti to
GHCJS/Foreign/Callback.hs. These functions utilize h$makeCallbackMulti.
* It bumps up the version to 0.2.1.0
* It documents `OnBlocked` and `syncCallback'` more clearly on README.
* It documents syncCallbackMulti, syncCallbackMulti', and
  asyncCallbackMulti on README.

I tested syncCallbackMulti, syncCallbackMulti', and asyncCallbackMulti
in the NodeJS example I included in README.
It interchanges a section on a callback example with a section on
multi-argument callbacks
README now shows an example of using `syncCallbackMulti'` instead of
`asyncCallbackMulti`.
asyncCallback1 :: (JSVal -> IO ()) -- ^ the function that the callback calls
-> IO (Callback (JSVal -> IO ())) -- ^ the calback
asyncCallback1 :: (JSVal -> IO ()) -- the function that the callback calls
-> IO (Callback (JSVal -> IO ())) -- the calback
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops. I don't think you meant to remove the haddock markup cues

Copy link
Contributor Author

@crocket crocket Oct 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I haven't learned haddock, yet. I'll restore haddock markup cues. I thought they were mere visual cues.

@hamishmack
Copy link
Member

LGTM

I made a mistake of removing haddock markup cues because I still don't
know how to use haddock.
@crocket
Copy link
Contributor Author

crocket commented Oct 31, 2016

@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
Copy link
Member

I think it would be fine.

@crocket
Copy link
Contributor Author

crocket commented Nov 9, 2016

@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?

ToArrayCallback was difficult to understand and use, and it used
`UndecidableInstances` extension. I replaced ToArrayCallback
with VariadicCallback and VariadicCallbackReturn.

Both VariadicCallback and VariadicCallbackReturn are much easier
to understand and use.

The functions that use VariadicCallback(Return) have simpler types
than those that use ToArrayCallback.
It is no longer necessary after replacing ToArrayCallback
with VariadicCallback(Return)
After IsJSVal was replaced with PFromJSVal,
the comments on variadic callback generators weren't updated.
With UndecidableInstances, I can remove helper functions.
Do not fear UndecidableInstances. UndecidableInstances just
makes programmers responsible for making instance resolution terminate
in a finite amount of time.
array copy is eliminated, and variadic callback generators work directly
on JSArray.
@crocket crocket closed this Nov 14, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add multi argument callback helpers
2 participants