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 #76

Open
hamishmack opened this issue Oct 27, 2016 · 4 comments
Open

Add multi argument callback helpers #76

hamishmack opened this issue Oct 27, 2016 · 4 comments

Comments

@hamishmack
Copy link
Member

We should add something like this to GHCJS.Foreign.Callback (based on function from jsaddle):

syncCallbackMulti :: OnBlocked -> (JSVal -> [JSVal] -> IO ())
                               -> IO (Callback (JSVal -> JSVal -> IO ()), Object)
syncCallbackMulti onBlocked f = do
    callback <- syncCallback2 onBlocked $ \this args -> do
        rargs <- Array.toListIO (coerce args)
        f this rargs
    (callback,) <$> makeMultiArgCallback callback

foreign import javascript unsafe "$r = function () { $1(this, arguments); }"
    makeMultiArgCallback :: Callback (JSVal -> JSVal -> IO ()) -> IO Object

Returns two values. The first is just for use with releaseCallback, the second is to pass on to JavaScript. We should change it to something more meaningful than Object.

@hamishmack
Copy link
Member Author

Not sure if we need the this argument (I have never used it in the jsaddle version), so we could probably get by with:

type CallbackMulti = Callback ([JSVal] -> IO ())

syncCallbackMulti :: OnBlocked -> ([JSVal] -> IO ())
                               -> IO (Callback (JSVal -> IO ()), CallbackMulti)
syncCallbackMulti onBlocked f = do
    callback <- syncCallback1 onBlocked $ \args ->
        Array.toListIO (coerce args) >>= f
    (callback,) <$> makeMultiArgCallback callback

foreign import javascript unsafe "$r = function () { $1(arguments); }"
    makeMultiArgCallback :: Callback (JSVal -> IO ()) -> IO CallbackMulti

@crocket
Copy link
Contributor

crocket commented Oct 28, 2016

I suggest documenting syncCallbackMulti, asyncCallbackMulti, and syncCallbackMulti' on README.

@crocket
Copy link
Contributor

crocket commented Oct 28, 2016

Is a polyvariadic function going to help here? I don't know much about polyvariadic functions.

@crocket
Copy link
Contributor

crocket commented Nov 3, 2016

@luite This is useful because http://electron.atom.io/docs/api/app/#event-certificate-error requires a callback of 6 arguments. Currently, ghcjs-base cannot handle event-certificate-error.

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 a pull request may close this issue.

2 participants