open-nomad/ui/app/components/streaming-file.js

122 lines
3.1 KiB
JavaScript
Raw Normal View History

import Component from '@ember/component';
import { run } from '@ember/runloop';
import { task } from 'ember-concurrency';
2019-07-02 19:59:15 +00:00
import WindowResizable from 'nomad-ui/mixins/window-resizable';
import { classNames, tagName } from '@ember-decorators/component';
import classic from 'ember-classic-decorator';
@classic
@tagName('pre')
@classNames('cli-window')
export default class StreamingFile extends Component.extend(WindowResizable) {
'data-test-log-cli' = true;
mode = 'streaming'; // head, tail, streaming
isStreaming = true;
logger = null;
follow = true;
// Internal bookkeeping to avoid multiple scroll events on one frame
requestFrame = true;
didReceiveAttrs() {
if (!this.logger) {
return;
}
run.scheduleOnce('actions', this, this.performTask);
}
performTask() {
switch (this.mode) {
case 'head':
this.set('follow', false);
this.head.perform();
break;
case 'tail':
this.set('follow', true);
this.tail.perform();
break;
case 'streaming':
this.set('follow', true);
if (this.isStreaming) {
this.stream.perform();
} else {
this.logger.stop();
}
break;
}
}
scrollHandler() {
const cli = this.element;
window.requestAnimationFrame(() => {
// If the scroll position is close enough to the bottom, autoscroll to the bottom
this.set('follow', cli.scrollHeight - cli.scrollTop - cli.clientHeight < 20);
this.requestFrame = true;
});
this.requestFrame = false;
}
didInsertElement() {
this.fillAvailableHeight();
this.set('_scrollHandler', this.scrollHandler.bind(this));
this.element.addEventListener('scroll', this._scrollHandler);
}
willDestroyElement() {
this.element.removeEventListener('scroll', this._scrollHandler);
}
windowResizeHandler() {
run.once(this, this.fillAvailableHeight);
}
fillAvailableHeight() {
// This math is arbitrary and far from bulletproof, but the UX
// of having the log window fill available height is worth the hack.
const margins = 30; // Account for padding and margin on either side of the CLI
const cliWindow = this.element;
cliWindow.style.height = `${window.innerHeight - cliWindow.offsetTop - margins}px`;
}
@task(function*() {
yield this.get('logger.gotoHead').perform();
run.scheduleOnce('afterRender', this, this.scrollToTop);
})
head;
scrollToTop() {
this.element.scrollTop = 0;
}
@task(function*() {
yield this.get('logger.gotoTail').perform();
})
tail;
synchronizeScrollPosition() {
if (this.follow) {
this.element.scrollTop = this.element.scrollHeight;
}
}
@task(function*() {
2019-08-07 22:11:32 +00:00
// Follow the log if the scroll position is near the bottom of the cli window
2019-10-08 18:44:19 +00:00
this.logger.on('tick', this, 'scheduleScrollSynchronization');
2019-08-07 22:11:32 +00:00
yield this.logger.startStreaming();
2019-10-08 18:44:19 +00:00
this.logger.off('tick', this, 'scheduleScrollSynchronization');
})
stream;
2019-10-08 18:44:19 +00:00
scheduleScrollSynchronization() {
run.scheduleOnce('afterRender', this, this.synchronizeScrollPosition);
}
2019-10-08 18:44:19 +00:00
willDestroy() {
this.logger.stop();
}
}