Skip to content

Commit 6da054b

Browse files
authored
Merge pull request #1 from mxstbr/travis
Travis
2 parents 9a2801a + 5715a96 commit 6da054b

4 files changed

Lines changed: 16 additions & 9 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
views.db
22
node_modules
33
.DS_STORE
4+
coverage

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
language: node_js
2+
node_js:
3+
- node
4+
- 7
5+
- 6
6+
script: npm run test -- --coverage
7+
cache: yarn
8+
after_success:
9+
- bash <(curl -s https://codecov.io/bash)

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module.exports = async function (req, res) {
3030
const currentViews = db.has(pathname) ? db.get(pathname).views.length : 0
3131
// Add a view and send the total views back to the client
3232
if (shouldIncrement) {
33-
pushView(pathname, { time: Date.now() })
33+
await pushView(pathname, { time: Date.now() })
3434
}
3535
if (req.method === 'GET') {
3636
send(res, 200, { views: shouldIncrement ? currentViews + 1 : currentViews })

src/utils.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ const db = require('./db')
44
// db messing up.
55
const pushView = async (key, view) => {
66
const locks = {}
7-
push()
7+
await push()
88

9-
function push() {
10-
if (locks[key]) return setImmediate(push)
9+
async function push() {
10+
if (locks[key]) return setImmediate(async () => { await push() })
1111
locks[key] = true
1212

1313
let views
@@ -18,11 +18,8 @@ const pushView = async (key, view) => {
1818
}
1919

2020
try {
21-
db.put(key, { views: views.concat([view]) })
22-
.then(() => {
23-
delete locks[key]
24-
})
25-
.catch(err => { throw err })
21+
await db.put(key, { views: views.concat([view]) })
22+
delete locks[key]
2623
} catch (err) {
2724
delete locks[key]
2825
throw err

0 commit comments

Comments
 (0)