perf: Cache SvgModules

It will prevent them to be parsed multiple times.
This commit is contained in:
J??zsef Farkas
2017-06-17 14:28:15 +02:00
committed by József Farkas
parent 10f44f974a
commit ecd495b7c2
5 changed files with 47 additions and 24 deletions

View File

@@ -73,6 +73,7 @@ import { SafeStylePipe } from './shared/pipes';
import { CaptureService } from './shared/services/capture.service';
import { MapperService } from './shared/services/mapper.service';
import { SvgModuleProviderService } from './shared/services/svg-module-provider.service';
import { UhkDeviceService } from './services/uhk-device.service';
import { KeymapEffects, MacroEffects, UserConfigEffects} from './shared/store/effects';
@@ -169,6 +170,7 @@ import { reducer } from '../../shared/src/store/reducers/index';
UhkDeviceDisconnectedGuard,
UhkDeviceInitializedGuard,
UhkDeviceUninitializedGuard,
SvgModuleProviderService,
MapperService,
appRoutingProviders,
KeymapEditGuard,

View File

@@ -2,11 +2,7 @@ import { Component, EventEmitter, Input, OnInit, Output, ChangeDetectionStrategy
import { Module } from '../../../config-serializer/config-items/Module';
import { SvgModule } from '../module';
enum KeyboardLayout {
ANSI,
ISO
}
import { SvgModuleProviderService } from '../../../services/svg-module-provider.service';
@Component({
selector: 'svg-keyboard',
@@ -27,13 +23,13 @@ export class SvgKeyboardComponent implements OnInit {
modules: SvgModule[];
viewBox: string;
constructor() {
constructor(private svgModuleProvider: SvgModuleProviderService) {
this.modules = [];
this.viewBox = '-520 582 1100 470';
}
ngOnInit() {
this.modules = this.getSvgModules();
this.modules = this.svgModuleProvider.getSvgModules();
}
onKeyClick(moduleId: number, keyId: number, keyTarget: HTMLElement): void {
@@ -61,21 +57,4 @@ export class SvgKeyboardComponent implements OnInit {
});
}
private getSvgModules(): SvgModule[] {
const leftModule = new SvgModule(this.getLeftModule());
const rightModule = new SvgModule(this.getRightModule());
return [rightModule, leftModule];
}
private getLeftModule(layout = KeyboardLayout.ANSI): any {
if (layout === KeyboardLayout.ISO) {
return require('xml-loader!../../../../../modules/uhk60-left-half/layout-iso.svg').svg;
}
return require('xml-loader!../../../../../modules/uhk60-left-half/layout-ansi.svg').svg;
}
private getRightModule(): any {
return require('xml-loader!../../../../../modules/uhk60-right-half/layout.svg').svg;
}
}

View File

@@ -0,0 +1,4 @@
export enum KeyboardLayout {
ANSI,
ISO
}

View File

@@ -0,0 +1,36 @@
import { Injectable } from '@angular/core';
import { SvgModule } from '../components/svg/module';
import { KeyboardLayout } from '../keyboard/keyboard-layout.enum';
@Injectable()
export class SvgModuleProviderService {
private ansiLeft: SvgModule;
private isoLeft: SvgModule;
private right: SvgModule;
getSvgModules(layout = KeyboardLayout.ANSI): SvgModule[] {
return [this.getRightModule(), this.getLeftModule(layout)];
}
private getLeftModule(layout = KeyboardLayout.ANSI): SvgModule {
if (layout === KeyboardLayout.ISO) {
if (!this.isoLeft) {
this.isoLeft = new SvgModule(require('xml-loader!../../../modules/uhk60-left-half/layout-iso.svg').svg);
}
return this.isoLeft;
}
if (!this.ansiLeft) {
this.ansiLeft = new SvgModule(require('xml-loader!../../../modules/uhk60-left-half/layout-ansi.svg').svg);
}
return this.ansiLeft;
}
private getRightModule(): SvgModule {
if (!this.right) {
this.right = new SvgModule(require('xml-loader!../../../modules/uhk60-right-half/layout.svg').svg);
}
return this.right;
}
}

View File

@@ -67,6 +67,7 @@ import { SafeStylePipe } from './shared/pipes';
import { CaptureService } from './shared/services/capture.service';
import { MapperService } from './shared/services/mapper.service';
import { SvgModuleProviderService } from './shared/services/svg-module-provider.service';
import { KeymapEffects, MacroEffects, UserConfigEffects } from './shared/store/effects';
@@ -149,6 +150,7 @@ import { reducer } from '../../shared/src/store/reducers/index';
EffectsModule.runAfterBootstrap(UserConfigEffects)
],
providers: [
SvgModuleProviderService,
MapperService,
appRoutingProviders,
KeymapEditGuard,