From 94f6acabc7f0bbdcd8a73d5cbbdd54af1487d159 Mon Sep 17 00:00:00 2001 From: crusader Date: Fri, 17 Aug 2018 09:10:51 +0900 Subject: [PATCH] ing --- @overflow/core/emitter.ts | 53 +++++++++++++++++++ src/app/app.component.ts | 15 +++++- src/app/app.module.ts | 3 ++ src/commons/commons.module.ts | 17 ++++++ src/commons/service/electron-proxy.service.ts | 17 ++++++ src/commons/service/index.ts | 5 ++ src/electron/app-window.ts | 19 +++---- src/electron/main.ts | 6 +-- 8 files changed, 121 insertions(+), 14 deletions(-) create mode 100644 @overflow/core/emitter.ts create mode 100644 src/commons/commons.module.ts create mode 100644 src/commons/service/electron-proxy.service.ts create mode 100644 src/commons/service/index.ts diff --git a/@overflow/core/emitter.ts b/@overflow/core/emitter.ts new file mode 100644 index 0000000..e6609b6 --- /dev/null +++ b/@overflow/core/emitter.ts @@ -0,0 +1,53 @@ +import { Subject, PartialObserver } from 'rxjs'; + +export class Emitter { + private subjects: Map>; + + public constructor() { + this.subjects = new Map(); + } + + private static createName(name: string): string { + return '$' + name; + } + + /** + * emit + */ + public emit(name: string, data?: any) { + const fnName = Emitter.createName(name); + + if (!this.subjects.has(fnName)) { + this.subjects.set(fnName, new Subject()); + } + + this.subjects.get(fnName).next(data); + } + + /** + * listen + */ + public listen(name: string, handler: (data?: any) => void) { + const fnName = Emitter.createName(name); + + if (!this.subjects.has(fnName)) { + this.subjects.set(fnName, new Subject()); + } + + return this.subjects.get(fnName).subscribe(handler); + } + + /** + * dispose + */ + public dispose() { + const _subjects = this.subjects; + _subjects.forEach((value: Subject, key: string) => { + if (_subjects.has(key)) { + _subjects.get(key).unsubscribe(); + } + }); + + this.subjects = new Map(); + } +} diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 05c4561..e68add2 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,10 +1,21 @@ -import { Component } from '@angular/core'; +import { Component, OnInit } from '@angular/core'; +import { ElectronProxyService } from '../commons/service/electron-proxy.service'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.scss'] }) -export class AppComponent { +export class AppComponent implements OnInit { title = 'scanner-app'; + + public constructor( + private electronProxyService: ElectronProxyService, + ) { + + } + + ngOnInit(): void { + this.electronProxyService.sendReady(performance.now()); + } } diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 715185a..5e81209 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -7,6 +7,8 @@ import { CommonsUIModule } from '@overflow/commons/ui/commons-ui.module'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; +import { CommonsModule } from '../commons/commons.module'; + @NgModule({ declarations: [ AppComponent @@ -17,6 +19,7 @@ import { AppComponent } from './app.component'; BrowserAnimationsModule, CommonsUIModule, + CommonsModule, ], providers: [], bootstrap: [AppComponent] diff --git a/src/commons/commons.module.ts b/src/commons/commons.module.ts new file mode 100644 index 0000000..f2f0d9a --- /dev/null +++ b/src/commons/commons.module.ts @@ -0,0 +1,17 @@ +import { NgModule, APP_INITIALIZER } from '@angular/core'; +import { CommonModule } from '@angular/common'; + + +import { SERVICES } from './service'; + +@NgModule({ + imports: [ + CommonModule, + ], + declarations: [ + ], + providers: [ + ...SERVICES, + ] +}) +export class CommonsModule { } diff --git a/src/commons/service/electron-proxy.service.ts b/src/commons/service/electron-proxy.service.ts new file mode 100644 index 0000000..ab45415 --- /dev/null +++ b/src/commons/service/electron-proxy.service.ts @@ -0,0 +1,17 @@ +import { Injectable } from '@angular/core'; +import { Observable } from 'rxjs'; + +import { ipcRenderer } from 'electron'; + +@Injectable() +export class ElectronProxyService { + + public constructor( + ) { + } + + public sendReady(time: number): void { + ipcRenderer.send('renderer-ready', time); + } + +} diff --git a/src/commons/service/index.ts b/src/commons/service/index.ts new file mode 100644 index 0000000..f9f0f35 --- /dev/null +++ b/src/commons/service/index.ts @@ -0,0 +1,5 @@ +import { ElectronProxyService } from './electron-proxy.service'; + +export const SERVICES = [ + ElectronProxyService, +]; diff --git a/src/electron/app-window.ts b/src/electron/app-window.ts index 9ded5c4..1a46536 100644 --- a/src/electron/app-window.ts +++ b/src/electron/app-window.ts @@ -1,7 +1,10 @@ import { BrowserWindow, ipcMain, Menu, app, dialog } from 'electron'; import * as path from 'path'; import * as URL from 'url'; -import { EventEmitter } from 'events'; + +import { Subscription, PartialObserver } from 'rxjs'; + +import { Emitter } from '@overflow/core/emitter'; import { encodePathAsUrl } from '@overflow/core/path'; import { registerWindowStateChangedEvents } from '@overflow/core/window-state'; @@ -14,7 +17,7 @@ let windowStateKeeper: any | null = null; export class AppWindow { private window: Electron.BrowserWindow; - private emitter = new EventEmitter(); + private emitter = new Emitter(); private _loadTime: number | null = null; private _rendererReadyTime: number | null = null; @@ -42,7 +45,7 @@ export class AppWindow { height: savedWindowState.height, minWidth: this.minWidth, minHeight: this.minHeight, - show: true, + show: false, // This fixes subpixel aliasing on Windows // See https://github.com/atom/atom/commit/683bef5b9d133cb194b476938c77cc07fd05b972 backgroundColor: '#fff', @@ -61,11 +64,9 @@ export class AppWindow { } else if (__WIN32__) { windowOptions.frame = false; } else if (__LINUX__) { - // windowOptions.icon = path.join(__dirname, 'static', 'icon-logo.png'); + windowOptions.icon = path.join(__dirname, 'static', 'icon-logo.png'); } - console.log(windowOptions); - this.window = new BrowserWindow(windowOptions); savedWindowState.manage(this.window); @@ -159,7 +160,7 @@ export class AppWindow { return; } - this.emitter.emit('did-load', null); + this.emitter.emit('did-load'); } /** Is the page loaded and has the renderer signalled it's ready? */ @@ -175,8 +176,8 @@ export class AppWindow { * Register a function to call when the window is done loading. At that point * the page has loaded and the renderer has signalled that it is ready. */ - public onDidLoad(fn: () => void): EventEmitter { - return this.emitter.on('did-load', fn); + public onDidLoad(fn: (data?: any) => void): Subscription { + return this.emitter.listen('did-load', fn); } public isMinimized() { diff --git a/src/electron/main.ts b/src/electron/main.ts index 618971f..3e3ba11 100644 --- a/src/electron/main.ts +++ b/src/electron/main.ts @@ -48,7 +48,7 @@ process.on('uncaughtException', (error: Error) => { handleUncaughtException(error); }); -let handlingSquirrelEvent = false; +const handlingSquirrelEvent = false; // if (__WIN32__ && process.argv.length > 1) { // const arg = process.argv[1]; @@ -78,7 +78,7 @@ let handlingSquirrelEvent = false; // }); // } -let isDuplicateInstance = false; +const isDuplicateInstance = false; // If we're handling a Squirrel event we don't want to enforce single instance. // We want to let the updated instance launch and do its work. It will then quit // once it's done. @@ -426,7 +426,7 @@ function createWindow() { } }); - window.onDidLoad(() => { + window.onDidLoad((data: any) => { window.show(); // window.sendLaunchTimingStats({ // mainReadyTime: readyTime!,