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 { |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 7 | onboard(): ng.IPromise<boolean>; |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 8 | } |
| 9 | |
| 10 | export class XosOnboarder implements IXosOnboarder { |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 11 | static $inject = [ |
| 12 | '$timeout', |
| 13 | '$log', |
| 14 | '$q', |
| 15 | 'WebSocket', |
| 16 | '$ocLazyLoad', |
| 17 | 'XosModelStore' |
| 18 | ]; |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 19 | |
| 20 | constructor( |
| 21 | private $timeout: ng.ITimeoutService, |
| 22 | private $log: ng.ILogService, |
| 23 | private $q: ng.IQService, |
| 24 | private webSocket: IWSEventService, |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 25 | private $ocLazyLoad: any, // TODO add definition |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 26 | private XosModelStore: IXosModelStoreService |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 27 | ) { |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 28 | |
| 29 | } |
| 30 | |
| 31 | public onboard(): ng.IPromise<boolean> { |
| 32 | const d = this.$q.defer(); |
| 33 | |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 34 | this.$log.info('[XosOnboarder] Setup'); |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 35 | |
Matteo Scandolo | 11b4a63 | 2017-02-09 10:28:41 -0800 | [diff] [blame] | 36 | // Load onboarded app |
| 37 | const ComponentObservable: Observable<any> = this.XosModelStore.query('XOSGuiExtension'); |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 38 | |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 39 | ComponentObservable.subscribe( |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 40 | (component) => { |
| 41 | if (component.length === 0) { |
| 42 | return d.resolve(); |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 43 | } |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 44 | _.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 Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 59 | } |
| 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 Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 67 | |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 68 | 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 | } |