Skip to content

Commit

Permalink
feat(Incito, IncitoPublicationKit): Implement pausing
Browse files Browse the repository at this point in the history
Currently this will only affect section visibility tracking

Fixes #180
  • Loading branch information
klarstrup committed Apr 26, 2023
1 parent 8a31734 commit f01b4fe
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
34 changes: 27 additions & 7 deletions lib/incito-browser/incito.ts
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,21 @@ export default class Incito extends MicroEvent<{

this.trigger('started');
}
pause() {
// Stop receiving events that could cause further section visibility logging
this.removeWindowEventListeners();
this.sectionObserver.disconnect();
// Dispatch any actively visible sections
this.onVisibilityChange('hidden');

// Reset section visiblity tracking
this.sectionVisibility = new Map();
}
unpause() {
this.onVisibilityChange('visible');
this.addWindowEventListeners();
if (this.canLazyload) this.observeElements(this.el);
}

destroy() {
this.lazyObserver?.disconnect();
Expand All @@ -793,15 +808,10 @@ export default class Incito extends MicroEvent<{

this.styleEl?.parentNode?.removeChild(this.styleEl);

window.removeEventListener('visibilitychange', this.handleVisibility);
window.removeEventListener('blur', this.handleBlur);
window.removeEventListener('focus', this.handleFocus);
window.removeEventListener('pagehide', this.handlePageHide);
window.removeEventListener('beforeunload', this.handleBeforeUnload);
this.removeWindowEventListeners();

this.trigger('destroyed');
}

observeElements(el: HTMLDivElement) {
this.sectionVisibility = new Map();
el.querySelectorAll('[data-role="section"]').forEach(
Expand Down Expand Up @@ -892,12 +902,22 @@ export default class Incito extends MicroEvent<{
handleVisibility = () => this.onVisibilityChange(document.visibilityState);
handlePageHide = () => this.onVisibilityChange('hidden');
handleBeforeUnload = () => this.onVisibilityChange('hidden');
enableLazyloading() {
addWindowEventListeners() {
window.addEventListener('visibilitychange', this.handleVisibility);
window.addEventListener('blur', this.handleBlur);
window.addEventListener('focus', this.handleFocus);
window.addEventListener('pagehide', this.handlePageHide);
window.addEventListener('beforeunload', this.handleBeforeUnload);
}
removeWindowEventListeners() {
window.removeEventListener('visibilitychange', this.handleVisibility);
window.removeEventListener('blur', this.handleBlur);
window.removeEventListener('focus', this.handleFocus);
window.removeEventListener('pagehide', this.handlePageHide);
window.removeEventListener('beforeunload', this.handleBeforeUnload);
}
enableLazyloading() {
this.addWindowEventListeners();

this.sectionObserver = new IntersectionObserver(
(entries) =>
Expand Down
8 changes: 8 additions & 0 deletions lib/kits/incito-publication/viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ class Viewer extends MicroEvent {
return this;
}

pause() {
this.incito.pause();
}

unpause() {
this.incito.unpause();
}

destroy() {
this.incito.destroy();
}
Expand Down

0 comments on commit f01b4fe

Please sign in to comment.