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

@@ -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;
}
}