├── .gitignore ├── .jshintrc ├── .jshintrc-test ├── README.md ├── package.json ├── LICENSE.txt ├── lib └── index.js └── test └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/ 2 | /coverage/ 3 | npm-debug.log 4 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "bitwise":false, 3 | "camelcase":false, 4 | "curly":false, 5 | "eqeqeq":true, 6 | "freeze":true, 7 | "immed":true, 8 | "indent":2, 9 | "latedef":"nofunc", 10 | "laxbreak":true, 11 | "laxcomma":true, 12 | "newcap":true, 13 | "noarg":true, 14 | "node":true, 15 | "strict": true, 16 | "trailing":true, 17 | "undef":true 18 | } -------------------------------------------------------------------------------- /.jshintrc-test: -------------------------------------------------------------------------------- 1 | { 2 | "bitwise":false, 3 | "camelcase":false, 4 | "curly":false, 5 | "eqeqeq":true, 6 | "freeze":true, 7 | "immed":true, 8 | "indent":2, 9 | "latedef":"nofunc", 10 | "laxbreak":true, 11 | "laxcomma":true, 12 | "newcap":true, 13 | "noarg":true, 14 | "node":true, 15 | "strict": true, 16 | "trailing":true, 17 | "undef":true, 18 | "globals":{ 19 | "it":true, 20 | "describe":true, 21 | "before":true, 22 | "after":true, 23 | "beforeEach":true, 24 | "afterEach":true, 25 | "assert":true 26 | } 27 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | callback-pool 2 | ========== 3 | 4 | Simple utility to queue callbacks in a pool until the drain has been unplugged. 5 | 6 | `npm install callback-pool --save` 7 | 8 | ##Example 9 | ````javascript 10 | var pool = require('callback-pool'); 11 | var myPool = pool.create();//creates a new pool 12 | 13 | //these are queued up 14 | myPool.add(function(a, b){console.log('foo');}); 15 | myPool.add(function(a, b){console.log('boo');}); 16 | 17 | myPool.drain(1,2);//all callbacks are executed asynchronously with 1 and 2. 18 | 19 | //these are now asynchronously executed immediately with 1 and 2 20 | myPool.add(function(a, b){console.log('execute me');}); 21 | myPool.add(function(a, b){console.log('me too');}); 22 | 23 | myPool.plug();//allows you to start over. 24 | ```` 25 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "callback-pool", 3 | "version": "1.0.1", 4 | "description": "Simple utility to queue callbacks in a pool until the drain has been unplugged.", 5 | "main": "lib", 6 | "directories": { 7 | "test": "test" 8 | }, 9 | "scripts": { 10 | "jshint": "jshint lib && jshint --config .jshintrc-test test", 11 | "cover": "istanbul cover ./node_modules/.bin/_mocha -- -r should -R spec 'test/**/*.js'", 12 | "report": "istanbul report cobertura", 13 | "test": "npm run-script jshint && npm run-script cover && npm run-script report" 14 | }, 15 | "repository": { 16 | "type": "git", 17 | "url": "https://github.com/godaddy/node-callback-pool.git" 18 | }, 19 | "keywords": [ 20 | "drain", 21 | "callbacks", 22 | "queue", 23 | "pool", 24 | "async" 25 | ], 26 | "author": "GoDaddy", 27 | "license": "MIT", 28 | "bugs": { 29 | "url": "https://github.com/godaddy/node-callback-pool/issues" 30 | }, 31 | "devDependencies": { 32 | "mocha": "~1.17.1", 33 | "istanbul": "~0.2.4", 34 | "jshint": "~2.4.3", 35 | "should": "~3.1.2" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 GoDaddy Operating Company, LLC 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a 4 | copy of this software and associated documentation files (the "Software"), 5 | to deal in the Software without restriction, including without limitation 6 | the rights to use, copy, modify, merge, publish, distribute, sublicense, 7 | and/or sell copies of the Software, and to permit persons to whom the 8 | Software is furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 16 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 19 | DEALINGS IN THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | module.exports.create = create; 4 | 5 | function create(max){ 6 | var callbacks = []; 7 | var plugged = true; 8 | var args = null; 9 | 10 | return { 11 | add:function(){ 12 | var newCallbacks = [].slice.call(arguments).filter(callbackFilter); 13 | if(plugged){ 14 | callbacks = callbacks.concat(newCallbacks); 15 | if(max > 0){ 16 | callbacks.splice(max); 17 | } 18 | } else { 19 | run(newCallbacks, args); 20 | } 21 | return this; 22 | }, 23 | drain:function(){ 24 | args = [].slice.call(arguments); 25 | plugged = false; 26 | run(callbacks, args); 27 | callbacks = []; 28 | return this; 29 | }, 30 | plug:function(){ 31 | args = null; 32 | plugged = true; 33 | return this; 34 | } 35 | }; 36 | } 37 | 38 | function callbackFilter(cb){ 39 | return typeof cb === 'function'; 40 | } 41 | 42 | function run(callbacks, args){ 43 | var cb; 44 | var len = callbacks.length; 45 | var i; 46 | 47 | for(i=0;i