fix event timing for events (#53)

This commit is contained in:
Lynn
2017-04-06 10:07:58 -07:00
committed by GitHub
parent 8d94893ea8
commit bcd2f8e645
4 changed files with 89 additions and 13 deletions

View File

@@ -12,7 +12,7 @@ AnimationQueue.prototype.push = function(object) {
this.queue.push(object);
} else {
this.running = true;
this.animate(object);
setTimeout(this.animate.bind(this, object), 0);
}
}

View File

@@ -131,7 +131,7 @@ let config = {
// calcDimensions();
// }
if (app.isReady) {
if (app.isReady()) {
setup();
} else {
app.on('ready', setup);
@@ -548,7 +548,9 @@ function moveNotificationAnimation(i, done) {
}
function setWindowPosition(browserWin, posX, posY) {
browserWin.setPosition(parseInt(posX, 10), parseInt(posY, 10))
if (!browserWin.isDestroyed()) {
browserWin.setPosition(parseInt(posX, 10), parseInt(posY, 10))
}
}
/*
@@ -605,10 +607,10 @@ function closeAll() {
animationQueue.clear();
activeNotifications.forEach(function(window) {
if (window.displayTimer) {
clearTimeout(window.displayTimer);
}
if (window.electronNotifyOnCloseFunc) {
window.electronNotifyOnCloseFunc({
event: 'close-all'
});
// ToDo: fix this: shouldn't delete method on arg
/* eslint-disable */
delete window.electronNotifyOnCloseFunc;
@@ -645,5 +647,4 @@ function log() {
}
module.exports.notify = notify
// module.exports.setConfig = setConfig
module.exports.closeAll = closeAll
module.exports.reset = setupConfig

View File

@@ -27,7 +27,8 @@ class Notify {
* }
*/
constructor(title, options) {
this.emitter = new EventEmitter();
let emitter = new EventEmitter();
this.emitter = Queue(emitter);
this._id = notify({
title: title,
@@ -47,14 +48,16 @@ class Notify {
function onShow(arg) {
if (arg.id === this._id) {
this.emitter.emit('show');
this.emitter.queue('show', {
target: this
});
this._closeNotification = arg.closeNotification;
}
}
function onClick(arg) {
if (arg.id === this._id) {
this.emitter.emit('click', {
this.emitter.queue('click', {
target: this
});
}
@@ -62,7 +65,7 @@ class Notify {
function onClose(arg) {
if (arg.id === this._id || arg.event === 'close-all') {
this.emitter.emit('close', {
this.emitter.queue('close', {
target: this
});
this.destroy();
@@ -74,7 +77,7 @@ class Notify {
// 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.emitter.queue('error', arg.error || 'notification error');
}
this.destroy();
}
@@ -130,6 +133,13 @@ class Notify {
}
}
/**
* removes all event listeners
*/
removeAllEvents() {
this.destroy();
}
//
// private stuff below here
//
@@ -139,4 +149,66 @@ class Notify {
}
/**
* Allow emitter events to be queued before addEventListener called.
* Code adapted from: https://github.com/bredele/emitter-queue
*
* @param {Object} emitter Instance of node emitter that will get augmented.
* @return {Object} Modified emitter
*/
function Queue(emitter) {
/**
* Cache emitter on.
* @api private
*/
var cache = emitter.on;
let modifiedEmitter = emitter;
/**
* Emit event and store it if no
* defined callbacks.
* example:
*
* .queue('message', 'hi');
*
* @param {String} event
*/
modifiedEmitter.queue = function(topic) {
this._queue = this._queue || {};
this._callbacks = this._callbacks || {};
if (this._callbacks[topic]) {
this.emit.apply(this, arguments);
} else {
(this._queue[topic] = this._queue[topic] || [])
.push([].slice.call(arguments, 1));
}
}
/**
* Listen on the given `event` with `fn`.
*
* @param {String} event
* @param {Function} fn
* @return {Emitter}
*/
modifiedEmitter.on = modifiedEmitter.addEventListener = function(topic, fn) {
this._queue = this._queue || {};
var topics = this._queue[topic];
cache.apply(this, arguments);
if (!this._callbacks) {
this._callbacks = {};
}
this._callbacks[topic] = true;
if (topics) {
for(var i = 0, l = topics.length; i < l; i++) {
fn.apply(this, topics[i]);
}
delete this._queue[topic];
}
};
return modifiedEmitter;
}
module.exports = Notify;

View File

@@ -10,6 +10,7 @@ const { isMac } = require('./utils/misc.js');
const getGuid = require('./utils/getGuid.js');
const log = require('./log.js')
const logLevels = require('./enums/logLevels.js');
const notify = require('./notify/electron-notify.js');
//context menu
const contextMenu = require('./menus/contextMenu.js');
@@ -69,6 +70,8 @@ function createMainWindow(url) {
if (!isOnline) {
loadErrors.showNetworkConnectivityError(mainWindow, url, retry);
} else {
// removes all existing notifications when main window reloads
notify.reset();
log.send(logLevels.INFO, 'main window loaded');
}
});