add max queue length for notifications (#46)

This commit is contained in:
Lynn
2017-03-28 18:09:51 -07:00
committed by GitHub
parent 06e93cd9c3
commit 75d1342b28
5 changed files with 44 additions and 8 deletions

View File

@@ -383,9 +383,10 @@ electron.ipcMain.on(apiProxyCmds.addEvent, function(event, args) {
/* eslint-enable no-console */ /* eslint-enable no-console */
let obj = liveObjs[args.objId]; let obj = liveObjs[args.objId];
let callbackFunc = function() { let callbackFunc = function(result) {
event.sender.send(apiProxyCmds.eventCallback, { event.sender.send(apiProxyCmds.eventCallback, {
callbackId: args.callbackId callbackId: args.callbackId,
result: result
}); });
} }
obj._callbacks[args.callbackId] = callbackFunc; obj._callbacks[args.callbackId] = callbackFunc;

View File

@@ -16,6 +16,10 @@ const app = electron.app;
const BrowserWindow = electron.BrowserWindow; const BrowserWindow = electron.BrowserWindow;
const ipc = electron.ipcMain; const ipc = electron.ipcMain;
// maximum number of notifications that can be queued, after limit is
// reached then error func callback will be invoked.
const MAX_QUEUE_SIZE = 30;
let AnimationQueue = require('./AnimationQueue.js'); let AnimationQueue = require('./AnimationQueue.js');
// Array of windows with currently showing notifications // Array of windows with currently showing notifications
@@ -225,12 +229,26 @@ function setupConfig() {
function notify(notification) { function notify(notification) {
if (notificationQueue.length >= MAX_QUEUE_SIZE) {
var id = latestID;
incrementId();
if (typeof notification.onErrorFunc === 'function') {
setTimeout(function() {
notification.onErrorFunc({
id: id,
error: 'max notification queue size reached: ' + MAX_QUEUE_SIZE
});
}, 0);
}
return id;
}
// Is it an object and only one argument? // Is it an object and only one argument?
if (arguments.length === 1 && typeof notification === 'object') { if (arguments.length === 1 && typeof notification === 'object') {
let notf = Object.assign({}, notification); let notf = Object.assign({}, notification);
// Use object instead of supplied parameters // Use object instead of supplied parameters
notf.id = latestID notf.id = latestID;
latestID++ incrementId();
animationQueue.push({ animationQueue.push({
func: showNotification, func: showNotification,
args: [ notf ] args: [ notf ]
@@ -241,6 +259,10 @@ function notify(notification) {
return null; return null;
} }
function incrementId() {
latestID++;
}
function showNotification(notificationObj) { function showNotification(notificationObj) {
return new Promise(function(resolve) { return new Promise(function(resolve) {
// Can we show it? // Can we show it?

View File

@@ -19,7 +19,8 @@ class Notify {
color: options.color, color: options.color,
onShowFunc: onShow.bind(this), onShowFunc: onShow.bind(this),
onClickFunc: onClick.bind(this), onClickFunc: onClick.bind(this),
onCloseFunc: onClose.bind(this) onCloseFunc: onClose.bind(this),
onErrorFunc: onError.bind(this)
}); });
this._data = options.data || null; this._data = options.data || null;
@@ -43,6 +44,17 @@ class Notify {
this.destroy(); this.destroy();
} }
} }
function onError(arg) {
if (arg.id === this._id) {
// don't raise error event if handler doesn't exist, node
// will throw an exception
if (this.emitter.eventNames().includes('error')) {
this.emitter.emit('error', arg.error || 'notification error');
}
this.destroy();
}
}
} }
close() { close() {

View File

@@ -46,7 +46,7 @@ class Notify {
get data() {} get data() {}
/** /**
* add event listeners for 'click' and 'close' events * add event listeners for 'click', 'close', 'show', 'error' events
* *
* @param {String} event event to listen for * @param {String} event event to listen for
* @param {func} cb callback invoked when event occurs * @param {func} cb callback invoked when event occurs
@@ -54,7 +54,7 @@ class Notify {
addEventListener(event, cb) {} addEventListener(event, cb) {}
/** /**
* remove event listeners for 'click' and 'close' events * remove event listeners for 'click', 'close', 'show', 'error' events
* *
* @param {String} event event to stop listening for. * @param {String} event event to stop listening for.
* @param {func} cb callback associated with original addEventListener * @param {func} cb callback associated with original addEventListener

View File

@@ -112,7 +112,8 @@ function addEventHandler(target) {
if (arg.callbackId === callbackId) { if (arg.callbackId === callbackId) {
callback({ callback({
target: this, target: this,
type: eventName type: eventName,
result: arg.result
}); });
} }
}.bind(this); }.bind(this);