Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
Connection/queue status (#893)
Oleg Isonen90ce5a06b101
diff --git a/apps/designer/app/designer/designer.tsx b/apps/designer/app/designer/designer.tsxindex 91d5f38e7d..a33bf455c3 100644--- a/apps/designer/app/designer/designer.tsx+++ b/apps/designer/app/designer/designer.tsx@@ -8,7 +8,7 @@ import {} from "@webstudio-is/project";import { theme, Box, type CSS, Flex, Grid } from "@webstudio-is/design-system";import { registerContainers, useDesignerStore } from "~/shared/sync";-import { useSyncServer } from "./shared/sync-server";+import { useSyncServer } from "./shared/sync/sync-server";// eslint-disable-next-line import/no-internal-modulesimport interFont from "@fontsource/inter/variable.css";import { SidebarLeft } from "./features/sidebar-left";diff --git a/apps/designer/app/designer/features/topbar/sync-status.tsx b/apps/designer/app/designer/features/topbar/sync-status.tsxindex d570539b40..fe8cf377ca 100644--- a/apps/designer/app/designer/features/topbar/sync-status.tsx+++ b/apps/designer/app/designer/features/topbar/sync-status.tsx@@ -1,50 +1,62 @@import { useStore } from "@nanostores/react";+import { atom } from "nanostores";import {- theme,Flex,- keyframes,AccessibleIcon,- styled,- Box,+ rawTheme,+ Tooltip,} from "@webstudio-is/design-system";-import { CheckIcon, DotsHorizontalIcon } from "@webstudio-is/icons";-import { syncStatus } from "~/designer/shared/sync-server";+import { CloudIcon } from "@webstudio-is/icons";+import { useEffect } from "react";+import { queueStatus } from "~/designer/shared/sync";-const iconSize = 15;+const isOnlineStore = atom(false);-const StyledCheckIcon = styled(CheckIcon, {- width: iconSize,- height: iconSize,- background: theme.colors.green9,- borderRadius: theme.borderRadius.round,-});--const ellipsisKeyframes = keyframes({- to: { width: iconSize },-});--const AnimatedDotsIcon = () => {- return (- <Flex direction="column" css={{ width: iconSize }}>- <Box- css={{- animation: `${ellipsisKeyframes} steps(4,end) 900ms infinite`,- width: 0,- overflow: "hidden",- }}- >- <DotsHorizontalIcon width="12" height="12" />- </Box>- </Flex>- );+const subscribeIsOnline = () => {+ const handle = () => isOnlineStore.set(navigator.onLine);+ addEventListener("offline", handle);+ addEventListener("online", handle);+ return () => {+ removeEventListener("offline", handle);+ removeEventListener("online", handle);+ };};export const SyncStatus = () => {- const status = useStore(syncStatus);+ const status = useStore(queueStatus);+ const isOnline = useStore(isOnlineStore);+ useEffect(subscribeIsOnline, []);++ if (status !== "failed") {+ return null;+ }+return (<Flex align="center" justify="center"><AccessibleIcon label={`Sync status: ${status}`}>- {status === "syncing" ? <AnimatedDotsIcon /> : <StyledCheckIcon />}+ <Tooltip+ variant="wrapped"+ content={+ <>+ Offline changes will be synced with Webstudio once you go online.+ {isOnline ? (+ ""+ ) : (+ <>+ <br />+ Please check your internet connection.+ </>+ )}+ </>+ }+ >+ {/* @todo replace the icon, waiting for figma */}+ <CloudIcon+ width={20}+ height={20}+ color={rawTheme.colors.foregroundDestructive}+ />+ </Tooltip></AccessibleIcon></Flex>);diff --git a/apps/designer/app/designer/shared/sync-server.ts b/apps/designer/app/designer/shared/sync-server.tsdeleted file mode 100644index 808f03a493..0000000000--- a/apps/designer/app/designer/shared/sync-server.ts+++ /dev/null@@ -1,82 +0,0 @@-import { atom } from "nanostores";-import { sync } from "immerhin";-import type { Build, Project } from "@webstudio-is/project";-import type { Tree } from "@webstudio-is/project-build";-import { restPatchPath } from "~/shared/router-utils";-import { useEffect } from "react";--export type SyncStatus = "syncing" | "idle";--export const syncStatus = atom<SyncStatus>("idle");--// @todo because of limitation with nested models and saving tree as a Json type-// we load that entire Json to manipulate and if a second request happens before the first one has written-// the result into the db, the second request will have outdated tree and will then write the outdated tree back-// to the database-// For now we will just have to queue the changes for all tree mutations.--type Job = () => Promise<unknown>;--const queue: Array<Job> = [];--export const enqueue = (job: Job) => {- queue.push(job);- if (isInProgress === false) {- dequeue();- }-};--let isInProgress = false;--const dequeue = () => {- const job = queue.shift();- if (job) {- isInProgress = true;- syncStatus.set("syncing");- job().finally(() => {- isInProgress = false;- syncStatus.set("idle");- dequeue();- });- }-};--export const useSyncServer = ({- treeId,- buildId,- projectId,-}: {- buildId: Build["id"];- treeId: Tree["id"];- projectId: Project["id"];-}) => {- useEffect(() => {- const intervalId = setInterval(() => {- // @todo prevent clearing queue in case request is failed- const entries = sync();- if (entries.length === 0) {- return;- }-- // @todo this entire queueing logic needs to be gone, it's a workaround,- // because prisma can't do atomic updates yet with sandbox documents- // and backend fetches and updates big objects, so if we send quickly,- // we end up overwriting things- enqueue(() =>- // @todo start next round only when the request is completed- fetch(restPatchPath(), {- method: "post",- body: JSON.stringify({- transactions: entries,- treeId,- buildId,- projectId,- }),- })- );- }, 1000);- return () => {- clearInterval(intervalId);- };- }, [treeId, buildId, projectId]);-};diff --git a/apps/designer/app/designer/shared/sync/index.ts b/apps/designer/app/designer/shared/sync/index.tsnew file mode 100644index 0000000000..118a3473fe--- /dev/null+++ b/apps/designer/app/designer/shared/sync/index.ts@@ -0,0 +1,2 @@+export { useSyncServer } from "./sync-server";+export { queueStatus } from "./queue";diff --git a/apps/designer/app/designer/shared/sync/queue.test.ts b/apps/designer/app/designer/shared/sync/queue.test.tsnew file mode 100644index 0000000000..6ca1d8e5f6--- /dev/null+++ b/apps/designer/app/designer/shared/sync/queue.test.ts@@ -0,0 +1,63 @@+import { describe } from "@jest/globals";+import { dequeue, enqueue, state, queueStatus } from "./queue";++describe("queue", () => {+ afterEach(() => {+ state.queue = [];+ state.failedAttempts = 0;+ queueStatus.set("idle");+ });++ test("dequeue empty", () => {+ dequeue();+ expect(queueStatus.get()).toBe("idle");+ });++ test("enqueue with success", async () => {+ enqueue(() => Promise.resolve({ ok: true }));+ const jobPromise = dequeue();+ expect(queueStatus.get()).toBe("running");+ expect(state.failedAttempts).toBe(0);+ expect(state.queue.length).toBe(0);+ await jobPromise;+ expect(queueStatus.get()).toBe("idle");+ expect(state.failedAttempts).toBe(0);+ expect(state.queue.length).toBe(0);+ });++ test("enqueue with failure", async () => {+ enqueue(() => Promise.resolve({ ok: false }));+ const jobPromise = dequeue();+ expect(queueStatus.get()).toBe("running");+ expect(state.failedAttempts).toBe(0);+ expect(state.queue.length).toBe(0);++ await Promise.allSettled([jobPromise]);+ expect(queueStatus.get()).toBe("recovering");+ expect(state.failedAttempts).toBe(1);+ expect(state.queue.length).toBe(1);+ });++ test("recovering > failed > idle", async () => {+ let response = Promise.resolve({ ok: false });+ await enqueue(() => response);++ expect(queueStatus.get()).toBe("recovering");++ await dequeue();+ await dequeue();+ await dequeue();+ await dequeue();++ expect(state.queue.length).toBe(1);+ expect(queueStatus.get()).toBe("failed");+ expect(state.failedAttempts).toBe(5);++ response = Promise.resolve({ ok: true });+ await dequeue();++ expect(state.queue.length).toBe(0);+ expect(queueStatus.get()).toBe("idle");+ expect(state.failedAttempts).toBe(0);+ });+});diff --git a/apps/designer/app/designer/shared/sync/queue.ts b/apps/designer/app/designer/shared/sync/queue.tsnew file mode 100644index 0000000000..de4c518bfb--- /dev/null+++ b/apps/designer/app/designer/shared/sync/queue.ts@@ -0,0 +1,67 @@+import { atom } from "nanostores";++// After this amount of retries without success, we consider connection status error.+const MAX_RETRY_RECOVERY = 5;++export type QueueStatus = "running" | "idle" | "recovering" | "failed";++type BaseResult = { ok: boolean };++export const queueStatus = atom<QueueStatus>("idle");++export const state: {+ queue: Array<Job<BaseResult>>;+ failedAttempts: number;+} = {+ queue: [],+ failedAttempts: 0,+};++type Job<Result extends BaseResult> = () => Promise<Result>;++export const enqueue = <Result extends BaseResult>(job: Job<Result>) => {+ state.queue.push(job);+ if (queueStatus.get() === "idle") {+ return dequeue();+ }+};++// We can't change to running or idle status from error or recovering status until+// we have one successful attempt.+const getStatus = (phase: "start" | "end"): QueueStatus => {+ if (state.failedAttempts > 0) {+ if (state.failedAttempts < MAX_RETRY_RECOVERY) {+ return "recovering";+ }+ return "failed";+ }+ return phase === "start" ? "running" : "idle";+};++export const dequeue = () => {+ const job = state.queue.shift();++ if (job === undefined) {+ return;+ }++ queueStatus.set(getStatus("start"));++ return job()+ .then((result) => {+ if (result.ok === false) {+ throw Error("Bad response");+ }+ state.failedAttempts = 0;+ queueStatus.set(getStatus("end"));+ // When the job was successful, we don't need to wait, we can attempt running another job immediately.+ dequeue();+ })+ .catch(() => {+ state.failedAttempts++;+ queueStatus.set(getStatus("end"));+ // Returning the job to the queue allows us to retry it later,+ // from now on, recovery intervals will try to execute the job.+ state.queue.unshift(job);+ });+};diff --git a/apps/designer/app/designer/shared/sync/sync-server.ts b/apps/designer/app/designer/shared/sync/sync-server.tsnew file mode 100644index 0000000000..780e936f6e--- /dev/null+++ b/apps/designer/app/designer/shared/sync/sync-server.ts@@ -0,0 +1,90 @@+import { sync } from "immerhin";+import type { Build, Project } from "@webstudio-is/project";+import type { Tree } from "@webstudio-is/project-build";+import { restPatchPath } from "~/shared/router-utils";+import { useEffect } from "react";+import { enqueue, dequeue, queueStatus } from "./queue";+import { useBeforeUnload } from "react-use";++// Periodic check for new entries to group them into one job/call in sync queue.+const NEW_ENTRIES_INTERVAL = 1000;++// First attempts we will simply retry without changing the state or notifying anyone.+const INTERVAL_RECOVERY = 2000;++// When we reached max failed attempts we will slow down the attempts interval.+const INTERVAL_ERROR = 5000;++const useRecoveryCheck = () => {+ useEffect(() => {+ const intervalId = setInterval(() => {+ if (queueStatus.get() === "recovering") {+ dequeue();+ }+ }, INTERVAL_RECOVERY);++ return () => clearInterval(intervalId);+ }, []);+};++const useErrorCheck = () => {+ useEffect(() => {+ const intervalId = setInterval(() => {+ if (queueStatus.get() === "failed") {+ dequeue();+ }+ }, INTERVAL_ERROR);++ return () => clearInterval(intervalId);+ }, []);+};++const useNewEntriesCheck = ({+ treeId,+ buildId,+ projectId,+}: UserSyncServerProps) => {+ useEffect(() => {+ // @todo setInterval can be completely avoided.+ // Right now prisma can't do atomic updates yet with sandbox documents+ // and backend fetches and updates big objects, so if we send quickly,+ // we end up overwriting things+ const intervalId = setInterval(() => {+ const transactions = sync();++ if (transactions.length === 0) {+ return;+ }++ enqueue(() =>+ fetch(restPatchPath(), {+ method: "post",+ body: JSON.stringify({+ transactions,+ treeId,+ buildId,+ projectId,+ }),+ })+ );+ }, NEW_ENTRIES_INTERVAL);++ return () => clearInterval(intervalId);+ }, [treeId, buildId, projectId]);+};++type UserSyncServerProps = {+ buildId: Build["id"];+ treeId: Tree["id"];+ projectId: Project["id"];+};++export const useSyncServer = (props: UserSyncServerProps) => {+ useNewEntriesCheck(props);+ useRecoveryCheck();+ useErrorCheck();+ useBeforeUnload(+ () => queueStatus.get() !== "idle",+ "You have unsaved changes. Are you sure you want to leave?"+ );+};