Fix optimization issue

This commit is contained in:
schmelczerandras 2019-09-01 18:47:31 +02:00
parent db6a31dd85
commit d89b43e055
7 changed files with 29 additions and 15 deletions

View file

@ -26,6 +26,7 @@
input[type='checkbox'] {
-webkit-appearance: none;
-moz-appearance: none;
width: 2 * $size;
height: $size;

View file

@ -4,7 +4,7 @@ import { Node } from '../store/node';
export class Block extends Serializable implements IBlock {
constructor(parent: Node, props: IBlock) {
super(parent, props);
super(parent, props, 'Block');
this.onAfterClone();
}

View file

@ -5,7 +5,7 @@ import { Node } from '../store/node';
export class Page extends Serializable implements IPage {
constructor(parent: Node, props: IPage) {
super(parent, props);
super(parent, props, 'Page');
}
readonly name: string;

View file

@ -2,11 +2,14 @@ import { Cloneable } from '../store/cloneable';
import { Node } from '../store/node';
export class Serializable extends Cloneable {
protected type: string;
private static propertyList: any = {};
static childrenMap: {
[type: string]: {
childrenConstructor: typeof Serializable;
childrenListName: string;
childrenType: string;
};
};
@ -14,29 +17,34 @@ export class Serializable extends Cloneable {
// pass
}
protected constructor(parent: Node, properties: any) {
protected constructor(parent: Node, properties: any, type: string) {
super(parent);
const type = this.constructor.name;
if (!Serializable.propertyList.hasOwnProperty(type)) {
Serializable.propertyList[type] = [];
const compiledType = this.constructor.name;
if (!Serializable.propertyList.hasOwnProperty(compiledType)) {
Serializable.propertyList[compiledType] = [];
}
for (const property in properties) {
if (properties.hasOwnProperty(property)) {
const propertyValue = properties[property];
// This should be ran after the original constructor has finished.
console.log(type);
if (property === Serializable.childrenMap[type].childrenListName) {
// This should be ran after the original constructor has finished.
new Promise(r => r()).then(() => {
for (let child of propertyValue) {
new Serializable.childrenMap[type].childrenConstructor(this, child);
new Serializable.childrenMap[type].childrenConstructor(
this,
child,
Serializable.childrenMap[type].childrenType
);
}
});
} else {
this[property] = properties[property];
}
if (!Serializable.propertyList[type].includes(property)) {
Serializable.propertyList[type].push(property);
if (!Serializable.propertyList[compiledType].includes(property)) {
Serializable.propertyList[compiledType].push(property);
}
}
}

View file

@ -9,6 +9,8 @@ import { IColor } from '../interfaces/color';
export type ColoredBlock = Block & { color: IColor };
export class Tower extends Serializable implements ITower {
protected type = 'Tower';
tags: string[];
name: string;
@ -21,7 +23,7 @@ export class Tower extends Serializable implements ITower {
readonly baseColor: IColor;
constructor(parent: Node, props: ITower) {
super(parent, props);
super(parent, props, 'Tower');
this.onAfterClone();
}

View file

@ -30,15 +30,18 @@ export class DataService extends Root<Page> {
Serializable.childrenMap = {
Page: {
childrenListName: 'towers',
childrenConstructor: Tower
childrenConstructor: Tower,
childrenType: 'Tower'
},
Tower: {
childrenListName: 'blocks',
childrenConstructor: Block
childrenConstructor: Block,
childrenType: 'Block'
},
Block: {
childrenListName: null,
childrenConstructor: null
childrenConstructor: null,
childrenType: null
}
};

View file

@ -114,7 +114,7 @@ export class StoreService<T> {
return this.storedData;
}
async save(data: T) {
async save(data: T): Promise<void> {
this.storedData = data;
const stringified = JSON.stringify(this.storedData, null, 2);
console.log('save');