feat(preloader): preloader service which can take a list of promises and execute smth once all promises are completed

This commit is contained in:
nixa 2016-05-24 16:40:17 +03:00
parent 02fdd09bc0
commit d1f368e571
6 changed files with 63 additions and 4 deletions

View file

@ -1,5 +1,7 @@
import {Component, ViewChild, ViewEncapsulation, Input, Output, ElementRef, EventEmitter} from '@angular/core';
import {BaThemePreloader} from '../../../theme/services';
import './baAmChart.loader.ts';
import {BaAmChartThemeService} from './baAmChartTheme.service';
@ -18,6 +20,7 @@ export class BaAmChart {
@ViewChild('baAmChart') private _selector:ElementRef;
constructor (private _baAmChartThemeService:BaAmChartThemeService) {
this._loadChartsLib();
}
ngOnInit() {
@ -28,4 +31,12 @@ export class BaAmChart {
let chart = AmCharts.makeChart(this._selector.nativeElement, this.baAmChartConfiguration);
this.onChartReady.emit(chart);
}
private _loadChartsLib():void {
BaThemePreloader.registerLoader(new Promise((resolve, reject) => {
AmCharts.ready(function(){
resolve('AmCharts ready');
});
}));
}
}

View file

@ -0,0 +1 @@
export * from './baImageLoader.service.ts';

View file

@ -0,0 +1,32 @@
import {Injectable} from '@angular/core';
@Injectable()
export class BaThemePreloader {
private static _loaders:Array<Promise<any>> = [];
public static registerLoader(method:Promise<any>):void {
BaThemePreloader._loaders.push(method);
}
public static clear():void {
BaThemePreloader._loaders = [];
}
public static load():Promise<any> {
return new Promise((resolve, reject) => {
BaThemePreloader._executeAll(resolve);
});
}
private static _executeAll(done:Function):void {
setTimeout(() => {
Promise.all(BaThemePreloader._loaders).then((values) => {
done.call(null, values);
}).catch((error) => {
console.error(error);
});
});
}
}

View file

@ -0,0 +1 @@
export * from './baThemePreloader.service.ts';

View file

@ -0,0 +1,2 @@
export * from './baImageLoader';
export * from './baThemePreloader';