Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
perf(web): keep loro-crdt off the boot path (#7779)
Wolf Mermelstein063c57388344
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/undo.ts b/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/undo.tsindex f0af3aae42..3c0eca13ab 100644--- a/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/undo.ts+++ b/apps/web/src/lib/core/component/LexicalMarkdown/collaboration/undo.ts@@ -4,6 +4,7 @@*/import { mergeRegister } from '@lexical/utils';+import type { LoroManager } from '@macro-inc/collaboration/collab/manager';import type { EditorState, LexicalEditor } from 'lexical';import {$isRangeSelection,@@ -17,7 +18,6 @@ import {REDO_COMMAND,UNDO_COMMAND,} from 'lexical';-import { type LoroDoc, UndoManager } from 'loro-crdt';type MergeAction = 0 | 1 | 2;const HISTORY_MERGE = 0;@@ -202,18 +202,18 @@ function isTextNodeUnchanged(* Uses lexical's history merge semantics, but with Loro's undo manager** @param editor - Lexical editor instance- * @param loroDoc - Loro's CRDT document+ * @param loroManager - Manager of Loro's CRDT document* @param delay - Delay between merges* @param maxUndoSteps - Maximum number of undo steps* @returns - Cleanup function*/export function registerLoroHistory(editor: LexicalEditor,- loroDoc: LoroDoc,+ loroManager: LoroManager,delay: number,maxUndoSteps: number = 100): () => void {- const undoManager = new UndoManager(loroDoc, {+ const undoManager = loroManager.createUndoManager({mergeInterval: delay,maxUndoSteps,excludeOriginPrefixes: ['history-'],diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/pluginManager.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/pluginManager.tsindex 9e0d70a525..1b607aea8b 100644--- a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/pluginManager.ts+++ b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/pluginManager.ts@@ -37,7 +37,7 @@ export function createPluginManager(editor: LexicalEditor, type: EditorType) {history(timeGap = 400, loroManager?: LoroManager) {if (type === 'markdown-sync' && loroManager) {cleanupFunctions.push(- registerLoroHistory(editor, loroManager.doc, timeGap)+ registerLoroHistory(editor, loroManager, timeGap));} else {cleanupFunctions.push(diff --git a/packages/collaboration/src/collab/manager-error.ts b/packages/collaboration/src/collab/manager-error.tsnew file mode 100644index 0000000000..d22f7f3415--- /dev/null+++ b/packages/collaboration/src/collab/manager-error.ts@@ -0,0 +1,13 @@+export enum LoroManagerError {+ ImportFailed = 'IMPORT_FAILED',+ /** The update arrived ahead of its causal dependencies. Loro holds it and+ * applies it automatically once the gap fills — not a failure. */+ ImportPending = 'IMPORT_PENDING',+ NotInitialized = 'NOT_INITIALIZED',+ InitializeFailed = 'INITIALIZE_FAILED',+ SyncFailed = 'SYNC_FAILED',+ ExportFailed = 'EXPORT_FAILED',+ GetCursorPosFailed = 'GET_CURSOR_POS_FAILED',+ GetContainerByIdFailed = 'GET_CONTAINER_BY_ID_FAILED',+ UnknownLoroError = 'UNKNOWN_LORO_ERROR',+}diff --git a/packages/collaboration/src/collab/manager.ts b/packages/collaboration/src/collab/manager.tsindex 259f1477c0..765b00f3b3 100644--- a/packages/collaboration/src/collab/manager.ts+++ b/packages/collaboration/src/collab/manager.ts@@ -12,12 +12,15 @@ import {LoroDoc,type PeerID,type Side,+ type UndoConfig,+ UndoManager,type VersionVector,} from 'loro-crdt';import { err, ok, type Result } from 'neverthrow';import { onCleanup } from 'solid-js';import type { ResultError } from '../internal/result';import { logSyncService } from './logger';+import { LoroManagerError } from './manager-error';import type { GenericRootSchema, LoroRawUpdate, RawUpdate } from './shared';import {disposeTelemetryFor,@@ -26,19 +29,7 @@ import {telemetrySpan,} from './telemetry';-export enum LoroManagerError {- ImportFailed = 'IMPORT_FAILED',- /** The update arrived ahead of its causal dependencies. Loro holds it and- * applies it automatically once the gap fills — not a failure. */- ImportPending = 'IMPORT_PENDING',- NotInitialized = 'NOT_INITIALIZED',- InitializeFailed = 'INITIALIZE_FAILED',- SyncFailed = 'SYNC_FAILED',- ExportFailed = 'EXPORT_FAILED',- GetCursorPosFailed = 'GET_CURSOR_POS_FAILED',- GetContainerByIdFailed = 'GET_CONTAINER_BY_ID_FAILED',- UnknownLoroError = 'UNKNOWN_LORO_ERROR',-}+export { LoroManagerError };export enum LoroStateTag {Initialize = 'INITIALIZE',@@ -161,6 +152,12 @@ export class LoroManager<S extends GenericRootSchema = GenericRootSchema>get doc(): LoroDoc {return this._doc;}++ /** An undo manager over this document, so callers need not import loro eagerly. */+ createUndoManager(config: UndoConfig): UndoManager {+ return new UndoManager(this._doc, config);+ }+/** The inner Mirror, once initialized. */get mirror(): Mirror<S> | undefined {return this._mirror;diff --git a/packages/collaboration/src/collab/snapshot-store.ts b/packages/collaboration/src/collab/snapshot-store.tsindex 36772ca48a..2881dba137 100644--- a/packages/collaboration/src/collab/snapshot-store.ts+++ b/packages/collaboration/src/collab/snapshot-store.ts@@ -1,6 +1,7 @@import { type DBSchema, type IDBPDatabase, openDB as idbOpen } from 'idb';import { logSyncService } from './logger';-import { type LoroManager, LoroManagerError } from './manager';+import type { LoroManager } from './manager';+import { LoroManagerError } from './manager-error';import type { GenericRootSchema, RawUpdate } from './shared';import type { WALStore } from './wal';diff --git a/packages/lexical-core/utils/buildWhoMap.ts b/packages/lexical-core/utils/buildWhoMap.tsindex e1857f413f..759d1b9a95 100644--- a/packages/lexical-core/utils/buildWhoMap.ts+++ b/packages/lexical-core/utils/buildWhoMap.ts@@ -1,18 +1,28 @@-import type { Change, ContainerID, LoroDoc } from 'loro-crdt';-import { LoroMap } from 'loro-crdt';+import type { Change, ContainerID, LoroDoc, LoroMap } from 'loro-crdt';++// Duck-typed so the editor bundle need not load loro (and its wasm) at boot.+function isLoroMap(c: unknown): c is LoroMap {+ return (+ typeof c === 'object' &&+ c !== null &&+ 'kind' in c &&+ typeof c.kind === 'function' &&+ c.kind() === 'Map'+ );+}// Read a node-map's `$.id`, if this container is a LoroMap with that structure.function dollarId(c: ReturnType<LoroDoc['getContainerById']>): string | null {- if (!(c instanceof LoroMap)) return null;+ if (!isLoroMap(c)) return null;const dollar = c.get('$');- if (!(dollar instanceof LoroMap)) return null;+ if (!isLoroMap(dollar)) return null;const id = dollar.get('id');return typeof id === 'string' ? id : null;}// Read a node-map's lexical `type` field (paragraph/table/tablecell/…).function nodeType(c: ReturnType<LoroDoc['getContainerById']>): string | null {- if (!(c instanceof LoroMap)) return null;+ if (!isLoroMap(c)) return null;const t = c.get('type');return typeof t === 'string' ? t : null;}