Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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() {});
});
});
});