refactor: Synchronized is a mixin

This commit is contained in:
cha0s 2019-04-16 17:52:56 -05:00
parent 81a6ecf5c4
commit e63766b045
10 changed files with 34 additions and 17 deletions

View File

@ -1,7 +1,7 @@
# TODO # TODO
- ✔ Remove dependency on decorators - ✔ Remove dependency on decorators
- Synchronized should be a mixin - Synchronized should be a mixin
- ❌ Behavior items should derive type/name - ❌ Behavior items should derive type/name
- ✔ Precompile behavior traversals - ✔ Precompile behavior traversals
- ❌ Abstract physics world tick (no destructuring) - ❌ Abstract physics world tick (no destructuring)

View File

@ -5,7 +5,7 @@ import without from 'lodash.without';
import {compose} from '@avocado/core'; import {compose} from '@avocado/core';
import {EventEmitter} from '@avocado/mixins'; import {EventEmitter} from '@avocado/mixins';
import {Resource} from '@avocado/resource'; import {Resource} from '@avocado/resource';
import {SynchronizedMixin} from '@avocado/state'; import {Synchronized} from '@avocado/state';
import {hasTrait, lookupTrait} from './trait-registry'; import {hasTrait, lookupTrait} from './trait-registry';
@ -47,7 +47,7 @@ function enumerateTraitProperties(prototype) {
const decorate = compose( const decorate = compose(
EventEmitter, EventEmitter,
SynchronizedMixin, Synchronized,
); );
export class Entity extends decorate(Resource) { export class Entity extends decorate(Resource) {

View File

@ -10,9 +10,10 @@ import {Entity} from '../index';
const decorate = compose( const decorate = compose(
EventEmitter, EventEmitter,
Synchronized,
); );
export class EntityList extends decorate(Synchronized) { export class EntityList extends decorate(class {}) {
constructor() { constructor() {
super(); super();

View File

@ -1,11 +1,16 @@
import * as I from 'immutable'; import * as I from 'immutable';
import {compose} from '@avocado/core';
import {Vector} from '@avocado/math'; import {Vector} from '@avocado/math';
import {Property} from '@avocado/mixins'; import {Property} from '@avocado/mixins';
import {Resource} from '@avocado/resource'; import {Resource} from '@avocado/resource';
import {Synchronized} from '@avocado/state'; import {Synchronized} from '@avocado/state';
export class Trait extends Synchronized { const decorate = compose(
Synchronized,
);
export class Trait extends decorate(class {}) {
constructor(entity, params, state) { constructor(entity, params, state) {
super(); super();

View File

@ -2,5 +2,5 @@ export {nextStep} from './next-step';
export {Packer, Unpacker} from './packer'; export {Packer, Unpacker} from './packer';
export {StatePacket} from './packet/state.packet'; export {StatePacket} from './packet/state.packet';
export {StateKeysPacket} from './packet/state-keys.packet'; export {StateKeysPacket} from './packet/state-keys.packet';
export {Synchronized, SynchronizedMixin} from './synchronized'; export {Synchronized} from './synchronized';
export {Synchronizer} from './synchronizer'; export {Synchronizer} from './synchronizer';

View File

@ -9,7 +9,7 @@ const decorate = compose(
Property('state'), Property('state'),
); );
export function SynchronizedMixin(Superclass) { export function Synchronized(Superclass) {
return class Synchronized extends decorate(Superclass) { return class Synchronized extends decorate(Superclass) {
constructor(...args) { constructor(...args) {
@ -48,7 +48,10 @@ export function SynchronizedMixin(Superclass) {
if (!(key in this)) { if (!(key in this)) {
return; return;
} }
if (this[key] instanceof Synchronized) { if (
'undefined' !== typeof this[key]
&& 'undefined' !== typeof this[key].patchState
) {
this[key].patchState([step]); this[key].patchState([step]);
} }
else { else {
@ -59,13 +62,19 @@ export function SynchronizedMixin(Superclass) {
tick(elapsed) { tick(elapsed) {
const children = this.synchronizedChildren(); const children = this.synchronizedChildren();
for (const key of children) { for (const key of children) {
if (this[key] instanceof Synchronized) { if (
'undefined' !== typeof this[key]
&& 'undefined' !== typeof this[key].tick
) {
this[key].tick(elapsed); this[key].tick(elapsed);
} }
} }
this.state = this.state.withMutations((state) => { this.state = this.state.withMutations((state) => {
for (const key of children) { for (const key of children) {
if (this[key] instanceof Synchronized) { if (
'undefined' !== typeof this[key]
&& 'undefined' !== typeof this[key].state
) {
state.set(key, this[key].state); state.set(key, this[key].state);
} }
else { else {
@ -77,5 +86,3 @@ export function SynchronizedMixin(Superclass) {
} }
} }
export class Synchronized extends SynchronizedMixin(class {}) {}

View File

@ -21,9 +21,10 @@ const decorate = compose(
Property('world', { Property('world', {
track: true, track: true,
}), }),
Synchronized,
); );
export class Layer extends decorate(Synchronized) { export class Layer extends decorate(class {}) {
constructor() { constructor() {
super(); super();

View File

@ -8,9 +8,10 @@ import {Layer} from './layer';
const decorate = compose( const decorate = compose(
EventEmitter, EventEmitter,
Synchronized,
); );
export class Layers extends decorate(Synchronized) { export class Layers extends decorate(class {}) {
constructor() { constructor() {
super(); super();

View File

@ -16,10 +16,11 @@ const decorate = compose(
Vector.Mixin('size', 'width', 'height', { Vector.Mixin('size', 'width', 'height', {
default: [0, 0], default: [0, 0],
track: true, track: true,
}) }),
Synchronized,
); );
export class Room extends decorate(Synchronized) { export class Room extends decorate(class {}) {
constructor() { constructor() {
super(); super();

View File

@ -10,9 +10,10 @@ const decorate = compose(
Vector.Mixin('size', 'width', 'height', { Vector.Mixin('size', 'width', 'height', {
default: [0, 0], default: [0, 0],
}), }),
Synchronized,
); );
export class Tiles extends decorate(Synchronized) { export class Tiles extends decorate(class {}) {
constructor() { constructor() {
super(); super();