diff --git a/src/document/crdt/root.ts b/src/document/crdt/root.ts index 9b67a56bc..beecb02f3 100644 --- a/src/document/crdt/root.ts +++ b/src/document/crdt/root.ts @@ -171,26 +171,22 @@ export class CRDTRoot { public deregisterElement(element: CRDTElement): number { let count = 0; - const callback = (elem: CRDTElement) => { + const deregisterElementInternal = (elem: CRDTElement) => { const createdAt = elem.getCreatedAt().toIDString(); this.elementPairMapByCreatedAt.delete(createdAt); this.removedElementSetByCreatedAt.delete(createdAt); count++; - }; - const deregisterDescendants = (container: CRDTContainer) => { - container.getDescendants((elem) => { - callback(elem); - if (elem instanceof CRDTContainer) { - deregisterDescendants(elem); - } - return false; - }); + + if (elem instanceof CRDTContainer) { + elem.getDescendants((e) => { + deregisterElementInternal(e); + return false; + }); + } }; - callback(element); - if (element instanceof CRDTContainer) { - deregisterDescendants(element); - } + deregisterElementInternal(element); + return count; } diff --git a/src/document/json/array.ts b/src/document/json/array.ts index ca26f17f4..7c58d347e 100644 --- a/src/document/json/array.ts +++ b/src/document/json/array.ts @@ -326,19 +326,19 @@ export class ArrayProxy { } /** - * `buildArray` constructs an array of CRDTElements based on the user-provided array. + * `buildArrayElements` constructs array elements based on the user-provided array. */ - public static buildArray( + public static buildArrayElements( context: ChangeContext, value: Array, ): Array { - const elementArray: Array = []; + const elements: Array = []; for (const v of value) { const createdAt = context.issueTimeTicket(); const elem = buildCRDTElement(context, v, createdAt); - elementArray.push(elem); + elements.push(elem); } - return elementArray; + return elements; } /** diff --git a/src/document/json/element.ts b/src/document/json/element.ts index 3c28bb71a..65e679414 100644 --- a/src/document/json/element.ts +++ b/src/document/json/element.ts @@ -150,7 +150,7 @@ export function buildCRDTElement( } else if (Array.isArray(value)) { element = CRDTArray.create( createdAt, - ArrayProxy.buildArray(context, value), + ArrayProxy.buildArrayElements(context, value), ); } else if (typeof value === 'object') { if (value instanceof Text) { @@ -169,7 +169,7 @@ export function buildCRDTElement( } else { element = CRDTObject.create( createdAt, - ObjectProxy.buildObject(context, value!), + ObjectProxy.buildObjectMembers(context, value!), ); } } else { diff --git a/src/document/json/object.ts b/src/document/json/object.ts index 030d6b488..e594d13da 100644 --- a/src/document/json/object.ts +++ b/src/document/json/object.ts @@ -163,16 +163,16 @@ export class ObjectProxy { } /** - * `buildObject` constructs an object where all values from the + * `buildObjectMembers` constructs an object where all values from the * user-provided object are transformed into CRDTElements. * This function takes an object and iterates through its values, * converting each value into a corresponding CRDTElement. */ - public static buildObject( + public static buildObjectMembers( context: ChangeContext, value: object, ): { [key: string]: CRDTElement } { - const elementObject: { [key: string]: CRDTElement } = {}; + const members: { [key: string]: CRDTElement } = {}; for (const [k, v] of Object.entries(value)) { if (k.includes('.')) { throw new YorkieError( @@ -183,9 +183,9 @@ export class ObjectProxy { const createdAt = context.issueTimeTicket(); const elem = buildCRDTElement(context, v, createdAt); - elementObject[k] = elem; + members[k] = elem; } - return elementObject; + return members; } /** diff --git a/src/document/operation/remove_operation.ts b/src/document/operation/remove_operation.ts index 666d24f5f..5e88da410 100644 --- a/src/document/operation/remove_operation.ts +++ b/src/document/operation/remove_operation.ts @@ -23,7 +23,10 @@ import { OperationInfo, ExecutionResult, } from '@yorkie-js-sdk/src/document/operation/operation'; -import { CRDTContainer } from '@yorkie-js-sdk/src/document/crdt/element'; +import { + CRDTContainer, + CRDTElement, +} from '@yorkie-js-sdk/src/document/crdt/element'; import { CRDTObject } from '@yorkie-js-sdk/src/document/crdt/object'; import { CRDTArray } from '@yorkie-js-sdk/src/document/crdt/array'; import { SetOperation } from '@yorkie-js-sdk/src/document/operation/set_operation'; @@ -61,23 +64,19 @@ export class RemoveOperation extends Operation { root: CRDTRoot, source: OpSource, ): ExecutionResult | undefined { - const parentObject = root.findByCreatedAt( + const container = root.findByCreatedAt( this.getParentCreatedAt(), ) as CRDTContainer; - if (!parentObject) { + if (!container) { logger.fatal(`fail to find ${this.getParentCreatedAt()}`); } - if (!(parentObject instanceof CRDTContainer)) { - logger.fatal(`only object and array can execute remove: ${parentObject}`); + if (!(container instanceof CRDTContainer)) { + logger.fatal(`only object and array can execute remove: ${container}`); } // NOTE(chacha912): Handle cases where operation cannot be executed during undo and redo. if (source === OpSource.UndoRedo) { - const targetElem = parentObject.getByID(this.createdAt); - if (targetElem?.isRemoved()) { - return; - } - let parent: CRDTContainer | undefined = parentObject; + let parent: CRDTElement | undefined = container.getByID(this.createdAt); while (parent) { if (parent.getRemovedAt()) { return; @@ -85,14 +84,14 @@ export class RemoveOperation extends Operation { parent = root.findElementPairByCreatedAt(parent.getCreatedAt())?.parent; } } - const key = parentObject.subPathOf(this.createdAt); - const reverseOp = this.toReverseOperation(parentObject); + const key = container.subPathOf(this.createdAt); + const reverseOp = this.toReverseOperation(container); - const elem = parentObject.delete(this.createdAt, this.getExecutedAt()); + const elem = container.delete(this.createdAt, this.getExecutedAt()); root.registerRemovedElement(elem); const opInfos: Array = - parentObject instanceof CRDTArray + container instanceof CRDTArray ? [ { type: 'remove', diff --git a/src/document/operation/set_operation.ts b/src/document/operation/set_operation.ts index 0d8d7240d..5d68dd8c8 100644 --- a/src/document/operation/set_operation.ts +++ b/src/document/operation/set_operation.ts @@ -67,17 +67,17 @@ export class SetOperation extends Operation { root: CRDTRoot, source: OpSource, ): ExecutionResult | undefined { - const parentObject = root.findByCreatedAt(this.getParentCreatedAt()); - if (!parentObject) { + const obj = root.findByCreatedAt(this.getParentCreatedAt()) as CRDTObject; + if (!obj) { logger.fatal(`fail to find ${this.getParentCreatedAt()}`); } - if (!(parentObject instanceof CRDTObject)) { + if (!(obj instanceof CRDTObject)) { logger.fatal(`fail to execute, only object can execute set`); } - const obj = parentObject as CRDTObject; + // NOTE(chacha912): Handle cases where operation cannot be executed during undo and redo. if (source === OpSource.UndoRedo) { - let parent: CRDTContainer | undefined = obj; + let parent: CRDTElement | undefined = obj; while (parent) { if (parent.getRemovedAt()) { return;