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

It's not possible to call `app.on('certificate-error', callback)` on electron. #75

Closed crocket closed 8 years ago

crocket commented 8 years ago

I'm trying to find a good way to wrap https://github.com/electron/electron/blob/master/docs/api/app.md#event-certificate-error

newtype AppBlahCallback = AppBlahCallback JSVal

foreign import javascript interruptible
  "app.on('blah', function (a, b, c, callback) { $c(a, b, c, callback); })"
  waitAppBlah :: IO (JSVal, JSVal, JSVal, AppBlahCallback)

foreign import javascript unsafe "$1($2)"
  appBlahCallback :: AppBlahCallback -> Bool -> IO ()

main :: IO ()
main = do
  forkIO $ do
    (_, _, _, cb) <- waitAppBlah
    appBlahCallback cb True

I cannot retrieve IO (JSVal, JSVal, JSVal, AppBlahCallback) from waitAppBlah more than once, but the event can call the callback multiple times.

If I retrieved

IO (JSVal, JSVal, JSVal, AppBlahCallback)

from waitAppBlah twice,

app.on('blah', function (a, b, c, callback) { $c(a, b, c, callback); })

would just be called twice. This doesn't make sense.

Since no function on GHCJS/Foreign/Callback.hs generates a callback of 4 or more parameters, it's not possible to write the following snippet, either.

newtype AppBlahCallback = AppBlahCallback JSVal

foreign import javascript unsafe
  "app.on('blah', $1)"
  onAppBlah :: Callback (JSVal -> JSVal -> JSVal -> AppBlahCallback -> IO ()) -> IO ()

foreign import javascript unsafe "$1($2)"
  appBlahCallback :: AppBlahCallback -> Bool -> IO ()

main :: IO ()
main = do
  onAppBlah $ asyncCallback4 $ \_ _ _ cb -> appBlahCallback cb True

Does ghcjs-base need modification before I become able to write a wrapper for https://github.com/electron/electron/blob/master/docs/api/app.md#event-certificate-error?

Perhaps, do we need the following functions?

syncCallbackN :: Int -> OnBlocked -> a -> IO (Callback a)

asyncCallbackN :: Int -> a -> IO (Callback a)

Or, how about

asyncCallback ::
  (JSVal -> JSVal -> ... -> JSVal -> IO a) ->
  IO (Callback (JSVal -> JSVal -> ... -> JSVal -> IO a))
crocket commented 8 years ago

I believe https://github.com/ghcjs/ghcjs-base/issues/76 subsumes this issue.

hamishmack commented 8 years ago

oops sorry did not see this and made #76 based on you gitter chat