Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: feat(client): タブがバックグラウンド(非表示)のときにタイムラインを更新しないように #6388

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/client/scripts/page-visibility.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export function onBecomeVisible(cb) {
const onVisible = (ev) => {
if (document.visibilityState !== 'visible') return;
document.removeEventListener('visibilitychange', onVisible);
cb();
};

document.addEventListener('visibilitychange', onVisible);

return {
remove: () => document.removeEventListener('visibilitychange', onVisible)
};
}
22 changes: 18 additions & 4 deletions src/client/scripts/paging.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { onScrollTop, isTopVisible } from './scroll';
import { onBecomeVisible } from './page-visibility';

const SECOND_FETCH_LIMIT = 30;

Expand Down Expand Up @@ -147,9 +148,10 @@ export default (opts) => ({
},

prepend(item) {
const isTop = this.isBackTop || (document.body.contains(this.$el) && isTopVisible(this.$el));
const isTop = () => this.isBackTop || (document.body.contains(this.$el) && isTopVisible(this.$el));
const isTabVisible = () => document.visibilityState === 'visible';

if (isTop) {
if (isTop() && isTabVisible()) {
// Prepend the item
this.items.unshift(item);

Expand All @@ -160,12 +162,24 @@ export default (opts) => ({
}
} else {
this.queue.push(item);
onScrollTop(this.$el, () => {

let scrollListener;
let visibleListener;

const comeback = () => {
if (!(isTop() && isTabVisible())) return;

for (const item of this.queue) {
this.prepend(item);
}
this.queue = [];
});

scrollListener.remove();
visibleListener.remove();
};

scrollListener = onScrollTop(this.$el, comeback);
visibleListener = onBecomeVisible(comeback);
}
},

Expand Down
5 changes: 5 additions & 0 deletions src/client/scripts/scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ export function onScrollBottom(el: Element, cb) {
container.removeEventListener('scroll', onScroll);
}
};

container.addEventListener('scroll', onScroll, { passive: true });

return {
remove: () => container.removeEventListener('scroll', onscroll)
};
}

export function scroll(el: Element, top: number) {
Expand Down