pooling of notification window is added

This commit is contained in:
richard-loafle 2020-02-20 15:44:38 +09:00
parent 8e88d78435
commit d555ff2cba
3 changed files with 88 additions and 9 deletions

View File

@ -13,6 +13,10 @@ export interface ElectronNotificationOptions {
appIcon?: string; appIcon?: string;
pathToModule?: string; pathToModule?: string;
logging?: boolean; logging?: boolean;
browserWindowPool?: {
min?: number;
max?: number;
};
defaultStyleContainer?: { defaultStyleContainer?: {
[attribute: string]: any; [attribute: string]: any;
}; };
@ -44,7 +48,10 @@ export const DefaultElectronNotificationOptions: ElectronNotificationOptions = {
appIcon: null, appIcon: null,
pathToModule: '', pathToModule: '',
logging: true, logging: true,
browserWindowPool: {
min: 0,
max: 7
},
defaultStyleContainer: { defaultStyleContainer: {
backgroundColor: '#f0f0f0', backgroundColor: '#f0f0f0',
overflow: 'hidden', overflow: 'hidden',

View File

@ -28,6 +28,69 @@ interface ENDimension {
height: number; height: number;
} }
class BrowserWindowPooler {
private readonly inactiveWindows: BrowserWindow[];
private handle: any;
constructor(private readonly minSize: number) {
this.minSize = 0 > this.minSize ? 0 : this.minSize;
this.inactiveWindows = [];
}
get length() {
return this.inactiveWindows.length;
}
push(...items: BrowserWindow[]): number {
const length = this.inactiveWindows.push(...items);
if (this.minSize < length) {
this.start();
}
return length;
}
pop(): BrowserWindow {
if (!this.inactiveWindows || 0 === this.inactiveWindows.length) {
return undefined;
}
const w = this.inactiveWindows.pop();
if (this.minSize >= this.inactiveWindows.length) {
this.stop();
}
return w;
}
closeAll() {
while (this.inactiveWindows.length > 0) {
const w = this.inactiveWindows.pop();
w.close();
}
}
private start() {
if (!!this.handle) {
this.stop();
}
this.handle = setTimeout(() => {
while (this.minSize < this.inactiveWindows.length) {
const w = this.inactiveWindows.pop();
w.close();
}
}, 3000);
}
private stop() {
if (!!this.handle) {
clearTimeout(this.handle);
this.handle = undefined;
}
}
}
export class ElectronNotificationService { export class ElectronNotificationService {
private animationQueue: AnimationQueue; private animationQueue: AnimationQueue;
private customOptions: ElectronNotificationOptions; private customOptions: ElectronNotificationOptions;
@ -37,7 +100,8 @@ export class ElectronNotificationService {
private lowerRightCornerPosition: ENPoint; private lowerRightCornerPosition: ENPoint;
private maxVisibleNotifications: number; private maxVisibleNotifications: number;
private activeNotifications: BrowserWindow[]; private activeNotifications: BrowserWindow[];
private inactiveWindows: BrowserWindow[]; private browserWindowPooler: BrowserWindowPooler;
private notificationQueue: ElectronNotification[]; private notificationQueue: ElectronNotification[];
private closedNotifications: Map<number, boolean>; private closedNotifications: Map<number, boolean>;
private latestId: number; private latestId: number;
@ -99,13 +163,13 @@ export class ElectronNotificationService {
dispose(): void { dispose(): void {
this.animationQueue.clear(); this.animationQueue.clear();
this.activeNotifications.forEach(window => window.close()); this.activeNotifications.forEach(window => window.close());
this.inactiveWindows.forEach(window => window.close()); this.browserWindowPooler.closeAll();
} }
closeAll(): void { closeAll(): void {
this.animationQueue.clear(); this.animationQueue.clear();
this.activeNotifications.forEach(window => window.close()); this.activeNotifications.forEach(window => window.close());
this.inactiveWindows.forEach(window => window.close()); this.browserWindowPooler.closeAll();
this.setup(); this.setup();
} }
@ -115,7 +179,9 @@ export class ElectronNotificationService {
this.totalDimension = { width: 0, height: 0 }; this.totalDimension = { width: 0, height: 0 };
this.firstPosition = { x: 0, y: 0 }; this.firstPosition = { x: 0, y: 0 };
this.activeNotifications = []; this.activeNotifications = [];
this.inactiveWindows = []; this.browserWindowPooler = new BrowserWindowPooler(
this.options.browserWindowPool.min
);
this.notificationQueue = []; this.notificationQueue = [];
this.closedNotifications = new Map(); this.closedNotifications = new Map();
this.latestId = 0; this.latestId = 0;
@ -136,7 +202,9 @@ export class ElectronNotificationService {
); );
this.maxVisibleNotifications = this.maxVisibleNotifications =
7 < this.maxVisibleNotifications ? 7 : this.maxVisibleNotifications; this.options.browserWindowPool.max < this.maxVisibleNotifications
? this.options.browserWindowPool.max
: this.maxVisibleNotifications;
} }
private setupEvents(): void { private setupEvents(): void {
@ -335,7 +403,7 @@ export class ElectronNotificationService {
} }
const i = self.activeNotifications.indexOf(notificationWindow); const i = self.activeNotifications.indexOf(notificationWindow);
self.activeNotifications.splice(i, 1); self.activeNotifications.splice(i, 1);
self.inactiveWindows.push(notificationWindow); self.browserWindowPooler.push(notificationWindow);
notificationWindow.hide(); notificationWindow.hide();
self.checkForQueuedNotifications(); self.checkForQueuedNotifications();
@ -376,8 +444,8 @@ export class ElectronNotificationService {
private getWindow(): Promise<BrowserWindow> { private getWindow(): Promise<BrowserWindow> {
const slef = this; const slef = this;
return new Promise<BrowserWindow>((resolve, reject) => { return new Promise<BrowserWindow>((resolve, reject) => {
if (0 < slef.inactiveWindows.length) { if (0 < slef.browserWindowPooler.length) {
resolve(slef.inactiveWindows.pop()); resolve(slef.browserWindowPooler.pop());
} else { } else {
const windowProperties = slef.customOptions.defaultWindow; const windowProperties = slef.customOptions.defaultWindow;
windowProperties.width = slef.customOptions.width; windowProperties.width = slef.customOptions.width;

View File

@ -291,6 +291,10 @@ app.on(ElectronAppChannel.Ready, () => {
padding: 0, padding: 0,
borderRadius: 0, borderRadius: 0,
// appIcon: iconPath, // appIcon: iconPath,
browserWindowPool: {
min: 0,
max: 7
},
displayTime: 5000, displayTime: 5000,
defaultStyleContainer: {}, defaultStyleContainer: {},
defaultStyleAppIcon: { display: 'none' }, defaultStyleAppIcon: { display: 'none' },