Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature to clear notifier processes #210

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 21 additions & 2 deletions notifiers/notificationcenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,23 @@ function NotificationCenter(options) {
return new NotificationCenter(options);
}
this.options = options;
this.notifications = [];

EventEmitter.call(this);
}
util.inherits(NotificationCenter, EventEmitter);
var activeId = null;

/**
* Call this to cancel timeouts on current notifications
*/
NotificationCenter.prototype.clearAll = function() {
for (var i in this.notifications) {
this.notifications[i].kill();
}
this.notifications = [];
};

function noop() {}
NotificationCenter.prototype.notify = function(options, callback) {
var fallbackNotifier;
Expand Down Expand Up @@ -75,11 +86,19 @@ NotificationCenter.prototype.notify = function(options, callback) {

var argsList = utils.constructArgumentList(options);
if (utils.isMountainLion()) {
utils.fileCommandJson(
// This will be the index of the newly added notification once its been
// added
var index = this.notifications.length;
var p = utils.fileCommandJson(
this.options.customPath || notifier,
argsList,
actionJackedCallback
function(e, data) {
// delete the notification since its been cleared
this.notifications.splice(index, 1);
actionJackedCallback(e, data);
}.bind(this)
);
this.notifications.push(p);
return this;
}

Expand Down
38 changes: 38 additions & 0 deletions test/terminal-notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,42 @@ describe('terminal-notifier', function() {
});
});
});

describe('#clearAll()', function() {
var mockProcess = {
kill: jest.fn()
};
beforeEach(function() {
// reset mock for each test
mockProcess.kill = jest.fn();
utils.fileCommandJson = function() {
return mockProcess;
};
});

afterEach(function() {
utils.fileCommandJson = originalUtils;
});

it('should kill all terminal-notifier processes', function() {
notifier.notify({ message: 'Hello World' }, function() {});
notifier.clearAll();
expect(mockProcess.kill.mock.calls.length).toBe(1);
});

it('should not kill finished terminal-notifier processes', function(done) {
utils.fileCommandJson = function(n, o, cb) {
setTimeout(function() {
cb(null, '');
// After the callback the notification will be cleared.
// Calling `clearAll` should be a no-op
notifier.clearAll();
expect(mockProcess.kill.mock.calls.length).toBe(0);
done();
}, 0);
return mockProcess;
};
notifier.notify({ message: 'Hello World' }, function() {});
});
});
});