added log streaming

This commit is contained in:
Chris Zhu
2025-04-06 21:08:31 -07:00
parent e236654c42
commit f382a3ab2b
6 changed files with 32 additions and 5 deletions

View File

@@ -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() {