diff --git a/src/document/change/change.ts b/src/document/change/change.ts index f33573154..663d4787a 100644 --- a/src/document/change/change.ts +++ b/src/document/change/change.ts @@ -17,7 +17,7 @@ import { ActorID } from '@yorkie-js-sdk/src/document/time/actor_id'; import { Operation, - Modified, + InternalOpInfo, } from '@yorkie-js-sdk/src/document/operation/operation'; import { CRDTRoot } from '@yorkie-js-sdk/src/document/crdt/root'; import { ChangeID } from '@yorkie-js-sdk/src/document/change/change_id'; @@ -86,17 +86,13 @@ export class Change { /** * `execute` executes the operations of this change to the given root. */ - public execute(root: CRDTRoot): Array { - const modifieds: Array = []; + public execute(root: CRDTRoot): Array { + const opInfos: Array = []; for (const operation of this.operations) { - const modified = operation.execute(root); - if (Array.isArray(modified)) { - modifieds.push(...modified); - } else if (modified) { - modifieds.push(modified); - } + const infos = operation.execute(root); + opInfos.push(...infos); } - return modifieds; + return opInfos; } /** diff --git a/src/document/document.ts b/src/document/document.ts index e8dbb27d6..25008c914 100644 --- a/src/document/document.ts +++ b/src/document/document.ts @@ -46,7 +46,7 @@ import { } from '@yorkie-js-sdk/src/document/change/checkpoint'; import { TimeTicket } from '@yorkie-js-sdk/src/document/time/ticket'; import { - Modified, + InternalOpInfo, OperationInfo, } from '@yorkie-js-sdk/src/document/operation/operation'; import { JSONObject } from './json/object'; @@ -250,7 +250,7 @@ export class Document { } const change = context.getChange(); - const modifieds = change.execute(this.root); + const internalOpInfos = change.execute(this.root); this.localChanges.push(change); this.changeID = change.getID(); @@ -260,8 +260,8 @@ export class Document { value: [ { message: change.getMessage() || '', - operations: modifieds.map((modified) => - this.getOperationInfo(modified), + operations: internalOpInfos.map((internalOpInfo) => + this.toOperationInfo(internalOpInfo), ), }, ], @@ -603,11 +603,11 @@ export class Document { const changeInfos: Array = []; for (const change of changes) { - const modifieds = change.execute(this.root); + const inernalOpInfos = change.execute(this.root); changeInfos.push({ message: change.getMessage() || '', - operations: modifieds.map((modified) => - this.getOperationInfo(modified), + operations: inernalOpInfos.map((opInfo) => + this.toOperationInfo(opInfo), ), }); this.changeID = this.changeID.syncLamport(change.getID().getLamport()); @@ -659,14 +659,14 @@ export class Document { return pathTrie.findPrefixes().map((element) => element.join('.')); } - private getOperationInfo(modified: Modified): OperationInfo { + private toOperationInfo(internalOpInfo: InternalOpInfo): OperationInfo { const opInfo = {} as OperationInfo; - for (const key of Object.keys(modified)) { + for (const key of Object.keys(internalOpInfo)) { if (key === 'element') { - opInfo.path = this.root.createSubPaths(modified[key])!.join('.'); + opInfo.path = this.root.createSubPaths(internalOpInfo[key])!.join('.'); } else { - const k = key as keyof Omit; - opInfo[k] = modified[k]; + const k = key as keyof Omit; + opInfo[k] = internalOpInfo[k]; } } return opInfo; diff --git a/src/document/operation/add_operation.ts b/src/document/operation/add_operation.ts index 597fbd870..9a79b56a1 100644 --- a/src/document/operation/add_operation.ts +++ b/src/document/operation/add_operation.ts @@ -21,7 +21,7 @@ import { CRDTRoot } from '@yorkie-js-sdk/src/document/crdt/root'; import { CRDTArray } from '@yorkie-js-sdk/src/document/crdt/array'; import { Operation, - Modified, + InternalOpInfo, } from '@yorkie-js-sdk/src/document/operation/operation'; /** @@ -57,7 +57,7 @@ export class AddOperation extends Operation { /** * `execute` executes this operation on the given `CRDTRoot`. */ - public execute(root: CRDTRoot): Modified { + public execute(root: CRDTRoot): Array { const parentObject = root.findByCreatedAt(this.getParentCreatedAt()); if (!parentObject) { logger.fatal(`fail to find ${this.getParentCreatedAt()}`); @@ -69,11 +69,13 @@ export class AddOperation extends Operation { const value = this.value.deepcopy(); array.insertAfter(this.prevCreatedAt, value); root.registerElement(value, array); - return { - type: 'add', - element: this.getParentCreatedAt(), - index: Number(array.subPathOf(this.getEffectedCreatedAt())), - }; + return [ + { + type: 'add', + element: this.getParentCreatedAt(), + index: Number(array.subPathOf(this.getEffectedCreatedAt())), + }, + ]; } /** diff --git a/src/document/operation/edit_operation.ts b/src/document/operation/edit_operation.ts index bdff652c6..e3c52af4f 100644 --- a/src/document/operation/edit_operation.ts +++ b/src/document/operation/edit_operation.ts @@ -21,7 +21,7 @@ import { RGATreeSplitNodePos } from '@yorkie-js-sdk/src/document/crdt/rga_tree_s import { CRDTText } from '@yorkie-js-sdk/src/document/crdt/text'; import { Operation, - Modified, + InternalOpInfo, } from '@yorkie-js-sdk/src/document/operation/operation'; import { Indexable } from '../document'; @@ -79,7 +79,7 @@ export class EditOperation extends Operation { /** * `execute` executes this operation on the given `CRDTRoot`. */ - public execute(root: CRDTRoot): Array { + public execute(root: CRDTRoot): Array { const parentObject = root.findByCreatedAt(this.getParentCreatedAt()); if (!parentObject) { logger.fatal(`fail to find ${this.getParentCreatedAt()}`); @@ -98,7 +98,7 @@ export class EditOperation extends Operation { if (!this.fromPos.equals(this.toPos)) { root.registerTextWithGarbage(text); } - const modified = changes.map(({ type, actor, from, to, value }) => { + return changes.map(({ type, actor, from, to, value }) => { return type === 'content' ? { type: 'edit', @@ -115,8 +115,7 @@ export class EditOperation extends Operation { to, element: this.getParentCreatedAt(), }; - }) as Array; - return modified; + }) as Array; } /** diff --git a/src/document/operation/increase_operation.ts b/src/document/operation/increase_operation.ts index 1993c2c9a..f38a1b49b 100644 --- a/src/document/operation/increase_operation.ts +++ b/src/document/operation/increase_operation.ts @@ -16,7 +16,7 @@ import { Operation, - Modified, + InternalOpInfo, } from '@yorkie-js-sdk/src/document/operation/operation'; import { TimeTicket } from '@yorkie-js-sdk/src/document/time/ticket'; import { CRDTElement } from '@yorkie-js-sdk/src/document/crdt/element'; @@ -55,7 +55,7 @@ export class IncreaseOperation extends Operation { /** * `execute` executes this operation on the given `CRDTRoot`. */ - public execute(root: CRDTRoot): Modified { + public execute(root: CRDTRoot): Array { const parentObject = root.findByCreatedAt(this.getParentCreatedAt()); if (!parentObject) { logger.fatal(`fail to find ${this.getParentCreatedAt()}`); @@ -66,11 +66,13 @@ export class IncreaseOperation extends Operation { const counter = parentObject as CRDTCounter; const value = this.value.deepcopy() as Primitive; counter.increase(value); - return { - type: 'increase', - element: this.getEffectedCreatedAt(), - value: value.getValue() as number, - }; + return [ + { + type: 'increase', + element: this.getEffectedCreatedAt(), + value: value.getValue() as number, + }, + ]; } /** diff --git a/src/document/operation/move_operation.ts b/src/document/operation/move_operation.ts index ed8ba5a4f..c5b1b57d7 100644 --- a/src/document/operation/move_operation.ts +++ b/src/document/operation/move_operation.ts @@ -20,7 +20,7 @@ import { CRDTRoot } from '@yorkie-js-sdk/src/document/crdt/root'; import { CRDTArray } from '@yorkie-js-sdk/src/document/crdt/array'; import { Operation, - Modified, + InternalOpInfo, } from '@yorkie-js-sdk/src/document/operation/operation'; /** @@ -61,7 +61,7 @@ export class MoveOperation extends Operation { /** * `execute` executes this operation on the given `CRDTRoot`. */ - public execute(root: CRDTRoot): Modified { + public execute(root: CRDTRoot): Array { const parentObject = root.findByCreatedAt(this.getParentCreatedAt()); if (!parentObject) { logger.fatal(`fail to find ${this.getParentCreatedAt()}`); @@ -73,12 +73,14 @@ export class MoveOperation extends Operation { const previousIndex = Number(array.subPathOf(this.createdAt)); array.moveAfter(this.prevCreatedAt, this.createdAt, this.getExecutedAt()); const index = Number(array.subPathOf(this.createdAt)); - return { - type: 'move', - element: this.getParentCreatedAt(), - index, - previousIndex, - }; + return [ + { + type: 'move', + element: this.getParentCreatedAt(), + index, + previousIndex, + }, + ]; } /** diff --git a/src/document/operation/operation.ts b/src/document/operation/operation.ts index ec57d4a05..ae78fce36 100644 --- a/src/document/operation/operation.ts +++ b/src/document/operation/operation.ts @@ -89,19 +89,19 @@ export type SelectOpInfo = { }; /** - * `Modified` represents the information of the modified element. It is used to + * `InternalOpInfo` represents the information of the operation. It is used to * internally and can be converted to `OperationInfo` to inform to the user. */ -export type Modified = - | OpToModified - | OpToModified - | OpToModified - | OpToModified - | OpToModified - | OpToModified - | OpToModified - | OpToModified; -type OpToModified = Omit & { +export type InternalOpInfo = + | ToInternalOpInfo + | ToInternalOpInfo + | ToInternalOpInfo + | ToInternalOpInfo + | ToInternalOpInfo + | ToInternalOpInfo + | ToInternalOpInfo + | ToInternalOpInfo; +type ToInternalOpInfo = Omit & { element: TimeTicket; }; @@ -152,7 +152,5 @@ export abstract class Operation { /** * `execute` executes this operation on the given `CRDTRoot`. */ - public abstract execute( - root: CRDTRoot, - ): Array | Modified | undefined; + public abstract execute(root: CRDTRoot): Array; } diff --git a/src/document/operation/remove_operation.ts b/src/document/operation/remove_operation.ts index a12d2f4a1..3f53757c4 100644 --- a/src/document/operation/remove_operation.ts +++ b/src/document/operation/remove_operation.ts @@ -19,7 +19,7 @@ import { TimeTicket } from '@yorkie-js-sdk/src/document/time/ticket'; import { CRDTRoot } from '@yorkie-js-sdk/src/document/crdt/root'; import { Operation, - Modified, + InternalOpInfo, } from '@yorkie-js-sdk/src/document/operation/operation'; import { CRDTContainer } from '@yorkie-js-sdk/src/document/crdt/element'; import { CRDTArray } from '@yorkie-js-sdk/src/document/crdt/array'; @@ -53,7 +53,7 @@ export class RemoveOperation extends Operation { /** * `execute` executes this operation on the given `CRDTRoot`. */ - public execute(root: CRDTRoot): Modified { + public execute(root: CRDTRoot): Array { const parentObject = root.findByCreatedAt(this.getParentCreatedAt()); if (!parentObject) { logger.fatal(`fail to find ${this.getParentCreatedAt()}`); @@ -67,16 +67,20 @@ export class RemoveOperation extends Operation { root.registerRemovedElement(elem); return parentObject instanceof CRDTArray - ? { - type: 'remove', - element: this.getEffectedCreatedAt(), - index: Number(key), - } - : { - type: 'remove', - element: this.getEffectedCreatedAt(), - key, - }; + ? [ + { + type: 'remove', + element: this.getEffectedCreatedAt(), + index: Number(key), + }, + ] + : [ + { + type: 'remove', + element: this.getEffectedCreatedAt(), + key, + }, + ]; } /** diff --git a/src/document/operation/select_operation.ts b/src/document/operation/select_operation.ts index 42eef9d19..cd9fc0bd9 100644 --- a/src/document/operation/select_operation.ts +++ b/src/document/operation/select_operation.ts @@ -21,7 +21,7 @@ import { RGATreeSplitNodePos } from '@yorkie-js-sdk/src/document/crdt/rga_tree_s import { CRDTText } from '@yorkie-js-sdk/src/document/crdt/text'; import { Operation, - Modified, + InternalOpInfo, } from '@yorkie-js-sdk/src/document/operation/operation'; import { Indexable } from '../document'; @@ -58,7 +58,7 @@ export class SelectOperation extends Operation { /** * `execute` executes this operation on the given `CRDTRoot`. */ - public execute(root: CRDTRoot): Modified | undefined { + public execute(root: CRDTRoot): Array { const parentObject = root.findByCreatedAt(this.getParentCreatedAt()); if (!parentObject) { logger.fatal(`fail to find ${this.getParentCreatedAt()}`); @@ -72,12 +72,14 @@ export class SelectOperation extends Operation { this.getExecutedAt(), ); return change - ? { - ...change, - type: 'select', - element: this.getParentCreatedAt(), - } - : undefined; + ? [ + { + ...change, + type: 'select', + element: this.getParentCreatedAt(), + }, + ] + : []; } /** diff --git a/src/document/operation/set_operation.ts b/src/document/operation/set_operation.ts index 38101b573..3120f6f49 100644 --- a/src/document/operation/set_operation.ts +++ b/src/document/operation/set_operation.ts @@ -21,7 +21,7 @@ import { CRDTRoot } from '@yorkie-js-sdk/src/document/crdt/root'; import { CRDTObject } from '@yorkie-js-sdk/src/document/crdt/object'; import { Operation, - Modified, + InternalOpInfo, } from '@yorkie-js-sdk/src/document/operation/operation'; /** @@ -58,7 +58,7 @@ export class SetOperation extends Operation { /** * `execute` executes this operation on the given `CRDTRoot`. */ - public execute(root: CRDTRoot): Modified { + public execute(root: CRDTRoot): Array { const parentObject = root.findByCreatedAt(this.getParentCreatedAt()); if (!parentObject) { logger.fatal(`fail to find ${this.getParentCreatedAt()}`); @@ -70,11 +70,13 @@ export class SetOperation extends Operation { const value = this.value.deepcopy(); obj.set(this.key, value); root.registerElement(value, obj); - return { - type: 'set', - element: this.getParentCreatedAt(), - key: this.key, - }; + return [ + { + type: 'set', + element: this.getParentCreatedAt(), + key: this.key, + }, + ]; } /** diff --git a/src/document/operation/style_operation.ts b/src/document/operation/style_operation.ts index a68bee223..76819df4c 100644 --- a/src/document/operation/style_operation.ts +++ b/src/document/operation/style_operation.ts @@ -21,7 +21,7 @@ import { RGATreeSplitNodePos } from '@yorkie-js-sdk/src/document/crdt/rga_tree_s import { CRDTText } from '@yorkie-js-sdk/src/document/crdt/text'; import { Operation, - Modified, + InternalOpInfo, } from '@yorkie-js-sdk/src/document/operation/operation'; import { Indexable } from '../document'; @@ -68,7 +68,7 @@ export class StyleOperation extends Operation { /** * `execute` executes this operation on the given `CRDTRoot`. */ - public execute(root: CRDTRoot): Array { + public execute(root: CRDTRoot): Array { const parentObject = root.findByCreatedAt(this.getParentCreatedAt()); if (!parentObject) { logger.fatal(`fail to find ${this.getParentCreatedAt()}`); @@ -91,7 +91,7 @@ export class StyleOperation extends Operation { value, element: this.getParentCreatedAt(), }; - }) as Array; + }) as Array; } /**