Skip to content

Commit

Permalink
Automagically Bind notify Methods
Browse files Browse the repository at this point in the history
Automagically bind `notify` methods on all wrapper classes so the notify method can be destructured within the import statement and not run into any `this` related errors.
  • Loading branch information
danii committed Dec 27, 2019
1 parent b368131 commit 789c544
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 21 deletions.
11 changes: 9 additions & 2 deletions notifiers/balloon.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function WindowsBalloon(options) {
util.inherits(WindowsBalloon, EventEmitter);

function noop() {}
WindowsBalloon.prototype.notify = function(options, callback) {
function notifyRaw(options, callback) {
var fallback;
var notifierOptions = this.options;
options = utils.clone(options || {});
Expand Down Expand Up @@ -105,7 +105,14 @@ WindowsBalloon.prototype.notify = function(options, callback) {
});

return this;
};
}

Object.defineProperty(WindowsBalloon.prototype, 'notify', {
get: function() {
if (!this._notify) this._notify = notifyRaw.bind(this);
return this._notify;
}
});

var allowedArguments = ['t', 'd', 'p', 'm', 'i', 'e', 'q', 'w', 'xp'];

Expand Down
11 changes: 9 additions & 2 deletions notifiers/growl.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function Growl(options) {
}
util.inherits(Growl, EventEmitter);

Growl.prototype.notify = function(options, callback) {
function notifyRaw(options, callback) {
growly.setHost(this.options.host, this.options.port);
options = utils.clone(options || {});

Expand Down Expand Up @@ -71,6 +71,13 @@ Growl.prototype.notify = function(options, callback) {
callback();
});
return this;
};
}

Object.defineProperty(Growl.prototype, 'notify', {
get: function() {
if (!this._notify) this._notify = notifyRaw.bind(this);
return this._notify;
}
});

function noop() {}
11 changes: 9 additions & 2 deletions notifiers/notificationcenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ util.inherits(NotificationCenter, EventEmitter);
var activeId = null;

function noop() {}
NotificationCenter.prototype.notify = function(options, callback) {
function notifyRaw(options, callback) {
var fallbackNotifier;
var id = identificator();
options = utils.clone(options || {});
Expand Down Expand Up @@ -93,7 +93,14 @@ NotificationCenter.prototype.notify = function(options, callback) {

callback(new Error(errorMessageOsX));
return this;
};
}

Object.defineProperty(NotificationCenter.prototype, 'notify', {
get: function() {
if (!this._notify) this._notify = notifyRaw.bind(this);
return this._notify;
}
});

function identificator() {
return { _ref: 'val' };
Expand Down
11 changes: 9 additions & 2 deletions notifiers/notifysend.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function NotifySend(options) {
util.inherits(NotifySend, EventEmitter);

function noop() {}
NotifySend.prototype.notify = function(options, callback) {
function notifyRaw(options, callback) {
options = utils.clone(options || {});
callback = callback || noop;

Expand Down Expand Up @@ -70,7 +70,14 @@ NotifySend.prototype.notify = function(options, callback) {
}

return this;
};
}

Object.defineProperty(NotifySend.prototype, 'notify', {
get: function() {
if (!this._notify) this._notify = notifyRaw.bind(this);
return this._notify;
}
});

var allowedArguments = ['urgency', 'expire-time', 'icon', 'category', 'hint'];

Expand Down
11 changes: 9 additions & 2 deletions notifiers/toaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function getPipeName() {
return `${PIPE_PATH_PREFIX}${PIPE_NAME}-${uuid()}`;
}

WindowsToaster.prototype.notify = function(options, callback) {
function notifyRaw(options, callback) {
options = utils.clone(options || {});
callback = callback || noop;
var is64Bit = os.arch() === 'x64';
Expand Down Expand Up @@ -142,4 +142,11 @@ WindowsToaster.prototype.notify = function(options, callback) {
);
});
return this;
};
}

Object.defineProperty(WindowsToaster.prototype, 'notify', {
get: function() {
if (!this._notify) this._notify = notifyRaw.bind(this);
return this._notify;
}
});
30 changes: 19 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 789c544

Please sign in to comment.