Skip to content
This repository was archived by the owner on Oct 4, 2020. It is now read-only.

Commit a657a4e

Browse files
Cootgaryb
authored andcommitted
Request animation frame (#91)
1 parent 903572f commit a657a4e

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

src/DOM/HTML/Window.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,37 @@ exports.sessionStorage = function (window) {
201201
return window.sessionStorage;
202202
};
203203
};
204+
205+
exports._requestAnimationFrame = function(fn) {
206+
return function(window) {
207+
return function() {
208+
return window.requestAnimationFrame(fn);
209+
};
210+
};
211+
};
212+
213+
exports._cancelAnimationFrame = function(id) {
214+
return function(window) {
215+
return function() {
216+
return window.cancelAnimationFrame(id);
217+
};
218+
};
219+
};
220+
221+
exports._requestIdleCallback = function(opts) {
222+
return function(fn) {
223+
return function(window) {
224+
return function() {
225+
return window.requestIdleCallback(fn, opts);
226+
};
227+
};
228+
};
229+
};
230+
231+
exports._cancelIdleCallback = function(id) {
232+
return function(window) {
233+
return function() {
234+
return window.cancelIdleCallback(id);
235+
};
236+
};
237+
};

src/DOM/HTML/Window.purs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ module DOM.HTML.Window
2626
, url
2727
, localStorage
2828
, sessionStorage
29+
, requestAnimationFrame
30+
, cancelAnimationFrame
31+
, RequestAnimationFrameId
32+
, requestIdleCallback
33+
, cancelIdleCallback
34+
, RequestIdleCallbackId
2935
) where
3036

3137
import Control.Monad.Eff (Eff)
@@ -34,7 +40,8 @@ import DOM.HTML.Types (ALERT, CONFIRM, HISTORY, HTMLDocument, History, Location,
3440
import DOM.WebStorage.Types (Storage)
3541
import Data.Maybe (Maybe)
3642
import Data.Nullable (Nullable, toMaybe)
37-
import Prelude (Unit, (<$>))
43+
import Data.Newtype (class Newtype, unwrap)
44+
import Prelude (class Eq, class Ord, Unit, (<$>), (<<<), map)
3845

3946
foreign import document :: forall eff. Window -> Eff (dom :: DOM | eff) HTMLDocument
4047

@@ -102,3 +109,37 @@ foreign import scrollY :: forall eff. Window -> Eff (dom :: DOM | eff) Int
102109
foreign import localStorage :: forall eff. Window -> Eff (dom :: DOM | eff) Storage
103110

104111
foreign import sessionStorage :: forall eff. Window -> Eff (dom :: DOM | eff) Storage
112+
113+
newtype RequestAnimationFrameId = RequestAnimationFrameId Int
114+
115+
derive instance newtypeRequestAnimationFrameId :: Newtype RequestAnimationFrameId _
116+
derive instance eqRequestAnimationFrameId :: Eq RequestAnimationFrameId
117+
derive instance ordRequestAnimationFrameId :: Ord RequestAnimationFrameId
118+
119+
foreign import _requestAnimationFrame :: forall eff. Eff (dom :: DOM | eff) Unit -> Window -> Eff (dom :: DOM | eff) Int
120+
121+
requestAnimationFrame :: forall eff. Eff (dom :: DOM | eff) Unit -> Window -> Eff (dom :: DOM | eff ) RequestAnimationFrameId
122+
requestAnimationFrame fn = map RequestAnimationFrameId <<< _requestAnimationFrame fn
123+
124+
foreign import _cancelAnimationFrame :: forall eff. Int -> Window -> Eff (dom :: DOM | eff) Unit
125+
126+
cancelAnimationFrame :: forall eff. RequestAnimationFrameId -> Window -> Eff (dom :: DOM | eff) Unit
127+
cancelAnimationFrame idAF = _cancelAnimationFrame (unwrap idAF)
128+
129+
newtype RequestIdleCallbackId = RequestIdleCallbackId Int
130+
131+
derive instance newtypeRequestIdleCallbackId :: Newtype RequestIdleCallbackId _
132+
derive instance eqRequestIdleCallbackId :: Eq RequestIdleCallbackId
133+
derive instance ordRequestIdleCallbackId :: Ord RequestIdleCallbackId
134+
135+
foreign import _requestIdleCallback :: forall eff. { timeout :: Int } -> Eff (dom :: DOM | eff) Unit -> Window -> Eff (dom :: DOM | eff) Int
136+
137+
-- | Set timeout to `0` to get the same behaviour as when it is `undefined` in
138+
-- | [JavaScript](https://w3c.github.io/requestidlecallback/#h-the-requestidle-callback-method).
139+
requestIdleCallback :: forall eff. { timeout :: Int } -> Eff (dom :: DOM | eff) Unit -> Window -> Eff (dom :: DOM | eff ) RequestIdleCallbackId
140+
requestIdleCallback opts fn = map RequestIdleCallbackId <<< _requestIdleCallback opts fn
141+
142+
foreign import _cancelIdleCallback :: forall eff. Int -> Window -> Eff (dom :: DOM | eff) Unit
143+
144+
cancelIdleCallback :: forall eff. RequestIdleCallbackId -> Window -> Eff (dom :: DOM | eff) Unit
145+
cancelIdleCallback idAF = _cancelIdleCallback (unwrap idAF)

0 commit comments

Comments
 (0)