Create immutable storage
This commit is contained in:
parent
72eaca7a3b
commit
ca0bf943f7
8 changed files with 229 additions and 8 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
import { Component } from '@angular/core';
|
import { Component } from '@angular/core';
|
||||||
import { ModalService } from './services/modal.service';
|
import { Cloneable } from './storage/cloneable';
|
||||||
|
import { Root } from './storage/root';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-root',
|
selector: 'app-root',
|
||||||
|
|
@ -8,4 +9,19 @@ import { ModalService } from './services/modal.service';
|
||||||
})
|
})
|
||||||
export class AppComponent {
|
export class AppComponent {
|
||||||
title = 'frontend';
|
title = 'frontend';
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
const root = new Root<Cloneable>();
|
||||||
|
|
||||||
|
const l1 = new Cloneable(root, 'l1');
|
||||||
|
const r1 = new Cloneable(root, 'r1');
|
||||||
|
const r1r1 = new Cloneable(r1, 'r1r1');
|
||||||
|
const r1l1 = new Cloneable(r1, 'r1l1');
|
||||||
|
|
||||||
|
r1r1.changeName('r1r1 new');
|
||||||
|
|
||||||
|
r1l1.changeName('r1l1 new');
|
||||||
|
|
||||||
|
r1l1.map((c: Cloneable) => c.changeNameMap('bdeiwf'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,11 @@
|
||||||
|
import { Subject } from 'rxjs/internal/Subject';
|
||||||
|
|
||||||
export class Base {
|
export class Base {
|
||||||
private static propertyList: any = {};
|
private static propertyList: any = {};
|
||||||
protected subscribers: (() => void)[] = [];
|
protected subscribers: (() => void)[] = [];
|
||||||
|
|
||||||
|
subject: Subject<this> = new Subject();
|
||||||
|
|
||||||
constructor(properties: any) {
|
constructor(properties: any) {
|
||||||
const type = this.constructor.name;
|
const type = this.constructor.name;
|
||||||
if (!Base.propertyList.hasOwnProperty(type)) {
|
if (!Base.propertyList.hasOwnProperty(type)) {
|
||||||
|
|
@ -37,7 +41,8 @@ export class Base {
|
||||||
[property]: this[property],
|
[property]: this[property],
|
||||||
...object
|
...object
|
||||||
}),
|
}),
|
||||||
{}
|
// TODO
|
||||||
|
{ type: this.constructor.name }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -46,6 +51,7 @@ export class Base {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected update() {
|
protected update() {
|
||||||
|
this.subject.next(this);
|
||||||
this.subscribers.map(f => f());
|
this.subscribers.map(f => f());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { ApplicationRef, Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { StoreService } from './store.service';
|
import { StoreService } from './store.service';
|
||||||
import { Page } from '../model/page';
|
import { Page } from '../model/page';
|
||||||
|
|
||||||
|
|
@ -22,7 +22,7 @@ export class DataService {
|
||||||
private hasLoaded = new Promise(resolve => (this.afterLoadFinished = resolve));
|
private hasLoaded = new Promise(resolve => (this.afterLoadFinished = resolve));
|
||||||
private afterLoadFinished: () => void;
|
private afterLoadFinished: () => void;
|
||||||
|
|
||||||
constructor(private storeService: StoreService, private applicationRef: ApplicationRef) {
|
constructor(private storeService: StoreService) {
|
||||||
this.init();
|
this.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -72,7 +72,6 @@ export class DataService {
|
||||||
|
|
||||||
private update() {
|
private update() {
|
||||||
this.subscribers.map(f => f());
|
this.subscribers.map(f => f());
|
||||||
this.applicationRef.tick();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private loadActiveIndex() {
|
private loadActiveIndex() {
|
||||||
|
|
|
||||||
71
src/app/storage/cloneable.ts
Normal file
71
src/app/storage/cloneable.ts
Normal file
|
|
@ -0,0 +1,71 @@
|
||||||
|
import { InnerNode } from './inner-node';
|
||||||
|
import { Node } from './node';
|
||||||
|
|
||||||
|
export class Cloneable extends InnerNode {
|
||||||
|
name;
|
||||||
|
constructor(parent: Node, name: any) {
|
||||||
|
super(parent);
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
changeNameMap = (newValue: string) => {
|
||||||
|
this.name = newValue;
|
||||||
|
};
|
||||||
|
|
||||||
|
changeName = (newValue: any) => {
|
||||||
|
this.changeValue({
|
||||||
|
oldValue: this.name,
|
||||||
|
newValue
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
protected cloneWithMap(map: (node: this) => void): this {
|
||||||
|
const insides = Object.getOwnPropertyDescriptors(this);
|
||||||
|
|
||||||
|
const insidesProxy = new Proxy(insides, {
|
||||||
|
get: (target, prop, proxy) => {
|
||||||
|
if (prop == '__target__') {
|
||||||
|
return target;
|
||||||
|
}
|
||||||
|
const value = target[prop as string].value;
|
||||||
|
if (typeof value === 'function') {
|
||||||
|
return value.bind(proxy);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
},
|
||||||
|
set: (target, prop, value) => {
|
||||||
|
return (target[prop as string].value = value);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
map(<any>insidesProxy);
|
||||||
|
|
||||||
|
return Object.create(Object.getPrototypeOf(this), <any>insidesProxy.__target__);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected cloneWithAdd({ value, propertyName }: { value: any; propertyName: string }): this {
|
||||||
|
const insides = Object.getOwnPropertyDescriptors(this);
|
||||||
|
insides[propertyName].value = value;
|
||||||
|
insides.id.value = Node.id++;
|
||||||
|
|
||||||
|
return Object.create(Object.getPrototypeOf(this), insides);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected cloneWithModify({ oldValue, newValue }: { oldValue: any; newValue: any }): this {
|
||||||
|
const insides = Object.getOwnPropertyDescriptors(this);
|
||||||
|
insides.id.value = Node.id++;
|
||||||
|
|
||||||
|
let wasMatch = false;
|
||||||
|
for (let name in insides) {
|
||||||
|
if (insides.hasOwnProperty(name) && insides[name].value === oldValue) {
|
||||||
|
insides[name].value = newValue;
|
||||||
|
wasMatch = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!wasMatch) {
|
||||||
|
throw new TypeError(`Object has no property with value: ${oldValue.toString()}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Object.create(Object.getPrototypeOf(this), insides);
|
||||||
|
}
|
||||||
|
}
|
||||||
67
src/app/storage/inner-node.ts
Normal file
67
src/app/storage/inner-node.ts
Normal file
|
|
@ -0,0 +1,67 @@
|
||||||
|
import { Node } from './node';
|
||||||
|
|
||||||
|
export abstract class InnerNode extends Node {
|
||||||
|
parent: Node;
|
||||||
|
nextVersion: this = null;
|
||||||
|
|
||||||
|
protected readonly children: Array<InnerNode> = [];
|
||||||
|
|
||||||
|
get latestVersion(): this {
|
||||||
|
let version;
|
||||||
|
for (version = this; version.nextVersion !== null; version = version.nextVersion) {
|
||||||
|
// pass
|
||||||
|
}
|
||||||
|
return version;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected constructor(parent: Node) {
|
||||||
|
super();
|
||||||
|
|
||||||
|
parent.addChild({
|
||||||
|
value: this
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
map(map: (a: this) => void) {
|
||||||
|
return this.update((self: this) => this.cloneWithMap.call(self, map));
|
||||||
|
}
|
||||||
|
|
||||||
|
changeKey(update: { value: any; propertyName: string }): this {
|
||||||
|
return this.update((self: this) => this.cloneWithAdd.call(self, update));
|
||||||
|
}
|
||||||
|
|
||||||
|
changeValue(update: { oldValue: any; newValue: any }): this {
|
||||||
|
return this.update((self: this) => this.cloneWithModify.call(self, update));
|
||||||
|
}
|
||||||
|
|
||||||
|
addChild(update: { value: InnerNode }) {
|
||||||
|
super.addChild.call(this.latestVersion, update);
|
||||||
|
}
|
||||||
|
|
||||||
|
changeChild(update: { oldValue: InnerNode; newValue: InnerNode }) {
|
||||||
|
super.changeChild.call(this.latestVersion, update);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract cloneWithMap(map: (a: this) => void): this;
|
||||||
|
protected abstract cloneWithAdd(update: { value: any; propertyName: string }): this;
|
||||||
|
protected abstract cloneWithModify(update: { oldValue: any; newValue: any }): this;
|
||||||
|
|
||||||
|
private update(cloneMethod: (self: this) => this): this {
|
||||||
|
if (this.nextVersion !== null) {
|
||||||
|
this.latestVersion.update(cloneMethod);
|
||||||
|
}
|
||||||
|
|
||||||
|
const clone = cloneMethod(this);
|
||||||
|
for (let child of clone.children) {
|
||||||
|
child.parent = clone;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.parent.changeChild({
|
||||||
|
oldValue: this,
|
||||||
|
newValue: clone
|
||||||
|
});
|
||||||
|
|
||||||
|
this.nextVersion = clone;
|
||||||
|
return clone;
|
||||||
|
}
|
||||||
|
}
|
||||||
29
src/app/storage/node.ts
Normal file
29
src/app/storage/node.ts
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
import { InnerNode } from './inner-node';
|
||||||
|
|
||||||
|
export abstract class Node {
|
||||||
|
public static id = 0;
|
||||||
|
protected abstract readonly children: Array<InnerNode>;
|
||||||
|
private id = Node.id++;
|
||||||
|
|
||||||
|
changeKey(update: { value: any; propertyName: string }) {
|
||||||
|
throw new TypeError('Not implemented!');
|
||||||
|
}
|
||||||
|
|
||||||
|
changeValue(update: { oldValue: any; newValue: any }) {
|
||||||
|
throw new TypeError('Not implemented!');
|
||||||
|
}
|
||||||
|
|
||||||
|
addChild(update: { value: InnerNode }) {
|
||||||
|
this.changeValue({
|
||||||
|
oldValue: this.children,
|
||||||
|
newValue: [...this.children, update.value]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
changeChild({ oldValue, newValue }: { oldValue: InnerNode; newValue: InnerNode }) {
|
||||||
|
this.changeValue({
|
||||||
|
oldValue: this.children,
|
||||||
|
newValue: this.children.map(c => (c === oldValue ? newValue : c))
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
28
src/app/storage/root.ts
Normal file
28
src/app/storage/root.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
import { BehaviorSubject } from 'rxjs/internal/BehaviorSubject';
|
||||||
|
import { Observable } from 'rxjs/internal/Observable';
|
||||||
|
import { Node } from './node';
|
||||||
|
import { InnerNode } from './inner-node';
|
||||||
|
|
||||||
|
export class Root<T extends InnerNode> extends Node {
|
||||||
|
private readonly _children: BehaviorSubject<Array<T>> = new BehaviorSubject([]);
|
||||||
|
readonly children$: Observable<Array<T>> = this._children.asObservable();
|
||||||
|
|
||||||
|
get children(): Array<T> {
|
||||||
|
return this._children.getValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
set children(value: Array<T>) {
|
||||||
|
this._children.next(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
changeValue({ oldValue, newValue }: { oldValue: any; newValue: any }) {
|
||||||
|
if (this.children === oldValue) {
|
||||||
|
this.children = newValue;
|
||||||
|
for (let child of this.children) {
|
||||||
|
child.parent = this;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new TypeError('Only children can be changed.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,8 +1,13 @@
|
||||||
export const hash = (text: string): number => {
|
export const hash = (text: string): number => {
|
||||||
// Return number between 0 and 1.
|
// Return number from [0, 1)
|
||||||
if (!text) {
|
if (!text) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
const hash = Array.prototype.reduce.call(text, (hash, char) => (hash << 5) - hash + char.charCodeAt(0), 7);
|
const hashValue = Array.prototype.reduce.call(
|
||||||
return hash / (Math.pow(2, 32) - 1);
|
// tslint:disable-next-line:no-bitwise
|
||||||
|
text,
|
||||||
|
(value, char) => ((value << 5) - value + (char.charCodeAt(0) as number)) | 0,
|
||||||
|
7
|
||||||
|
);
|
||||||
|
return hashValue / (Math.pow(2, 32) - 2) + 0.5;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue