refactor: DRY

This commit is contained in:
cha0s 2019-04-07 00:19:32 -04:00
parent 6438736e43
commit dcd61c29d4
4 changed files with 87 additions and 120 deletions

View File

@ -60,58 +60,44 @@ export class EntityList extends decorate(class {}) {
for (const step of patch) {
const {op, path, value} = step;
if ('/' === path) {
for (const uuid in value) {
const localUuid = this.uuidMap_PRIVATE[uuid];
const entity = this.entities_PRIVATE[localUuid];
if (entity) {
if (false === value[uuid]) {
// Entity removed.
this.removeEntity(entity);
}
else {
entity.patchState([
{
op: 'replace',
path: '/',
value: value[uuid],
}
]);
this.state_PRIVATE = this.state_PRIVATE.set(localUuid, entity.state);
}
}
else {
// New entity. Create with patch as traits.
const newEntity = create().fromJSON({
traits: value[uuid],
});
this.uuidMap_PRIVATE[uuid] = newEntity.instanceUuid;
this.addEntity(newEntity);
}
}
this.patchStateEntities(value);
}
else {
const [uuid, substep] = StateSynchronizer.forwardStep(step);
const localUuid = this.uuidMap_PRIVATE[uuid];
const entity = this.entities_PRIVATE[localUuid];
if (entity) {
if (false === substep.value[uuid]) {
// Entity removed.
this.removeEntity(entity);
}
else {
entity.patchState([substep]);
this.state_PRIVATE = this.state_PRIVATE.set(localUuid, entity.state);
}
this.patchStateEntities({[value]: substep.value});
}
}
}
patchStateEntities(entities) {
for (const uuid in entities) {
const localUuid = this.uuidMap_PRIVATE[uuid];
const entity = this.entities_PRIVATE[localUuid];
if (entity) {
if (false === entities[uuid]) {
// Entity removed.
this.removeEntity(entity);
}
else {
// New entity. Create with patch as traits.
const newEntity = create().fromJSON({
traits: substep.value[uuid],
});
this.uuidMap_PRIVATE[uuid] = newEntity.instanceUuid;
this.addEntity(newEntity);
// Exists; patch.
entity.patchState([
{
op: 'replace',
path: '/',
value: entities[uuid],
}
]);
this.state_PRIVATE = this.state_PRIVATE.set(localUuid, entity.state);
}
}
else {
// New entity. Create with patch as traits.
const newEntity = create().fromJSON({
traits: entities[uuid],
});
this.uuidMap_PRIVATE[uuid] = newEntity.instanceUuid;
this.addEntity(newEntity);
}
}
}

View File

@ -42,38 +42,34 @@ export class Trait {
for (const step of patch) {
const {op, path, value} = step;
if ('/' === path) {
if (!value.state) {
continue;
if (value.state) {
this.patchStateEntity(value.state);
}
const undefinedProperties = {};
for (const key in value.state) {
const value = value.state[key];
if (key in this.entity) {
this.entity[key] = value;
}
else {
undefinedProperties[key] = value;
}
}
this.state = this.state.merge(undefinedProperties);
}
else {
const [stepKey, substep] = StateSynchronizer.forwardStep(step);
if ('state' === stepKey) {
const key = substep.path.substr(1);
const undefinedProperties = {};
if (key in this.entity) {
this.entity[key] = substep.value;
}
else {
undefinedProperties[key] = substep.value;
}
this.state = this.state.merge(undefinedProperties);
this.patchStateEntity({[key]: substep.value});
}
}
}
}
patchStateEntity(state) {
const undefinedProperties = {};
for (const key in state) {
const value = state[key];
if (key in this.entity) {
this.entity[key] = value;
}
else {
undefinedProperties[key] = value;
}
}
this.state = this.state.merge(undefinedProperties);
}
toJSON() {
return {
params: this.params.toJS(),

View File

@ -188,51 +188,41 @@ export class Traits {
for (const step of patch) {
const {op, path, value} = step;
if ('/' === path) {
for (const type in value) {
let instance = this.traits_PRIVATE[type];
// New trait requested?
if (!this.traits_PRIVATE[type]) {
// Doesn't exist?
if (!hasTrait(type)) {
continue;
}
this.addTrait(type, value[type]);
instance = this.traits_PRIVATE[type];
}
else {
// Accept state.
instance.patchState([
{
op: 'replace',
path: '/',
value: patch[type],
}
]);
}
this._setInstanceState(type, instance);
}
this.patchStateTraits(value);
}
else {
const [type, substep] = StateSynchronizer.forwardStep(step);
let instance = this.traits_PRIVATE[type];
// New trait requested?
if (!this.traits_PRIVATE[type]) {
// Doesn't exist?
if (!hasTrait(type)) {
continue;
}
this.addTrait(type, substep.value[type]);
instance = this.traits_PRIVATE[type];
}
else {
// Accept state.
instance.patchState([substep]);
}
this._setInstanceState(type, instance);
this.patchStateTraits({[type]: substep.value});
}
}
}
patchStateTraits(traits) {
for (const type in traits) {
let instance = this.traits_PRIVATE[type];
// New trait requested?
if (!this.traits_PRIVATE[type]) {
// Doesn't exist?
if (!hasTrait(type)) {
continue;
}
this.addTrait(type, traits[type]);
instance = this.traits_PRIVATE[type];
}
else {
// Accept state.
instance.patchState([
{
op: 'replace',
path: '/',
value: traits[type],
}
]);
}
this._setInstanceState(type, instance);
}
}
removeAllTraits() {
const types = this.allTypes();
this.removeTraits(types);

View File

@ -89,30 +89,25 @@ export class Layers extends decorate(class {}) {
const {op, path, value} = step;
if ('/' === path) {
for (const index in value) {
const layer = this.layers[index] ? this.layers[index] : new Layer();
if (!this.layers[index]) {
this.addLayer(index, layer);
}
layer.patchState([
{
op: 'replace',
path: '/',
value: value[index],
}
]);
const substep = {op, path, value: value[index]};
this.patchStateLayer(index, substep);
}
}
else {
const [index, substep] = StateSynchronizer.forwardStep(step);
const layer = this.layers[index] ? this.layers[index] : new Layer();
if (!this.layers[index]) {
this.addLayer(index, layer);
}
layer.patchState([substep]);
this.patchStateLayer(index, substep);
}
}
}
patchStateLayer(index, step) {
const layer = this.layers[index] ? this.layers[index] : new Layer();
if (!this.layers[index]) {
this.addLayer(index, layer);
}
layer.patchState([step]);
}
removeEntityFromLayer(entity, layerIndex) {
const layer = this.layers[layerIndex];
if (!layer) {