more refactor

This commit is contained in:
Andras Schmelczer 2019-09-06 23:16:05 +02:00
parent 9933f4f9ff
commit d612678682
12 changed files with 36 additions and 52 deletions

View file

@ -9,14 +9,13 @@ import { Page } from '../../../../model/page';
styleUrls: ['./settings.component.scss'] styleUrls: ['./settings.component.scss']
}) })
export class SettingsComponent implements OnInit { export class SettingsComponent implements OnInit {
page: Page;
constructor(public modalService: ModalService, public dataService: DataService) {} constructor(public modalService: ModalService, public dataService: DataService) {}
ngOnInit() { ngOnInit() {
this.modalService.active.input.subscribe(p => (this.page = p)); this.modalService.active.input.subscribe(p => (this.page = p));
} }
page: Page;
async deletePage() { async deletePage() {
try { try {
await this.modalService.showRemovePage(this.page.name); await this.modalService.showRemovePage(this.page.name);

View file

@ -41,7 +41,7 @@ export class TowerComponent implements OnInit {
this.blocks = value.coloredBlocks this.blocks = value.coloredBlocks
.filter(b => b.isDone) .filter(b => b.isDone)
.map(b => { .map(b => {
let classedBlock = b as StyledBlock; const classedBlock = b as StyledBlock;
classedBlock.shouldDraw = true; classedBlock.shouldDraw = true;
classedBlock.style = { transform: 'translateY(0)', opacity: '1' }; classedBlock.style = { transform: 'translateY(0)', opacity: '1' };
classedBlock.cssClass = ''; classedBlock.cssClass = '';
@ -52,7 +52,7 @@ export class TowerComponent implements OnInit {
console.log(this.tower, this.tower.latestVersion, value); console.log(this.tower, this.tower.latestVersion, value);
} }
if (this.tower && this.tower.latestVersion === value) { if (this.tower && this.tower.latestVersion === value) {
let difference = this.tower.blocks.map((b, index) => { const difference = this.tower.blocks.map((b, index) => {
return b === value.blocks[index]; return b === value.blocks[index];
}); });

View file

@ -93,7 +93,7 @@ export class DoubleSliderComponent {
} }
private emitValue() { private emitValue() {
const range = { this.range.emit({
from: from:
this.oneValue < this.otherValue this.oneValue < this.otherValue
? this.values[this.indexFromValue(this.oneValue)] ? this.values[this.indexFromValue(this.oneValue)]
@ -102,7 +102,6 @@ export class DoubleSliderComponent {
this.oneValue < this.otherValue this.oneValue < this.otherValue
? this.values[this.indexFromValue(this.otherValue)] ? this.values[this.indexFromValue(this.otherValue)]
: this.values[this.indexFromValue(this.oneValue)] : this.values[this.indexFromValue(this.oneValue)]
}; });
this.range.emit(range);
} }
} }

View file

@ -1,6 +1,5 @@
import { Serializable } from './serializable'; import { Serializable } from './serializable';
import { IBlock } from '../interfaces/persistance/block'; import { IBlock } from '../interfaces/persistance/block';
import { Node } from '../store/node';
import { InnerNodeState } from '../store/inner-node'; import { InnerNodeState } from '../store/inner-node';
export interface BlockState extends IBlock, InnerNodeState {} export interface BlockState extends IBlock, InnerNodeState {}
@ -12,7 +11,6 @@ export class Block extends Serializable implements IBlock, BlockState {
readonly tag: string; readonly tag: string;
constructor(props: IBlock) { constructor(props: IBlock) {
console.log('b');
if (props.created.constructor.name !== 'Date') { if (props.created.constructor.name !== 'Date') {
props.created = new Date(props.created); props.created = new Date(props.created);
} }

View file

@ -1,7 +1,7 @@
import { Serializable } from './serializable'; import { Serializable } from './serializable';
import { IPage } from '../interfaces/persistance/page'; import { IPage } from '../interfaces/persistance/page';
import { Tower } from './tower'; import { Tower } from './tower';
import { InnerNodeState } from '../store/inner-node'; import { InnerNode, InnerNodeState } from '../store/inner-node';
export interface PageState extends InnerNodeState, IPage {} export interface PageState extends InnerNodeState, IPage {}
@ -17,7 +17,7 @@ export class Page extends Serializable implements IPage, PageState {
}; };
constructor(props: IPage) { constructor(props: IPage) {
super(props, 'Page'); super(props, 'Page', props.towers.map(t => new Tower(t)));
} }
get towers(): Array<Tower> { get towers(): Array<Tower> {

View file

@ -12,8 +12,8 @@ export class Serializable extends InnerNode {
private static propertyList: any = {}; private static propertyList: any = {};
protected type: string; protected type: string;
protected constructor(properties: any, type: string) { protected constructor(properties: any, type: string, children?: Array<InnerNode>) {
super(); super(children);
const compiledType = this.constructor.name; const compiledType = this.constructor.name;
if (!Serializable.propertyList.hasOwnProperty(compiledType)) { if (!Serializable.propertyList.hasOwnProperty(compiledType)) {
@ -21,19 +21,7 @@ export class Serializable extends InnerNode {
} }
for (const property in properties) { for (const property in properties) {
if (properties.hasOwnProperty(property)) { if (properties.hasOwnProperty(property)) {
const propertyValue = properties[property]; if (property !== Serializable.childrenMap[type].childrenListName) {
// This should be ran after the original constructor has finished.
if (property === Serializable.childrenMap[type].childrenListName) {
new Promise(r => r()).then(() => {
const children = propertyValue.map(
c =>
new Serializable.childrenMap[type].childrenConstructor(c, Serializable.childrenMap[type].childrenType)
);
console.log(type, 'created');
this.addChildren(children);
console.log(type, 'added');
});
} else {
this[property] = properties[property]; this[property] = properties[property];
} }

View file

@ -4,7 +4,7 @@ import { Block } from './block';
import { Serializable } from './serializable'; import { Serializable } from './serializable';
import { hash } from '../utils/hash'; import { hash } from '../utils/hash';
import { IColor } from '../interfaces/color'; import { IColor } from '../interfaces/color';
import { InnerNodeState } from '../store/inner-node'; import { InnerNode, InnerNodeState } from '../store/inner-node';
export type ColoredBlock = Block & { color: IColor }; export type ColoredBlock = Block & { color: IColor };
@ -23,7 +23,8 @@ export class Tower extends Serializable implements ITower, TowerState {
readonly baseColor: IColor; readonly baseColor: IColor;
constructor(props: ITower) { constructor(props: ITower) {
super(props, 'Tower'); super(props, 'Tower', props.blocks.map(b => new Block(b)));
this.onAfterClone();
} }
protected onAfterClone() { protected onAfterClone() {

View file

@ -13,9 +13,6 @@ import { Observable } from 'rxjs/internal/Observable';
providedIn: 'root' providedIn: 'root'
}) })
export class DataService extends Root<Page> { export class DataService extends Root<Page> {
private readonly _safeChildren: BehaviorSubject<Array<Page>> = new BehaviorSubject(null);
readonly safeChildren$: Observable<Array<Page>> = this._safeChildren.asObservable();
constructor(private storeService: StoreService<Array<IPage>>) { constructor(private storeService: StoreService<Array<IPage>>) {
super(); super();
this.init().catch(); this.init().catch();
@ -66,13 +63,9 @@ export class DataService extends Root<Page> {
}; };
this.children$.subscribe(value => { this.children$.subscribe(value => {
this.log(); this.log();
this.save(0);
}); });
this.addChildren(pages.map(p => new Page(p))); this.addChildren(pages.map(p => new Page(p)));
this.children$.subscribe(value => {
this._safeChildren.next(value);
this.save(0);
});
} }
} }

View file

@ -10,8 +10,9 @@ export class InnerNode extends Node implements InnerNodeState {
private nextVersion: this = null; private nextVersion: this = null;
readonly children: Array<InnerNode> = []; readonly children: Array<InnerNode> = [];
constructor() { constructor(children?: Array<InnerNode>) {
super(); super();
this.children = children ? children : [];
} }
get latestVersion(): this { get latestVersion(): this {
@ -22,12 +23,12 @@ export class InnerNode extends Node implements InnerNodeState {
return version; return version;
} }
addChildren(children: Array<InnerNode>) { addChildren(children: Array<InnerNode>): this {
super.addChildren.call(this.latestVersion, children); return super.addChildren.call(this.latestVersion, children);
} }
replaceChild(update: { oldValue: InnerNode; newValue: InnerNode }) { replaceChild(update: { oldValue: InnerNode; newValue: InnerNode }): this {
super.replaceChild.call(this.latestVersion, update); return super.replaceChild.call(this.latestVersion, update);
} }
changeKeys<T extends NodeState>(props: Partial<T>): this { changeKeys<T extends NodeState>(props: Partial<T>): this {

View file

@ -9,25 +9,28 @@ export abstract class Node extends Unique implements NodeState {
protected copyCount = 1; protected copyCount = 1;
abstract readonly children: Array<InnerNode>; abstract readonly children: Array<InnerNode>;
protected constructor() {
super();
}
protected abstract changeKeys<T extends NodeState>(props: Partial<T>): this; protected abstract changeKeys<T extends NodeState>(props: Partial<T>): this;
protected initiate() { protected initiate() {
super.initiate(); super.initiate();
++this.copyCount; this.copyCount++;
} }
addChildren(children: Array<InnerNode>) { addChildren(children: Array<InnerNode>): this {
this.changeKeys<NodeState>({ return this.changeKeys<NodeState>({
children: [...this.children, ...children] children: [...this.children, ...children]
}); });
} }
replaceChild({ oldValue, newValue }: { oldValue: InnerNode; newValue: InnerNode }) { replaceChild({ oldValue, newValue }: { oldValue: InnerNode; newValue: InnerNode }): this {
if (oldValue === newValue) { if (oldValue === newValue) {
return; return this;
} }
this.changeKeys<NodeState>({ return this.changeKeys<NodeState>({
children: this.children.map(c => (c === oldValue ? newValue : c)) children: this.children.map(c => (c === oldValue ? newValue : c))
}); });
} }

View file

@ -4,11 +4,13 @@ import { Node, NodeState } from './node';
import { InnerNode } from './inner-node'; import { InnerNode } from './inner-node';
export class Root<T extends InnerNode> extends Node { export class Root<T extends InnerNode> extends Node {
private readonly _children: BehaviorSubject<Array<T>> = new BehaviorSubject([]); readonly children$: Observable<Array<T>>;
readonly children$: Observable<Array<T>> = this._children.asObservable(); private readonly _children: BehaviorSubject<Array<T>>;
constructor() { constructor(children?: Array<T>) {
super(); super();
this._children = new BehaviorSubject(children ? children : []);
this.children$ = this._children.asObservable();
} }
get children(): Array<T> { get children(): Array<T> {
@ -16,6 +18,7 @@ export class Root<T extends InnerNode> extends Node {
} }
set children(value: Array<T>) { set children(value: Array<T>) {
console.log(value);
this._children.next(value); this._children.next(value);
} }

View file

@ -4,8 +4,7 @@ export const hash = (text: string): number => {
return 0; return 0;
} }
const hashValue = Array.prototype.reduce.call( const hashValue = Array.prototype.reduce.call(
// tslint:disable-next-line:no-bitwise text, // tslint:disable-next-line:no-bitwise
text,
(value, char) => ((value << 5) - value + (char.charCodeAt(0) as number)) | 0, (value, char) => ((value << 5) - value + (char.charCodeAt(0) as number)) | 0,
7 7
); );