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
Concurency mode for React 18 (#1474)
Ivan Starkov5ad198f39ee0
diff --git a/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsx b/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsxindex 810689d483..422c59578f 100644--- a/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsx+++ b/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsx@@ -29,7 +29,6 @@ import {useRef,} from "react";import { useUnitSelect } from "./unit-select";-import { unstable_batchedUpdates as unstableBatchedUpdates } from "react-dom";import { parseIntermediateOrInvalidValue } from "./parse-intermediate-or-invalid-value";import { toValue } from "@webstudio-is/css-engine";import { useDebouncedCallback } from "use-debounce";@@ -115,12 +114,11 @@ const useScrub = ({onValueChange(event) {// Will work without but depends on order of setState updates// at text-control, now fixed in both places (order of updates is right, and batched here)- unstableBatchedUpdates(() => {- onChangeCompleteRef.current({- type,- unit,- value: event.value,- });++ onChangeCompleteRef.current({+ type,+ unit,+ value: event.value,});// Returning focus that we've moved abovediff --git a/apps/builder/app/entry.client.tsx b/apps/builder/app/entry.client.tsxindex 881f1f81ab..2147ee7ffe 100644--- a/apps/builder/app/entry.client.tsx+++ b/apps/builder/app/entry.client.tsx@@ -1,10 +1,8 @@-import { hydrate } from "react-dom";-import { RemixBrowser } from "@remix-run/react";-import { initSentry } from "./shared/sentry";--import { useLocation, useMatches } from "@remix-run/react";+import { RemixBrowser, useLocation, useMatches } from "@remix-run/react";+import { startTransition, StrictMode, useEffect } from "react";+import { hydrateRoot } from "react-dom/client";import { BrowserTracing, remixRouterInstrumentation } from "@sentry/remix";-import { useEffect } from "react";+import { initSentry } from "./shared/sentry";import env from "./shared/env";initSentry({@@ -35,4 +33,11 @@ try {console.warn("Failed to set localStorage.debug due to Error:", error);}-hydrate(<RemixBrowser />, document);+startTransition(() => {+ hydrateRoot(+ document,+ <StrictMode>+ <RemixBrowser />+ </StrictMode>+ );+});diff --git a/apps/builder/app/shared/instance-utils.ts b/apps/builder/app/shared/instance-utils.tsindex 3808950990..8c3b970803 100644--- a/apps/builder/app/shared/instance-utils.ts+++ b/apps/builder/app/shared/instance-utils.ts@@ -20,132 +20,123 @@ import {getAncestorInstanceSelector,} from "./tree-utils";import { removeByMutable } from "./array-utils";-import { unstable_batchedUpdates as batchedUpdates } from "react-dom";export const insertNewComponentInstance = (component: string,dropTarget: DroppableTarget) => {- batchedUpdates(() => {- const instance = createComponentInstance(component);- store.createTransaction([instancesStore], (instances) => {- insertInstancesMutable(instances, [instance], [instance.id], dropTarget);- });-- selectedInstanceSelectorStore.set([- instance.id,- ...dropTarget.parentSelector,- ]);- selectedStyleSourceSelectorStore.set(undefined);+ const instance = createComponentInstance(component);+ store.createTransaction([instancesStore], (instances) => {+ insertInstancesMutable(instances, [instance], [instance.id], dropTarget);});++ selectedInstanceSelectorStore.set([+ instance.id,+ ...dropTarget.parentSelector,+ ]);+ selectedStyleSourceSelectorStore.set(undefined);};export const reparentInstance = (targetInstanceSelector: InstanceSelector,dropTarget: DroppableTarget) => {- batchedUpdates(() => {- store.createTransaction([instancesStore], (instances) => {- reparentInstanceMutable(instances, targetInstanceSelector, dropTarget);- });- selectedInstanceSelectorStore.set(targetInstanceSelector);- selectedStyleSourceSelectorStore.set(undefined);+ store.createTransaction([instancesStore], (instances) => {+ reparentInstanceMutable(instances, targetInstanceSelector, dropTarget);});+ selectedInstanceSelectorStore.set(targetInstanceSelector);+ selectedStyleSourceSelectorStore.set(undefined);};export const deleteInstance = (instanceSelector: InstanceSelector) => {- batchedUpdates(() => {- store.createTransaction(- [- instancesStore,- propsStore,- styleSourceSelectionsStore,- styleSourcesStore,- stylesStore,- ],- (instances, props, styleSourceSelections, styleSources, styles) => {- let targetInstanceId = instanceSelector[0];- const parentInstanceId = instanceSelector[1];- const grandparentInstanceId = instanceSelector[2];- let parentInstance =- parentInstanceId === undefined- ? undefined- : instances.get(parentInstanceId);+ store.createTransaction(+ [+ instancesStore,+ propsStore,+ styleSourceSelectionsStore,+ styleSourcesStore,+ stylesStore,+ ],+ (instances, props, styleSourceSelections, styleSources, styles) => {+ let targetInstanceId = instanceSelector[0];+ const parentInstanceId = instanceSelector[1];+ const grandparentInstanceId = instanceSelector[2];+ let parentInstance =+ parentInstanceId === undefined+ ? undefined+ : instances.get(parentInstanceId);- // delete parent fragment too if its last child is going to be deleted- // use case for slots: slot became empty and remove display: contents- // to be displayed properly on canvas- if (- parentInstance?.component === "Fragment" &&- parentInstance.children.length === 1 &&- grandparentInstanceId !== undefined- ) {- targetInstanceId = parentInstance.id;- parentInstance = instances.get(grandparentInstanceId);- }+ // delete parent fragment too if its last child is going to be deleted+ // use case for slots: slot became empty and remove display: contents+ // to be displayed properly on canvas+ if (+ parentInstance?.component === "Fragment" &&+ parentInstance.children.length === 1 &&+ grandparentInstanceId !== undefined+ ) {+ targetInstanceId = parentInstance.id;+ parentInstance = instances.get(grandparentInstanceId);+ }- const subtreeIds = findTreeInstanceIdsExcludingSlotDescendants(- instances,- targetInstanceId- );- const subtreeLocalStyleSourceIds = findSubtreeLocalStyleSources(- subtreeIds,- styleSources,- styleSourceSelections- );+ const subtreeIds = findTreeInstanceIdsExcludingSlotDescendants(+ instances,+ targetInstanceId+ );+ const subtreeLocalStyleSourceIds = findSubtreeLocalStyleSources(+ subtreeIds,+ styleSources,+ styleSourceSelections+ );- // may not exist when delete root- if (parentInstance) {- removeByMutable(- parentInstance.children,- (child) => child.type === "id" && child.value === targetInstanceId- );- }+ // may not exist when delete root+ if (parentInstance) {+ removeByMutable(+ parentInstance.children,+ (child) => child.type === "id" && child.value === targetInstanceId+ );+ }- for (const instanceId of subtreeIds) {- instances.delete(instanceId);- }- // delete props and styles of deleted instance and its descendants- for (const prop of props.values()) {- if (subtreeIds.has(prop.instanceId)) {- props.delete(prop.id);- }- }- for (const instanceId of subtreeIds) {- styleSourceSelections.delete(instanceId);- }- for (const styleSourceId of subtreeLocalStyleSourceIds) {- styleSources.delete(styleSourceId);+ for (const instanceId of subtreeIds) {+ instances.delete(instanceId);+ }+ // delete props and styles of deleted instance and its descendants+ for (const prop of props.values()) {+ if (subtreeIds.has(prop.instanceId)) {+ props.delete(prop.id);}- for (const [styleDeclKey, styleDecl] of styles) {- if (subtreeLocalStyleSourceIds.has(styleDecl.styleSourceId)) {- styles.delete(styleDeclKey);- }+ }+ for (const instanceId of subtreeIds) {+ styleSourceSelections.delete(instanceId);+ }+ for (const styleSourceId of subtreeLocalStyleSourceIds) {+ styleSources.delete(styleSourceId);+ }+ for (const [styleDeclKey, styleDecl] of styles) {+ if (subtreeLocalStyleSourceIds.has(styleDecl.styleSourceId)) {+ styles.delete(styleDeclKey);}+ }- if (parentInstance) {- selectedInstanceSelectorStore.set(- getAncestorInstanceSelector(instanceSelector, parentInstance.id)- );- selectedStyleSourceSelectorStore.set(undefined);- }+ if (parentInstance) {+ selectedInstanceSelectorStore.set(+ getAncestorInstanceSelector(instanceSelector, parentInstance.id)+ );+ selectedStyleSourceSelectorStore.set(undefined);}- );- });+ }+ );};export const deleteSelectedInstance = () => {- batchedUpdates(() => {- const selectedInstanceSelector = selectedInstanceSelectorStore.get();- // @todo tell user they can't delete root- if (- selectedInstanceSelector === undefined ||- selectedInstanceSelector.length === 1- ) {- return;- }- deleteInstance(selectedInstanceSelector);- });+ const selectedInstanceSelector = selectedInstanceSelectorStore.get();+ // @todo tell user they can't delete root+ if (+ selectedInstanceSelector === undefined ||+ selectedInstanceSelector.length === 1+ ) {+ return;+ }+ deleteInstance(selectedInstanceSelector);};export const escapeSelection = () => {diff --git a/apps/builder/app/shared/sync/sync-stores.ts b/apps/builder/app/shared/sync/sync-stores.tsindex 6a8603ae3c..4194c61e63 100644--- a/apps/builder/app/shared/sync/sync-stores.ts+++ b/apps/builder/app/shared/sync/sync-stores.ts@@ -36,7 +36,8 @@ type SyncEventSource = "canvas" | "builder";declare module "~/shared/pubsub" {export interface PubsubMap {- canvasStoreReady: void;+ handshake: { source: SyncEventSource };+sendStoreData: {// distinct source to avoid infinite loopsource: SyncEventSource;@@ -112,6 +113,7 @@ const syncStoresChanges = (name: SyncEventSource, publish: Publish) => {if (source === "remote") {return;}+publish({type: "sendStoreChanges",payload: {@@ -168,6 +170,7 @@ const syncStoresState = (name: SyncEventSource, publish: Publish) => {return;}latestData.set(namespace, value);+publish({type: "sendStoreData",payload: {@@ -192,25 +195,82 @@ const syncStoresState = (name: SyncEventSource, publish: Publish) => {};};+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) => {+ if (destinationReady) {+ return publish(action);+ }+ actions.push(action);+ };++ const unsubscribe = sync(publishAction);++ const handshakeAction = {+ type: "handshake",+ payload: {+ source,+ },+ } as const;++ publish(handshakeAction);++ const destinationStoreReady = subscribe("handshake", (payload) => {+ if (source === payload.source) {+ return;+ }++ if (destinationReady) {+ return;+ }++ // We need to publish it here last time+ publish(handshakeAction);++ for (const action of actions) {+ publish(action);+ }++ // cleanup+ actions.length = 0;++ destinationReady = true;++ destinationStoreReady();+ });++ return () => {+ destinationStoreReady();+ unsubscribe();+ };+};+export const useCanvasStore = (publish: Publish) => {useEffect(() => {- const unsubscribeStoresState = syncStoresState("canvas", publish);- const unsubscribeStoresChanges = syncStoresChanges("canvas", publish);-- publish({- type: "canvasStoreReady",+ return handshakeAndSyncStores("canvas", publish, (publish) => {+ const unsubscribeStoresState = syncStoresState("canvas", publish);+ const unsubscribeStoresChanges = syncStoresChanges("canvas", publish);+ return () => {+ unsubscribeStoresState();+ unsubscribeStoresChanges();+ };});-- return () => {- unsubscribeStoresState();- unsubscribeStoresChanges();- };}, [publish]);};export const useBuilderStore = (publish: Publish) => {useEffect(() => {- const unsubscribeCanvasStoreReady = subscribe("canvasStoreReady", () => {+ return handshakeAndSyncStores("builder", publish, (publish) => {+ const unsubscribeStoresState = syncStoresState("builder", publish);+ const unsubscribeStoresChanges = syncStoresChanges("builder", publish);+// immerhin data is sent only initially so not part of syncStoresState// expect data to be populated by the time effect is calledconst data = [];@@ -233,15 +293,11 @@ export const useBuilderStore = (publish: Publish) => {data,},});- });-- const unsubscribeStoresState = syncStoresState("builder", publish);- const unsubscribeStoresChanges = syncStoresChanges("builder", publish);- return () => {- unsubscribeCanvasStoreReady();- unsubscribeStoresState();- unsubscribeStoresChanges();- };+ return () => {+ unsubscribeStoresState();+ unsubscribeStoresChanges();+ };+ });}, [publish]);};diff --git a/packages/react-sdk/src/pubsub/raf-queue.ts b/packages/react-sdk/src/pubsub/raf-queue.tsindex 5e2312b0a8..c64bf75995 100644--- a/packages/react-sdk/src/pubsub/raf-queue.ts+++ b/packages/react-sdk/src/pubsub/raf-queue.ts@@ -1,18 +1,12 @@-import { unstable_batchedUpdates as batchedUpdates } from "react-dom";-type Task = () => void;let handle: ReturnType<typeof requestAnimationFrame> | undefined;let updateQueue: Task[] = [];const processUpdates = (updates: Task[]) => {- // Prior to React v18, updates not called within React event handlers would not be batched- // To ensure all updates are batched into a single React update, we wrap them in a batchedUpdates callback- batchedUpdates(() => {- for (const update of updates) {- update();- }- });+ for (const update of updates) {+ update();+ }};export const batchUpdate = (update: () => void) => {