mirror of
https://github.com/czhu12/canine.git
synced 2026-01-06 03:30:16 -06:00
added log streaming
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import { Controller } from "@hotwired/stimulus"
|
||||
import { get } from "@rails/request.js"
|
||||
|
||||
const REFRESH_INTERVAL = 4000;
|
||||
export default class extends Controller {
|
||||
static targets = ["container"]
|
||||
|
||||
@@ -9,6 +11,24 @@ export default class extends Controller {
|
||||
|
||||
// Add event listener for Turbo Frame load
|
||||
document.addEventListener('turbo:frame-load', this.scrollToBottom.bind(this));
|
||||
this.interval = setInterval(() => {
|
||||
// Fetch new logs
|
||||
this.loadNewLogs();
|
||||
}, REFRESH_INTERVAL);
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
clearInterval(this.interval);
|
||||
}
|
||||
|
||||
async loadNewLogs() {
|
||||
// Only do this if the scroll is at the bottom
|
||||
if (this.containerTarget.scrollTop === this.containerTarget.scrollHeight) {
|
||||
await get(this.element.dataset.url, {
|
||||
responseKind: 'turbo-stream'
|
||||
});
|
||||
this.scrollToBottom();
|
||||
}
|
||||
}
|
||||
|
||||
scrollToBottom() {
|
||||
|
||||
Reference in New Issue
Block a user