Skip to content

Commit 93d71a2

Browse files
committed
Initial commit
0 parents  commit 93d71a2

File tree

11 files changed

+293
-0
lines changed

11 files changed

+293
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/.*
2+
!/.gitignore
3+
!/.jscsrc
4+
!/.jshintrc
5+
!/.travis.yml
6+
/bower_components/
7+
/node_modules/
8+
/output/

.jscsrc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"preset": "grunt",
3+
"disallowSpacesInFunctionExpression": null,
4+
"requireSpacesInFunctionExpression": {
5+
"beforeOpeningRoundBrace": true,
6+
"beforeOpeningCurlyBrace": true
7+
},
8+
"disallowSpacesInAnonymousFunctionExpression": null,
9+
"requireSpacesInAnonymousFunctionExpression": {
10+
"beforeOpeningRoundBrace": true,
11+
"beforeOpeningCurlyBrace": true
12+
},
13+
"disallowSpacesInsideObjectBrackets": null,
14+
"requireSpacesInsideObjectBrackets": "all",
15+
"validateQuoteMarks": "\"",
16+
"requireCurlyBraces": null
17+
}

.jshintrc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"bitwise": true,
3+
"eqeqeq": true,
4+
"forin": true,
5+
"freeze": true,
6+
"funcscope": true,
7+
"futurehostile": true,
8+
"strict": "global",
9+
"latedef": true,
10+
"noarg": true,
11+
"nocomma": true,
12+
"nonew": true,
13+
"notypeof": true,
14+
"singleGroups": true,
15+
"undef": true,
16+
"unused": true,
17+
"eqnull": true,
18+
"predef": ["exports", "setTimeout", "clearTimeout", "setInterval", "clearInterval"]
19+
}

.travis.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
language: node_js
2+
dist: trusty
3+
sudo: required
4+
node_js: 6
5+
env:
6+
- PATH=$HOME/purescript:$PATH
7+
install:
8+
- TAG=$(wget -q -O - https://github.com/purescript/purescript/releases/latest --server-response --max-redirect 0 2>&1 | sed -n -e 's/.*Location:.*tag\///p')
9+
- wget -O $HOME/purescript.tar.gz https://github.com/purescript/purescript/releases/download/$TAG/linux64.tar.gz
10+
- tar -xvf $HOME/purescript.tar.gz -C $HOME/
11+
- chmod a+x $HOME/purescript
12+
- npm install -g bower
13+
- npm install
14+
script:
15+
- bower install --production
16+
- npm run -s build
17+
- bower install
18+
- npm run -s test
19+
after_success:
20+
- >-
21+
test $TRAVIS_TAG &&
22+
echo $GITHUB_TOKEN | pulp login &&
23+
echo y | pulp publish --no-push

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 PureScript
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# purescript-js-timers
2+
3+
[![Latest release](http://img.shields.io/bower/v/purescript-js-timers.svg)](https://github.com/purescript-contrib/purescript-js-timers/releases)
4+
[![Build Status](https://travis-ci.org/purescript-contrib/purescript-js-timers.svg?branch=master)](https://travis-ci.org/purescript-contrib/purescript-js-timers)
5+
[![Maintainer: garyb](https://img.shields.io/badge/maintainer-garyb-lightgrey.svg)](http://github.com/garyb)
6+
7+
Low level bindings for JavaScript's timers API.
8+
9+
## Installation
10+
11+
```
12+
bower install purescript-js-timers
13+
```
14+
15+
## Documentation
16+
17+
Module documentation is [published on Pursuit](http://pursuit.purescript.org/packages/purescript-js-timers).

bower.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "purescript-js-timers",
3+
"homepage": "https://github.com/purescript-contrib/purescript-js-timers",
4+
"description": "PureScript library for native JavaScript timers",
5+
"license": "MIT",
6+
"repository": {
7+
"type": "git",
8+
"url": "git://github.com/purescript-contrib/purescript-js-timers.git"
9+
},
10+
"ignore": [
11+
"**/.*",
12+
"bower_components",
13+
"node_modules",
14+
"output",
15+
"test",
16+
"bower.json",
17+
"package.json"
18+
],
19+
"dependencies": {
20+
"purescript-eff": "^1.0.0"
21+
},
22+
"devDependencies": {
23+
"purescript-assert": "^1.0.0",
24+
"purescript-refs": "^1.0.0",
25+
"purescript-console": "^1.0.0"
26+
}
27+
}

package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"private": true,
3+
"scripts": {
4+
"clean": "rimraf output && rimraf .pulp-cache",
5+
"build": "jshint src && jscs src && pulp build --censor-lib --strict",
6+
"test": "pulp test"
7+
},
8+
"devDependencies": {
9+
"jscs": "^2.8.0",
10+
"jshint": "^2.9.1",
11+
"pulp": "^9.0.0",
12+
"purescript-psa": "^0.3.8",
13+
"rimraf": "^2.5.0"
14+
}
15+
}

src/Control/Monad/Eff/Timer.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* global exports */
2+
"use strict";
3+
4+
exports.setTimeout = function (ms) {
5+
return function (fn) {
6+
return function () {
7+
return setTimeout(fn, ms);
8+
};
9+
};
10+
};
11+
12+
exports.clearTimeout = function (id) {
13+
return function () {
14+
clearTimeout(id);
15+
};
16+
};
17+
18+
exports.setInterval = function (ms) {
19+
return function (fn) {
20+
return function () {
21+
return setInterval(fn, ms);
22+
};
23+
};
24+
};
25+
26+
exports.clearInterval = function (id) {
27+
return function () {
28+
clearInterval(id);
29+
};
30+
};

src/Control/Monad/Eff/Timer.purs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
module Control.Monad.Eff.Timer
2+
( TIMER
3+
, TimeoutId
4+
, setTimeout
5+
, clearTimeout
6+
, IntervalId
7+
, setInterval
8+
, clearInterval
9+
) where
10+
11+
import Prelude
12+
13+
import Control.Monad.Eff (Eff)
14+
15+
-- | The effect for the usage of a JavaScript timer.
16+
foreign import data TIMER :: !
17+
18+
-- | The ID of a timer started with `setTimeout`.
19+
newtype TimeoutId = TimeoutId Int
20+
21+
derive instance eqTimeoutId :: Eq TimeoutId
22+
derive instance ordTimeoutId :: Ord TimeoutId
23+
24+
-- | Runs an effectful function after the specified delay in milliseconds. The
25+
-- | returned `TimeoutId` can be used to cancel the timer before it completes.
26+
-- |
27+
-- | The timeout delay value is capped at 4ms by the JS API, any value less than
28+
-- | this will be clamped.
29+
foreign import setTimeout
30+
:: forall eff
31+
. Int
32+
-> Eff (timer :: TIMER | eff) Unit
33+
-> Eff (timer :: TIMER | eff) TimeoutId
34+
35+
-- | Cancels a timeout. If the timeout has already been cancelled or has already
36+
-- | elapsed this will have no effect.
37+
foreign import clearTimeout
38+
:: forall eff
39+
. TimeoutId
40+
-> Eff (timer :: TIMER | eff) Unit
41+
42+
-- | The ID of a timer started with `setInterval`.
43+
newtype IntervalId = IntervalId Int
44+
45+
derive instance eqIntervalId :: Eq IntervalId
46+
derive instance ordIntervalId :: Ord IntervalId
47+
48+
-- | Runs an effectful function after on a set interval with the specified delay
49+
-- | in milliseconds between iterations. The returned `IntervalId` can be used
50+
-- | to cancel the timer and prevent the interval from running any further.
51+
-- |
52+
-- | The interval delay value is capped at 4ms by the JS API, any value less
53+
-- | than this will be clamped.
54+
foreign import setInterval
55+
:: forall eff
56+
. Int
57+
-> Eff (timer :: TIMER | eff) Unit
58+
-> Eff (timer :: TIMER | eff) IntervalId
59+
60+
-- | Cancels an interval timer. If the interval has already been cancelled this
61+
-- | will have no effect.
62+
foreign import clearInterval
63+
:: forall eff
64+
. IntervalId
65+
-> Eff (timer :: TIMER | eff) Unit

test/Main.purs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
module Test.Main where
2+
3+
import Prelude
4+
5+
import Control.Monad.Eff (Eff)
6+
import Control.Monad.Eff.Console as C
7+
import Control.Monad.Eff.Ref as R
8+
import Control.Monad.Eff.Timer as T
9+
10+
import Test.Assert (ASSERT, assert)
11+
12+
main :: Eff (ref :: R.REF, timer :: T.TIMER, console :: C.CONSOLE, assert :: ASSERT) Unit
13+
main = do
14+
counter <- R.newRef 0
15+
16+
void $ T.setTimeout 10 do
17+
C.log "timeout increment counter"
18+
R.modifyRef counter (_ + 1)
19+
20+
void $ T.setTimeout 50 do
21+
C.log "timeout check counter"
22+
n <- R.readRef counter
23+
assert $ n == 1
24+
25+
void $ T.setTimeout 100 do
26+
27+
t <- T.setTimeout 20 do
28+
R.modifyRef counter (_ + 1)
29+
30+
T.clearTimeout t
31+
32+
void $ T.setTimeout 50 do
33+
C.log "check timeout never ran"
34+
n <- R.readRef counter
35+
assert $ n == 1
36+
37+
void $ T.setTimeout 200 do
38+
39+
i <- T.setInterval 20 do
40+
C.log "interval increment counter"
41+
R.modifyRef counter (_ + 1)
42+
43+
void $ T.setTimeout 90 do
44+
T.clearInterval i
45+
C.log "interval check counter"
46+
n <- R.readRef counter
47+
assert $ n == 5
48+
49+
void $ T.setTimeout 150 do
50+
C.log "check interval has stopped"
51+
n <- R.readRef counter
52+
assert $ n == 5

0 commit comments

Comments
 (0)