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
use immerhin
Oleg Isonen41a9eb65fcc8
diff --git a/app/canvas/canvas.tsx b/app/canvas/canvas.tsxindex f58828b181..3cd1dfba51 100644--- a/app/canvas/canvas.tsx+++ b/app/canvas/canvas.tsx@@ -1,6 +1,7 @@import { useMemo, useState } from "react";import { DndProvider } from "react-dnd";import { TouchBackend } from "react-dnd-touch-backend";+import { createTransaction } from "immerhin";import {type OnChangeChildren,type Data,@@ -31,7 +32,6 @@ import { useActiveElementTracking } from "./shared/active-element";import { WrapperComponentDev } from "./features/wrapper-component";import { rootInstanceContainer, useRootInstance } from "./shared/nano-values";import { usePeriodicSync } from "./shared/use-periodic-sync";-import { createTransaction } from "~/lib/sync-engine";import { useManageProps } from "./shared/props";const useElementsTree = () => {diff --git a/app/canvas/shared/nano-values.ts b/app/canvas/shared/nano-values.tsindex 8d759e3963..f5725ccfc1 100644--- a/app/canvas/shared/nano-values.ts+++ b/app/canvas/shared/nano-values.ts@@ -1,7 +1,7 @@import { createValueContainer, useValue } from "react-nano-state";import { type Instance } from "@webstudio-is/sdk";import { type DropData } from "~/shared/component";-import { register } from "~/lib/sync-engine";+import { register } from "immerhin";const dropDataContainer = createValueContainer<DropData | undefined>();export const useDropData = () => useValue(dropDataContainer);diff --git a/app/canvas/shared/props.ts b/app/canvas/shared/props.tsindex 58cbd65460..ce39747b60 100644--- a/app/canvas/shared/props.ts+++ b/app/canvas/shared/props.ts@@ -4,7 +4,7 @@ import {allUserPropsContainer,DeleteProp,} from "@webstudio-is/sdk";-import { createTransaction, register } from "~/lib/sync-engine";+import { createTransaction, register } from "immerhin";import {updateAllUserPropsMutable,deletePropMutable,diff --git a/app/canvas/shared/style.ts b/app/canvas/shared/style.tsindex 5d0a4cd7bd..5964a4456b 100644--- a/app/canvas/shared/style.ts+++ b/app/canvas/shared/style.ts@@ -1,4 +1,4 @@-import { createTransaction } from "~/lib/sync-engine";+import { createTransaction } from "immerhin";import { type StyleUpdates } from "~/shared/component";import { setInstanceStyleMutable } from "~/shared/tree-utils";import { rootInstanceContainer, useSelectedInstance } from "./nano-values";diff --git a/app/canvas/shared/tree.ts b/app/canvas/shared/tree.tsindex d2e83e04b5..4f99c015af 100644--- a/app/canvas/shared/tree.ts+++ b/app/canvas/shared/tree.ts@@ -7,7 +7,7 @@ import {findClosestSiblingInstance,insertInstanceMutable,} from "~/shared/tree-utils";-import { createTransaction } from "~/lib/sync-engine";+import { createTransaction } from "immerhin";import { DropData } from "~/shared/component";import {rootInstanceContainer,diff --git a/app/canvas/shared/use-periodic-sync.ts b/app/canvas/shared/use-periodic-sync.tsindex 2700e8e576..5fca0e8032 100644--- a/app/canvas/shared/use-periodic-sync.ts+++ b/app/canvas/shared/use-periodic-sync.ts@@ -1,5 +1,5 @@import useInterval from "react-use/lib/useInterval";-import { sync, SyncItem } from "~/lib/sync-engine";+import { sync, SyncItem } from "immerhin";import { publish } from "./pubsub";export const usePeriodicSync = () => {diff --git a/app/canvas/shared/use-shortcuts.ts b/app/canvas/shared/use-shortcuts.tsindex 28105915de..a8afa65c25 100644--- a/app/canvas/shared/use-shortcuts.ts+++ b/app/canvas/shared/use-shortcuts.ts@@ -2,7 +2,7 @@ import { useHotkeys } from "react-hotkeys-hook";import { type Instance } from "@webstudio-is/sdk";import { useRootInstance, useSelectedInstance } from "./nano-values";import { publish, useSubscribe } from "./pubsub";-import { redo, undo } from "~/lib/sync-engine";+import { redo, undo } from "immerhin";import { shortcuts } from "~/shared/shortcuts";const shortcutHandlerMap = {diff --git a/app/designer/features/sync/use-sync.ts b/app/designer/features/sync/use-sync.tsindex cac3c8a049..37a717c703 100644--- a/app/designer/features/sync/use-sync.ts+++ b/app/designer/features/sync/use-sync.ts@@ -2,7 +2,7 @@ import { type Project } from "@webstudio-is/sdk";import type { Config } from "~/config";import { useSubscribe } from "~/designer/features/canvas-iframe";import { enqueue } from "./queue";-import { type SyncItem } from "~/lib/sync-engine";+import { type SyncItem } from "immerhin";// @todo this entire queueing logic needs to be gone, it's a workaround,// because prisma can't do atomic updates yet with embeded documentsdiff --git a/app/lib/sync-engine/index.ts b/app/lib/sync-engine/index.tsdeleted file mode 100644index 02eaba738e..0000000000--- a/app/lib/sync-engine/index.ts+++ /dev/null@@ -1,3 +0,0 @@-export { register, createTransaction } from "./store";-export { sync, type SyncItem } from "./sync-queue";-export { undo, redo } from "./transactions-manager";diff --git a/app/lib/sync-engine/store.ts b/app/lib/sync-engine/store.tsdeleted file mode 100644index eca44d04ec..0000000000--- a/app/lib/sync-engine/store.ts+++ /dev/null@@ -1,64 +0,0 @@-import { createDraft, finishDraft, enablePatches, type Patch } from "immer";-import { ValueContainer } from "react-nano-state";-import { Transaction } from "./transaction";-import { add } from "./transactions-manager";--enablePatches();--// eslint-disable-next-line @typescript-eslint/no-explicit-any-type Any = any;--const registry: Map<ValueContainer<Any>, string> = new Map();--export const register = <Value>(- namespace: string,- container: ValueContainer<Value>-) => {- registry.set(container, namespace);-};--type UnwrapContainers<Containers extends Array<ValueContainer<unknown>>> = {- [Index in keyof Containers]: Containers[Index] extends ValueContainer<- infer Value- >- ? Value- : never;-};--export const createTransaction = <- Containers extends Array<ValueContainer<Any>>->(- containers: [...Containers],- recipe: (...values: UnwrapContainers<Containers>) => void-): UnwrapContainers<Containers> => {- type Values = UnwrapContainers<Containers>;- const drafts = [] as unknown as Values;- for (const container of containers) {- drafts.push(createDraft(container.value));- }- recipe(...drafts);- const transaction = new Transaction();- const values = [] as unknown as Values;- drafts.forEach((draft, index) => {- const namespace = registry.get(containers[index]);- if (namespace === undefined) {- throw new Error(- "Container used for transaction is not registered in sync engine"- );- }- const value = finishDraft(- draft,- (patches: Array<Patch>, revisePatches: Array<Patch>) => {- transaction.add({- namespace,- patches,- revisePatches,- container: containers[index],- });- }- );- values.push(value);- });- add(transaction);- return values;-};diff --git a/app/lib/sync-engine/sync-queue.ts b/app/lib/sync-engine/sync-queue.tsdeleted file mode 100644index ef58619fbf..0000000000--- a/app/lib/sync-engine/sync-queue.ts+++ /dev/null@@ -1,35 +0,0 @@-import { type Change } from "./transaction";--export type SyncItem = {- transactionId: string;- changes: Array<Change>;-};--const queue: Array<SyncItem> = [];--const dequeue = (transactionId: string) => {- const index = queue.findIndex(- (entry) => entry.transactionId === transactionId- );- if (index === -1) return false;- queue.splice(index, 1);- return true;-};--export const enqueue = (transactionId: string, changes: Array<Change>) => {- // We are trying to delete that transaction from the queue,- // if it was not found - we are adding the patches, because they are new- // if it was found - we don't add it because it is technically an undo operation.- // This can happen if user runs undo before sync happened, so we we are avoiding- // sending it to the server unnecessarily.- if (dequeue(transactionId) === false) {- queue.push({ transactionId, changes });- }-};--export const sync = () => {- if (queue.length === 0) return [];- const queueCopy = [...queue];- queue.splice(0);- return queueCopy;-};diff --git a/app/lib/sync-engine/transaction.ts b/app/lib/sync-engine/transaction.tsdeleted file mode 100644index c4b3dcbf29..0000000000--- a/app/lib/sync-engine/transaction.ts+++ /dev/null@@ -1,50 +0,0 @@-import ObjectId from "bson-objectid";-import { applyPatches, type Patch } from "immer";-import { ValueContainer } from "react-nano-state";--export type Change = {- namespace: string;- patches: Array<Patch>;-};--type TransactionSpec = {- namespace: string;- patches: Array<Patch>;- revisePatches: Array<Patch>;- container: ValueContainer<Record<string, unknown>>;-};--export class Transaction {- id: string;- specs: Array<TransactionSpec> = [];- constructor() {- this.id = ObjectId().toString();- }- applyPatches() {- for (const change of this.specs) {- const value = applyPatches(change.container.value, change.patches);- change.container.dispatch(value);- }- }- applyRevisePatches() {- for (const change of this.specs) {- const value = applyPatches(change.container.value, change.revisePatches);- change.container.dispatch(value);- }- }- add(change: TransactionSpec) {- this.specs.push(change);- }- getChanges(): Array<Change> {- return this.specs.map(({ namespace, patches }) => ({- namespace,- patches,- }));- }- getReviseChanges(): Array<Change> {- return this.specs.map(({ namespace, revisePatches }) => ({- namespace,- patches: revisePatches,- }));- }-}diff --git a/app/lib/sync-engine/transactions-manager.ts b/app/lib/sync-engine/transactions-manager.tsdeleted file mode 100644index 73639253f5..0000000000--- a/app/lib/sync-engine/transactions-manager.ts+++ /dev/null@@ -1,35 +0,0 @@-import { enqueue } from "./sync-queue";-import { type Transaction } from "./transaction";--const max = 100;--const currentStack: Array<Transaction> = [];-const undoneStack: Array<Transaction> = [];--export const add = (transaction: Transaction) => {- transaction.applyPatches();- currentStack.push(transaction);- enqueue(transaction.id, transaction.getChanges());- if (currentStack.length > max) {- currentStack.shift();- }- // After we add a change, we can't redo something we have undone before.- // It would make undo unpredictable, because there are new changes.- undoneStack.splice(0);-};--export const undo = () => {- const transaction = currentStack.pop();- if (transaction === undefined) return;- transaction.applyRevisePatches();- enqueue(transaction.id, transaction.getReviseChanges());- undoneStack.push(transaction);-};--export const redo = () => {- const transaction = undoneStack.pop();- if (transaction === undefined) return;- transaction.applyPatches();- currentStack.push(transaction);- enqueue(transaction.id, transaction.getChanges());-};diff --git a/app/routes/rest/patch.ts b/app/routes/rest/patch.tsindex cb668389ba..dba1f8f022 100644--- a/app/routes/rest/patch.ts+++ b/app/routes/rest/patch.ts@@ -1,6 +1,6 @@import { type ActionFunction } from "remix";import * as db from "~/shared/db";-import { type SyncItem } from "~/lib/sync-engine";+import { type SyncItem } from "immerhin";import { type Project, type Tree } from "@webstudio-is/sdk";const updaters = {diff --git a/package.json b/package.jsonindex 52552c8b42..b31ca5c003 100644--- a/package.json+++ b/package.json@@ -48,6 +48,7 @@"camelcase": "^6.3.0","hyphenate-style-name": "^1.0.4","immer": "^9.0.12",+ "immerhin": "^0.1.0","lodash.capitalize": "^4.2.1","lodash.debounce": "^4.0.8","lodash.memoize": "^4.1.2",diff --git a/yarn.lock b/yarn.lockindex 623e804fb0..c74fc673d3 100644--- a/yarn.lock+++ b/yarn.lock@@ -2295,6 +2295,11 @@ bson-objectid@^2.0.2:resolved "https://registry.yarnpkg.com/bson-objectid/-/bson-objectid-2.0.2.tgz#e72c9c51841f5e842dcc17d3cc7b7064f7abacae"integrity sha512-61Yo6MBAftkyc+nU0smq+VX2SCeKgtC6+cRI+JkatdCy1tPdAzvR1ezEQFlio6St2tDhKZm/IUXc2bzg0QSnqQ==+bson-objectid@^2.0.3:+ version "2.0.3"+ resolved "https://registry.yarnpkg.com/bson-objectid/-/bson-objectid-2.0.3.tgz#d840185172846b2f10c42ce2bcdb4a50956a9db5"+ integrity sha512-WYwVtY9yqk179EPMNuF3vcxufdrGLEo2XwqdRVbfLVe9X6jLt7WKZQgP+ObOcprakBGbHxzl76tgTaieqsH29g==+buffer-from@^1.0.0:version "1.1.2"resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.2.tgz#2b146a6fd72e80b4f55d255f35ed59a3a9a41bd5"@@ -4259,6 +4264,14 @@ immer@^9.0.12:resolved "https://registry.yarnpkg.com/immer/-/immer-9.0.12.tgz#2d33ddf3ee1d247deab9d707ca472c8c942a0f20"integrity sha512-lk7UNmSbAukB5B6dh9fnh5D0bJTOFKxVg2cyJWTYrWRfhLrLMBquONcUs3aFq507hNoIZEDDh8lb8UtOizSMhA==+immerhin@^0.1.0:+ version "0.1.0"+ resolved "https://registry.yarnpkg.com/immerhin/-/immerhin-0.1.0.tgz#ea8fdd1318e70f6da9ca16b7f252f017ce01675e"+ integrity sha512-hqa6nMuGarF3pUv/3y1mLhCU8XwRU6o70PAHw8X/AbcjnmIdm0uDSkXVVbAO/MEX0PjdPS9ULgA/DG5UnpfvNA==+ dependencies:+ bson-objectid "^2.0.3"+ immer "^9.0.12"+import-fresh@^3.0.0, import-fresh@^3.2.1:version "3.3.0"resolved "https://registry.yarnpkg.com/import-fresh/-/import-fresh-3.3.0.tgz#37162c25fcb9ebaa2e6e53d5b4d88ce17d9e0c2b"