Refactor store
This commit is contained in:
parent
952a3cd34c
commit
9933f4f9ff
2 changed files with 207 additions and 286 deletions
|
|
@ -13,10 +13,6 @@ import { Observable } from 'rxjs/internal/Observable';
|
|||
providedIn: 'root'
|
||||
})
|
||||
export class DataService extends Root<Page> {
|
||||
get pages(): Array<Page> {
|
||||
return this.children;
|
||||
}
|
||||
|
||||
private readonly _safeChildren: BehaviorSubject<Array<Page>> = new BehaviorSubject(null);
|
||||
readonly safeChildren$: Observable<Array<Page>> = this._safeChildren.asObservable();
|
||||
|
||||
|
|
@ -25,6 +21,30 @@ export class DataService extends Root<Page> {
|
|||
this.init().catch();
|
||||
}
|
||||
|
||||
get pages(): Array<Page> {
|
||||
return this.children;
|
||||
}
|
||||
|
||||
save(timeout: number) {
|
||||
this.storeService.scheduleSave(this.pages, timeout);
|
||||
}
|
||||
|
||||
addPage(name: string) {
|
||||
const page = new Page({
|
||||
name,
|
||||
userData: {},
|
||||
towers: []
|
||||
});
|
||||
this.addChildren([page]);
|
||||
page.addTower();
|
||||
}
|
||||
|
||||
removePage(page: Page) {
|
||||
this.changeKeys<any>({
|
||||
children: this.children.filter(c => c !== page)
|
||||
});
|
||||
}
|
||||
|
||||
private async init() {
|
||||
const pages = await this.storeService.load();
|
||||
Serializable.childrenMap = {
|
||||
|
|
@ -44,43 +64,15 @@ export class DataService extends Root<Page> {
|
|||
childrenType: null
|
||||
}
|
||||
};
|
||||
this.children$.subscribe(value => {
|
||||
this.log();
|
||||
});
|
||||
|
||||
for (let page of pages) {
|
||||
new Page(this, page);
|
||||
}
|
||||
setTimeout(() => {
|
||||
this.children$.subscribe(value => {
|
||||
this.log();
|
||||
});
|
||||
}, 0);
|
||||
this.addChildren(pages.map(p => new Page(p)));
|
||||
|
||||
this.children$.subscribe(value => {
|
||||
this._safeChildren.next(value);
|
||||
this.save(0);
|
||||
});
|
||||
}
|
||||
|
||||
mutatedUpdate() {
|
||||
this.save(2500);
|
||||
}
|
||||
|
||||
save(timeout: number) {
|
||||
this.storeService.scheduleSave(this.pages, timeout);
|
||||
}
|
||||
|
||||
addPage(name: string) {
|
||||
const page = new Page(this, {
|
||||
name,
|
||||
userData: {},
|
||||
towers: []
|
||||
});
|
||||
page.addTower();
|
||||
}
|
||||
|
||||
removePage(page: Page) {
|
||||
this.changeValue({
|
||||
oldValue: this.children,
|
||||
newValue: this.children.filter(c => c !== page)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -95,7 +95,7 @@ export class StoreService<T> {
|
|||
|
||||
constructor() {
|
||||
const localStorageData = localStorage.getItem(LOCAL_STORAGE_KEY);
|
||||
this.storedData = JSON.parse(localStorageData ? localStorageData : this.mockData) as T;
|
||||
this.storedData = JSON.parse(false ? localStorageData : this.mockData) as T;
|
||||
}
|
||||
|
||||
scheduleSave(data: T, timeout: number) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue