Skip to content

Commit

Permalink
Revise codes
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed Nov 23, 2023
1 parent 5a8b835 commit aa5105a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 45 deletions.
24 changes: 10 additions & 14 deletions src/document/crdt/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
10 changes: 5 additions & 5 deletions src/document/json/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown>,
): Array<CRDTElement> {
const elementArray: Array<CRDTElement> = [];
const elements: Array<CRDTElement> = [];
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;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/document/json/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -169,7 +169,7 @@ export function buildCRDTElement(
} else {
element = CRDTObject.create(
createdAt,
ObjectProxy.buildObject(context, value!),
ObjectProxy.buildObjectMembers(context, value!),
);
}
} else {
Expand Down
10 changes: 5 additions & 5 deletions src/document/json/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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;
}

/**
Expand Down
27 changes: 13 additions & 14 deletions src/document/operation/remove_operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -61,38 +64,34 @@ 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;
}
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<OperationInfo> =
parentObject instanceof CRDTArray
container instanceof CRDTArray
? [
{
type: 'remove',
Expand Down
10 changes: 5 additions & 5 deletions src/document/operation/set_operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit aa5105a

Please sign in to comment.