Skip to content

Commit

Permalink
Specify the event value for the document subscription - increase oper…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
chacha912 committed Mar 28, 2023
1 parent 9a14fc7 commit f34d434
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 41 deletions.
18 changes: 15 additions & 3 deletions src/document/change/change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@
*/

import { ActorID } from '@yorkie-js-sdk/src/document/time/actor_id';
import { Operation } from '@yorkie-js-sdk/src/document/operation/operation';
import {
Operation,
ExecuteOperationResult,
} 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';

export type ExecuteChangeResult = {
modified: Array<ExecuteOperationResult>;
};

/**
* `Change` represents a unit of modification in the document.
*/
Expand Down Expand Up @@ -83,10 +90,15 @@ export class Change {
/**
* `execute` executes the operations of this change to the given root.
*/
public execute(root: CRDTRoot): void {
public execute(root: CRDTRoot): ExecuteChangeResult {
const operationResult: Array<ExecuteOperationResult> = [];
for (const operation of this.operations) {
operation.execute(root);
const result = operation.execute(root);
if (result) operationResult.push(result);
}
return {
modified: operationResult,
};
}

/**
Expand Down
68 changes: 41 additions & 27 deletions src/document/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {
InitialCheckpoint,
} from '@yorkie-js-sdk/src/document/change/checkpoint';
import { TimeTicket } from '@yorkie-js-sdk/src/document/time/ticket';
import { Operation } from '@yorkie-js-sdk/src/document/operation/operation';
import { ExecuteOperationResult } from '@yorkie-js-sdk/src/document/operation/operation';
import { JSONObject } from './json/object';
import { Trie } from '../util/trie';

Expand Down Expand Up @@ -126,10 +126,12 @@ export interface SnapshotEvent extends BaseDocEvent {
* `ChangeInfo` represents a pair of `Change` and the JsonPath of the changed
* element.
*/
export interface ChangeInfo {
change: Change;
paths: Array<string>;
}
export type ChangeInfo = {
message: string;
modified: Array<ExecuteResult>;
};

type ExecuteResult = Omit<ExecuteOperationResult, 'element'> & { path: string };

/**
* `LocalChangeEvent` is an event that occurs when the document is changed
Expand Down Expand Up @@ -247,17 +249,25 @@ export class Document<T> {
}

const change = context.getChange();
change.execute(this.root);
const changeResult = change.execute(this.root).modified;
this.localChanges.push(change);
this.changeID = change.getID();
const modified = changeResult.map(({ type, element, value }) => {
return {
type,
value,
path: this.root.createSubPaths(element)!.join('.'),
};
});
this.changeID = this.changeID.syncLamport(change.getID().getLamport());

if (this.eventStreamObserver) {
this.eventStreamObserver.next({
type: DocEventType.LocalChange,
value: [
{
change,
paths: this.createPaths(change),
message: change.getMessage() || '',
modified,
},
],
});
Expand Down Expand Up @@ -296,21 +306,17 @@ export class Document<T> {
}

const changes: Array<ChangeInfo> = [];
event.value.forEach(({ change }) => {
const ops: Array<Operation> = [];
const paths: Array<string> = [];
change.getOperations().forEach((op) => {
const createdAt = op.getEffectedCreatedAt();
const subPaths = this.root.createSubPaths(createdAt)!.join('.');
if (this.isSameElementOrChildOf(subPaths, target)) {
ops.push(op);
paths.push(subPaths);
event.value.forEach(({ message, modified }) => {
const targetModified: Array<ExecuteResult> = [];
modified.forEach((result) => {
if (this.isSameElementOrChildOf(result.path, target)) {
targetModified.push(result);
}
});
ops.length &&
targetModified.length &&
changes.push({
change: Change.create(change.getID(), ops, change.getMessage()),
paths,
message,
modified: targetModified,
});
});
changes.length &&
Expand Down Expand Up @@ -585,20 +591,28 @@ export class Document<T> {
change.execute(this.clone!);
}

const changeInfos: Array<ChangeInfo> = [];
for (const change of changes) {
change.execute(this.root);
const modified = change
.execute(this.root)
.modified.map(({ type, element, value }) => {
return {
type,
value,
path: this.root.createSubPaths(element)!.join('.'),
};
});
changeInfos.push({
message: change.getMessage() || '',
modified,
});
this.changeID = this.changeID.syncLamport(change.getID().getLamport());
}

if (changes.length && this.eventStreamObserver) {
this.eventStreamObserver.next({
type: DocEventType.RemoteChange,
value: changes.map((change) => {
return {
change,
paths: this.createPaths(change),
};
}),
value: changeInfos,
});
}

Expand Down
24 changes: 14 additions & 10 deletions src/document/operation/increase_operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { CRDTRoot } from '@yorkie-js-sdk/src/document/crdt/root';
import { Primitive } from '@yorkie-js-sdk/src/document/crdt/primitive';
import { logger } from '@yorkie-js-sdk/src/util/logger';
import { CRDTCounter } from '@yorkie-js-sdk/src/document/crdt/counter';
import { ExecuteOperationResult } from '@yorkie-js-sdk/src/document/operation/operation';

/**
* `IncreaseOperation` represents an operation that increments a numeric value to Counter.
Expand Down Expand Up @@ -52,19 +53,22 @@ export class IncreaseOperation extends Operation {
/**
* `execute` executes this operation on the given `CRDTRoot`.
*/
public execute(root: CRDTRoot): void {
public execute(root: CRDTRoot): ExecuteOperationResult {
const parentObject = root.findByCreatedAt(this.getParentCreatedAt());
if (parentObject instanceof CRDTCounter) {
const counter = parentObject as CRDTCounter;
const value = this.value.deepcopy() as Primitive;
counter.increase(value);
} else {
if (!parentObject) {
logger.fatal(`fail to find ${this.getParentCreatedAt()}`);
}

if (!parentObject) {
logger.fatal(`fail to find ${this.getParentCreatedAt()}`);
}
if (!(parentObject instanceof CRDTCounter)) {
logger.fatal(`fail to execute, only Counter can execute increase`);
}
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,
};
}

/**
Expand Down
8 changes: 7 additions & 1 deletion src/document/operation/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ import { ActorID } from '@yorkie-js-sdk/src/document/time/actor_id';
import { TimeTicket } from '@yorkie-js-sdk/src/document/time/ticket';
import { CRDTRoot } from '@yorkie-js-sdk/src/document/crdt/root';

export type ExecuteOperationResult = {
type: 'increase';
element: TimeTicket;
value: number;
};

/**
* `Operation` represents an operation to be executed on a document.
*/
Expand Down Expand Up @@ -65,5 +71,5 @@ export abstract class Operation {
/**
* `execute` executes this operation on the given `CRDTRoot`.
*/
public abstract execute(root: CRDTRoot): void;
public abstract execute(root: CRDTRoot): ExecuteOperationResult;
}

0 comments on commit f34d434

Please sign in to comment.