@@ -182,57 +182,62 @@ var Signal = function() {
182182 };
183183
184184 var HTTP = function() {
185− var fetch = function(how) { return function(url) {
186− var thread = Elm.Input(["Waiting"]);
187− var request = {};
188− if (window.XMLHttpRequest) { request = new XMLHttpRequest(); }
189− else if (window.ActiveXObject) { request = new ActiveXObject("Microsoft.XMLHTTP"); }
190− request.onreadystatechange = function(e) {
191− if (request.readyState === 4) {
192− Dispatcher.notify(thread.id,
193− request.status === 200
194− ? ["Success", toElmString(request.responseText)]
195− : ["Failure", request.status, toElmString(request.statusText)]);
196− }
197− };
198− request.open(how, Value.toText(url), true);
199− request.send(null);
200− return thread;
201− };
202− };
203− var fetches = function(how) { return function(input) {
204− var output = Elm.Input(["Nothing"]);
205− var fetcher = Elm.Lift(update, [input]);
206− var combine = Elm.Lift(function(x) { return function(y) { return x; } }, [output,fetcher]);
207− function update(strOpt) {
208− if (strOpt[0] !== "Just") {
209− try { Dispatcher.notify(output.id, ["Nothing"]); } catch(e) {}
210− return [];
211− }
212− try {
213− Dispatcher.notify(output.id, ["Just", ["Waiting"]]);
214− } catch(e) { output.value = ["Just", ["Waiting"]]; }
215− var request = {};
216− if (window.XMLHttpRequest) { request = new XMLHttpRequest(); }
217− else if (window.ActiveXObject) { request = new ActiveXObject("Microsoft.XMLHTTP"); }
218− request.onreadystatechange = function(e) {
219− if (request.readyState === 4) {
220− Dispatcher.notify(output.id,
221− ["Just", request.status === 200
222− ? ["Success", toElmString(request.responseText)]
223− : ["Failure", request.status, toElmString(request.statusText)]]);
224− }
225− };
226− request.open(how, Value.toText(strOpt[1]), true);
227− request.send(null);
228− return [];
229− }
230− return combine;
231− };
185+
186+ function request(verb) { return function(url) { return function(data) {
187+ return function(headers) {
188+ return {0 : "Request",
189+ length : 1,
190+ verb : Foreign.JavaScript.castStringToJSString(verb),
191+ url : Foreign.JavaScript.castStringToJSString(url),
192+ data : data === null ? null : Foreign.JavaScript.castStringToJSString(data),
193+ headers : headers }; }; }; };
194+ }
195+ function get(url) { return request("GET")(url)(null)(["Nil"]); }
196+ function post(url) { return function(data) {
197+ return request("POST")(url)(data)(["Nil"]); }; }
198+
199+ function sendReq(responses) { return function(req) {
200+ Dispatcher.notify(responses.id,["Waiting"]);
201+ var request = null;
202+ if (window.XMLHttpRequest) {
203+ request = new XMLHttpRequest();
204+ } else if (window.ActiveXObject) {
205+ request = new ActiveXObject("Microsoft.XMLHTTP");
206+ }
207+ request.onreadystatechange = function(e) {
208+ if (request.readyState === 4) {
209+ Dispatcher.notify(
210+ responses.id,
211+ request.status === 200
212+ ? ["Success", toElmString(request.responseText)]
213+ : ["Failure", request.status, toElmString(request.statusText)]);
214+ }
232215 };
233− return {get : fetch("GET"), post : fetch("POST"),
234− gets : fetches("GET"), posts : fetches("POST")
235− };
216+ request.open(req.verb, req.url, true);
217+ List.map(function(pair) {
218+ request.setRequestHeader(
219+ Foreign.JavaScript.castStringToJSString(pair[1]),
220+ Foreign.JavaScript.castStringToJSString(pair[2]));
221+ })(req.headers);
222+ request.send(req.data);
223+ return null;
224+ };
225+ }
226+
227+ function send(requests) {
228+ var responses = Elm.Input(["Waiting"]);
229+ var sender = Elm.Lift(sendReq(responses), [requests]);
230+ var combine = Elm.Lift(function(x){return function(y){return x;}},
231+ [responses,sender]);
232+ return combine;
233+ }
234+
235+ return {get : get,
236+ post : post,
237+ request : request,
238+ send : send,
239+ sendGet : function(urls){return send(Elm.Lift(get,[urls]));}
240+ };
236241 }();
237242 var Random = function() {
238243 var inRange = function(min) { return function(max) {