From 60adda92eaddf5ee1a44c5db0578d7bf6ef95f37 Mon Sep 17 00:00:00 2001 From: chrisramsey Date: Fri, 17 Jun 2016 12:56:48 -0500 Subject: [PATCH 1/9] Adds ability to wait until after callback returns truthy --- index.js | 57 +++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index 2d6a423..f771637 100644 --- a/index.js +++ b/index.js @@ -3,12 +3,26 @@ var map = require('map-stream'), _log = console.log; +function runAfter(settings) { + if (typeof settings.after === 'function') { + _log('Wait: Calling after()'); + settings.after(); + } +} + module.exports = function (settings) { if (typeof settings !== 'object') { - settings = { - duration: settings - }; + + if (typeof settings === 'function') { + settings = { + callback: settings + }; + } else { + settings = { + duration: settings + }; + } } if (!settings.verbose) { @@ -26,16 +40,33 @@ module.exports = function (settings) { settings.before(); } - var timeout = setTimeout(function () { - timeout = null; - _log('Wait: Waited', settings.duration); - if (typeof settings.after === 'function') { - _log('Wait: Calling after()'); - settings.after(); + if (typeof settings.callback === 'function') { + var result = false; + var run_count = 0; + _log('Waiting for callback to return true'); + + while(true) { + run_count++; + result = settings.callback(); + + if (result) { + break; + } } - cb(null, file); - }, settings.duration); - }); + _log('Callback has return truthy. Times called', run_count); + + runAfter(settings); + + } else { + var timeout = setTimeout(function () { + timeout = null; + _log('Wait: Waited', settings.duration); -}; \ No newline at end of file + runAfter(settings); + + cb(null, file); + }, settings.duration); + } + }); +}; From aad0946be541787ce44bfc645ec6ce17076f8a3a Mon Sep 17 00:00:00 2001 From: chrisramsey Date: Fri, 17 Jun 2016 13:02:52 -0500 Subject: [PATCH 2/9] Updates examples --- README.md | 52 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index e879276..5fe12fa 100644 --- a/README.md +++ b/README.md @@ -5,36 +5,44 @@ A gulp task that inserts a delay before calling the next function in a chain. ## Example -The following example will watch for changes to templates and delay the livereload server refresh until after the nodemon script is expected to have restarted. +### Time based delay + +Waits a specified number of milliseconds before executing the next item in the chain. ```javascript -// Gulpfile.js -var gulp = require('gulp') - , r = require('tiny-lr') - , refresh = require('gulp-livereload') - , nodemon = require('gulp-nodemon') - , wait = require('../gulp-wait') - , server = lr(); -gulp.task('dev', function () { + var wait = require('gulp-wait'); - gulp.src('./index.js') - .pipe(nodemon()); + gulp.task('wait', function() { + gulp.src('yourfile.js') + .pipe(wait(1500)) + .pipe(gulp.dest('destfile.js')); + }); - server.listen(35729, function (err) { +``` - if (err) return console.log(err); +### Callback based delay - gulp.watch('./app/views/**/*.html', function (e) { - gulp.src(e.path) - .pipe(wait(1500)) - .pipe(refresh(server)); - }); +Waits for a callback to be truthy before executing the next item in the chain. - }); +```javascript -}) + var wait = require('gulp-wait'); + var fs = require('fs'); + + function doesFileExist(filePath) { + try { + fs.accessSync(filePath); + return true; + } catch () { + return false; + } + } + + gulp.task('wait', function() { + gulp.src('yourfile.js') + .pipe(wait(doesFileExist('../path/to/some/file.js'))) + .pipe(gulp.dest('destfile.js')); + }); ``` - -Yes, ideally there would be an event from nodemon to trigger the livereload. \ No newline at end of file From eb0e1890a45f2086f0a5c70518d5c5b3ccf99be7 Mon Sep 17 00:00:00 2001 From: chrisramsey Date: Fri, 17 Jun 2016 14:44:25 -0500 Subject: [PATCH 3/9] Adds tests --- tests/index.spec.js | 157 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 tests/index.spec.js diff --git a/tests/index.spec.js b/tests/index.spec.js new file mode 100644 index 0000000..9b2b534 --- /dev/null +++ b/tests/index.spec.js @@ -0,0 +1,157 @@ +'use strict'; + +const test = require('tape'); +const wait = require('../index'); + +test('wait returns a stream object', (t) => { + t.plan(1); + const res = wait(); + + t.ok(res instanceof require('stream').Stream, 'is instanceof Stream'); +}); + +test('waits 1 second before returning if no params are given', (t) => { + t.plan(1); + + const stream = wait(); + const now = Date.now(); + + stream.on('data', () => { + const time = Date.now() - now; + t.ok(time < 1200 && time > 800, 'Waits for 1 second'); + t.end(); + }); + + stream.write(null); + +}); + +test('Accurately waits before calling the cb if given a number', (t) => { + t.plan(1); + + const stream = wait(100); + const now = Date.now(); + + stream.on('data', () => { + const time = Date.now() - now; + t.ok(time < 120 && time > 80, 'Waits for 150ms'); + t.end(); + }); + + stream.write(null); +}); + +test('Waits until callback returns truthy before calling resuming stream', (t) => { + + let num = 100; + + const stream = wait(() => { + num--; + return num === 0; + }); + + stream.on('data', () => { + t.ok(true, 'Accepts function for timeout'); + t.end(); + }); + + stream.write(null); +}); + +test('Defaults to callback if both cb and time are passed in', (t) => { + + let num = 100; + + const stream = wait({ + time: 5000, + callback() { + num--; + return num === 0; + } + }); + + stream.on('data', () => { + t.ok(true, 'Defaults to callback'); + t.end(); + }); + + stream.write(null); +}); + +test('Runs the `before` callback when using time', (t) => { + + let result = false; + + const stream = wait({ + time: 100, + before() { + result = true; + } + }); + + stream.on('data', () => { + t.ok(result === true, 'Sets variable from `before` callback'); + t.end(); + }); + + stream.write(null); +}); + +test('Runs the `after` callback when using time', (t) => { + + let after_result = false; + + const stream = wait({ + time: 100, + after: () => { + after_result = true; + } + }); + + stream.write(null); + + // wait for stream to finish + setTimeout(() => { + t.ok(after_result === true, 'Sets variable from `after` callback'); + t.end(); + }, 200) +}); + +test('Runs the `before` callback when using callback', (t) => { + + let result = false; + + const stream = wait({ + callback: () => true, + before() { + result = true; + } + }); + + stream.on('data', () => { + t.ok(result === true, 'Sets variable from `before` callback'); + t.end(); + }); + + stream.write(null); +}); + +test('Runs the `after` callback when using callback', (t) => { + + let after_result = false; + + const stream = wait({ + callback: () => true, + after: () => { + after_result = true; + } + }); + + stream.write(null); + + // wait for stream to finish + setTimeout(() => { + t.ok(after_result === true, 'Sets variable from `after` callback'); + t.end(); + }, 200) +}); From 6f26adf79f387e8b60f2e94d8485dea98c620d30 Mon Sep 17 00:00:00 2001 From: chrisramsey Date: Fri, 17 Jun 2016 14:44:38 -0500 Subject: [PATCH 4/9] Removes gulp, adds testing and babel --- package.json | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index e2cc3ec..93882c3 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,15 @@ { - "name": "gulp-wait", - "version": "0.0.2", - "description": "A gulp task that inserts a delay before calling the next function in a chain.", - "main": "index.js", + "name": "gulp-wait2", + "version": "0.0.3", + "description": "A gulp task that inserts a delay (time or callback-based) before calling the next function in a chain. (Forked from bpartridge/gulp-wait)", + "main": "index-compiled.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "build": "babel index.es6 --out-file index.js --source-maps inline", + "test": "npm run build && tape 'tests/**/*.js'" }, "repository": { "type": "git", - "url": "https://github.com/bpartridge83/gulp-wait.git" + "url": "https://github.com/cwramsey/gulp-wait.git" }, "keywords": [ "gulp", @@ -16,12 +17,20 @@ "delay", "gulpplugin" ], - "author": "Brian Partridge", + "author": "Chris Ramsey", + "contributors": [ + "Brian Partridge" + ], "license": "BSD-2-Clause", "bugs": { - "url": "https://github.com/bpartridge/gulp-wait/issues" + "url": "https://github.com/cwramsey/gulp-wait/issues" }, "dependencies": { "map-stream": "0.0.4" + }, + "devDependencies": { + "babel-cli": "^6.10.1", + "babel-preset-es2015": "^6.9.0", + "tape": "^4.5.1" } } From 9a3f1f33d26e2988af25b469f7a5abb9cbff2768 Mon Sep 17 00:00:00 2001 From: chrisramsey Date: Fri, 17 Jun 2016 14:44:47 -0500 Subject: [PATCH 5/9] Adds babelrc --- .babelrc | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .babelrc diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..c13c5f6 --- /dev/null +++ b/.babelrc @@ -0,0 +1,3 @@ +{ + "presets": ["es2015"] +} From 4507800e1a98d48cb1de2f0be875207d6efcbdee Mon Sep 17 00:00:00 2001 From: chrisramsey Date: Fri, 17 Jun 2016 14:45:01 -0500 Subject: [PATCH 6/9] Compiles from es6 --- index.es6 | 69 +++++++++++++++++++++++++++++++++++ index.js | 105 +++++++++++++++++++++++++++--------------------------- 2 files changed, 121 insertions(+), 53 deletions(-) create mode 100644 index.es6 diff --git a/index.es6 b/index.es6 new file mode 100644 index 0000000..40b240e --- /dev/null +++ b/index.es6 @@ -0,0 +1,69 @@ +'use strict'; + +const map = require('map-stream'); +let log = console.log; + +function waitByTime(opts) { + return (file, callback) => { + if (opts.before) { + opts.before(opts); + } + + setTimeout(() => { + log(`Waiting for ${opts}ms`); + callback(null, file); + + if (typeof opts.after !== 'undefined') { + opts.after(opts); + } + }, opts.time) + } +} + +function waitByCallback(opts) { + return (file, callback) => { + if (opts.before) { + opts.before(opts); + } + + let run_count = 0; + + log('Waiting for callback to return truthy'); + + while (!opts.callback()) { + run_count++; + } + + callback(null, file); + + log(`Ran callback ${run_count} times`); + + if (opts.after) { + opts.after(opts); + } + } +} + +module.exports = (opts) => { + if (typeof opts === 'number' || typeof opts === 'undefined') { + opts = { + time: opts || 1000 + }; + } + + if (typeof opts === 'function') { + opts = { + callback: opts + } + }; + + if (!opts.verbose) { + log = () => {}; + } + + if (opts.callback) { + return map(waitByCallback(opts)); + } else { + return map(waitByTime(opts)); + } +}; diff --git a/index.js b/index.js index f771637..61f3d7e 100644 --- a/index.js +++ b/index.js @@ -1,72 +1,71 @@ 'use strict'; -var map = require('map-stream'), - _log = console.log; +var map = require('map-stream'); +var log = console.log; -function runAfter(settings) { - if (typeof settings.after === 'function') { - _log('Wait: Calling after()'); - settings.after(); - } -} +function waitByTime(opts) { + return function (file, callback) { + if (opts.before) { + opts.before(opts); + } -module.exports = function (settings) { + setTimeout(function () { + log('Waiting for ' + opts + 'ms'); + callback(null, file); - if (typeof settings !== 'object') { + if (typeof opts.after !== 'undefined') { + opts.after(opts); + } + }, opts.time); + }; +} - if (typeof settings === 'function') { - settings = { - callback: settings - }; - } else { - settings = { - duration: settings - }; +function waitByCallback(opts) { + return function (file, callback) { + if (opts.before) { + opts.before(opts); } - } - if (!settings.verbose) { - _log = function () { - return; - }; - } + var run_count = 0; - settings.duration = settings.duration || 1000; + log('Waiting for callback to return truthy'); - return map(function (file, cb) { - - if (typeof settings.before === 'function') { - _log('Wait: Calling before()'); - settings.before(); + while (!opts.callback()) { + run_count++; } - if (typeof settings.callback === 'function') { - var result = false; - var run_count = 0; - _log('Waiting for callback to return true'); - - while(true) { - run_count++; - result = settings.callback(); + callback(null, file); - if (result) { - break; - } - } + log('Ran callback ' + run_count + ' times'); - _log('Callback has return truthy. Times called', run_count); + if (opts.after) { + opts.after(opts); + } + }; +} - runAfter(settings); +module.exports = function (opts) { + if (typeof opts === 'number' || typeof opts === 'undefined') { + opts = { + time: opts || 1000 + }; + } - } else { - var timeout = setTimeout(function () { - timeout = null; - _log('Wait: Waited', settings.duration); + if (typeof opts === 'function') { + opts = { + callback: opts + }; + }; - runAfter(settings); + if (!opts.verbose) { + log = function log() {}; + } - cb(null, file); - }, settings.duration); - } - }); + if (opts.callback) { + return map(waitByCallback(opts)); + } else { + return map(waitByTime(opts)); + } }; + +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImluZGV4LmVzNiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTs7QUFFQSxJQUFNLE1BQU0sUUFBUSxZQUFSLENBQVo7QUFDQSxJQUFJLE1BQU0sUUFBUSxHQUFsQjs7QUFFQSxTQUFTLFVBQVQsQ0FBb0IsSUFBcEIsRUFBMEI7QUFDeEIsU0FBTyxVQUFDLElBQUQsRUFBTyxRQUFQLEVBQW9CO0FBQ3pCLFFBQUksS0FBSyxNQUFULEVBQWlCO0FBQ2YsV0FBSyxNQUFMLENBQVksSUFBWjtBQUNEOztBQUVELGVBQVcsWUFBTTtBQUNmLDJCQUFtQixJQUFuQjtBQUNBLGVBQVMsSUFBVCxFQUFlLElBQWY7O0FBRUEsVUFBSSxPQUFPLEtBQUssS0FBWixLQUFzQixXQUExQixFQUF1QztBQUNyQyxhQUFLLEtBQ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KSB7XG4gICAgICBvcHRzLmFmdGVyKG9wdHMpO1xuICAgIH1cbiAgfVxufVxuXG5tb2R1bGUuZXhwb3J0cyA9IChvcHRzKSA9PiB7XG4gIGlmICh0eXBlb2Ygb3B0cyA9PT0gJ251bWJlcicgfHwgdHlwZW9mIG9wdHMgPT09ICd1bmRlZmluZWQnKSB7XG4gICAgb3B0cyA9IHtcbiAgICAgIHRpbWU6IG9wdHMgfHwgMTAwMFxuICAgIH07XG4gIH1cblxuICBpZiAodHlwZW9mIG9wdHMgPT09ICdmdW5jdGlvbicpIHtcbiAgICBvcHRzID0ge1xuICAgICAgY2FsbGJhY2s6IG9wdHNcbiAgICB9XG4gIH07XG5cbiAgaWYgKCFvcHRzLnZlcmJvc2UpIHtcbiAgICBsb2cgPSAoKSA9PiB7fTtcbiAgfVxuXG4gIGlmIChvcHRzLmNhbGxiYWNrKSB7XG4gICAgcmV0dXJuIG1hcCh3YWl0QnlDYWxsYmFjayhvcHRzKSk7XG4gIH0gZWxzZSB7XG4gICAgcmV0dXJuIG1hcCh3YWl0QnlUaW1lKG9wdHMpKTtcbiAgfVxufTtcbiJdfQ== \ No newline at end of file From 5afd305c55656845e3f33e94dfb9eef410b3ae34 Mon Sep 17 00:00:00 2001 From: chrisramsey Date: Fri, 17 Jun 2016 14:45:16 -0500 Subject: [PATCH 7/9] Increments version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 93882c3..952a880 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gulp-wait2", - "version": "0.0.3", + "version": "0.0.4", "description": "A gulp task that inserts a delay (time or callback-based) before calling the next function in a chain. (Forked from bpartridge/gulp-wait)", "main": "index-compiled.js", "scripts": { From 76e5c40922e1dfcb0dd3089882ecce8114fb057b Mon Sep 17 00:00:00 2001 From: chrisramsey Date: Fri, 17 Jun 2016 14:47:55 -0500 Subject: [PATCH 8/9] Adds docs for options object --- README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/README.md b/README.md index 5fe12fa..f8259d2 100644 --- a/README.md +++ b/README.md @@ -46,3 +46,24 @@ Waits for a callback to be truthy before executing the next item in the chain. }); ``` + +### Using an options object + +You can pass in an options object as well. + +```javascript + +const options = { + time: 100, // same as wait(100) + callback: () => true, // same as wait(() => true) - defaults to callback if both time and callback are passed + before: () => {}, + after: () => {} +}; + +gulp.task('wait', function() { + gulp.src('yourfile.js') + .pipe(wait(options)) + .pipe(gulp.dest('destfile.js')); +}); + +``` From f470a353bc4eeb3182857c55bc5cc55f72e1f928 Mon Sep 17 00:00:00 2001 From: chrisramsey Date: Fri, 17 Jun 2016 14:48:15 -0500 Subject: [PATCH 9/9] increments version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 952a880..7a92d1c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gulp-wait2", - "version": "0.0.4", + "version": "0.0.5", "description": "A gulp task that inserts a delay (time or callback-based) before calling the next function in a chain. (Forked from bpartridge/gulp-wait)", "main": "index-compiled.js", "scripts": {