blob: b6a9abcea6286e64fcc417e83ab4117ffc827920 [file] [log] [blame]
Matteo Scandolo11b4a632017-02-09 10:28:41 -08001import {IWSEventService} from '../../datasources/websocket/global';
Matteo Scandolo47860fe2017-02-02 12:05:55 -08002import {IXosModelStoreService} from '../../datasources/stores/model.store';
3import * as _ from 'lodash';
4import {Observable} from 'rxjs';
Matteo Scandolo4e870232017-01-30 13:43:05 -08005
6export interface IXosOnboarder {
7
8}
9
10export class XosOnboarder implements IXosOnboarder {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080011 static $inject = ['$timeout', '$log', '$q', 'WebSocket', '$ocLazyLoad', 'XosModelStore'];
Matteo Scandolo4e870232017-01-30 13:43:05 -080012
13 constructor(
14 private $timeout: ng.ITimeoutService,
15 private $log: ng.ILogService,
16 private $q: ng.IQService,
17 private webSocket: IWSEventService,
Matteo Scandolo47860fe2017-02-02 12:05:55 -080018 private $ocLazyLoad: any, // TODO add definition
Matteo Scandolo1aee1982017-02-17 08:33:23 -080019 private XosModelStore: IXosModelStoreService
Matteo Scandolo4e870232017-01-30 13:43:05 -080020 ) {
21 this.$log.info('[XosOnboarder] Setup');
Matteo Scandolo47860fe2017-02-02 12:05:55 -080022
Matteo Scandolo11b4a632017-02-09 10:28:41 -080023 // Load onboarded app
24 const ComponentObservable: Observable<any> = this.XosModelStore.query('XOSGuiExtension');
Matteo Scandolo47860fe2017-02-02 12:05:55 -080025
Matteo Scandolo47860fe2017-02-02 12:05:55 -080026 ComponentObservable.subscribe(
27 (component) => {
Matteo Scandolo47860fe2017-02-02 12:05:55 -080028 _.forEach(component, (c) => {
Matteo Scandolo11b4a632017-02-09 10:28:41 -080029 this.$log.info(`[XosOnboarder] Loading files for app: ${c.name}`);
30 const files = c.files.split(',').map(s => s.trim());
31 this.loadFile(files)
32 .then((res) => {
33 this.$log.info(`[XosOnboarder] All files loaded for app: ${c.name}`);
34 });
Matteo Scandolo47860fe2017-02-02 12:05:55 -080035 });
Matteo Scandolo4e870232017-01-30 13:43:05 -080036 }
37 );
38 }
39
40 // NOTE files needs to be loaded in order, so async loop!
41 private loadFile(files: string[], d?: ng.IDeferred<any>): ng.IPromise<string[]> {
42 if (!angular.isDefined(d)) {
43 d = this.$q.defer();
44 }
45 const file = files.shift();
46 this.$log.info(`[XosOnboarder] Loading file: ${file}`);
47 this.$ocLazyLoad.load(file)
48 .then((res) => {
49 this.$log.info(`[XosOnboarder] Loaded file: `, file);
50 if (files.length > 0) {
51 return this.loadFile(files, d);
52 }
53 return d.resolve(file);
54 })
55 .catch((err) => {
56 this.$log.error(`[XosOnboarder] Failed to load file: `, err);
57 d.reject(err);
58 });
59
60 return d.promise;
61 }
62}