fixup! refactor(frontend/player):player lists
This commit is contained in:
parent
f31ae3efa0
commit
695a414caf
6 changed files with 0 additions and 246 deletions
|
|
@ -1,4 +1,3 @@
|
|||
import { goTo as listsGoTo } from './lists';
|
||||
import { update, getState } from './store';
|
||||
import MessageDistributor, { INITIAL_STATE as SUPER_INITIAL_STATE } from './MessageDistributor/MessageDistributor';
|
||||
import { Note } from 'App/services/NotesService';
|
||||
|
|
@ -68,7 +67,6 @@ export default class Player extends MessageDistributor {
|
|||
completed: false,
|
||||
});
|
||||
super.move(time, index);
|
||||
listsGoTo(time, index);
|
||||
}
|
||||
|
||||
private _startAnimation() {
|
||||
|
|
|
|||
|
|
@ -1,124 +0,0 @@
|
|||
export default class ListReader {
|
||||
_callback;
|
||||
_p = -1;
|
||||
_list = [];
|
||||
_offset = 0;
|
||||
|
||||
constructor(callback = Function.prototype) {
|
||||
if (typeof callback !== 'function') {
|
||||
return console.error("List Reader: wrong constructor argument. `callback` must be a function.");
|
||||
}
|
||||
this._callback = callback;
|
||||
}
|
||||
|
||||
static checkItem(item) {
|
||||
if(typeof item !== 'object' || item === null) {
|
||||
console.error("List Reader: expected item to be not null object but got ", item);
|
||||
return false;
|
||||
}
|
||||
if (typeof item.time !== 'number') {
|
||||
console.error("List Reader: expected item to have number property 'time', ", item);
|
||||
return false;
|
||||
}
|
||||
// if (typeof item.index !== 'number') {
|
||||
// console.error("List Reader: expected item to have number property 'index', ", item);
|
||||
// return false;
|
||||
// } // future: All will have index
|
||||
return true;
|
||||
}
|
||||
/* EXTENDABLE METHODS */
|
||||
_onIncrement() {}
|
||||
_onDecrement() {}
|
||||
_onStartTimeChange() {}
|
||||
|
||||
inc() {
|
||||
const item = this._list[ ++this._p ];
|
||||
this._onIncrement(item);
|
||||
return item;
|
||||
}
|
||||
|
||||
dec() {
|
||||
const item = this._list[ this._p-- ];
|
||||
this._onDecrement(item);
|
||||
return item
|
||||
}
|
||||
|
||||
get _goToReturn() {
|
||||
return { listNow: this.listNow };
|
||||
}
|
||||
|
||||
goTo(time) {
|
||||
const prevPointer = this._p;
|
||||
while (!!this._list[ this._p + 1 ] && this._list[ this._p + 1 ].time <= time) {
|
||||
this.inc();
|
||||
}
|
||||
while (this._p >= 0 && this._list[ this._p ].time > time) {
|
||||
this.dec();
|
||||
}
|
||||
if (prevPointer !== this._p) {
|
||||
//this._notify([ "listNow" ]);
|
||||
return this._goToReturn;
|
||||
}
|
||||
}
|
||||
|
||||
goToIndex(index) { // thinkaboutit
|
||||
const prevPointer = this._p;
|
||||
while (!!this._list[ this._p + 1 ] &&
|
||||
this._list[ this._p + 1 ].index <= index
|
||||
) {
|
||||
this.inc();
|
||||
}
|
||||
while (this._p >= 0 && this._list[ this._p ].index > index) {
|
||||
this.dec();
|
||||
}
|
||||
if (prevPointer !== this._p) {
|
||||
//this._notify([ "listNow" ]);
|
||||
return this._goToReturn;
|
||||
}
|
||||
}
|
||||
|
||||
// happens rare MBTODO only in class ResourceListReader extends ListReaderWithRed
|
||||
set startTime(time) {
|
||||
const prevOffset = this._offset;
|
||||
const prevPointer = this._p;
|
||||
this._offset = this._list.findIndex(({ time, duration = 0 }) => time + duration >= time); // TODO: strict for duration rrrrr
|
||||
this._p = Math.max(this._p, this._offset - 1);
|
||||
if (prevOffset !== this._offset || prevPointer !== this._p) {
|
||||
this._notify([ "listNow" ]);
|
||||
}
|
||||
this._onStartTimeChange();
|
||||
}
|
||||
|
||||
get list() {
|
||||
return this._list;
|
||||
}
|
||||
get count() {
|
||||
return this._list.length;
|
||||
}
|
||||
get listNow() {
|
||||
return this._list.slice(this._offset, this._p + 1);
|
||||
}
|
||||
|
||||
set list(_list) {
|
||||
if (!Array.isArray(_list)) {
|
||||
console.error("List Reader: wrong list value.", _list)
|
||||
}
|
||||
const valid = _list.every(this.constructor.checkItem);
|
||||
if (!valid) return;
|
||||
this._list = _list; // future: time + index sort
|
||||
this._notify([ "list", "count" ]);
|
||||
}
|
||||
|
||||
append(item) {
|
||||
if (!this.constructor.checkItem(item)) return;
|
||||
this._list.push(item); // future: time + index sort
|
||||
this._notify([ "count" ]); // list is the same by ref, CAREFULL
|
||||
}
|
||||
|
||||
_notify(propertyList) {
|
||||
const changedState = {};
|
||||
propertyList.forEach(p => changedState[ p ] = this[ p ]);
|
||||
this._callback(changedState);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,48 +0,0 @@
|
|||
import ListReader from './ListReader';
|
||||
|
||||
export default class ListReaderWithRed extends ListReader {
|
||||
_redCountNow = 0;
|
||||
|
||||
static checkItem(item) {
|
||||
const superCheckResult = super.checkItem(item);
|
||||
if (typeof item.isRed !== 'function') {
|
||||
console.error("List Reader With Red: expected item to have method 'isRed', ", item);
|
||||
return false;
|
||||
}
|
||||
return superCheckResult;
|
||||
}
|
||||
|
||||
get _goToReturn() {
|
||||
return {
|
||||
listNow: this.listNow,
|
||||
redCountNow: this.redCountNow,
|
||||
}
|
||||
}
|
||||
|
||||
_onIncrement(item) {
|
||||
if (item.isRed()) {
|
||||
this._redCountNow++;
|
||||
//this._notify([ "redCountNow" ]);
|
||||
}
|
||||
}
|
||||
|
||||
_onDecrement(item) {
|
||||
if (item.isRed()) {
|
||||
this._redCountNow--;
|
||||
//this._notify([ "redCountNow" ]);
|
||||
}
|
||||
}
|
||||
|
||||
_onStartTimeChange() {
|
||||
this._redCountNow = this._list
|
||||
.slice(this._offset, this._p + 1)
|
||||
.filter(item => item.isRed())
|
||||
.length;
|
||||
this._notify([ "redCountNow" ]);
|
||||
}
|
||||
|
||||
get redCountNow() {
|
||||
return this._redCountNow;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,68 +0,0 @@
|
|||
import ListReader from './ListReader';
|
||||
import ListReaderWithRed from './ListReaderWithRed';
|
||||
import { update as updateStore } from '../store';
|
||||
|
||||
const l = n => `${ n }List`;
|
||||
const c = n => `${ n }Count`;
|
||||
const ln = n => `${ n }ListNow`;
|
||||
const rcn = n => `${ n }RedCountNow`;
|
||||
|
||||
const entityNamesWithRed = [ "log", "resource", "fetch", "stack" ];
|
||||
const entityNamesSimple = [ "event", "profile" ];
|
||||
const entityNames = /*[ "redux" ].*/entityNamesWithRed.concat(entityNamesSimple);
|
||||
|
||||
const is = {};
|
||||
entityNames.forEach(n => {
|
||||
is[ l(n) ] = [];
|
||||
is[ c(n) ] = 0;
|
||||
is[ ln(n) ] = [];
|
||||
if (entityNamesWithRed.includes(n)) {
|
||||
is[ rcn(n) ] = 0;
|
||||
}
|
||||
});
|
||||
//is["reduxState"] = {};
|
||||
//is["reduxFinalStates"] = [];
|
||||
|
||||
|
||||
const createCallback = n => {
|
||||
const entityfy = s => `${ n }${ s[ 0 ].toUpperCase() }${ s.slice(1) }`;
|
||||
return state => {
|
||||
if (!state) return;
|
||||
const namedState = {};
|
||||
Object.keys(state).forEach(key => {
|
||||
namedState[ entityfy(key) ] = state[ key ];
|
||||
});
|
||||
return updateStore(namedState);
|
||||
}
|
||||
}
|
||||
|
||||
let readers = null;
|
||||
|
||||
export function init(lists) {
|
||||
readers = {};
|
||||
entityNamesSimple.forEach(n => readers[ n ] = new ListReader(createCallback(n)));
|
||||
entityNamesWithRed.forEach(n => readers[ n ] = new ListReaderWithRed(createCallback(n)));
|
||||
|
||||
entityNames.forEach(n => readers[ n ].list = lists[ n ] || []);
|
||||
}
|
||||
export function append(name, item) {
|
||||
readers[ name ].append(item);
|
||||
}
|
||||
export function setStartTime(time) {
|
||||
readers.resource.startTime = time;
|
||||
}
|
||||
const byTimeNames = [ "event", "stack" ]; // TEMP
|
||||
const byIndexNames = entityNames.filter(n => !byTimeNames.includes(n));
|
||||
export function goTo(time, index) {
|
||||
if (readers === null) return;
|
||||
if (typeof index === 'number') {
|
||||
byTimeNames.forEach(n => readers[ n ] && readers[ n ]._callback(readers[ n ].goTo(time)));
|
||||
byIndexNames.forEach(n => readers[ n ] && readers[ n ]._callback(readers[ n ].goToIndex(index)));
|
||||
} else {
|
||||
entityNames.forEach(n => readers[ n ] && readers[ n ]._callback(readers[ n ].goTo(time)));
|
||||
}
|
||||
}
|
||||
export function clean() {
|
||||
entityNames.forEach(n => delete readers[ n ]);
|
||||
}
|
||||
export const INITIAL_STATE = is;
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
import Player from './Player';
|
||||
import { update, cleanStore, getState } from './store';
|
||||
import { clean as cleanLists } from './lists';
|
||||
|
||||
/** @type {Player} */
|
||||
let instance = null;
|
||||
|
|
@ -49,7 +48,6 @@ export function clean() {
|
|||
if (instance === null) return;
|
||||
instance.clean();
|
||||
cleanStore();
|
||||
cleanLists();
|
||||
instance = null;
|
||||
}
|
||||
export const jump = initCheck((...args) => instance.jump(...args));
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { applyChange, revertChange } from 'deep-diff';
|
||||
import { INITIAL_STATE as listsInitialState } from '../lists';
|
||||
import { INITIAL_STATE as playerInitialState, INITIAL_NON_RESETABLE_STATE as playerInitialNonResetableState } from '../Player';
|
||||
|
||||
const UPDATE = 'player/UPDATE';
|
||||
|
|
@ -7,7 +6,6 @@ const CLEAN = 'player/CLEAN';
|
|||
const REDUX = 'player/REDUX';
|
||||
|
||||
const resetState = {
|
||||
...listsInitialState,
|
||||
...playerInitialState,
|
||||
initialized: false,
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue