Matteo Scandolo | 11b4a63 | 2017-02-09 10:28:41 -0800 | [diff] [blame] | 1 | import {IWSEventService} from '../../datasources/websocket/global'; |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 2 | import {IXosModelStoreService} from '../../datasources/stores/model.store'; |
| 3 | import * as _ from 'lodash'; |
| 4 | import {Observable} from 'rxjs'; |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 5 | |
| 6 | export interface IXosOnboarder { |
| 7 | |
| 8 | } |
| 9 | |
| 10 | export class XosOnboarder implements IXosOnboarder { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 11 | static $inject = ['$timeout', '$log', '$q', 'WebSocket', '$ocLazyLoad', 'XosModelStore']; |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 12 | |
| 13 | constructor( |
| 14 | private $timeout: ng.ITimeoutService, |
| 15 | private $log: ng.ILogService, |
| 16 | private $q: ng.IQService, |
| 17 | private webSocket: IWSEventService, |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 18 | private $ocLazyLoad: any, // TODO add definition |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 19 | private XosModelStore: IXosModelStoreService |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 20 | ) { |
| 21 | this.$log.info('[XosOnboarder] Setup'); |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 22 | |
Matteo Scandolo | 11b4a63 | 2017-02-09 10:28:41 -0800 | [diff] [blame] | 23 | // Load onboarded app |
| 24 | const ComponentObservable: Observable<any> = this.XosModelStore.query('XOSGuiExtension'); |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 25 | |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 26 | ComponentObservable.subscribe( |
| 27 | (component) => { |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 28 | _.forEach(component, (c) => { |
Matteo Scandolo | 11b4a63 | 2017-02-09 10:28:41 -0800 | [diff] [blame] | 29 | 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 Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 35 | }); |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 36 | } |
| 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 | } |