few files to ts

This commit is contained in:
Alex Kaminskii 2022-05-16 20:24:58 +02:00
parent d495f1aa97
commit 8ff0249814
5 changed files with 199 additions and 149 deletions

View file

@ -1,53 +0,0 @@
//@flow
import type StatedScreen from '../StatedScreen';
import type { Message } from '../messages';
import type { Timed } from '../Timed';
import ListWalker from './ListWalker';
import DOMManager from './DOMManager';
type TimedMessage = Timed & Message;
export default class PagesManager extends ListWalker<TimedMessage> {
#currentPage: DOMManager;
#isMobile: boolean;
#screen: StatedScreen;
constructor(screen: StatedScreen, isMobile: boolean): void {
super();
this.#screen = screen;
this.#isMobile = isMobile;
}
/*
Assumed that messages added in a correct time sequence.
*/
add(m: TimedMessage): void {
if (m.tp === "create_document") {
super.append(new DOMManager(this.#screen, this.#isMobile, m.time))
}
if (this.last === null) {
// Log wrong
return;
}
this.last.append(m);
}
sort(comparator) {
this.forEach(page => page.sort(comparator))
}
moveReady(t: number): Promise<void> {
const requiredPage = this.moveGetLast(t);
if (!!requiredPage) {
this.#currentPage = requiredPage;
this.#currentPage.reset(); // Otherwise it won't apply create_document
}
if (!!this.#currentPage) {
return this.#currentPage.moveReady(t);
}
return Promise.resolve();
}
}

View file

@ -0,0 +1,50 @@
import type StatedScreen from '../StatedScreen';
import type { Message } from '../messages';
import ListWalker from './ListWalker';
import DOMManager from './DOMManager';
export default class PagesManager extends ListWalker<DOMManager> {
private currentPage: DOMManager | null = null
private isMobile: boolean;
private screen: StatedScreen;
constructor(screen: StatedScreen, isMobile: boolean) {
super()
this.screen = screen
this.isMobile = isMobile
}
/*
Assumed that messages added in a correct time sequence.
*/
add(m: Message): void {
if (m.tp === "create_document") {
super.append(new DOMManager(this.screen, this.isMobile, m.time))
}
if (this.last === null) {
// Log wrong
return;
}
this.last.append(m)
}
sort(comparator) {
this.forEach(page => page.sort(comparator))
}
moveReady(t: number): Promise<void> {
const requiredPage = this.moveGetLast(t)
if (requiredPage != null) {
this.currentPage = requiredPage
this.currentPage.reset() // Otherwise it won't apply create_document
}
if (this.currentPage != null) {
return this.currentPage.moveReady(t)
}
return Promise.resolve()
}
}

View file

@ -0,0 +1,53 @@
import { applyChange, revertChange } from 'deep-diff';
import ListWalker from './ListWalker';
import type { Redux } from '../messages';
export default class ReduxStateManager extends ListWalker<Redux> {
private state: Object = {}
private finalStates: Object[] = []
moveWasUpdated(time, index) {
super.moveApply(
time,
this.onIncrement,
this.onDecrement,
)
}
onIncrement = (item) => {
this.processRedux(item, true);
}
onDecrement = (item) => {
this.processRedux(item, false);
}
private processRedux(action, forward) {
if (forward) {
if (!!action.state) {
this.finalStates.push(this.state);
this.state = JSON.parse(JSON.stringify(action.state)); // Deep clone :(
} else {
action.diff.forEach(d => {
try {
applyChange(this.state, d);
} catch (e) {
//console.warn("Deepdiff error")
}
});
}
} else {
if (!!action.state) {
this.state = this.finalStates.pop();
} else {
action.diff.forEach(d => {
try {
revertChange(this.state, 1, d); // bad lib :( TODO: write our own diff
} catch (e) {
//console.warn("Deepdiff error")
}
});
}
}
}
}

View file

@ -1,96 +0,0 @@
// @flow
class NodeCounter {
_id: number;
_parent: NodeCount | null = null;
_count: number = 0;
_children: Array<NodeCounter> = [];
bubbleCount(count: number) {
this._count += count;
if (this._parent != null) {
this._parent.bubbleCount(count);
}
}
newChild(): NodeCounter {
const child = new NodeCounter();
this._children.push(child);
child._parent = this;
this.bubbleCount(1);
return child
}
removeChild(child: NodeCounter) {
this._children = this._children.filter(c => c != child);
this.bubbleCount(-(child._count + 1));
}
removeNode() {
this._parent.removeChild(this);
this._parent = null;
}
moveNode(newParent: NodeCounter) {
this.removeNode();
newParent._children.push(this);
this._parent = newParent;
newParent.bubbleCount(this._count + 1);
}
get count() {
return this._count;
}
}
export default class WindowNodeCounter {
_root: NodeCounter = new NodeCounter();
_nodes: Array<NodeCounter> = [ this._root ];
reset() {
this._root = new NodeCounter();
this._nodes = [ this._root ];
}
addNode(id: number, parentID: number) {
if (!this._nodes[ parentID ]) {
//TODO: iframe case
//console.error(`Wrong! Node with id ${ parentID } (parentId) not found.`);
return;
}
if (!!this._nodes[ id ]) {
//console.error(`Wrong! Node with id ${ id } already exists.`);
return;
}
this._nodes[id] = this._nodes[ parentID ].newChild();
}
removeNode(id: number) {
if (!this._nodes[ id ]) {
// Might be text node
//console.error(`Wrong! Node with id ${ id } not found.`);
return;
}
this._nodes[ id ].removeNode();
}
moveNode(id: number, parentId: number) {
if (!this._nodes[ id ]) {
console.error(`Wrong! Node with id ${ id } not found.`);
return;
}
if (!this._nodes[ parentId ]) {
console.error(`Wrong! Node with id ${ parentId } (parentId) not found.`);
return;
}
this._nodes[ id ].moveNode(this._nodes[ parentId ]);
}
get count() {
return this._root.count;
}
}

View file

@ -0,0 +1,96 @@
class NodeCounter {
private parent: NodeCounter | null = null
private _count: number = 0
private children: Array<NodeCounter> = []
bubbleCount(count: number) {
this._count += count
if (this.parent != null) {
this.parent.bubbleCount(count)
}
}
newChild(): NodeCounter {
const child = new NodeCounter()
this.children.push(child)
child.parent = this
this.bubbleCount(1)
return child
}
removeChild(child: NodeCounter) {
this.children = this.children.filter(c => c != child)
this.bubbleCount(-(child._count + 1))
}
removeNode() {
if (this.parent) {
this.parent.removeChild(this)
}
this.parent = null
}
moveNode(newParent: NodeCounter) {
this.removeNode()
newParent.children.push(this)
this.parent = newParent
newParent.bubbleCount(this._count + 1)
}
get count() {
return this._count
}
}
export default class WindowNodeCounter {
private root: NodeCounter = new NodeCounter()
private nodes: Array<NodeCounter> = [ this.root ]
reset() {
this.root = new NodeCounter()
this.nodes = [ this.root ]
}
addNode(id: number, parentID: number) {
if (!this.nodes[ parentID ]) {
//TODO: iframe case
//console.error(`Wrong! Node with id ${ parentID } (parentId) not found.`);
return
}
if (!!this.nodes[ id ]) {
//console.error(`Wrong! Node with id ${ id } already exists.`);
return
}
this.nodes[id] = this.nodes[ parentID ].newChild()
}
removeNode(id: number) {
if (!this.nodes[ id ]) {
// Might be text node
//console.error(`Wrong! Node with id ${ id } not found.`);
return
}
this.nodes[ id ].removeNode()
}
moveNode(id: number, parentId: number) {
if (!this.nodes[ id ]) {
console.error(`Wrong! Node with id ${ id } not found.`)
return
}
if (!this.nodes[ parentId ]) {
console.error(`Wrong! Node with id ${ parentId } (parentId) not found.`)
return
}
this.nodes[ id ].moveNode(this.nodes[ parentId ])
}
get count() {
return this.root.count
}
}