static getStream() { return Observable.from(waitForPolyfill()).ignoreElements().concat( Observable.create(observer => { const eventSource = new EventSource('applications'); eventSource.onmessage = message => observer.next({ ...message, data: Application._transformResponse(message.data) }); eventSource.onerror = err => observer.error(err); return () => { eventSource.close(); }; })); }
start() { const listing = Observable.defer(() => Application.list()).concatMap(message => message.data); const stream = Application.getStream().map(message => message.data); this.subscription = listing.concat(stream) .doFirst(() => this.dispatchEvent('connected')) .retryWhen(errors => errors .do(error => this.dispatchEvent('error', error)) .delay(5000) ).subscribe({ next: application => { const idx = this.applications.indexOfApplication(application.name); if (idx >= 0) { const oldApplication = this.applications[idx]; if (application.instances.length > 0) { this.applications.splice(idx, 1, application); this.dispatchEvent('updated', application, oldApplication); } else { this.applications.splice(idx, 1); this.dispatchEvent('removed', oldApplication); } } else { this.applications.push(application); this.dispatchEvent('added', application); } } }); }
static getEventStream() { return concat( from(waitForPolyfill()).pipe(ignoreElements()), Observable.create(observer => { const eventSource = new EventSource('instances/events'); eventSource.onmessage = message => observer.next({ ...message, data: JSON.parse(message.data) }); eventSource.onerror = err => observer.error(err); return () => { eventSource.close(); }; }) ); }
render() { return this._v(this.clock); }, computed: { clock() { if (!this.value) { return null; } const duration = moment.duration(this.value * 1000 + this.offset); return `${Math.floor(duration.asDays())}d ${duration.hours()}h ${duration.minutes()}m ${duration.seconds()}s`; } }, watch: { value: 'subscribe' }, methods: { createSubscription() { if (this.value) { const vm = this; this.startTs = moment.now(); this.offset = 0; return Observable.timer(0, 1000).subscribe({ next: () => { vm.offset = moment.now().valueOf() - this.startTs.valueOf(); } }) } } } }