Skip to content

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.

Add handshaken store (#1498)

Bogdan Chadkin197a03ede36e
diff --git a/apps/builder/app/builder/builder.tsx b/apps/builder/app/builder/builder.tsx
index aef5fb64a0..611b3cb592 100644
--- a/apps/builder/app/builder/builder.tsx
+++ b/apps/builder/app/builder/builder.tsx
@@ -1,5 +1,5 @@
import { useCallback, useEffect } from "react";
-import { type Publish, usePublish, useSubscribe } from "~/shared/pubsub";
+import { type Publish, usePublish } from "~/shared/pubsub";
import type { Build } from "@webstudio-is/project-build";
import type { Project } from "@webstudio-is/project";
import { theme, Box, type CSS, Flex, Grid } from "@webstudio-is/design-system";
@@ -74,12 +74,6 @@ const useNavigatorLayout = () => {
return isLoaded ? clientSettings.navigatorLayout : "undocked";
};
-const useSubscribeCanvasReady = (publish: Publish) => {
- useSubscribe("canvasReady", () => {
- publish({ type: "canvasReadyAck" });
- });
-};
-
const useSetWindowTitle = () => {
const [project] = useProject();
useEffect(() => {
@@ -290,7 +284,6 @@ export const Builder = ({
const [isPreviewMode] = useIsPreviewMode();
usePublishShortcuts(publish);
const { onRef: onRefReadCanvas, onTransitionEnd } = useReadCanvasRect();
- useSubscribeCanvasReady(publish);
// We need to initialize this in both canvas and builder,
// because the events will fire in either one, depending on where the focus is
useCopyPaste();
diff --git a/apps/builder/app/canvas/canvas.tsx b/apps/builder/app/canvas/canvas.tsx
index 89acff28ff..5b1b3de5e2 100644
--- a/apps/builder/app/canvas/canvas.tsx
+++ b/apps/builder/app/canvas/canvas.tsx
@@ -1,4 +1,4 @@
-import { useMemo, Fragment, useEffect } from "react";
+import { useMemo, useEffect } from "react";
import { useStore } from "@nanostores/react";
import { computed } from "nanostores";
import type { Params } from "@webstudio-is/react-sdk";
@@ -15,7 +15,11 @@ import {
type GetComponent,
} from "@webstudio-is/react-sdk";
import { publish } from "~/shared/pubsub";
-import { registerContainers, useCanvasStore } from "~/shared/sync";
+import {
+ handshakenStore,
+ registerContainers,
+ useCanvasStore,
+} from "~/shared/sync";
import { useSharedShortcuts } from "~/shared/shortcuts";
import { useCanvasShortcuts } from "./canvas-shortcuts";
import { useManageDesignModeStyles, GlobalStyles } from "./shared/styles";
@@ -29,7 +33,6 @@ import {
selectedPageStore,
} from "~/shared/nano-states";
import { useDragAndDrop } from "./shared/use-drag-drop";
-import { useSubscribeBuilderReady } from "./shared/use-builder-ready";
import { useCopyPaste } from "~/shared/copy-paste";
import { setDataCollapsed, subscribeCollapsedToPubSub } from "./collapsed";
import { useWindowResizeDebounced } from "~/shared/dom-hooks";
@@ -124,7 +127,7 @@ export const Canvas = ({
params,
getComponent,
}: CanvasProps): JSX.Element | null => {
- const isBuilderReady = useSubscribeBuilderReady();
+ const handshaken = useStore(handshakenStore);
setParams(params ?? null);
useCanvasStore(publish);
const [isPreviewMode] = useIsPreviewMode();
@@ -156,25 +159,11 @@ export const Canvas = ({
const elements = useElementsTree(getComponent);
- if (elements === undefined) {
- return null;
- }
-
- if (isPreviewMode || isBuilderReady === false) {
- return (
- <>
- <GlobalStyles />
- {/* Without fragment elements will be recreated in DesignMode */}
- <Fragment key="elements">{elements}</Fragment>
- </>
- );
- }
-
return (
<>
<GlobalStyles />
- <DesignMode />
- <Fragment key="elements">{elements}</Fragment>
+ {isPreviewMode === false && handshaken === true && <DesignMode />}
+ {elements}
</>
);
};
diff --git a/apps/builder/app/canvas/shared/use-builder-ready.ts b/apps/builder/app/canvas/shared/use-builder-ready.ts
deleted file mode 100644
index e252c3b3cc..0000000000
--- a/apps/builder/app/canvas/shared/use-builder-ready.ts
+++ /dev/null
@@ -1,30 +0,0 @@
-import { useState } from "react";
-import { useInterval } from "react-use";
-import { publish, useSubscribe } from "~/shared/pubsub";
-
-declare module "~/shared/pubsub" {
- export interface PubsubMap {
- canvasReady: undefined;
- canvasReadyAck: undefined;
- }
-}
-
-export const useSubscribeBuilderReady = () => {
- const [isAcknowledged, setIsAcknowledged] = useState(false);
-
- useInterval(
- () => {
- // We publish this event to make sure builder is listening to the events,
- // otherwise if canvas loads faster than builder, which is possible with SSR,
- // we can miss the events and builder will just not connect to the canvas.
- publish({ type: "canvasReady" });
- },
- isAcknowledged ? null : 100
- );
-
- useSubscribe("canvasReadyAck", () => {
- setIsAcknowledged(true);
- });
-
- return isAcknowledged;
-};
diff --git a/apps/builder/app/shared/sync/sync-stores.ts b/apps/builder/app/shared/sync/sync-stores.ts
index 4194c61e63..0d855f4a1a 100644
--- a/apps/builder/app/shared/sync/sync-stores.ts
+++ b/apps/builder/app/shared/sync/sync-stores.ts
@@ -1,6 +1,6 @@
import store, { type Change } from "immerhin";
import { enableMapSet } from "immer";
-import type { WritableAtom } from "nanostores";
+import { atom, type WritableAtom } from "nanostores";
import { useEffect } from "react";
import { type Publish, subscribe } from "~/shared/pubsub";
import {
@@ -195,16 +195,18 @@ const syncStoresState = (name: SyncEventSource, publish: Publish) => {
};
};
+export const handshakenStore = atom(false);
+
const handshakeAndSyncStores = (
source: SyncEventSource,
publish: Publish,
sync: (publish: Publish) => () => void
) => {
const actions: Parameters<typeof publish>[0][] = [];
- let destinationReady = false;
// Until builder is ready store action in local cache
const publishAction: typeof publish = (action) => {
+ const destinationReady = handshakenStore.get();
if (destinationReady) {
return publish(action);
}
@@ -227,6 +229,7 @@ const handshakeAndSyncStores = (
return;
}
+ const destinationReady = handshakenStore.get();
if (destinationReady) {
return;
}
@@ -241,7 +244,7 @@ const handshakeAndSyncStores = (
// cleanup
actions.length = 0;
- destinationReady = true;
+ handshakenStore.set(true);
destinationStoreReady();
});