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 edb7ca2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 26 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

0 comments on commit edb7ca2

Please sign in to comment.