Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 1 | import {IWSEventService, IWSEvent} from '../../datasources/websocket/global'; |
| 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 | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 11 | static $inject = ['$timeout', '$log', '$q', 'WebSocket', '$ocLazyLoad', 'ModelStore']; |
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 |
| 19 | private ModelStore: 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 | |
| 23 | // Listen for new app (we need a pause to allow the container to boot) |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 24 | this.webSocket.list() |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 25 | .filter((e: IWSEvent) => { |
| 26 | if (e.model === 'XOSComponent' && e.msg.object.extra) { |
| 27 | e.msg.object.extra = JSON.parse(e.msg.object.extra); |
| 28 | return true; |
| 29 | } |
| 30 | return false; |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 31 | }) |
| 32 | .subscribe( |
| 33 | (event) => { |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 34 | this.$timeout(() => { |
| 35 | this.$log.info(`[XosOnboarder] Loading files for app: ${event.msg.object.name}`); |
| 36 | // NOTE we need the timeout because the event is triggered when the model is created, |
| 37 | // XOS take around 15s to boot it |
| 38 | this.loadFile(event.msg.object.extra) |
| 39 | .then((res) => { |
| 40 | this.$log.info(`[XosOnboarder] All files loaded for app: ${event.msg.object.name}`); |
| 41 | }); |
| 42 | }, 20 * 1000); |
| 43 | } |
| 44 | ); |
| 45 | |
| 46 | // Load previously onboarded app (containers are already running, so we don't need to wait) |
| 47 | let componentsLoaded = false; |
| 48 | const ComponentObservable: Observable<any> = this.ModelStore.query('XOSComponent'); |
| 49 | ComponentObservable.subscribe( |
| 50 | (component) => { |
| 51 | if (componentsLoaded) { |
| 52 | // if we have already loaded the component present when we loaded the page |
| 53 | // do nothing, we are intercepting WS to give the container time to boot |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | _.forEach(component, (c) => { |
| 58 | if (c.extra) { |
| 59 | this.$log.info(`[XosOnboarder] Loading files for app: ${c.name}`); |
| 60 | let extra; |
| 61 | try { |
| 62 | extra = JSON.parse(c.extra); |
| 63 | } catch (e) { |
| 64 | extra = c.extra; |
| 65 | } |
| 66 | this.loadFile(extra) |
| 67 | .then((res) => { |
| 68 | this.$log.info(`[XosOnboarder] All files loaded for app: ${c.name}`); |
| 69 | }); |
| 70 | } |
| 71 | }); |
| 72 | componentsLoaded = true; |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 73 | } |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | // NOTE files needs to be loaded in order, so async loop! |
| 78 | private loadFile(files: string[], d?: ng.IDeferred<any>): ng.IPromise<string[]> { |
| 79 | if (!angular.isDefined(d)) { |
| 80 | d = this.$q.defer(); |
| 81 | } |
| 82 | const file = files.shift(); |
| 83 | this.$log.info(`[XosOnboarder] Loading file: ${file}`); |
| 84 | this.$ocLazyLoad.load(file) |
| 85 | .then((res) => { |
| 86 | this.$log.info(`[XosOnboarder] Loaded file: `, file); |
| 87 | if (files.length > 0) { |
| 88 | return this.loadFile(files, d); |
| 89 | } |
| 90 | return d.resolve(file); |
| 91 | }) |
| 92 | .catch((err) => { |
| 93 | this.$log.error(`[XosOnboarder] Failed to load file: `, err); |
| 94 | d.reject(err); |
| 95 | }); |
| 96 | |
| 97 | return d.promise; |
| 98 | } |
| 99 | } |