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
Reduce renders from 5 to 1 (#1371)
Ivan Starkov6fbc97b2f53e
diff --git a/apps/builder/app/canvas/canvas.tsx b/apps/builder/app/canvas/canvas.tsxindex bacbf2cc64..ab274a3fa4 100644--- a/apps/builder/app/canvas/canvas.tsx+++ b/apps/builder/app/canvas/canvas.tsx@@ -54,14 +54,16 @@ const temporaryRootInstance: Instance = {const useElementsTree = (getComponent: GetComponent) => {const [rootInstance] = useRootInstance();- // @todo remove after https://github.com/webstudio-is/webstudio-builder/issues/1313 now its needed to be sure that no leaks exists- // eslint-disable-next-line no-console- console.log({- rootInstance,- assetsStore: assetsStore.get().size,- pagesStore: pagesStore.get()?.pages.length ?? 0,- instancesStore: instancesStore.get().size,- });+ if (typeof window === "undefined") {+ // @todo remove after https://github.com/webstudio-is/webstudio-builder/issues/1313 now its needed to be sure that no leaks exists+ // eslint-disable-next-line no-console+ console.log({+ rootInstance,+ assetsStore: assetsStore.get().size,+ pagesStore: pagesStore.get()?.pages.length ?? 0,+ instancesStore: instancesStore.get().size,+ });+ }const pagesMapStore = useMemo(() =>diff --git a/packages/react-sdk/src/pubsub/create.ts b/packages/react-sdk/src/pubsub/create.tsindex 2b6170e217..ebb51b8410 100644--- a/packages/react-sdk/src/pubsub/create.ts+++ b/packages/react-sdk/src/pubsub/create.ts@@ -1,5 +1,6 @@import { createNanoEvents } from "nanoevents";import { useCallback, useEffect, useRef } from "react";+import { batchUpdate } from "./raf-queue";export const createPubsub = <PublishMap>() => {type Action<Type extends keyof PublishMap> =@@ -18,7 +19,8 @@ export const createPubsub = <PublishMap>() => {// we could potentially maintain a list of valid event types at runtime// at the very least we could add a brand property or something to our eventsif (typeof event.data?.type === "string") {- emitter.emit(event.data.type, event.data.payload);+ // Execute all updates within a single batch to improve performance+ batchUpdate(() => emitter.emit(event.data.type, event.data.payload));}},falsediff --git a/packages/react-sdk/src/pubsub/raf-queue.ts b/packages/react-sdk/src/pubsub/raf-queue.tsnew file mode 100644index 0000000000..5e2312b0a8--- /dev/null+++ b/packages/react-sdk/src/pubsub/raf-queue.ts@@ -0,0 +1,31 @@+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();+ }+ });+};++export const batchUpdate = (update: () => void) => {+ updateQueue.push(update);++ if (handle !== undefined) {+ return;+ }++ handle = requestAnimationFrame(() => {+ const updates = updateQueue;+ updateQueue = [];+ handle = undefined;+ processUpdates(updates);+ });+};