forked from gulpjs/glob-watcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
261 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict'; | ||
|
||
function debounce(fn, delay) { | ||
var timeout; | ||
var args; | ||
var self; | ||
|
||
return function() { | ||
self = this; | ||
args = arguments; | ||
clear(); | ||
timeout = setTimeout(run, delay); | ||
}; | ||
|
||
function run() { | ||
clear(); | ||
fn.apply(self, args); | ||
} | ||
|
||
function clear() { | ||
clearTimeout(timeout); | ||
timeout = null; | ||
} | ||
} | ||
|
||
module.exports = debounce; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
|
||
var defaultOpts = { | ||
delay: 200, | ||
events: ['add', 'change', 'unlink'], | ||
ignored: [], | ||
ignoreInitial: true, | ||
queue: true, | ||
}; | ||
|
||
function normalizeArgs(glob, options, cb, next) { | ||
if (typeof options === 'function') { | ||
cb = options; | ||
options = {}; | ||
} | ||
|
||
var opts = Object.assign({}, defaultOpts, options); | ||
|
||
if (!Array.isArray(opts.events)) { | ||
opts.events = [opts.events]; | ||
} | ||
|
||
if (Array.isArray(glob)) { | ||
// We slice so we don't mutate the passed globs array | ||
glob = glob.slice(); | ||
} else { | ||
glob = [glob]; | ||
} | ||
|
||
return next(glob, opts, cb); | ||
} | ||
|
||
module.exports = normalizeArgs; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
'use strict'; | ||
|
||
var expect = require('expect'); | ||
var sinon = require('sinon'); | ||
var debounce = require('../../lib/debounce'); | ||
|
||
describe('lib/debounce', function() { | ||
|
||
it('should call an original function with specified delay', function(done) { | ||
expect.assertions(1); | ||
|
||
var executed = false; | ||
var fn = debounce(function() { | ||
executed = true; | ||
}, 10); | ||
|
||
fn(); | ||
|
||
expect(executed).toBeFalsy(); | ||
|
||
setTimeout(function() { | ||
expect(executed).toBeTruthy(); | ||
done(); | ||
}, 11); | ||
}); | ||
|
||
it('should extend delay against multiple calls', function(done) { | ||
expect.assertions(1); | ||
|
||
var fn = debounce(function(a) { | ||
expect(a).toBe(3); | ||
done(); | ||
}, 50); | ||
|
||
fn(1); | ||
fn(2); | ||
|
||
setTimeout(function() { | ||
fn(3); | ||
}, 3); | ||
}); | ||
|
||
it('should extends delay if a preceding call doesn\'t run yet', function(done) { | ||
expect.assertions(1); | ||
|
||
var fn = debounce(function(a) { | ||
expect(a).toBe(3); | ||
done(); | ||
}, 10); | ||
|
||
fn(1); | ||
|
||
setTimeout(function() { | ||
fn(2); | ||
|
||
setTimeout(function() { | ||
fn(3); | ||
}, 5); | ||
}, 5); | ||
}); | ||
|
||
it('should run if a preceding call already run', function(done) { | ||
expect.assertions(2); | ||
|
||
var spy = sinon.spy(function(a) { | ||
switch (spy.callCount) { | ||
case 1: | ||
expect(a).toBe(2); | ||
break; | ||
case 2: | ||
expect(a).toBe(3); | ||
done(); | ||
break; | ||
default: | ||
throw new Error(); | ||
} | ||
}); | ||
var fn = debounce(spy, 6); | ||
|
||
fn(1); | ||
|
||
setTimeout(function() { | ||
fn(2); | ||
|
||
setTimeout(function() { | ||
fn(3); | ||
}, 10); | ||
}, 3); | ||
}); | ||
}); |
Oops, something went wrong.