on master help sign in
Change name of `WebSocket.send` to `WebSocket.open`.
51accf6evancz
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+
1Elm.HTTP = function() {
8Elm.HTTP = function() {
2 var JS = Elm.JavaScript;
9 var JS = Elm.JavaScript;
3 var toElmString = Elm.JavaScript.castJSStringToString;
10 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+ **/
4 function request(verb) { return function(url) { return function(data) {
29 function request(verb) { return function(url) { return function(data) {
5 return function(headers) {
30 return function(headers) {
6 return {0 : "Request",
31 return {0 : "Request",
@@ -10,12 +35,6 @@ Elm.HTTP = function() {
10 data : data === null ? null : JS.castStringToJSString(data),
35 data : data === null ? null : JS.castStringToJSString(data),
11 headers : headers }; }; }; };
36 headers : headers }; }; }; };
12 }
37 }
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"]); }; }
19
38
20 function registerReq(queue,responses) { return function(req) {
39 function registerReq(queue,responses) { return function(req) {
21 if (req.url !== "") { sendReq(queue,responses,req); }
40 if (req.url !== "") { sendReq(queue,responses,req); }
@@ -58,6 +77,19 @@ Elm.HTTP = function() {
58 return null;
77 return null;
59 }
78 }
60
79
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+ **/
61 function send(requests) {
93 function send(requests) {
62 var responses = Elm.Signal.constant(["Waiting"]);
94 var responses = Elm.Signal.constant(["Waiting"]);
63 var sender = Elm.Signal.lift(registerReq([],responses))(requests);
95 var sender = Elm.Signal.lift(registerReq([],responses))(requests);
@@ -65,6 +97,11 @@ Elm.HTTP = function() {
65 return Elm.Signal.lift2(f)(responses)(sender);
97 return Elm.Signal.lift2(f)(responses)(sender);
66 }
98 }
67
99
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+
68 return {get : get,
105 return {get : get,
69 post : post,
106 post : post,
70 request : request,
107 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+
1Elm.Time = function() {
6Elm.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+
2 function timeNow() { return (new window.Date).getTime(); }
15 function timeNow() { return (new window.Date).getTime(); }
3− function everyWhen(isOn) { return function(t) {
16+
4− var clock = Elm.Signal.constant(timeNow());
17+ /*[Tickers]*/
5− function tellTime() { Dispatcher.notify(clock.id, timeNow()); }
18+
6− setInterval(tellTime, t);
19+ /** fps :: Number -> Signal Time
7− return clock;
20+ Takes desired number of frames per second (fps). The resulting signal
8− };
21+ gives a sequence of time deltas as quickly as possible until it reaches
9− }
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+ **/
10 function fpsWhen(desiredFPS) { return function (isOn) {
33 function fpsWhen(desiredFPS) { return function (isOn) {
11 var msPerFrame = 1000 / desiredFPS;
34 var msPerFrame = 1000 / desiredFPS;
12 var prev = timeNow(), curr = prev, diff = 0, wasOn = true;
35 var prev = timeNow(), curr = prev, diff = 0, wasOn = true;
@@ -32,6 +55,19 @@ Elm.Time = function() {
32 return Elm.Signal.lift2(f)(isOn)(ticker);
55 return Elm.Signal.lift2(f)(isOn)(ticker);
33 };
56 };
34 }
57 }
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+
35 function since(t) { return function(s) {
71 function since(t) { return function(s) {
36 function cmp(a) { return function(b) { return !Value.eq(a,b); }; }
72 function cmp(a) { return function(b) { return !Value.eq(a,b); }; }
37 var dcount = Elm.Signal.count(Elm.Signal.delay(t)(s));
73 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+!*/
1
5
2Elm.Touch = function() {
6Elm.Touch = function() {
3
7
@@ -73,9 +77,25 @@ Elm.Touch = function() {
73 return sig;
77 return sig;
74 }
78 }
75
79
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+ **/
76 var touches = dependency(function(ts) {
89 var touches = dependency(function(ts) {
77 return Elm.JavaScript.castJSArrayToList(ts);
90 return Elm.JavaScript.castJSArrayToList(ts);
78 });
91 });
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+ **/
79 var taps = function() {
99 var taps = function() {
80 var sig = dependency(function(_) { return tap; });
100 var sig = dependency(function(_) { return tap; });
81 sig.defaultNumberOfKids = 1;
101 sig.defaultNumberOfKids = 1;
core-js/Signal/WebSocket.js+8−8
@@ -1,36 +1,36 @@
1Elm.WebSocket = function() {
1Elm.WebSocket = function() {
2 var JS = Elm.JavaScript;
2 var JS = Elm.JavaScript;
3
3
4− /** send :: String -> Signal String -> Signal String
4+ /** open :: String -> Signal String -> Signal String
5 Create a web-socket. The first argument is the URL of the desired
5 Create a web-socket. The first argument is the URL of the desired
6 web-socket server. The input signal holds the outgoing messages,
6 web-socket server. The input signal holds the outgoing messages,
7 and the resulting signal contains the incoming ones.
7 and the resulting signal contains the incoming ones.
8 **/
8 **/
9− function send(url) { return function(outgoing) {
9+ function open(url) { return function(outgoing) {
10 var incoming = Elm.Signal.constant(["Nil"]);
10 var incoming = Elm.Signal.constant(["Nil"]);
11 var ws = new window.WebSocket(JS.castStringToJSString(url));
11 var ws = new window.WebSocket(JS.castStringToJSString(url));
12
12
13 var pending = [];
13 var pending = [];
14− var open = false;
14+ var ready = false;
15
15
16 ws.onopen = function(e) {
16 ws.onopen = function(e) {
17 var len = pending.length;
17 var len = pending.length;
18 for (var i = 0; i < len; ++i) { ws.send(pending[i]); }
18 for (var i = 0; i < len; ++i) { ws.send(pending[i]); }
19− open = true;
19+ ready = true;
20 };
20 };
21 ws.onmessage = function(event) {
21 ws.onmessage = function(event) {
22 Dispatcher.notify(incoming.id, JS.castJSStringToString(event.data));
22 Dispatcher.notify(incoming.id, JS.castJSStringToString(event.data));
23 };
23 };
24
24
25− function sendMsg(msg) {
25+ function send(msg) {
26 var s = JS.castStringToJSString(msg);
26 var s = JS.castStringToJSString(msg);
27− open ? ws.send(s) : pending.push(s);
27+ ready ? ws.send(s) : pending.push(s);
28 }
28 }
29
29
30 function take1(x) { return function(y) { return x; } }
30 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));
32 };
32 };
33 }
33 }
34
34
35− return {send:send};
35+ return {open:open};
36}();
36}();
core-js/Signal/Window.js+16
@@ -1,10 +1,26 @@
1+/*! Window !*/
2+
1Elm.Window = function() {
3Elm.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+ **/
2 var dimensions = Elm.Signal.constant(Value.Tuple(window.innerWidth,
11 var dimensions = Elm.Signal.constant(Value.Tuple(window.innerWidth,
3 window.innerHeight));
12 window.innerHeight));
4 dimensions.defaultNumberOfKids = 2;
13 dimensions.defaultNumberOfKids = 2;
5
14
15+ /** width :: Signal Int
16+ The current width of the window.
17+ **/
6 var width = Elm.Signal.lift(function(p){return p[1];})(dimensions);
18 var width = Elm.Signal.lift(function(p){return p[1];})(dimensions);
7 width.defaultNumberOfKids = 0;
19 width.defaultNumberOfKids = 0;
20+
21+ /** height :: Signal Int
22+ The current height of the window.
23+ **/
8 var height = Elm.Signal.lift(function(p){return p[2];})(dimensions);
24 var height = Elm.Signal.lift(function(p){return p[2];})(dimensions);
9 height.defaultNumberOfKids = 0;
25 height.defaultNumberOfKids = 0;
10
26