Skip to content

Commit

Permalink
refactor(json-crdt): 💡 use namespace import
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Oct 27, 2023
1 parent fbe21ae commit 4e6788b
Showing 1 changed file with 15 additions and 31 deletions.
46 changes: 15 additions & 31 deletions src/json-crdt/model/Model.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,4 @@
// TODO: perf: try namespace import
import {
NewConOp,
NewObjOp,
NewValOp,
NewVecOp,
NewStrOp,
NewBinOp,
NewArrOp,
InsValOp,
InsObjOp,
InsVecOp,
InsStrOp,
InsBinOp,
InsArrOp,
DelOp,
} from '../../json-crdt-patch/operations';
import * as operations from '../../json-crdt-patch/operations';
import {ArrayRga} from '../types/rga-array/ArrayRga';
import {BinaryRga} from '../types/rga-binary/BinaryRga';
import {Const} from '../types/const/Const';
Expand Down Expand Up @@ -168,23 +152,23 @@ export class Model<RootJsonNode extends JsonNode = JsonNode> implements Printabl
this.clock.observe(op.id, op.span());
const index = this.index;
// TODO: Use switch statement here? And rearrange cases by frequency of use?
if (op instanceof InsStrOp) {
if (op instanceof operations.InsStrOp) {
const node = index.get(op.obj);
if (node instanceof StringRga) node.ins(op.ref, op.id, op.data);
} else if (op instanceof NewObjOp) {
} else if (op instanceof operations.NewObjOp) {
if (!index.get(op.id)) index.set(new ObjectLww(this, op.id));
} else if (op instanceof NewArrOp) {
} else if (op instanceof operations.NewArrOp) {
if (!index.get(op.id)) index.set(new ArrayRga(this, op.id));
} else if (op instanceof NewStrOp) {
} else if (op instanceof operations.NewStrOp) {
if (!index.get(op.id)) index.set(new StringRga(op.id));
} else if (op instanceof NewValOp) {
} else if (op instanceof operations.NewValOp) {
if (!index.get(op.id)) {
const val = index.get(op.val);
if (val) index.set(new ValueLww(this, op.id, op.val));
}
} else if (op instanceof NewConOp) {
} else if (op instanceof operations.NewConOp) {
if (!index.get(op.id)) index.set(new Const(op.id, op.val));
} else if (op instanceof InsObjOp) {
} else if (op instanceof operations.InsObjOp) {
const node = index.get(op.obj);
const tuples = op.data;
const length = tuples.length;
Expand All @@ -198,7 +182,7 @@ export class Model<RootJsonNode extends JsonNode = JsonNode> implements Printabl
if (old) this.deleteNodeTree(old);
}
}
} else if (op instanceof InsVecOp) {
} else if (op instanceof operations.InsVecOp) {
const node = index.get(op.obj);
const tuples = op.data;
const length = tuples.length;
Expand All @@ -212,7 +196,7 @@ export class Model<RootJsonNode extends JsonNode = JsonNode> implements Printabl
if (old) this.deleteNodeTree(old);
}
}
} else if (op instanceof InsValOp) {
} else if (op instanceof operations.InsValOp) {
const obj = op.obj;
const node = obj.sid === SESSION.SYSTEM && obj.time === SYSTEM_SESSION_TIME.ORIGIN ? this.root : index.get(obj);
if (node instanceof ValueLww) {
Expand All @@ -222,7 +206,7 @@ export class Model<RootJsonNode extends JsonNode = JsonNode> implements Printabl
if (old) this.deleteNodeTree(old);
}
}
} else if (op instanceof InsArrOp) {
} else if (op instanceof operations.InsArrOp) {
const node = index.get(op.obj);
if (node instanceof ArrayRga) {
const nodes: ITimestampStruct[] = [];
Expand All @@ -237,7 +221,7 @@ export class Model<RootJsonNode extends JsonNode = JsonNode> implements Printabl
}
if (nodes.length) node.ins(op.ref, op.id, nodes);
}
} else if (op instanceof DelOp) {
} else if (op instanceof operations.DelOp) {
const node = index.get(op.obj);
if (node instanceof ArrayRga) {
const length = op.what.length;
Expand All @@ -251,12 +235,12 @@ export class Model<RootJsonNode extends JsonNode = JsonNode> implements Printabl
node.delete(op.what);
} else if (node instanceof StringRga) node.delete(op.what);
else if (node instanceof BinaryRga) node.delete(op.what);
} else if (op instanceof NewBinOp) {
} else if (op instanceof operations.NewBinOp) {
if (!index.get(op.id)) index.set(new BinaryRga(op.id));
} else if (op instanceof InsBinOp) {
} else if (op instanceof operations.InsBinOp) {
const node = index.get(op.obj);
if (node instanceof BinaryRga) node.ins(op.ref, op.id, op.data);
} else if (op instanceof NewVecOp) {
} else if (op instanceof operations.NewVecOp) {
if (!index.get(op.id)) index.set(new ArrayLww(this, op.id));
}
}
Expand Down

0 comments on commit 4e6788b

Please sign in to comment.