diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5c49d4c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +node_modules/ +coverage/ +tmp/ +npm-debug.log* +.DS_Store diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..e533238 --- /dev/null +++ b/.travis.yml @@ -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" diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c03ab53 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e2beef1 --- /dev/null +++ b/README.md @@ -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 diff --git a/index.js b/index.js new file mode 100644 index 0000000..a60e477 --- /dev/null +++ b/index.js @@ -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) + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..b5b7500 --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/test.js b/test.js new file mode 100644 index 0000000..035cc41 --- /dev/null +++ b/test.js @@ -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/) +})