fix snapping to new messages

This commit is contained in:
MathMan05 2024-09-09 11:24:02 -05:00
parent 7dd6082f24
commit 7cf4e40a5f
3 changed files with 22 additions and 28 deletions

View file

@ -89,7 +89,6 @@ class InfiniteScroller {
this.div.appendChild(html);
this.HTMLElements.push([html, id]);
}
currrunning = false;
async addedBottom() {
await this.updatestuff();
const func = this.snapBottom();
@ -98,9 +97,11 @@ class InfiniteScroller {
}
snapBottom() {
const scrollBottom = this.scrollBottom;
console.log(scrollBottom);
return () => {
if (this.div && scrollBottom < 4) {
this.div.scrollTop = this.div.scrollHeight + 20;
console.log("snap");
this.div.scrollTop = this.div.scrollHeight;
}
};
}
@ -154,6 +155,9 @@ class InfiniteScroller {
}
}
async watchForBottom(already = false, fragement = new DocumentFragment()) {
let func;
if (!already)
func = this.snapBottom();
if (!this.div)
return false;
try {
@ -192,8 +196,8 @@ class InfiniteScroller {
finally {
if (!already) {
this.div.append(fragement);
if (this.scrollBottom < 30) {
this.div.scrollTop = this.div.scrollHeight;
if (func) {
func();
}
}
}
@ -201,18 +205,12 @@ class InfiniteScroller {
watchtime = false;
changePromise;
async watchForChange() {
if (this.currrunning) {
if (this.changePromise) {
this.watchtime = true;
if (this.changePromise) {
return await this.changePromise;
}
else {
return false;
}
return await this.changePromise;
}
else {
this.watchtime = false;
this.currrunning = true;
}
this.changePromise = new Promise(async (res) => {
try {
@ -226,7 +224,7 @@ class InfiniteScroller {
if (this.timeout === null && changed) {
this.timeout = setTimeout(this.updatestuff.bind(this), 300);
}
if (!this.currrunning) {
if (!this.changePromise) {
console.error("something really bad happened");
}
res(Boolean(changed));
@ -244,7 +242,6 @@ class InfiniteScroller {
finally {
setTimeout(_ => {
this.changePromise = undefined;
this.currrunning = false;
if (this.watchtime) {
this.watchForChange();
}