-
-
Notifications
You must be signed in to change notification settings - Fork 77
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
Call a Haskell function from JavaScript and have that function return data to JavaScript? #213
Comments
Maybe you could do something like this: haskellValidateString("this String may be valid", function (x) {
// Do something with x...
}); The callback function would be passed to Haskell as a haskellValidateString :: Window -> String -> JSObject -> IO ()
haskellValidateString = runUI $ \s callback -> runFunction $ ffi "callback(%1)" $ validate s
-- Then, when you have access to a Window object...
createHaskellFunction "haskellValidateString" $ haskellValidateString window |
Hey, thanks for the quick response. I tried that (with the haskellValidateString :: Window -> String -> JS.JSObject -> IO ()
haskellValidateString w = runUI w $ \s callback -> runFunction $ ffi "callback(%1)" $ validate s and got this:
I also tried this: haskellValidateString :: Window -> String -> JS.JSObject -> IO ()
haskellValidateString w s callback = runUI w $ runFunction $ ffi "callback(%1)" $ validate s which did typecheck and register, but got I think I'm misunderstanding how the Any further insight would be greatly appreciated. |
Oops - maybe I should have tested that function before I posted it... The correct function is: haskellValidateString :: Window -> String -> JS.JSObject -> IO ()
haskellValidateString w s callback = runUI w $ runFunction $ ffi "%1(%2)" callback $ validate s I did actually test this one and it worked: I successfully managed to send a result to JS using this function. It would be nice to have a less hacky way to do this though: the nicest way to do it would probably be to add an instance |
That worked great, thanks. Unfortunately since the callback is an asynchronous call it can't do anything outside of its scope. Still useful, though. I started experimenting with passing in a variable as a At some point when I have some free time I plan to dig a bit deeper into this, I'd love to be able to contribute to the project! Thanks again, |
At the moment, asynchronous calls are the only way to call Haskell from JavaScript. The reason is that I don't know how to handle nested chains like Haskell → JavaScript → Haskell → JavaScript → … For this to be possible, the JavaScript runtime would have to be multi-threaded (because a synchronous function has to freeze the program flow until the result is available, but a nested chain requires another program flow to be run.) Of course, that opens another can of worms. By the way, I have written up some details about the design of the JavaScript FFI. |
I'm not a JavaScript expert by any stretch, but promises look like they could work. Something like the following API could be used: newtype Promise = Promise { getPromise :: JSObject } -- Constructor and accessor are kept private; this newtype is for type-safety
toPromise :: ToJS a => a -> IO Promise
instance IsHandler (IO Promise) -- This returns the value as a JavaScript promise
--- then, in the program:
haskellValidateString :: String -> IO Promise
haskellValidateString s = toPromise $ validate s
obj <- exportHandler window haskellValidateString
runFunction $ ffi "window.validate = %1" obj // In the JavaScript program:
validate("test string").then(function(isValid) {
console.log(isValid);
}); Potentially a type argument could be added to |
Offshoot of #182.
I've been able to create a Haskell function that my JavaScript code can call and send variables back to Haskell:
Then in my JavaScript code I can do this:
and it prints in the REPL.
What I need to do is be able to pass back something from Haskell, so I can then use it in the JavaScript code:
The reason for this is to use Haskell functions to validate text in a prebuilt JavaScript json editing widget (https://github.com/jdorn/json-editor) which do validation inside a JS lambda.
Any insight on this? I've been banging my head on it for a bit...
Thanks!
The text was updated successfully, but these errors were encountered: