├── LICENSE ├── README.md ├── index.js ├── package.json └── test └── index.js /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 Dominic Tarr 2 | 3 | Permission is hereby granted, free of charge, 4 | to any person obtaining a copy of this software and 5 | associated documentation files (the "Software"), to 6 | deal in the Software without restriction, including 7 | without limitation the rights to use, copy, modify, 8 | merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom 10 | the Software is furnished to do so, 11 | subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 18 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 20 | ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pull-pause 2 | 3 | a through pull-stream that can be turned on and off like a tap. 4 | 5 | ## Example 6 | 7 | ``` js 8 | 9 | var pull = require('pull-stream') 10 | var pause = require('pull-pause')() 11 | 12 | 13 | pull( 14 | source, 15 | pause, 16 | sink 17 | ) 18 | 19 | pause.pause() //stop reading. 20 | 21 | pause.resume() //resume reading. 22 | 23 | 24 | ``` 25 | 26 | ## License 27 | 28 | MIT 29 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 2 | 3 | module.exports = function (onPause) { 4 | 5 | var wait, read, paused 6 | 7 | function reader (_read) { 8 | read = _read 9 | return function (abort, cb) { 10 | if(!paused) read(abort, cb) 11 | else wait = [abort, cb] 12 | } 13 | } 14 | 15 | reader.pause = function () { 16 | if(paused) return 17 | paused = true 18 | onPause && onPause(paused) 19 | } 20 | 21 | reader.resume = function () { 22 | if(!paused) return 23 | paused = false 24 | onPause && onPause(paused) 25 | if(wait) { 26 | var _wait = wait 27 | wait = null 28 | read(_wait[0], _wait[1]) 29 | } 30 | } 31 | 32 | return reader 33 | 34 | } 35 | 36 | 37 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "pull-pause", 3 | "description": "", 4 | "version": "0.0.2", 5 | "homepage": "https://github.com/dominictarr/pull-pause", 6 | "repository": { 7 | "type": "git", 8 | "url": "git://github.com/dominictarr/pull-pause.git" 9 | }, 10 | "dependencies": {}, 11 | "devDependencies": { 12 | "pull-stream": "^3.5.0", 13 | "tape": "^4.6.3" 14 | }, 15 | "scripts": { 16 | "test": "set -e; for t in test/*.js; do node $t; done" 17 | }, 18 | "author": "Dominic Tarr (http://dominictarr.com)", 19 | "license": "MIT" 20 | } 21 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | var tape = require('tape') 2 | var Pause = require('../') 3 | var pull = require('pull-stream') 4 | 5 | tape('simple', function (t) { 6 | 7 | var pause = Pause(function (paused) { 8 | t.ok(paused) 9 | t.end() 10 | }) 11 | var c = 0 12 | 13 | pull( 14 | pull.count(10), 15 | pause, 16 | pull.drain(function (e) { 17 | c += e 18 | 19 | if(c > 10) 20 | pause.pause() 21 | 22 | }) 23 | ) 24 | 25 | }) 26 | 27 | tape('pause, resume', function (t) { 28 | var p = false 29 | var pause = Pause(function (paused) { 30 | p = true 31 | if(paused) 32 | setTimeout(pause.resume) 33 | }) 34 | var c = 0 35 | 36 | pull( 37 | pull.count(10), 38 | pause, 39 | pull.drain(function (e) { 40 | c += e 41 | if(c > 10 && !p) { 42 | pause.pause() 43 | } 44 | }, function () { 45 | t.equal(c, 55) 46 | t.end() 47 | }) 48 | ) 49 | 50 | }) 51 | 52 | tape('pause, resume', function (t) { 53 | 54 | var pause = Pause(function (paused) { 55 | setTimeout(pause.resume, 100) 56 | }) 57 | var c = 0 58 | 59 | pull( 60 | pull.count(10), 61 | pause, 62 | pull.drain(function (e) { 63 | c += e 64 | console.log(c) 65 | if(c > 5) pause.pause() 66 | }, function () { 67 | t.equal(c, 55) 68 | t.end() 69 | }) 70 | ) 71 | 72 | }) 73 | 74 | tape('without callback', function (t) { 75 | var pause = Pause() 76 | var c = 0 77 | 78 | setTimeout(pause.resume, 50) 79 | pull( 80 | pull.count(10), 81 | pause, 82 | pull.drain(function (e) { 83 | c += e 84 | if(c == 5) pause.pause() 85 | }, function () { 86 | t.equal(c, 55) 87 | t.end() 88 | }) 89 | ) 90 | }) 91 | 92 | 93 | --------------------------------------------------------------------------------