Skip to content

Commit ff06a65

Browse files
committed
Merge pull request #33 from ocharles/raf-timestamp
Add Performance.now and add time stamp information to requestAnimationFrame bindings
2 parents b0cce20 + f35b668 commit ff06a65

File tree

4 files changed

+56
-19
lines changed

4 files changed

+56
-19
lines changed

JavaScript/Web/AnimationFrame.hs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,24 @@ newtype AnimationFrameHandle = AnimationFrameHandle JSRef
3434
{- |
3535
Wait for an animation frame callback to continue running the current
3636
thread. Use 'GHCJS.Concurrent.synchronously' if the thread should
37-
not be preempted.
37+
not be preempted. This will return the high-performance clock time
38+
stamp once an animation frame is reached.
3839
-}
39-
waitForAnimationFrame :: IO ()
40+
waitForAnimationFrame :: IO Double
4041
waitForAnimationFrame = do
4142
h <- js_makeAnimationFrameHandle
4243
js_waitForAnimationFrame h `onException` js_cancelAnimationFrame h
4344

4445
{- |
4546
Run the action in an animationframe callback. The action runs in a
46-
synchronous thread.
47+
synchronous thread, and is passed the high-performance clock time
48+
stamp for that frame.
4749
-}
48-
inAnimationFrame :: OnBlocked -- ^ what to do when encountering a blocking call
49-
-> IO () -- ^ the action to run
50+
inAnimationFrame :: OnBlocked -- ^ what to do when encountering a blocking call
51+
-> (Double -> IO ()) -- ^ the action to run
5052
-> IO AnimationFrameHandle
5153
inAnimationFrame onBlocked x = do
52-
cb <- syncCallback onBlocked x
54+
cb <- syncCallback1 onBlocked (x . pFromJSRef)
5355
h <- js_makeAnimationFrameHandleCallback (jsref cb)
5456
js_requestAnimationFrame h
5557
return h
@@ -68,6 +70,6 @@ foreign import javascript unsafe "h$animationFrameCancel"
6870
js_cancelAnimationFrame :: AnimationFrameHandle -> IO ()
6971
foreign import javascript interruptible
7072
"$1.handle = window.requestAnimationFrame($c);"
71-
js_waitForAnimationFrame :: AnimationFrameHandle -> IO ()
73+
js_waitForAnimationFrame :: AnimationFrameHandle -> IO Double
7274
foreign import javascript unsafe "h$animationFrameRequest"
7375
js_requestAnimationFrame :: AnimationFrameHandle -> IO ()

JavaScript/Web/Performance.hs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{-# LANGUAGE ForeignFunctionInterface, JavaScriptFFI, InterruptibleFFI,
2+
DeriveDataTypeable
3+
#-}
4+
5+
{- | The Performance interface represents timing-related performance information for the given page.
6+
-}
7+
8+
module JavaScript.Web.Performance
9+
( now
10+
) where
11+
12+
import GHCJS.Foreign.Callback
13+
import GHCJS.Marshal.Pure
14+
import GHCJS.Types
15+
16+
import Control.Exception (onException)
17+
import Data.Typeable
18+
19+
newtype AnimationFrameHandle = AnimationFrameHandle (JSRef ())
20+
deriving (Typeable)
21+
22+
{- | The 'now' computation returns a high resolution time stamp, measured in
23+
milliseconds, accurate to one thousandth of a millisecond.
24+
25+
The value represented by 0 varies according the context, but
26+
in dedicated workers created from a Window context, the epoch is the value
27+
of the @PerformanceTiming.navigationStart@ property.
28+
-}
29+
now :: IO Double
30+
now = js_performanceNow
31+
{-# INLINE now #-}
32+
33+
-- -----------------------------------------------------------------------------
34+
35+
foreign import javascript unsafe "performance.now()"
36+
js_performanceNow :: IO Double

ghcjs-base.cabal

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ library
4444
GeneralizedNewtypeDeriving
4545
ScopedTypeVariables
4646
TypeOperators
47-
47+
4848
ghc-options: -O
4949
exposed-modules: Data.JSString
5050
Data.JSString.Int
@@ -107,6 +107,7 @@ library
107107
JavaScript.Web.File
108108
JavaScript.Web.MessageEvent
109109
JavaScript.Web.MessageEvent.Internal
110+
JavaScript.Web.Performance
110111
JavaScript.Web.Storage
111112
JavaScript.Web.Storage.Internal
112113
JavaScript.Web.StorageEvent
@@ -168,5 +169,3 @@ test-suite tests
168169
test-framework >= 0.4,
169170
test-framework-hunit >= 0.2,
170171
test-framework-quickcheck2 >= 0.2
171-
172-

jsbits/animationFrame.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
function h$animationFrameCancel(h) {
22
if(h.handle) window.cancelAnimationFrame(h.handle);
33
if(h.callback) {
4-
h$release(h.callback)
5-
h.callback = null;
4+
h$release(h.callback)
5+
h.callback = null;
66
}
77
}
88

99
function h$animationFrameRequest(h) {
10-
h.handle = window.requestAnimationFrame(function() {
11-
var cb = h.callback;
12-
if(cb) {
13-
h$release(cb);
14-
h.callback = null;
15-
cb();
16-
}
10+
h.handle = window.requestAnimationFrame(function(ts) {
11+
var cb = h.callback;
12+
if(cb) {
13+
h$release(cb);
14+
h.callback = null;
15+
cb(ts);
16+
}
1717
});
1818
}

0 commit comments

Comments
 (0)