on d9d1dfd help sign in
elk/core-js/Signal/HTTP.js
111 lines 3.6 kB history raw
2/*! HTTP
3A library for asynchronous HTTP requests (AJAX). See the
4[WebSocket](http://elm-lang.org/docs/WebSocket.elm) library if
5you have very strict latency requirements.
6!*/
8Elm.HTTP = function() {
9 var JS = Elm.JavaScript;
10 var toElmString = Elm.JavaScript.castJSStringToString;
12 /*[Creating Requests]*/
14 /** get : String -> Request String
15 Create a GET request to the given url.
16 **/
17 function get(url) { return request("GET")(url)(null)(["Nil"]); }
19 /** post : String -> String -> Request String
20 Create a POST request to the given url, carrying the given data.
21 **/
22 function post(url) { return function(data) {
23 return request("POST")(url)(data)(["Nil"]); }; }
25 /** request : String -> String -> String -> [(String,String)] -> Request String
26 Create a customized request. Arguments are request type (get, post, put,
27 delete, etc.), target url, data, and a list of additional headers.
28 **/
29 function request(verb) { return function(url) { return function(data) {
30 return function(headers) {
31 return {0 : "Request",
32 length : 1,
33 verb : JS.castStringToJSString(verb),
34 url : JS.castStringToJSString(url),
35 data : data === null ? null : JS.castStringToJSString(data),
36 headers : headers }; }; }; };
37 }
39 function registerReq(queue,responses) { return function(req) {
40 if (req.url !== "") { sendReq(queue,responses,req); }
41 };
42 }
44 function updateQueue(queue,responses) {
45 if (queue.length > 0) {
46 Dispatcher.notify(responses.id, queue[0].value);
47 if (queue[0].value[0] !== "Waiting") {
48 queue.shift();
49 setTimeout(function() { updateQueue(queue,responses); }, 0);
50 }
51 }
52 }
54 function sendReq(queue,responses,req) {
55 var response = { value: ["Waiting"] };
56 queue.push(response);
58 var request = null;
59 if (window.ActiveXObject) { request = new ActiveXObject("Microsoft.XMLHTTP"); }
60 if (window.XMLHttpRequest) { request = new XMLHttpRequest(); }
61 request.onreadystatechange = function(e) {
62 if (request.readyState === 4) {
63 response.value = (request.status === 200
64 ? ["Success", toElmString(request.responseText)]
65 : ["Failure", request.status,
66 toElmString(request.statusText)]);
67 setTimeout(function() { updateQueue(queue,responses); }, 0);
68 }
69 };
70 request.open(req.verb, req.url, true);
71 Elm.List.map(function(pair) {
72 request.setRequestHeader(
73 JS.castStringToJSString(pair[1]),
74 JS.castStringToJSString(pair[2]));
75 })(req.headers);
76 request.send(req.data);
77 return null;
78 }
80 /*[Responses]*/
82 /** data Response a = Waiting | Success a | Failure Int String
83 The datatype for responses. Success contains only the returned message.
84 Failures contain both an error code and an error message.
85 **/
87 /*[Sending Requests]*/
89 /** send : Signal (Request a) -> Signal (Response String)
90 Performs an HTTP request with the given requests. Produces a signal
91 that carries the responses.
92 **/
93 function send(requests) {
94 var responses = Elm.Signal.constant(["Waiting"]);
95 var sender = Elm.Signal.lift(registerReq([],responses))(requests);
96 function f(x) { return function(y) { return x; } }
97 return Elm.Signal.lift2(f)(responses)(sender);
98 }
100 /** sendGet : Signal String -> Signal (Response String)
101 Performs an HTTP GET request with the given urls. Produces a signal
102 that carries the responses.
103 **/
105 return {get : get,
106 post : post,
107 request : request,
108 send : send,
109 sendGet : function(urls){return send(Elm.Signal.lift(get)(urls));}
110 };
111}();