Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 1 | import {IWSEventService} from '../../datasources/websocket/global'; |
| 2 | |
| 3 | export interface IXosOnboarder { |
| 4 | |
| 5 | } |
| 6 | |
| 7 | export class XosOnboarder implements IXosOnboarder { |
| 8 | static $inject = ['$timeout', '$log', '$q', 'WebSocket', '$ocLazyLoad']; |
| 9 | |
| 10 | constructor( |
| 11 | private $timeout: ng.ITimeoutService, |
| 12 | private $log: ng.ILogService, |
| 13 | private $q: ng.IQService, |
| 14 | private webSocket: IWSEventService, |
| 15 | private $ocLazyLoad: any // TODO add definition |
| 16 | ) { |
| 17 | this.$log.info('[XosOnboarder] Setup'); |
| 18 | this.webSocket.list() |
| 19 | .filter((e) => { |
| 20 | this.$log.log(e); |
| 21 | // TODO define event format |
| 22 | return e.msg['files'].length > 0; |
| 23 | }) |
| 24 | .subscribe( |
| 25 | (event) => { |
| 26 | this.loadFile(event.msg['files']) |
| 27 | .then((res) => { |
| 28 | this.$log.info(`[XosOnboarder] All files loaded for app: ${event.msg['app']}`); |
| 29 | }); |
| 30 | } |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | // NOTE files needs to be loaded in order, so async loop! |
| 35 | private loadFile(files: string[], d?: ng.IDeferred<any>): ng.IPromise<string[]> { |
| 36 | if (!angular.isDefined(d)) { |
| 37 | d = this.$q.defer(); |
| 38 | } |
| 39 | const file = files.shift(); |
| 40 | this.$log.info(`[XosOnboarder] Loading file: ${file}`); |
| 41 | this.$ocLazyLoad.load(file) |
| 42 | .then((res) => { |
| 43 | this.$log.info(`[XosOnboarder] Loaded file: `, file); |
| 44 | if (files.length > 0) { |
| 45 | return this.loadFile(files, d); |
| 46 | } |
| 47 | return d.resolve(file); |
| 48 | }) |
| 49 | .catch((err) => { |
| 50 | this.$log.error(`[XosOnboarder] Failed to load file: `, err); |
| 51 | d.reject(err); |
| 52 | }); |
| 53 | |
| 54 | return d.promise; |
| 55 | } |
| 56 | } |