on master help sign in
Change name of `WebSocket.send` to `WebSocket.open`.

Begin adding documentation to raw JS files.

commit51accf643dcf1b2d9999e351a68b1912ce08fb14 authorevancz <info@elm-lang.org> dateWed 30 Jan 2013, 19:47 (14 years ago) parentcd35719
5 files changed+130−21
core-js/Signal/HTTP.js+43−6 core-js/Signal/Time.js+43−7 core-js/Signal/Touch.js+20 core-js/Signal/WebSocket.js+8−8 core-js/Signal/Window.js+16
core-js/Signal/HTTP.js+43−6
@@ -1,6 +1,31 @@
1+
2+/*! HTTP
3+A library for asynchronous HTTP requests (AJAX). See the
4+[WebSocket](http://elm-lang.org/docs/WebSocket.elm) library if
5+you have very strict latency requirements.
6+!*/
7+
18Elm.HTTP = function() {
29 var JS = Elm.JavaScript;
310 var toElmString = Elm.JavaScript.castJSStringToString;
11+
12+ /*[Creating Requests]*/
13+
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"]); }
18+
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"]); }; }
24+
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+ **/
429 function request(verb) { return function(url) { return function(data) {
530 return function(headers) {
631 return {0 : "Request",
@@ -10,12 +35,6 @@ Elm.HTTP = function() {
1035 data : data === null ? null : JS.castStringToJSString(data),
1136 headers : headers }; }; }; };
1237 }
13− /** get :: String -> Request String
14− Create a GET request to the given url.
15− **/
16− function get(url) { return request("GET")(url)(null)(["Nil"]); }
17− function post(url) { return function(data) {
18− return request("POST")(url)(data)(["Nil"]); }; }
1938
2039 function registerReq(queue,responses) { return function(req) {
2140 if (req.url !== "") { sendReq(queue,responses,req); }
@@ -58,6 +77,19 @@ Elm.HTTP = function() {
5877 return null;
5978 }
6079
80+ /*[Responses]*/
81+
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+ **/
86+
87+ /*[Sending Requests]*/
88+
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+ **/
6193 function send(requests) {
6294 var responses = Elm.Signal.constant(["Waiting"]);
6395 var sender = Elm.Signal.lift(registerReq([],responses))(requests);
@@ -65,6 +97,11 @@ Elm.HTTP = function() {
6597 return Elm.Signal.lift2(f)(responses)(sender);
6698 }
6799
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+ **/
104+
68105 return {get : get,
69106 post : post,
70107 request : request,
core-js/Signal/Time.js+43−7
@@ -1,12 +1,35 @@
1+/*! Time
2+Library for working with time. Type `Time` represents some number of
3+milliseconds.
4+!*/
5+
16Elm.Time = function() {
7+
8+ /*[Times]*/
9+
10+ /** hour, minute, second, ms :: Time
11+ Units of time, making it easier to specify things like a
12+ half-second `(second / 2)`.
13+ **/
14+
215 function timeNow() { return (new window.Date).getTime(); }
3− function everyWhen(isOn) { return function(t) {
4− var clock = Elm.Signal.constant(timeNow());
5− function tellTime() { Dispatcher.notify(clock.id, timeNow()); }
6− setInterval(tellTime, t);
7− return clock;
8− };
9− }
16+
17+ /*[Tickers]*/
18+
19+ /** fps :: Number -> Signal Time
20+ Takes desired number of frames per second (fps). The resulting signal
21+ gives a sequence of time deltas as quickly as possible until it reaches
22+ the desired FPS. A time delta is the time between the last frame and the
23+ current frame.
24+ **/
25+
26+ /** fpsWhen :: Number -> Signal Bool -> Signal Time
27+ Same as the fps function, but you can turn it on and off. Allows you
28+ to do brief animations based on user input without major ineffeciencies.
29+ The first time delta after a pause is always zero, no matter how long
30+ the pause was. This way summing the deltas will actually give the amount
31+ of time that the output signal has been running.
32+ **/
1033 function fpsWhen(desiredFPS) { return function (isOn) {
1134 var msPerFrame = 1000 / desiredFPS;
1235 var prev = timeNow(), curr = prev, diff = 0, wasOn = true;
@@ -32,6 +55,19 @@ Elm.Time = function() {
3255 return Elm.Signal.lift2(f)(isOn)(ticker);
3356 };
3457 }
58+
59+ /** every :: Time -> Signal Time
60+ Takes a time interval t. The resulting signal is the current time,
61+ updated every t.
62+ **/
63+ function everyWhen(isOn) { return function(t) {
64+ var clock = Elm.Signal.constant(timeNow());
65+ function tellTime() { Dispatcher.notify(clock.id, timeNow()); }
66+ setInterval(tellTime, t);
67+ return clock;
68+ };
69+ }
70+
3571 function since(t) { return function(s) {
3672 function cmp(a) { return function(b) { return !Value.eq(a,b); }; }
3773 var dcount = Elm.Signal.count(Elm.Signal.delay(t)(s));
core-js/Signal/Touch.js+20
@@ -1,3 +1,7 @@
1+/*! Touch
2+This is an early version of the touch library. It will likely grow to
3+include gestures that would be useful for both games and web-pages.
4+!*/
15
26Elm.Touch = function() {
37
@@ -73,9 +77,25 @@ Elm.Touch = function() {
7377 return sig;
7478 }
7579
80+ /*[Touches]*/
81+
82+ /** touches :: Signal [{ x :: Int, y :: Int, id :: Int, x0 :: Int, y0 :: Int, t0 :: Time }]
83+ A list of touches. Each ongoing touch is represented by a set of
84+ coordinates and an identifier id that allows you to distinguish
85+ between different touches. Each touch also contains the coordinates and
86+ time of the initial contact (x0, y0, and t0) which helps compute more
87+ complicated gestures.
88+ **/
7689 var touches = dependency(function(ts) {
7790 return Elm.JavaScript.castJSArrayToList(ts);
7891 });
92+
93+ /*[Gestures]*/
94+
95+ /** taps :: Signal { x :: Int, y :: Int }
96+ The last position that was tapped. Default value is `{x=0,y=0}`.
97+ Updates whenever the user taps the screen.
98+ **/
7999 var taps = function() {
80100 var sig = dependency(function(_) { return tap; });
81101 sig.defaultNumberOfKids = 1;
core-js/Signal/WebSocket.js+8−8
@@ -1,36 +1,36 @@
11Elm.WebSocket = function() {
22 var JS = Elm.JavaScript;
33
4− /** send :: String -> Signal String -> Signal String
4+ /** open :: String -> Signal String -> Signal String
55 Create a web-socket. The first argument is the URL of the desired
66 web-socket server. The input signal holds the outgoing messages,
77 and the resulting signal contains the incoming ones.
88 **/
9− function send(url) { return function(outgoing) {
9+ function open(url) { return function(outgoing) {
1010 var incoming = Elm.Signal.constant(["Nil"]);
1111 var ws = new window.WebSocket(JS.castStringToJSString(url));
1212
1313 var pending = [];
14− var open = false;
14+ var ready = false;
1515
1616 ws.onopen = function(e) {
1717 var len = pending.length;
1818 for (var i = 0; i < len; ++i) { ws.send(pending[i]); }
19− open = true;
19+ ready = true;
2020 };
2121 ws.onmessage = function(event) {
2222 Dispatcher.notify(incoming.id, JS.castJSStringToString(event.data));
2323 };
2424
25− function sendMsg(msg) {
25+ function send(msg) {
2626 var s = JS.castStringToJSString(msg);
27− open ? ws.send(s) : pending.push(s);
27+ ready ? ws.send(s) : pending.push(s);
2828 }
2929
3030 function take1(x) { return function(y) { return x; } }
31− return Elm.Signal.lift2(take1)(incoming)(Elm.Signal.lift(sendMsg)(outgoing));
31+ return Elm.Signal.lift2(take1)(incoming)(Elm.Signal.lift(send)(outgoing));
3232 };
3333 }
3434
35− return {send:send};
35+ return {open:open};
3636}();
core-js/Signal/Window.js+16
@@ -1,10 +1,26 @@
1+/*! Window !*/
2+
13Elm.Window = function() {
4+
5+ /*[Dimensions]*/
6+
7+ /** dimensions :: Signal (Int,Int)
8+ The current dimensions of the window (i.e. the area viewable to the
9+ user, not including scroll bars).
10+ **/
211 var dimensions = Elm.Signal.constant(Value.Tuple(window.innerWidth,
312 window.innerHeight));
413 dimensions.defaultNumberOfKids = 2;
514
15+ /** width :: Signal Int
16+ The current width of the window.
17+ **/
618 var width = Elm.Signal.lift(function(p){return p[1];})(dimensions);
719 width.defaultNumberOfKids = 0;
20+
21+ /** height :: Signal Int
22+ The current height of the window.
23+ **/
824 var height = Elm.Signal.lift(function(p){return p[2];})(dimensions);
925 height.defaultNumberOfKids = 0;
1026