on 54ffaab help sign in
elk/core/Native/Time.js
112 lines 3.5 kB history raw
1/*! Time
2Library for working with time. Type `Time` represents some number of
3milliseconds.
4!*/
6Elm.Time = function() {
8 /*[Times]*/
10 /** hour, minute, second, ms : Time
11 Units of time, making it easier to specify things like a
12 half-second `(second / 2)`.
13 **/
15 function timeNow() { return (new window.Date).getTime(); }
17 /*[Tickers]*/
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 **/
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 **/
33 function fpsWhen(desiredFPS) { return function (isOn) {
34 var msPerFrame = 1000 / desiredFPS;
35 var prev = timeNow(), curr = prev, diff = 0, wasOn = true;
36 var ticker = Elm.Signal.constant(diff);
37 function tick(zero) { return function() {
38 curr = timeNow();
39 diff = zero ? 0 : curr - prev;
40 prev = curr;
41 Dispatcher.notify(ticker.id, diff);
42 };
43 }
44 var timeoutID = 0;
45 function f(isOn) { return function(t) {
46 if (isOn) {
47 timeoutID = setTimeout(tick(!wasOn && isOn), msPerFrame);
48 } else if (wasOn) {
49 clearTimeout(timeoutID);
50 }
51 wasOn = isOn;
52 return t;
53 };
54 }
55 return Elm.Signal.lift2(f)(isOn)(ticker);
56 };
57 }
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 }
71 function since(t) { return function(s) {
72 function cmp(a) { return function(b) { return !Value.eq(a,b); }; }
73 var dcount = Elm.Signal.count(Elm.Signal.delay(t)(s));
74 return Elm.Signal.lift2(cmp)(Elm.Signal.count(s))(dcount);
75 };
76 }
77 function after(t) {
78 t *= 1000;
79 var thread = Elm.Signal.constant(false);
80 setTimeout(function() { Dispatcher.notify(thread.id, true); }, t);
81 return thread;
82 }
83 function before(t) {
84 t *= 1000;
85 var thread = Elm.Signal.constant(true);
86 setTimeout(function() { Dispatcher.notify(thread.id, false); }, t);
87 return thread;
88 }
89 function read(s) {
90 var t = window.Date.parse(s);
91 return isNaN(t) ? ["Nothing"] : ["Just",t];
92 }
93 return {fpsWhen : fpsWhen,
94 fps : function(t) { return fpsWhen(t)(Elm.Signal.constant(true)); },
95 every : everyWhen(Elm.Signal.constant(true)),
96 delay : Elm.Signal.delay,
97 since : since,
98 after : after,
99 before : before,
100 hour : 3600000,
101 minute : 60000,
102 second : 1000,
103 ms : 1,
104 inHours : function(t) { return t / 3600000; },
105 inMinutes : function(t) { return t / 60000; },
106 inSeconds : function(t) { return t / 1000; },
107 inMss : function(t) { return t; },
108 toDate : function(t) { return new window.Date(t); },
109 read : read
110 };
112}();