blob: 206a968ef4f8d52086255bc1260da418daf5a844 [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 {
Matteo Scandolo9b460042017-04-14 16:24:45 -07007 onboard(): ng.IPromise<boolean>;
Matteo Scandolo4e870232017-01-30 13:43:05 -08008}
9
10export class XosOnboarder implements IXosOnboarder {
Matteo Scandolo9b460042017-04-14 16:24:45 -070011 static $inject = [
12 '$timeout',
13 '$log',
14 '$q',
15 'WebSocket',
16 '$ocLazyLoad',
17 'XosModelStore'
18 ];
Matteo Scandolo4e870232017-01-30 13:43:05 -080019
20 constructor(
21 private $timeout: ng.ITimeoutService,
22 private $log: ng.ILogService,
23 private $q: ng.IQService,
24 private webSocket: IWSEventService,
Matteo Scandolo47860fe2017-02-02 12:05:55 -080025 private $ocLazyLoad: any, // TODO add definition
Matteo Scandolo1aee1982017-02-17 08:33:23 -080026 private XosModelStore: IXosModelStoreService
Matteo Scandolo4e870232017-01-30 13:43:05 -080027 ) {
Matteo Scandolo9b460042017-04-14 16:24:45 -070028
29 }
30
31 public onboard(): ng.IPromise<boolean> {
32 const d = this.$q.defer();
33
Matteo Scandolo4e870232017-01-30 13:43:05 -080034 this.$log.info('[XosOnboarder] Setup');
Matteo Scandolo47860fe2017-02-02 12:05:55 -080035
Matteo Scandolo11b4a632017-02-09 10:28:41 -080036 // Load onboarded app
37 const ComponentObservable: Observable<any> = this.XosModelStore.query('XOSGuiExtension');
Matteo Scandolo47860fe2017-02-02 12:05:55 -080038
Matteo Scandolo47860fe2017-02-02 12:05:55 -080039 ComponentObservable.subscribe(
Matteo Scandolo9b460042017-04-14 16:24:45 -070040 (component) => {
41 if (component.length === 0) {
42 return d.resolve();
Matteo Scandolo4e870232017-01-30 13:43:05 -080043 }
Matteo Scandolo9b460042017-04-14 16:24:45 -070044 _.forEach(component, (c) => {
45 this.$log.info(`[XosOnboarder] Loading files for app: ${c.name}`);
46 const files = c.files.split(',').map(s => s.trim());
47 this.loadFile(files)
48 .then((res) => {
49 this.$log.info(`[XosOnboarder] All files loaded for app: ${c.name}`);
50 d.resolve();
51 })
52 .catch(e => {
53 this.$log.info(`[XosOnboarder] Error while onboarding apps: `, e);
54 });
55 });
56 }
57 );
58 return d.promise;
Matteo Scandolo4e870232017-01-30 13:43:05 -080059 }
60
61 // NOTE files needs to be loaded in order, so async loop!
62 private loadFile(files: string[], d?: ng.IDeferred<any>): ng.IPromise<string[]> {
63 if (!angular.isDefined(d)) {
64 d = this.$q.defer();
65 }
66 const file = files.shift();
Matteo Scandolo9b460042017-04-14 16:24:45 -070067
Matteo Scandolo4e870232017-01-30 13:43:05 -080068 this.$log.info(`[XosOnboarder] Loading file: ${file}`);
69 this.$ocLazyLoad.load(file)
70 .then((res) => {
71 this.$log.info(`[XosOnboarder] Loaded file: `, file);
72 if (files.length > 0) {
73 return this.loadFile(files, d);
74 }
75 return d.resolve(file);
76 })
77 .catch((err) => {
78 this.$log.error(`[XosOnboarder] Failed to load file: `, err);
79 d.reject(err);
80 });
81
82 return d.promise;
83 }
84}