From 5da1bacb6ff26057f69b367d53c5e294a3b4a25d Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 30 Oct 2017 22:29:02 +1100 Subject: [PATCH 1/2] Add feature to clear notifier processes --- notifiers/notificationcenter.js | 23 ++++++++++++++++++-- test/terminal-notifier.js | 38 +++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/notifiers/notificationcenter.js b/notifiers/notificationcenter.js index 87ce072..8b6a61b 100644 --- a/notifiers/notificationcenter.js +++ b/notifiers/notificationcenter.js @@ -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; @@ -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 + let index = this.notifications.length; + let 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; } diff --git a/test/terminal-notifier.js b/test/terminal-notifier.js index 17a1f2c..ef06ae1 100644 --- a/test/terminal-notifier.js +++ b/test/terminal-notifier.js @@ -308,4 +308,42 @@ describe('terminal-notifier', function() { }); }); }); + + describe('#clearAll()', function() { + let 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() {}); + }); + }); }); From 8f05d611d61ef79ff04a6876a286afb25f61fff6 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 31 Oct 2017 08:35:40 +1100 Subject: [PATCH 2/2] Fix incorrect use of let in older versions of node --- notifiers/notificationcenter.js | 4 ++-- test/terminal-notifier.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/notifiers/notificationcenter.js b/notifiers/notificationcenter.js index 8b6a61b..28b12d9 100644 --- a/notifiers/notificationcenter.js +++ b/notifiers/notificationcenter.js @@ -88,8 +88,8 @@ NotificationCenter.prototype.notify = function(options, callback) { if (utils.isMountainLion()) { // This will be the index of the newly added notification once its been // added - let index = this.notifications.length; - let p = utils.fileCommandJson( + var index = this.notifications.length; + var p = utils.fileCommandJson( this.options.customPath || notifier, argsList, function(e, data) { diff --git a/test/terminal-notifier.js b/test/terminal-notifier.js index ef06ae1..6474277 100644 --- a/test/terminal-notifier.js +++ b/test/terminal-notifier.js @@ -310,7 +310,7 @@ describe('terminal-notifier', function() { }); describe('#clearAll()', function() { - let mockProcess = { + var mockProcess = { kill: jest.fn() }; beforeEach(function() {