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 more tests for nanostores sync object (#4457)

Bogdan Chadkinfd2bc2607db1
diff --git a/apps/builder/app/shared/sync-client.test.ts b/apps/builder/app/shared/sync-client.test.ts
index 56192b14cb..c719a9a27a 100644
--- a/apps/builder/app/shared/sync-client.test.ts
+++ b/apps/builder/app/shared/sync-client.test.ts
@@ -1,4 +1,4 @@
-import { describe, expect, test } from "vitest";
+import { describe, expect, test, vi } from "vitest";
import { createNanoEvents } from "nanoevents";
import { atom } from "nanostores";
import { Store } from "immerhin";
@@ -8,7 +8,6 @@ import {
NanostoresSyncObject,
SyncClient,
SyncObjectPool,
- type Transaction,
} from "./sync-client";
enableMapSet();
@@ -243,31 +242,51 @@ describe("nanostores sync object", () => {
expect($follower.get()).toEqual(5);
});
- test("prevent sending back state", () => {
+ test("set received state and access it", () => {
const $store = atom(1);
const object = new NanostoresSyncObject("nanostores", $store);
- const transactions: Transaction[] = [];
- object.subscribe((transaction) => {
- transactions.push(transaction);
- }, new AbortController().signal);
- expect(transactions).toEqual([]);
+ expect($store.get()).toEqual(1);
+ expect(object.getState()).toEqual(1);
object.setState(2);
- expect(transactions).toEqual([]);
+ expect($store.get()).toEqual(2);
+ expect(object.getState()).toEqual(2);
});
- test("prevent sending back transactions", () => {
+ test("send new store value as transaction", () => {
const $store = atom(1);
const object = new NanostoresSyncObject("nanostores", $store);
- const transactions: Transaction[] = [];
- object.subscribe((transaction) => {
- transactions.push(transaction);
- }, new AbortController().signal);
- expect(transactions).toEqual([]);
- object.addTransaction({
+ const sendTransaction = vi.fn();
+ object.subscribe(sendTransaction, new AbortController().signal);
+ $store.set(2);
+ expect(sendTransaction).toBeCalledTimes(1);
+ expect(sendTransaction).toBeCalledWith({
+ id: expect.any(String),
+ object: "nanostores",
+ payload: 2,
+ });
+ });
+
+ test("prevent sending back received state", () => {
+ const $store = atom(1);
+ const object = new NanostoresSyncObject("nanostores", $store);
+ const sendTransaction = vi.fn();
+ object.subscribe(sendTransaction, new AbortController().signal);
+ expect(sendTransaction).toBeCalledTimes(0);
+ object.setState(2);
+ expect(sendTransaction).toBeCalledTimes(0);
+ });
+
+ test("prevent sending back applied transactions", () => {
+ const $store = atom(1);
+ const object = new NanostoresSyncObject("nanostores", $store);
+ const sendTransaction = vi.fn();
+ object.subscribe(sendTransaction, new AbortController().signal);
+ expect(sendTransaction).toBeCalledTimes(0);
+ object.applyTransaction({
id: "my-transaction",
object: "nanostores",
payload: 2,
});
- expect(transactions).toEqual([]);
+ expect(sendTransaction).toBeCalledTimes(0);
});
});
diff --git a/apps/builder/app/shared/sync-client.ts b/apps/builder/app/shared/sync-client.ts
index ee7a784612..3634ba931d 100644
--- a/apps/builder/app/shared/sync-client.ts
+++ b/apps/builder/app/shared/sync-client.ts
@@ -3,13 +3,13 @@ import { createNanoEvents, type Emitter } from "nanoevents";
import type { Change, Store } from "immerhin";
import type { WritableAtom } from "nanostores";
-export type Transaction<Payload = unknown> = {
+type Transaction<Payload = unknown> = {
id: string;
object: string;
payload: Payload;
};
-export type RevertedTransaction = {
+type RevertedTransaction = {
id: string;
object: string;
};
@@ -18,10 +18,10 @@ interface SyncObject {
name: string;
getState(): unknown;
setState(state: unknown): void;
- addTransaction(transaction: Transaction): void;
+ applyTransaction(transaction: Transaction): void;
revertTransaction(transaction: RevertedTransaction): void;
subscribe(
- callback: (transaction: Transaction) => void,
+ sendTransaction: (transaction: Transaction) => void,
signal: AbortSignal
): void;
}
@@ -48,21 +48,21 @@ export class ImmerhinSyncObject implements SyncObject {
$store.set(structuredClone(state.get(namespace)));
}
}
- addTransaction(transaction: Transaction<Change[]>) {
+ applyTransaction(transaction: Transaction<Change[]>) {
this.store.addTransaction(transaction.id, transaction.payload, "remote");
}
revertTransaction(transaction: RevertedTransaction) {
this.store.revertTransaction(transaction.id);
}
subscribe(
- callback: (transaction: Transaction<Change[]>) => void,
+ sendTransaction: (transaction: Transaction<Change[]>) => void,
signal: AbortSignal
) {
const unsubscribe = this.store.subscribe((id, payload, source) => {
if (source === "remote") {
return;
}
- callback({ id, object: this.name, payload });
+ sendTransaction({ id, object: this.name, payload });
});
signal.addEventListener("abort", unsubscribe);
}
@@ -85,7 +85,7 @@ export class NanostoresSyncObject implements SyncObject {
this.store.set(state);
this.operation = "local";
}
- addTransaction(transaction: Transaction) {
+ applyTransaction(transaction: Transaction) {
this.operation = "add";
// `instanceof` checks do not work with instances like Map, File, etc., from another realm.
// Use `clone` to recreate the data with the current realm's classes.
@@ -96,13 +96,16 @@ export class NanostoresSyncObject implements SyncObject {
revertTransaction(_transaction: RevertedTransaction) {
// @todo store the list of transactions
}
- subscribe(callback: (transaction: Transaction) => void, signal: AbortSignal) {
+ subscribe(
+ sendTransaction: (transaction: Transaction) => void,
+ signal: AbortSignal
+ ) {
const unsubscribe = this.store.listen((payload) => {
if (this.operation !== "local") {
return;
}
const transaction = { id: nanoid(), object: this.name, payload };
- callback(transaction);
+ sendTransaction(transaction);
});
signal.addEventListener("abort", unsubscribe);
}
@@ -128,15 +131,18 @@ export class SyncObjectPool implements SyncObject {
object.setState(state.get(name) as never);
}
}
- addTransaction(transaction: Transaction) {
- this.objects.get(transaction.object)?.addTransaction(transaction);
+ applyTransaction(transaction: Transaction) {
+ this.objects.get(transaction.object)?.applyTransaction(transaction);
}
revertTransaction(transaction: RevertedTransaction) {
this.objects.get(transaction.object)?.revertTransaction(transaction);
}
- subscribe(callback: (transaction: Transaction) => void, signal: AbortSignal) {
+ subscribe(
+ sendTransaction: (transaction: Transaction) => void,
+ signal: AbortSignal
+ ) {
for (const object of this.objects.values()) {
- object.subscribe(callback, signal);
+ object.subscribe(sendTransaction, signal);
}
}
}
@@ -144,7 +150,7 @@ export class SyncObjectPool implements SyncObject {
type SyncMessage =
| { type: "connect"; clientId: string }
| { type: "state"; clientId: string; state: unknown }
- | { type: "commit"; clientId: string; transaction: Transaction }
+ | { type: "apply"; clientId: string; transaction: Transaction }
| { type: "revert"; clientId: string; transaction: RevertedTransaction };
export type SyncEmitter = Emitter<{
@@ -202,7 +208,7 @@ export class SyncClient {
this.connection = "connected";
this.object.setState(message.state);
}
- if (message.type === "commit") {
+ if (message.type === "apply") {
/*
// leader interacts with server
// and should validate transactions from various actors
@@ -211,7 +217,7 @@ export class SyncClient {
return;
}
*/
- this.object.addTransaction(message.transaction);
+ this.object.applyTransaction(message.transaction);
}
if (message.type === "revert") {
this.object.revertTransaction(message.transaction);
@@ -222,7 +228,7 @@ export class SyncClient {
this.object.subscribe((transaction) => {
this.emitter.emit("message", {
clientId: this.clientId,
- type: "commit",
+ type: "apply",
transaction,
});
}, signal);