Created
May 27, 2015 16:33
-
-
Save luite/85f4c56f24412d363be6 to your computer and use it in GitHub Desktop.
XHR with interruptible FFI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# LANGUAGE JavaScriptFFI #-} | |
| module XhrB (xhr) where | |
| import GHCJS.Types | |
| import Control.Exception (onException) | |
| data XHR_ | |
| type XHR = JSRef XHR_ | |
| foreign import javascript unsafe | |
| "new XMLHttpRequest()" | |
| js_createXHR :: IO XHR | |
| foreign import javascript unsafe | |
| "$1.abort();" | |
| js_abortXHR :: IO () | |
| foreign import javascript interruptible | |
| "$2.open('GET', $1);\ | |
| \$2.onload = function() { $c($2.responseText); };\ | |
| \$2.send();" | |
| js_sendXHR :: JSString -> XHR -> IO JSString | |
| xhr :: JSString -> IO JSString | |
| xhr url = do | |
| x <- js_createXHR | |
| js_sendXHR url x `onException` js_abortXHR x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment