Skip to content

Commit

Permalink
Modify to return ExecutionResult type when operation is executed
Browse files Browse the repository at this point in the history
  • Loading branch information
chacha912 committed Sep 21, 2023
1 parent f511039 commit c17dec1
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 108 deletions.
20 changes: 6 additions & 14 deletions src/document/change/change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import { ActorID } from '@yorkie-js-sdk/src/document/time/actor_id';
import {
ExecutionResult,
Operation,
OperationInfo,
} from '@yorkie-js-sdk/src/document/operation/operation';
Expand Down Expand Up @@ -144,20 +143,13 @@ export class Change<P extends Indexable> {
opInfos: Array<OperationInfo>;
reverseOps: Array<HistoryOperation<P>>;
} {
const opInfos: Array<OperationInfo> = [];
const changeOpInfos: Array<OperationInfo> = [];
const reverseOps: Array<HistoryOperation<P>> = [];
for (const operation of this.operations) {
const executionResult = operation.execute(root);
if (
(executionResult as ExecutionResult).opInfos !== undefined &&
(executionResult as ExecutionResult).reverseOps !== undefined
) {
opInfos.push(...(executionResult as ExecutionResult).opInfos);
reverseOps.unshift(...(executionResult as ExecutionResult).reverseOps);
} else {
// TODO(Hyemmie): need to edit return type as "ExecutionResult"
// after implementing every operation's reverse operation
opInfos.push(...(executionResult as Array<OperationInfo>));
const { opInfos, reverseOp } = operation.execute(root);
changeOpInfos.push(...opInfos);
if (reverseOp) {
reverseOps.unshift(reverseOp);
}
}

Expand All @@ -171,7 +163,7 @@ export class Change<P extends Indexable> {
presences.delete(this.id.getActorID()!);
}
}
return { opInfos, reverseOps };
return { opInfos: changeOpInfos, reverseOps };
}

/**
Expand Down
20 changes: 11 additions & 9 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,
OperationInfo,
ExecutionResult,
} 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): Array<OperationInfo> {
public execute(root: CRDTRoot): ExecutionResult {
const parentObject = root.findByCreatedAt(this.getParentCreatedAt());
if (!parentObject) {
logger.fatal(`fail to find ${this.getParentCreatedAt()}`);
Expand All @@ -69,13 +69,15 @@ export class AddOperation extends Operation {
const value = this.value.deepcopy();
array.insertAfter(this.prevCreatedAt, value);
root.registerElement(value, array);
return [
{
type: 'add',
path: root.createPath(this.getParentCreatedAt()),
index: Number(array.subPathOf(this.getEffectedCreatedAt())),
},
];
return {
opInfos: [
{
type: 'add',
path: root.createPath(this.getParentCreatedAt()),
index: Number(array.subPathOf(this.getEffectedCreatedAt())),
},
],
};
}

/**
Expand Down
23 changes: 13 additions & 10 deletions src/document/operation/edit_operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { CRDTText } from '@yorkie-js-sdk/src/document/crdt/text';
import {
Operation,
OperationInfo,
ExecutionResult,
} from '@yorkie-js-sdk/src/document/operation/operation';
import { Indexable } from '../document';

Expand Down Expand Up @@ -79,7 +80,7 @@ export class EditOperation extends Operation {
/**
* `execute` executes this operation on the given `CRDTRoot`.
*/
public execute<A extends Indexable>(root: CRDTRoot): Array<OperationInfo> {
public execute<A extends Indexable>(root: CRDTRoot): ExecutionResult {
const parentObject = root.findByCreatedAt(this.getParentCreatedAt());
if (!parentObject) {
logger.fatal(`fail to find ${this.getParentCreatedAt()}`);
Expand All @@ -100,15 +101,17 @@ export class EditOperation extends Operation {
if (!this.fromPos.equals(this.toPos)) {
root.registerElementHasRemovedNodes(text);
}
return changes.map(({ from, to, value }) => {
return {
type: 'edit',
from,
to,
value,
path: root.createPath(this.getParentCreatedAt()),
};
}) as Array<OperationInfo>;
return {
opInfos: changes.map(({ from, to, value }) => {
return {
type: 'edit',
from,
to,
value,
path: root.createPath(this.getParentCreatedAt()),
} as OperationInfo;
}),
};
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/document/operation/increase_operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class IncreaseOperation extends Operation {
value: value.getValue() as number,
},
],
reverseOps: [this.getReverseOperation()],
reverseOp: this.getReverseOperation(),
};
}

Expand Down
22 changes: 12 additions & 10 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,
OperationInfo,
ExecutionResult,
} 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): Array<OperationInfo> {
public execute(root: CRDTRoot): ExecutionResult {
const parentObject = root.findByCreatedAt(this.getParentCreatedAt());
if (!parentObject) {
logger.fatal(`fail to find ${this.getParentCreatedAt()}`);
Expand All @@ -73,14 +73,16 @@ 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',
path: root.createPath(this.getParentCreatedAt()),
index,
previousIndex,
},
];
return {
opInfos: [
{
type: 'move',
path: root.createPath(this.getParentCreatedAt()),
index,
previousIndex,
},
],
};
}

/**
Expand Down
13 changes: 7 additions & 6 deletions src/document/operation/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ export type TreeStyleOpInfo = {
*/
export type ExecutionResult = {
opInfos: Array<OperationInfo>;
reverseOps: Array<Operation>;
// TODO(chacha912): After implementing all of the reverseOperation,
// we change the type to non-optional.
reverseOp?: Operation;
};

/**
Expand Down Expand Up @@ -222,9 +224,8 @@ export abstract class Operation {
/**
* `execute` executes this operation on the given `CRDTRoot`.
*/
// TODO(Hyemmie): need to standardize the return type as "ExecutionResult"
// after implement every operation's reverse operation
public abstract execute(
root: CRDTRoot,
): ExecutionResult | Array<OperationInfo>;
public abstract execute(root: CRDTRoot): ExecutionResult;

// TODO(chacha912): We need to implement all of the reverseOperation
// public abstract getReverseOperation(): Operation;
}
36 changes: 20 additions & 16 deletions src/document/operation/remove_operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { CRDTRoot } from '@yorkie-js-sdk/src/document/crdt/root';
import {
Operation,
OperationInfo,
ExecutionResult,
} 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 +54,7 @@ export class RemoveOperation extends Operation {
/**
* `execute` executes this operation on the given `CRDTRoot`.
*/
public execute(root: CRDTRoot): Array<OperationInfo> {
public execute(root: CRDTRoot): ExecutionResult {
const parentObject = root.findByCreatedAt(this.getParentCreatedAt());
if (!parentObject) {
logger.fatal(`fail to find ${this.getParentCreatedAt()}`);
Expand All @@ -66,21 +67,24 @@ export class RemoveOperation extends Operation {
const elem = obj.delete(this.createdAt, this.getExecutedAt());
root.registerRemovedElement(elem);

return parentObject instanceof CRDTArray
? [
{
type: 'remove',
path: root.createPath(this.getParentCreatedAt()),
index: Number(key),
},
]
: [
{
type: 'remove',
path: root.createPath(this.getParentCreatedAt()),
key,
},
];
const opInfos: Array<OperationInfo> =
parentObject instanceof CRDTArray
? [
{
type: 'remove',
path: root.createPath(this.getParentCreatedAt()),
index: Number(key),
},
]
: [
{
type: 'remove',
path: root.createPath(this.getParentCreatedAt()),
key,
},
];

return { opInfos };
}

/**
Expand Down
20 changes: 11 additions & 9 deletions src/document/operation/set_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 { CRDTObject } from '@yorkie-js-sdk/src/document/crdt/object';
import {
Operation,
OperationInfo,
ExecutionResult,
} from '@yorkie-js-sdk/src/document/operation/operation';

/**
Expand Down Expand Up @@ -58,7 +58,7 @@ export class SetOperation extends Operation {
/**
* `execute` executes this operation on the given `CRDTRoot`.
*/
public execute(root: CRDTRoot): Array<OperationInfo> {
public execute(root: CRDTRoot): ExecutionResult {
const parentObject = root.findByCreatedAt(this.getParentCreatedAt());
if (!parentObject) {
logger.fatal(`fail to find ${this.getParentCreatedAt()}`);
Expand All @@ -70,13 +70,15 @@ export class SetOperation extends Operation {
const value = this.value.deepcopy();
obj.set(this.key, value);
root.registerElement(value, obj);
return [
{
type: 'set',
path: root.createPath(this.getParentCreatedAt()),
key: this.key,
},
];
return {
opInfos: [
{
type: 'set',
path: root.createPath(this.getParentCreatedAt()),
key: this.key,
},
],
};
}

/**
Expand Down
23 changes: 13 additions & 10 deletions src/document/operation/style_operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { CRDTText } from '@yorkie-js-sdk/src/document/crdt/text';
import {
Operation,
OperationInfo,
ExecutionResult,
} from '@yorkie-js-sdk/src/document/operation/operation';
import { Indexable } from '../document';

Expand Down Expand Up @@ -73,7 +74,7 @@ export class StyleOperation extends Operation {
/**
* `execute` executes this operation on the given `CRDTRoot`.
*/
public execute<A extends Indexable>(root: CRDTRoot): Array<OperationInfo> {
public execute<A extends Indexable>(root: CRDTRoot): ExecutionResult {
const parentObject = root.findByCreatedAt(this.getParentCreatedAt());
if (!parentObject) {
logger.fatal(`fail to find ${this.getParentCreatedAt()}`);
Expand All @@ -88,15 +89,17 @@ export class StyleOperation extends Operation {
this.getExecutedAt(),
this.maxCreatedAtMapByActor,
);
return changes.map(({ from, to, value }) => {
return {
type: 'style',
from,
to,
value,
path: root.createPath(this.getParentCreatedAt()),
};
}) as Array<OperationInfo>;
return {
opInfos: changes.map(({ from, to, value }) => {
return {
type: 'style',
from,
to,
value,
path: root.createPath(this.getParentCreatedAt()),
} as OperationInfo;
}),
};
}

/**
Expand Down
27 changes: 15 additions & 12 deletions src/document/operation/tree_edit_operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
import {
Operation,
OperationInfo,
ExecutionResult,
} from '@yorkie-js-sdk/src/document/operation/operation';

/**
Expand Down Expand Up @@ -75,7 +76,7 @@ export class TreeEditOperation extends Operation {
/**
* `execute` executes this operation on the given `CRDTRoot`.
*/
public execute(root: CRDTRoot): Array<OperationInfo> {
public execute(root: CRDTRoot): ExecutionResult {
const parentObject = root.findByCreatedAt(this.getParentCreatedAt());
if (!parentObject) {
logger.fatal(`fail to find ${this.getParentCreatedAt()}`);
Expand All @@ -94,17 +95,19 @@ export class TreeEditOperation extends Operation {
if (!this.fromPos.equals(this.toPos)) {
root.registerElementHasRemovedNodes(tree);
}
return changes.map(({ from, to, value, fromPath, toPath }) => {
return {
type: 'tree-edit',
from,
to,
value,
fromPath,
toPath,
path: root.createPath(this.getParentCreatedAt()),
};
}) as Array<OperationInfo>;
return {
opInfos: changes.map(({ from, to, value, fromPath, toPath }) => {
return {
type: 'tree-edit',
from,
to,
value,
fromPath,
toPath,
path: root.createPath(this.getParentCreatedAt()),
} as OperationInfo;
}),
};
}

/**
Expand Down
Loading

0 comments on commit c17dec1

Please sign in to comment.