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.

refactor: add sync object pool (#4453)

Bogdan Chadkin35ebd60b4696
diff --git a/apps/builder/app/canvas/canvas.tsx b/apps/builder/app/canvas/canvas.tsx
index 185774e8af..c5d044fa1f 100644
--- a/apps/builder/app/canvas/canvas.tsx
+++ b/apps/builder/app/canvas/canvas.tsx
@@ -54,7 +54,7 @@ import {
initCopyPaste,
initCopyPasteForContentEditMode,
} from "~/shared/copy-paste/init-copy-paste";
-import { setDataCollapsed, subscribeCollapsedToPubSub } from "./collapsed";
+import { setDataCollapsed, subscribeCollapsed } from "./collapsed";
import { useWindowResizeDebounced } from "~/shared/dom-hooks";
import { subscribeInstanceSelection } from "./instance-selection";
import { subscribeInstanceHovering } from "./instance-hovering";
@@ -294,7 +294,7 @@ export const Canvas = ({ params, imageLoader }: CanvasProps) => {
}
});
- useEffect(subscribeCollapsedToPubSub, []);
+ useEffect(subscribeCollapsed, []);
useHashLinkSync();
diff --git a/apps/builder/app/canvas/collapsed.ts b/apps/builder/app/canvas/collapsed.ts
index 105c3d1fd8..a1c452e422 100644
--- a/apps/builder/app/canvas/collapsed.ts
+++ b/apps/builder/app/canvas/collapsed.ts
@@ -1,5 +1,6 @@
import htmlTags, { voidHtmlTags, type HtmlTags } from "html-tags";
import { collapsedAttribute, idAttribute } from "@webstudio-is/react-sdk";
+import { compareMedia, StyleValue } from "@webstudio-is/css-engine";
import {
$breakpoints,
$instances,
@@ -7,9 +8,8 @@ import {
$stylesIndex,
} from "~/shared/nano-states";
import { $selectedBreakpoint } from "~/shared/nano-states";
-import { subscribe } from "~/shared/pubsub";
import { $selectedPage } from "~/shared/awareness";
-import { compareMedia, StyleValue } from "@webstudio-is/css-engine";
+import { serverSyncStore } from "~/shared/sync";
const isHtmlTag = (tag: string): tag is HtmlTags =>
htmlTags.includes(tag as HtmlTags);
@@ -297,8 +297,8 @@ export const setDataCollapsed = (instanceId: string, syncExec = false) => {
* For optimisation reasons try to extract instanceId of changed elements from pubsub
* In that case we just check the subtree of parent/common ancestor of changed elements to find collapsed elements
**/
-export const subscribeCollapsedToPubSub = () =>
- subscribe("sendStoreChanges", ({ changes }) => {
+export const subscribeCollapsed = () => {
+ return serverSyncStore.subscribe((_transactionId, changes) => {
for (const change of changes) {
if (change.namespace === "styleSourceSelections") {
for (const patch of change.patches) {
@@ -311,3 +311,4 @@ export const subscribeCollapsedToPubSub = () =>
}
}
});
+};
diff --git a/apps/builder/app/shared/sync-client.test.ts b/apps/builder/app/shared/sync-client.test.ts
index b8be9ce254..81b606b0ea 100644
--- a/apps/builder/app/shared/sync-client.test.ts
+++ b/apps/builder/app/shared/sync-client.test.ts
@@ -3,7 +3,7 @@ import { createNanoEvents } from "nanoevents";
import { atom } from "nanostores";
import { Store } from "immerhin";
import { enableMapSet } from "immer";
-import { SyncClient } from "./sync-client";
+import { ImmerhinSyncObject, SyncClient, SyncObjectPool } from "./sync-client";
enableMapSet();
@@ -33,12 +33,12 @@ test("synchronize initial state when follower is connected", () => {
const followerStore = createFollowerStore();
const leader = new SyncClient({
role: "leader",
- store: leaderStore,
+ object: new ImmerhinSyncObject("my-data", leaderStore),
emitter,
});
const follower = new SyncClient({
role: "follower",
- store: followerStore,
+ object: new ImmerhinSyncObject("my-data", followerStore),
emitter,
});
expect(leaderStore.containers.get("users")?.get().size).toEqual(2);
@@ -62,12 +62,12 @@ test("synchronize initial state when leader is connected", () => {
const followerStore = createFollowerStore();
const leader = new SyncClient({
role: "leader",
- store: leaderStore,
+ object: new ImmerhinSyncObject("my-data", leaderStore),
emitter,
});
const follower = new SyncClient({
role: "follower",
- store: followerStore,
+ object: new ImmerhinSyncObject("my-data", followerStore),
emitter,
});
expect(leaderStore.containers.get("users")?.get().size).toEqual(2);
@@ -91,12 +91,12 @@ test("synchronize initial state when one of followers become leader", () => {
const followerStore = createFollowerStore();
const leader = new SyncClient({
role: "follower",
- store: leaderStore,
+ object: new ImmerhinSyncObject("my-data", leaderStore),
emitter,
});
const follower = new SyncClient({
role: "follower",
- store: followerStore,
+ object: new ImmerhinSyncObject("my-data", followerStore),
emitter,
});
leader.connect({ signal: new AbortController().signal });
@@ -119,12 +119,12 @@ test("exchange transactions between leader and follower", async () => {
const followerStore = createFollowerStore();
const leader = new SyncClient({
role: "leader",
- store: leaderStore,
+ object: new ImmerhinSyncObject("my-data", leaderStore),
emitter,
});
const follower = new SyncClient({
role: "follower",
- store: followerStore,
+ object: new ImmerhinSyncObject("my-data", followerStore),
emitter,
});
leader.connect({ signal: new AbortController().signal });
@@ -156,12 +156,12 @@ test("exchange transactions between followers", async () => {
const follower2Store = createFollowerStore();
const follower1 = new SyncClient({
role: "follower",
- store: follower1Store,
+ object: new ImmerhinSyncObject("my-data", follower1Store),
emitter,
});
const follower2 = new SyncClient({
role: "follower",
- store: follower2Store,
+ object: new ImmerhinSyncObject("my-data", follower2Store),
emitter,
});
follower1.connect({ signal: new AbortController().signal });
@@ -186,3 +186,28 @@ test("exchange transactions between followers", async () => {
follower2Store.containers.get("users")?.get()
);
});
+
+test("support pool of objects", () => {
+ const store1 = createFollowerStore();
+ const store2 = createFollowerStore();
+ const leader = new SyncClient({
+ role: "leader",
+ object: new SyncObjectPool([
+ new ImmerhinSyncObject("store1", store1),
+ new ImmerhinSyncObject("store2", store2),
+ ]),
+ });
+ leader.connect({ signal: new AbortController().signal });
+ store1.createTransaction([store1.containers.get("users")!], (users) => {
+ users.set("james", "James Jameson");
+ });
+ store2.createTransaction([store2.containers.get("users")!], (users) => {
+ users.set("mark", "Mark Grayson");
+ });
+ expect(store1.containers.get("users")?.get()).toEqual(
+ new Map([["james", "James Jameson"]])
+ );
+ expect(store2.containers.get("users")?.get()).toEqual(
+ new Map([["mark", "Mark Grayson"]])
+ );
+});
diff --git a/apps/builder/app/shared/sync-client.ts b/apps/builder/app/shared/sync-client.ts
index a468d1a5c7..342e410e25 100644
--- a/apps/builder/app/shared/sync-client.ts
+++ b/apps/builder/app/shared/sync-client.ts
@@ -4,14 +4,91 @@ import type { Change, Store } from "immerhin";
type Transaction = {
id: string;
+ object: string;
changes: Change[];
};
+type RevertedTransaction = {
+ id: string;
+ object: string;
+};
+
+export class ImmerhinSyncObject implements SyncObject {
+ name: string;
+ store: Store;
+ constructor(name: string, store: Store) {
+ this.name = name;
+ this.store = store;
+ }
+ getState() {
+ const state = new Map<string, unknown>();
+ for (const [namespace, $store] of this.store.containers) {
+ state.set(namespace, $store.get());
+ }
+ return state;
+ }
+ setState(state: Map<string, unknown>) {
+ for (const [namespace, $store] of this.store.containers) {
+ // immer is not able to work with Map instances from another realm
+ // use clone to recreate data with current realm classes
+ $store.set(structuredClone(state.get(namespace)));
+ }
+ }
+ addTransaction(transaction: Transaction) {
+ this.store.addTransaction(transaction.id, transaction.changes, "remote");
+ }
+ revertTransaction(transaction: RevertedTransaction) {
+ this.store.revertTransaction(transaction.id);
+ }
+ subscribe(callback: (transaction: Transaction) => void, signal: AbortSignal) {
+ const unsubscribe = this.store.subscribe((id, changes, source) => {
+ if (source === "remote") {
+ return;
+ }
+ callback({ id, object: this.name, changes });
+ });
+ signal.addEventListener("abort", unsubscribe);
+ }
+}
+
+export class SyncObjectPool implements SyncObject {
+ name = "SyncObjectPool";
+ objects = new Map<string, SyncObject>();
+ constructor(objects: SyncObject[]) {
+ for (const object of objects) {
+ this.objects.set(object.name, object);
+ }
+ }
+ getState() {
+ const state = new Map<string, unknown>();
+ for (const [name, object] of this.objects) {
+ state.set(name, object.getState());
+ }
+ return state;
+ }
+ setState(state: Map<string, unknown>) {
+ for (const [name, object] of this.objects) {
+ object.setState(state.get(name) as never);
+ }
+ }
+ addTransaction(transaction: Transaction) {
+ this.objects.get(transaction.object)?.addTransaction(transaction);
+ }
+ revertTransaction(transaction: RevertedTransaction) {
+ this.objects.get(transaction.object)?.revertTransaction(transaction);
+ }
+ subscribe(callback: (transaction: Transaction) => void, signal: AbortSignal) {
+ for (const object of this.objects.values()) {
+ object.subscribe(callback, signal);
+ }
+ }
+}
+
type SyncMessage =
| { type: "connect"; clientId: string }
| { type: "state"; clientId: string; state: Map<string, unknown> }
| { type: "commit"; clientId: string; transaction: Transaction }
- | { type: "revert"; clientId: string; transactionId: string };
+ | { type: "revert"; clientId: string; transaction: RevertedTransaction };
export type SyncEmitter = Emitter<{
message: (message: SyncMessage) => void;
@@ -19,40 +96,44 @@ export type SyncEmitter = Emitter<{
type SyncClientOptions = {
role: "leader" | "follower";
- store: Store;
+ object: SyncObject;
emitter?: SyncEmitter;
};
+interface SyncObject {
+ name: string;
+ getState(): Map<string, unknown>;
+ setState(state: Map<string, unknown>): void;
+ addTransaction(transaction: Transaction): void;
+ revertTransaction(transaction: RevertedTransaction): void;
+ subscribe(
+ callback: (transaction: Transaction) => void,
+ signal: AbortSignal
+ ): void;
+}
+
export class SyncClient {
clientId = nanoid();
role: SyncClientOptions["role"];
- store: Store;
+ object: SyncObject;
emitter: SyncEmitter;
connection: "disconnected" | "connecting" | "connected" = "disconnected";
constructor(options: SyncClientOptions) {
this.role = options.role;
- this.store = options.store;
+ this.object = options.object;
this.emitter = options.emitter ?? createNanoEvents();
}
- broadcastState() {
- const state = new Map<string, unknown>();
- for (const [namespace, $store] of this.store.containers) {
- state.set(namespace, $store.get());
- }
+ lead() {
+ this.role = "leader";
this.emitter.emit("message", {
clientId: this.clientId,
type: "state",
- state,
+ state: this.object.getState(),
});
}
- lead() {
- this.role = "leader";
- this.broadcastState();
- }
-
connect({ signal }: { signal: AbortSignal }) {
const off = this.emitter.on("message", (message) => {
// ignore own messages
@@ -63,18 +144,18 @@ export class SyncClient {
if (this.role !== "leader") {
return;
}
- this.broadcastState();
+ this.emitter.emit("message", {
+ clientId: this.clientId,
+ type: "state",
+ state: this.object.getState(),
+ });
}
if (message.type === "state") {
if (this.connection === "connected") {
return;
}
this.connection = "connected";
- for (const [namespace, $store] of this.store.containers) {
- // immer is not able to work with Map instances from another realm
- // use clone to recreate data with current realm classes
- $store.set(structuredClone(message.state.get(namespace)));
- }
+ this.object.setState(message.state);
}
if (message.type === "commit") {
/*
@@ -85,33 +166,21 @@ export class SyncClient {
return;
}
*/
- this.store.addTransaction(
- message.transaction.id,
- message.transaction.changes,
- "remote"
- );
+ this.object.addTransaction(message.transaction);
}
if (message.type === "revert") {
- this.store.revertTransaction(message.transactionId);
+ this.object.revertTransaction(message.transaction);
}
});
+ signal.addEventListener("abort", off);
- const unsubscribe = this.store.subscribe((id, changes, source) => {
- if (source === "remote") {
- return;
- }
- const transaction: Transaction = { id, changes };
+ this.object.subscribe((transaction) => {
this.emitter.emit("message", {
clientId: this.clientId,
type: "commit",
transaction,
});
- });
-
- signal.addEventListener("abort", () => {
- off();
- unsubscribe();
- });
+ }, signal);
this.emitter.emit("message", {
clientId: this.clientId,
@@ -121,7 +190,11 @@ export class SyncClient {
this.connection = "connecting";
}
if (this.role === "leader") {
- this.broadcastState();
+ this.emitter.emit("message", {
+ clientId: this.clientId,
+ type: "state",
+ state: this.object.getState(),
+ });
}
}
}
diff --git a/apps/builder/app/shared/sync/sync-stores.ts b/apps/builder/app/shared/sync/sync-stores.ts
index 63ad0d4fd1..d286e5d1a8 100644
--- a/apps/builder/app/shared/sync/sync-stores.ts
+++ b/apps/builder/app/shared/sync/sync-stores.ts
@@ -1,5 +1,5 @@
import { nanoid } from "nanoid";
-import { Store, type Change } from "immerhin";
+import { Store } from "immerhin";
import { enableMapSet, setAutoFreeze } from "immer";
import type { WritableAtom } from "nanostores";
import { useEffect } from "react";
@@ -44,7 +44,12 @@ import {
} from "~/shared/nano-states";
import { $ephemeralStyles } from "~/canvas/stores";
import { $awareness } from "../awareness";
-import { SyncClient, type SyncEmitter } from "../sync-client";
+import {
+ ImmerhinSyncObject,
+ SyncClient,
+ SyncObjectPool,
+ type SyncEmitter,
+} from "../sync-client";
enableMapSet();
// safari structuredClone fix
@@ -68,12 +73,6 @@ declare module "~/shared/pubsub" {
source: SyncEventSource;
data: StoreData[];
};
- sendStoreChanges: {
- // distinct source to avoid infinite loop
- source: SyncEventSource;
- namespace: "client";
- changes: Change[];
- };
}
}
@@ -157,44 +156,6 @@ export const registerContainers = () => {
}
};
-const syncStoresChanges = (name: SyncEventSource, publish: Publish) => {
- const unsubscribeRemoteChanges = subscribe(
- "sendStoreChanges",
- ({ source, namespace, changes }) => {
- /// prevent reapplying own changes
- if (source === name) {
- return;
- }
- if (namespace === "client") {
- clientSyncStore.createTransactionFromChanges(changes, "remote");
- }
- }
- );
-
- const unsubscribeClientImmerhinStoreChanges = clientSyncStore.subscribe(
- (_transactionId, changes, source) => {
- // prevent sending remote patches back
- if (source === "remote") {
- return;
- }
-
- publish({
- type: "sendStoreChanges",
- payload: {
- source: name,
- namespace: "client",
- changes,
- },
- });
- }
- );
-
- return () => {
- unsubscribeRemoteChanges();
- unsubscribeClientImmerhinStoreChanges();
- };
-};
-
const syncStoresState = (name: SyncEventSource, publish: Publish) => {
const latestData = new Map<string, unknown>();
@@ -206,11 +167,6 @@ const syncStoresState = (name: SyncEventSource, publish: Publish) => {
return;
}
for (const { namespace, value } of data) {
- // apply immerhin stores data
- const clientContainer = clientSyncStore.containers.get(namespace);
- if (clientContainer) {
- clientContainer.set(value);
- }
// apply state stores data
const $state = clientStores.get(namespace);
if ($state) {
@@ -275,11 +231,16 @@ const sharedSyncEmitter =
? undefined
: window.__webstudioSharedSyncEmitter__;
+const objectPool = new SyncObjectPool([
+ new ImmerhinSyncObject("server", serverSyncStore),
+ new ImmerhinSyncObject("client", clientSyncStore),
+]);
+
export const useCanvasStore = (publish: Publish) => {
useEffect(() => {
const canvasClient = new SyncClient({
role: "follower",
- store: serverSyncStore,
+ object: objectPool,
emitter: sharedSyncEmitter,
});
@@ -313,7 +274,6 @@ export const useCanvasStore = (publish: Publish) => {
// subscribe stores after connect even so builder is ready to receive
// changes from immerhin queue
const unsubscribeStoresState = syncStoresState("canvas", publish);
- const unsubscribeStoresChanges = syncStoresChanges("canvas", publish);
return () => {
publish({
@@ -321,7 +281,6 @@ export const useCanvasStore = (publish: Publish) => {
payload: { sourceAppId: appId },
});
unsubscribeStoresState();
- unsubscribeStoresChanges();
controller.abort();
};
}, [publish]);
@@ -329,7 +288,7 @@ export const useCanvasStore = (publish: Publish) => {
export const builderClient = new SyncClient({
role: "leader",
- store: serverSyncStore,
+ object: objectPool,
});
export const useBuilderStore = (publish: Publish) => {
@@ -337,7 +296,6 @@ export const useBuilderStore = (publish: Publish) => {
const controller = new AbortController();
builderClient.connect({ signal: controller.signal });
let unsubscribeStoresState: undefined | (() => void);
- let unsubscribeStoresChanges: undefined | (() => void);
const unsubscribeConnect = subscribe("connect", () => {
// subscribe stores after connection so canvas is ready to receive
@@ -363,20 +321,16 @@ export const useBuilderStore = (publish: Publish) => {
data,
},
});
-
- unsubscribeStoresChanges = syncStoresChanges("builder", publish);
});
const unsubscribeDisconnect = subscribe("disconnect", () => {
unsubscribeStoresState?.();
- unsubscribeStoresChanges?.();
});
return () => {
unsubscribeConnect();
unsubscribeDisconnect();
unsubscribeStoresState?.();
- unsubscribeStoresChanges?.();
controller.abort();
};
}, [publish]);