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

feat: make polling interval longer #38

Merged
merged 4 commits into from
Mar 2, 2024
Merged
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
1 change: 1 addition & 0 deletions workspaces/e2e/tests/tooltip-error/tooltip-error.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ test("should not emit any error event if it gets invalidates quickly", async ({
await page.locator(".start-flow").click();
await expect(page.locator("[data-type='tooltipError']")).toHaveCount(0);
await page.locator(".add-target").click();
await expect(page.locator(".flows-tooltip")).toBeVisible();
await page.locator(".remove-target").click();
await page.locator(".add-target").click();
await expect(page.locator("[data-type='tooltipError']")).toHaveCount(0);
Expand Down
5 changes: 4 additions & 1 deletion workspaces/js/src/core/document-change.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { throttle } from "../lib/throttle";
import { FlowsContext } from "./flows-context";

export const handleDocumentChange = (): void => {
const _handleDocumentChange = (): void => {
FlowsContext.getInstance().instances.forEach((state) => {
if (state.waitingForElement) return state.render();

Expand All @@ -12,3 +13,5 @@ export const handleDocumentChange = (): void => {
}
});
};

export const handleDocumentChange = throttle(_handleDocumentChange, 250);
4 changes: 2 additions & 2 deletions workspaces/js/src/core/flow-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class FlowState {
type: "tooltipError",
targetElement: step.targetElement,
});
}, 1000);
}, 2000);

const isFork = Array.isArray(step);
if (isFork) {
Expand Down Expand Up @@ -217,8 +217,8 @@ export class FlowState {
* Remove the flow element from the DOM. Used before rendering next step and when flow is finished.
*/
unmount(): this {
if (!this.flowElement) return this;
this.waitingForElement = false;
if (!this.flowElement) return this;
this.flowElement.cleanup?.();
this.flowElement.element.remove();
return this;
Expand Down
2 changes: 1 addition & 1 deletion workspaces/js/src/core/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ const _init = (options: FlowsInitOptions): void => {
if (locationChangeInterval !== null) clearInterval(locationChangeInterval);
locationChangeInterval = window.setInterval(() => {
handleLocationChange();
}, 50);
}, 250);

addHandlers([
{ type: "click", handler: handleClick },
Expand Down
4 changes: 2 additions & 2 deletions workspaces/js/src/core/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,11 @@ const getBoundaryEl = (state: FlowState): Element | undefined => {
};

export const render = (state: FlowState): void => {
state.unmount();

const step = state.currentStep;
if (!step) return;

state.unmount();

if (isTooltipStep(step)) {
const target = document.querySelector(step.targetElement);
if (target) {
Expand Down
17 changes: 17 additions & 0 deletions workspaces/js/src/lib/throttle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* eslint-disable @typescript-eslint/no-explicit-any -- handy in this file */

export const throttle = <T extends (...args: any[]) => any>(
fn: T,
delay: number,
): ((...args: Parameters<T>) => void) => {
let timerFlag: number | null = null;

return (...args) => {
if (timerFlag === null) {
timerFlag = window.setTimeout(() => {
timerFlag = null;
fn(...args);
}, delay);
}
};
};
Loading