Skip to content

Commit

Permalink
Rename Modified to InternalOpInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed Apr 11, 2023
1 parent 7f8eee8 commit 2147252
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 93 deletions.
16 changes: 6 additions & 10 deletions src/document/change/change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -86,17 +86,13 @@ export class Change {
/**
* `execute` executes the operations of this change to the given root.
*/
public execute(root: CRDTRoot): Array<Modified> {
const modifieds: Array<Modified> = [];
public execute(root: CRDTRoot): Array<InternalOpInfo> {
const opInfos: Array<InternalOpInfo> = [];
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;
}

/**
Expand Down
24 changes: 12 additions & 12 deletions src/document/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -250,7 +250,7 @@ export class Document<T> {
}

const change = context.getChange();
const modifieds = change.execute(this.root);
const internalOpInfos = change.execute(this.root);
this.localChanges.push(change);
this.changeID = change.getID();

Expand All @@ -260,8 +260,8 @@ export class Document<T> {
value: [
{
message: change.getMessage() || '',
operations: modifieds.map((modified) =>
this.getOperationInfo(modified),
operations: internalOpInfos.map((internalOpInfo) =>
this.toOperationInfo(internalOpInfo),
),
},
],
Expand Down Expand Up @@ -603,11 +603,11 @@ export class Document<T> {

const changeInfos: Array<ChangeInfo> = [];
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());
Expand Down Expand Up @@ -659,14 +659,14 @@ export class Document<T> {
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<Modified, 'element'>;
opInfo[k] = modified[k];
const k = key as keyof Omit<InternalOpInfo, 'element'>;
opInfo[k] = internalOpInfo[k];
}
}
return opInfo;
Expand Down
16 changes: 9 additions & 7 deletions src/document/operation/add_operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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<InternalOpInfo> {
const parentObject = root.findByCreatedAt(this.getParentCreatedAt());
if (!parentObject) {
logger.fatal(`fail to find ${this.getParentCreatedAt()}`);
Expand All @@ -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())),
},
];
}

/**
Expand Down
9 changes: 4 additions & 5 deletions src/document/operation/edit_operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -79,7 +79,7 @@ export class EditOperation extends Operation {
/**
* `execute` executes this operation on the given `CRDTRoot`.
*/
public execute<A extends Indexable>(root: CRDTRoot): Array<Modified> {
public execute<A extends Indexable>(root: CRDTRoot): Array<InternalOpInfo> {
const parentObject = root.findByCreatedAt(this.getParentCreatedAt());
if (!parentObject) {
logger.fatal(`fail to find ${this.getParentCreatedAt()}`);
Expand All @@ -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',
Expand All @@ -115,8 +115,7 @@ export class EditOperation extends Operation {
to,
element: this.getParentCreatedAt(),
};
}) as Array<Modified>;
return modified;
}) as Array<InternalOpInfo>;
}

/**
Expand Down
16 changes: 9 additions & 7 deletions src/document/operation/increase_operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<InternalOpInfo> {
const parentObject = root.findByCreatedAt(this.getParentCreatedAt());
if (!parentObject) {
logger.fatal(`fail to find ${this.getParentCreatedAt()}`);
Expand All @@ -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,
},
];
}

/**
Expand Down
18 changes: 10 additions & 8 deletions src/document/operation/move_operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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<InternalOpInfo> {
const parentObject = root.findByCreatedAt(this.getParentCreatedAt());
if (!parentObject) {
logger.fatal(`fail to find ${this.getParentCreatedAt()}`);
Expand All @@ -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,
},
];
}

/**
Expand Down
26 changes: 12 additions & 14 deletions src/document/operation/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AddOpInfo>
| OpToModified<IncreaseOpInfo>
| OpToModified<RemoveOpInfo>
| OpToModified<SetOpInfo>
| OpToModified<MoveOpInfo>
| OpToModified<EditOpInfo>
| OpToModified<StyleOpInfo>
| OpToModified<SelectOpInfo>;
type OpToModified<T extends OperationInfo> = Omit<T, 'path'> & {
export type InternalOpInfo =
| ToInternalOpInfo<AddOpInfo>
| ToInternalOpInfo<IncreaseOpInfo>
| ToInternalOpInfo<RemoveOpInfo>
| ToInternalOpInfo<SetOpInfo>
| ToInternalOpInfo<MoveOpInfo>
| ToInternalOpInfo<EditOpInfo>
| ToInternalOpInfo<StyleOpInfo>
| ToInternalOpInfo<SelectOpInfo>;
type ToInternalOpInfo<T extends OperationInfo> = Omit<T, 'path'> & {
element: TimeTicket;
};

Expand Down Expand Up @@ -152,7 +152,5 @@ export abstract class Operation {
/**
* `execute` executes this operation on the given `CRDTRoot`.
*/
public abstract execute(
root: CRDTRoot,
): Array<Modified> | Modified | undefined;
public abstract execute(root: CRDTRoot): Array<InternalOpInfo>;
}
28 changes: 16 additions & 12 deletions src/document/operation/remove_operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<InternalOpInfo> {
const parentObject = root.findByCreatedAt(this.getParentCreatedAt());
if (!parentObject) {
logger.fatal(`fail to find ${this.getParentCreatedAt()}`);
Expand All @@ -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,
},
];
}

/**
Expand Down
18 changes: 10 additions & 8 deletions src/document/operation/select_operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -58,7 +58,7 @@ export class SelectOperation extends Operation {
/**
* `execute` executes this operation on the given `CRDTRoot`.
*/
public execute<A extends Indexable>(root: CRDTRoot): Modified | undefined {
public execute<A extends Indexable>(root: CRDTRoot): Array<InternalOpInfo> {
const parentObject = root.findByCreatedAt(this.getParentCreatedAt());
if (!parentObject) {
logger.fatal(`fail to find ${this.getParentCreatedAt()}`);
Expand All @@ -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(),
},
]
: [];
}

/**
Expand Down
Loading

0 comments on commit 2147252

Please sign in to comment.