Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshuawuyts committed Dec 12, 2016
0 parents commit 188219b
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules/
coverage/
tmp/
npm-debug.log*
.DS_Store
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node_js:
- "0.12"
- "4"
sudo: false
language: node_js
script: "npm run test:cov"
after_script: "npm i -g codecov.io && cat ./coverage/lcov.info | codecov"
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Yoshua Wuyts

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# nanotick [![stability][0]][1]
[![npm version][2]][3] [![build status][4]][5] [![test coverage][6]][7]
[![downloads][8]][9] [![js-standard-style][10]][11]

Process.nextTick() batching utility.

## Usage
```js
var nanotick = require('nanotick')

var tick = nanotick()

var myFunc = tick(function () {
// do sync thing
})

// now resolves async
myFunc()
```

## API
### nanotick

## Installation
```sh
$ npm install nanotick
```

## License
[MIT](https://tldrlegal.com/license/mit-license)

[0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square
[1]: https://nodejs.org/api/documentation.html#documentation_stability_index
[2]: https://img.shields.io/npm/v/nanotick.svg?style=flat-square
[3]: https://npmjs.org/package/nanotick
[4]: https://img.shields.io/travis/yoshuawuyts/nanotick/master.svg?style=flat-square
[5]: https://travis-ci.org/yoshuawuyts/nanotick
[6]: https://img.shields.io/codecov/c/github/yoshuawuyts/nanotick/master.svg?style=flat-square
[7]: https://codecov.io/github/yoshuawuyts/nanotick
[8]: http://img.shields.io/npm/dm/nanotick.svg?style=flat-square
[9]: https://npmjs.org/package/nanotick
[10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
[11]: https://github.com/feross/standard
47 changes: 47 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
var assert = require('assert')

module.exports = nanotick

// Process.nextTick() batching ulity
// null -> fn(any) -> fn(any)
function nanotick () {
var callbacks = []
var interval = false

return function tick (cb) {
assert.equal(typeof cb, 'function', 'nanotick.tick: cb should be a function')

var isAsync = false

executeAsync(function () { isAsync = true })

return function wrappedTick () {
var length = arguments.length
var args = []
for (var i = 0; i < length; i++) {
args.push[arguments[i]]
}

if (isAsync) {
cb.apply(cb, arguments)
} else {
executeAsync(function () {
cb.apply(cb, arguments)
})
}
}
}

function executeAsync (cb) {
if (interval) return callbacks.push(cb)

interval = true
process.setTimeout(function () {
var length = callbacks.length
for (var i = 0; i < length; i++) {
callbacks[i]()
}
interval = false
}, 0)
}
}
27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "nanotick",
"version": "1.0.0",
"description": "Process.nextTick() batching utility",
"main": "index.js",
"scripts": {
"deps": "dependency-check . && dependency-check . --extra --no-dev",
"test": "standard && npm run deps && NODE_ENV=test node test",
"test:cov": "standard && npm run deps && NODE_ENV=test istanbul cover test.js"
},
"repository": "yoshuawuyts/nanotick",
"keywords": [
"batch",
"process",
"next-tick",
"settimeout",
"timer"
],
"license": "MIT",
"dependencies": {},
"devDependencies": {
"dependency-check": "^2.6.0",
"istanbul": "^0.4.5",
"standard": "^8.6.0",
"tape": "^4.6.3"
}
}
8 changes: 8 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var test = require('tape')
var nanotick = require('./')

test('should assert input types', function (t) {
t.plan(1)
var tick = nanotick()
t.throws(tick.bind(null), /function/)
})

0 comments on commit 188219b

Please sign in to comment.