Skip to content

Commit

Permalink
Specify the doc.subscribe event value - set operation
Browse files Browse the repository at this point in the history
  • Loading branch information
chacha912 committed Mar 30, 2023
1 parent 93ace97 commit 1be1d8a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
12 changes: 11 additions & 1 deletion src/document/operation/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ export type AddOpModified = {
value: CRDTElement | PrimitiveValue;
index: number | undefined;
};
export type SetOpModified = {
type: 'set';
element: TimeTicket;
value: CRDTElement | PrimitiveValue;
key?: string;
};
export type RemoveOpModified = {
type: 'remove';
element: TimeTicket;
Expand All @@ -37,7 +43,11 @@ export type IncreaseOpModified = {
element: TimeTicket;
value: number;
};
export type Modified = AddOpModified | IncreaseOpModified | RemoveOpModified;
export type Modified =
| AddOpModified
| IncreaseOpModified
| RemoveOpModified
| SetOpModified;

/**
* `Operation` represents an operation to be executed on a document.
Expand Down
32 changes: 20 additions & 12 deletions src/document/operation/set_operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ import { TimeTicket } from '@yorkie-js-sdk/src/document/time/ticket';
import { CRDTElement } from '@yorkie-js-sdk/src/document/crdt/element';
import { CRDTRoot } from '@yorkie-js-sdk/src/document/crdt/root';
import { CRDTObject } from '@yorkie-js-sdk/src/document/crdt/object';
import { Operation } from '@yorkie-js-sdk/src/document/operation/operation';
import { Primitive } from '@yorkie-js-sdk/src/document/crdt/primitive';
import {
Operation,
Modified,
} from '@yorkie-js-sdk/src/document/operation/operation';

/**
* `SetOperation` represents an operation that stores the value corresponding to the
Expand Down Expand Up @@ -55,20 +59,24 @@ export class SetOperation extends Operation {
/**
* `execute` executes this operation on the given `CRDTRoot`.
*/
public execute(root: CRDTRoot): void {
public execute(root: CRDTRoot): Modified {
const parentObject = root.findByCreatedAt(this.getParentCreatedAt());
if (parentObject instanceof CRDTObject) {
const obj = parentObject as CRDTObject;
const value = this.value.deepcopy();
obj.set(this.key, value);
root.registerElement(value, obj);
} else {
if (!parentObject) {
logger.fatal(`fail to find ${this.getParentCreatedAt()}`);
}

if (!parentObject) {
logger.fatal(`fail to find ${this.getParentCreatedAt()}`);
}
if (!(parentObject instanceof CRDTObject)) {
logger.fatal(`fail to execute, only object can execute set`);
}
const obj = parentObject as CRDTObject;
const value = this.value.deepcopy();
obj.set(this.key, value);
root.registerElement(value, obj);
return {
type: 'set',
element: this.getParentCreatedAt(),
value: value instanceof Primitive ? value.getValue() : value,
key: this.key,
};
}

/**
Expand Down

0 comments on commit 1be1d8a

Please sign in to comment.