// // Copyright 2023 DXOS.org // import { type BaseObject, ObjectId } from '@dxos/echo-schema'; import { invariant, assertArgument } from '@dxos/invariant'; import { type AnyLiveObject, initEchoReactiveObjectRootProxy, isEchoObject } from './echo-handler'; import { getObjectCore } from './echo-handler'; import { symbolInternals } from './echo-proxy-target'; import { ObjectCore } from '../core-db'; export type CloneOptions = { /** * @default true */ retainId?: boolean; /** * Additional list of objects to clone preserving references. */ additional?: (AnyLiveObject | undefined)[]; }; const requireAutomergeCore = (obj: AnyLiveObject) => { const core = getObjectCore(obj); invariant(core, 'object is not an EchoObject'); return core; }; /** * Returns new unbound clone of the object. * @deprecated */ export const clone = ( obj: AnyLiveObject, { retainId = true, additional = [] }: CloneOptions = {}, ): T => { assertArgument(isEchoObject(obj), 'expect obj to be an EchoObject'); assertArgument(retainId === true || additional.length === 0, 'retainId must be true when additional is not empty'); const clone = cloneInner(obj, retainId ? obj.id : ObjectId.random()); const clones: AnyLiveObject[] = [clone]; for (const innerObj of additional) { if (innerObj) { clones.push(cloneInner(innerObj, retainId ? innerObj.id : ObjectId.random())); } } // Update links. // Ensures references work before the object is bound. for (const clone of clones) { if (!isEchoObject(clone)) { continue; } for (const ref of clones) { if (ref === clone) { continue; } clone[symbolInternals as any].linkCache!.set(ref.id, ref); } } return clone; }; const cloneInner = (obj: AnyLiveObject, id: string): AnyLiveObject => { const core = requireAutomergeCore(obj); const coreClone = new ObjectCore(); coreClone.initNewObject(); coreClone.id = id; const proxy = initEchoReactiveObjectRootProxy(coreClone); const automergeSnapshot = getObjectDoc(core); coreClone.change((doc: any) => { for (const key of Object.keys(automergeSnapshot)) { doc[key] = automergeSnapshot[key]; } }); return proxy as any; }; const getObjectDoc = (core: ObjectCore): any => { let value = core.doc ?? core.docHandle!.doc(); for (const key of core.mountPath) { value = (value as any)?.[key]; } return value; };