Skip to content

Commit

Permalink
v3.3.0. Allow gulp.unwatch() to unwatch all listeners.
Browse files Browse the repository at this point in the history
  • Loading branch information
cb1kenobi committed Mar 14, 2017
1 parent f57523b commit cf2489d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gawk",
"version": "3.2.2",
"version": "3.3.0",
"description": "Observable JavaScript object model",
"main": "./dist/index.js",
"author": "Chris Barber <chris@cb1inc.com> (https://github.com/cb1kenobi)",
Expand Down
19 changes: 13 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ gawk.watch = function watch(subject, filter, listener) {
* Removes a listener from the specified gawked object.
*
* @param {Object|GawkObject|Array|GawkArray} subject - The object to unwatch.
* @param {Function} listener - The function to call when something changes.
* @param {Function} [listener] - The function to call when something changes.
* @returns {GawkObject|GawkArray} Returns a gawked object or array depending on
* the input object.
*/
Expand All @@ -441,13 +441,20 @@ gawk.unwatch = function unwatch(subject, listener) {
throw new TypeError('Expected source to be a GawkArray or GawkObject');
}

if (typeof listener !== 'function') {
throw new TypeError('Expected listener to be a function');
if (listener) {
if (typeof listener !== 'function') {
throw new TypeError('Expected listener to be a function');
}
subject.__gawk__.listeners.delete(listener);
subject.__gawk__.previous.delete(listener);
} else {
// remove all listeners
for (const [ listener, filter ] of subject.__gawk__.listeners) {
subject.__gawk__.listeners.delete(listener);
subject.__gawk__.previous.delete(listener);
}
}

subject.__gawk__.listeners.delete(listener);
subject.__gawk__.previous.delete(listener);

return subject;
};

Expand Down
27 changes: 23 additions & 4 deletions test/test-watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,10 +546,6 @@ describe('gawk.unwatch()', () => {
});

it('should fail to unwatch with non-function listener', () => {
expect(() => {
gawk.unwatch(gawk({}));
}).to.throw(TypeError, 'Expected listener to be a function');

expect(() => {
gawk.unwatch(gawk({}), 'foo');
}).to.throw(TypeError, 'Expected listener to be a function');
Expand All @@ -575,6 +571,29 @@ describe('gawk.unwatch()', () => {
expect(count).to.equal(2);
});

it('should unwatch all listeners', () => {
const gobj = gawk({});
let count = 0;

gawk.watch(gobj, () => {
count++;
});

gawk.watch(gobj, () => {
count++;
});

gobj.a = 'b';
gobj.c = 'd';

gawk.unwatch(gobj);

gobj.e = 'f';
gobj.g = 'h';

expect(count).to.equal(4);
});

it('should unwatch GawkArray changes', () => {
const garr = gawk(['a']);
let count = 0;
Expand Down

0 comments on commit cf2489d

Please sign in to comment.