feat: BehaviorItem::clone

This commit is contained in:
cha0s 2019-04-25 00:48:53 -05:00
parent 080fbb0f34
commit a5c3b4190e
7 changed files with 58 additions and 2 deletions

View File

@ -13,7 +13,6 @@ export class Actions extends decorate(Traversals) {
constructor() {
super();
this._index = 0;
this.traversals = [];
this.pending = null;
}

View File

@ -8,6 +8,17 @@ export function Collection(type) {
this[plural] = [];
}
clone(other) {
if (0 === other[plural].length) {
return;
}
const Item = other[plural][0].constructor;
for (let i = 0; i < other[plural].length; ++i) {
this[plural][i] = new Item();
this[plural][i].clone(other[plural][i]);
}
}
count() {
return this[plural].length;
}

View File

@ -11,6 +11,11 @@ export class Condition {
return this.get(...args);
}
clone(other) {
this.operator = other.operator;
this.operands = other.operands.map((operand) => operand.clone());
}
fromJSON(json) {
this.operator = json.operator;
this.operands = json.operands.map((operand) => {

View File

@ -4,6 +4,10 @@ export class Literal {
this.value = null;
}
clone(other) {
this.value = other.value;
}
fromJSON(json) {
this.value = json.value;
return this;

View File

@ -11,6 +11,10 @@ export class Routine {
this._context = context;
}
clone(other) {
this.actions = other.actions.clone();
}
fromJSON(json) {
this.actions.fromJSON(json.routine);
return this;

View File

@ -21,9 +21,17 @@ export class Routines {
}
}
clone(other) {
for (const i in other.routines) {
const routine = other.routines[i];
this.routines[i] = routine.clone();
}
}
fromJSON(json) {
for (const i in json.routines) {
this.routines[i] = (new Routine()).fromJSON(json.routines[i]);
const routineJSON = json.routines[i];
this.routines[i] = (new Routine()).fromJSON(routineJSON);
}
return this;
}

View File

@ -8,6 +8,31 @@ export class Traversal {
this.value = undefined;
}
clone(other) {
this.steps = other.steps.map((step) => {
switch (step.type) {
case 'key':
return step;
case 'invoke':
return {
type: 'invoke',
args: step.args.map((arg, index) => {
const Item = arg.constructor;
const item = new Item();
item.clone(arg);
return item;
}),
};
}
});
if (other.value) {
this.value = other.value.clone();
}
if (other.hash) {
this.hash = other.hash;
}
}
fromJSON(json) {
this.steps = json.steps.map((step) => {
switch (step.type) {