├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | coverage/ 3 | tmp/ 4 | dist/ 5 | npm-debug.log* 6 | .DS_Store 7 | .nyc_output 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | node_js: 2 | - "4" 3 | - "5" 4 | - "6" 5 | - "7" 6 | sudo: false 7 | language: node_js 8 | script: "npm run test" 9 | # after_success: "npm i -g codecov && npm run coverage && codecov" 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Yoshua Wuyts 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, 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, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nanotask [![stability][0]][1] 2 | [![npm version][2]][3] [![build status][4]][5] 3 | [![downloads][8]][9] [![js-standard-style][10]][11] 4 | 5 | Microtask queue scheduler for the browser. Falls back to 6 | `window.requestAnimationFrame` in environments that don't allow 7 | `MutationObserver` based scheduling. 8 | 9 | The microtask queue is a set of tasks that runs at the end of the current frame 10 | in the browser, before recalculating style and painting. This is analogous to 11 | `process.nextTick()` in Node. There's no direct API for the microtask queue in 12 | the browser, but using `document.MutationObserver` we can trick the browser 13 | into running functions as part of the microtask queue. This is useful to create 14 | tight, 60fps loops in the main browser thread. 15 | 16 | ## Usage 17 | ```js 18 | var nanotask = require('nanotask') 19 | var queue = nanotask() 20 | 21 | queue(function () { 22 | console.log('resolved at the start of the next frame') 23 | }) 24 | ``` 25 | 26 | ## API 27 | ### `queue = nanotask()` 28 | Create a new Nanotask instance. 29 | 30 | ### `queue(fn)` 31 | Queue a function on the browser's microtask queue. 32 | 33 | ## Installation 34 | ```sh 35 | $ npm i nanotask 36 | ``` 37 | 38 | ## License 39 | [MIT](https://tldrlegal.com/license/mit-license) 40 | 41 | [0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square 42 | [1]: https://nodejs.org/api/documentation.html#documentation_stability_index 43 | [2]: https://img.shields.io/npm/v/nanotask.svg?style=flat-square 44 | [3]: https://npmjs.org/package/nanotask 45 | [4]: https://img.shields.io/travis/yoshuawuyts/nanotask/master.svg?style=flat-square 46 | [5]: https://travis-ci.org/yoshuawuyts/nanotask 47 | [6]: https://img.shields.io/codecov/c/github/yoshuawuyts/nanotask/master.svg?style=flat-square 48 | [7]: https://codecov.io/github/yoshuawuyts/nanotask 49 | [8]: http://img.shields.io/npm/dm/nanotask.svg?style=flat-square 50 | [9]: https://npmjs.org/package/nanotask 51 | [10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square 52 | [11]: https://github.com/feross/standard 53 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = nanotask 2 | 3 | function nanotask () { 4 | var node = document.createTextNode('') 5 | var queue = [] 6 | var i = 0 7 | 8 | new window.MutationObserver(function () { 9 | while (queue.length) queue.shift()() 10 | }).observe(node, { characterData: true }) 11 | 12 | return function (fn) { 13 | queue.push(fn) 14 | node.data = (i = 1 - i) 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nanotask", 3 | "description": "Microtask queue scheduler for the browser", 4 | "repository": "yoshuawuyts/nanotask", 5 | "version": "2.0.0", 6 | "scripts": { 7 | "deps": "dependency-check . && dependency-check . --extra --no-dev", 8 | "start": "node .", 9 | "test": "standard && npm run deps", 10 | "coverage": "nyc report --reporter=text-lcov > coverage.lcov" 11 | }, 12 | "dependencies": {}, 13 | "devDependencies": { 14 | "dependency-check": "^2.8.0", 15 | "nyc": "^10.1.2", 16 | "standard": "^9.0.2", 17 | "tape": "^4.6.3" 18 | }, 19 | "keywords": [ 20 | "microtask", 21 | "queue" 22 | ] 23 | } 24 | --------------------------------------------------------------------------------