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

Setup a disabled state on autoplayTimeout #222

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
35 changes: 33 additions & 2 deletions src/ui/carousel/carousel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,36 @@ class AutoplayTimeout {
private readonly delay: number;
private lastStartTime: number;
private timePassed: number;
private disabled: boolean;
private wasPausedWhenDisabled: boolean;

constructor(callback: TimerHandler, delay: number) {
this.callback = callback;
this.delay = delay;
this.timePassed = 0;
this.timeout = setTimeout(callback, delay);
this.lastStartTime = +new Date();
this.disabled = false;
this.wasPausedWhenDisabled = false;
}

enable() {
if (!this.disabled) {
return;
}
this.disabled = false;
if (!this.wasPausedWhenDisabled) {
this.unpause();
}
}

disable() {
if (this.disabled) {
return;
}
this.wasPausedWhenDisabled = this.isPaused();
this.pause();
this.disabled = true;
}

/**
Expand Down Expand Up @@ -653,17 +676,25 @@ export class Carousel implements EventDispatcher {
}
}

private disable() {
this.autoplayTimeout && this.autoplayTimeout.disable();
}

private enable() {
this.autoplayTimeout && this.autoplayTimeout.enable();
}

/**
* Run the loop to do all the necessary work for the carousel.
*/
private onRaf(): void {
this.raf.read(() => {
if (!this.condition()) {
// Don't continue autoplay timing when the carousel isn't running
this.pause();
this.disable();
return;
} else {
this.unpause();
this.enable();
}

if (this.isBeingInteractedWith()) {
Expand Down