openreplay/frontend/app/player/common/ListWalker.ts
Delirium 35461acaf3
[WIP] Mobile replayer (#1452)
* fix(ui): fix up mobile recordings display

* fix(ui): some messages

* fix(ui): some messages

* fix(player): fix msg generation for ios messages

* feat(player): add generic mmanager interface for ios player impl

* feat(player): mobile player and message manager; touch manager; videoplayer

* feat(player): add iphone shells, add log panel,

* feat(player): detect ios sessions and inject correct player

* feat(player): move screen mapper to utils

* feat(player): events panel for mobile, map shell sizes to device type data,

* feat(player): added network tab to mobile player; unify network message (ios and web)

* feat(player): resize canvas up to phone screen size, fix capitalize util

* feat(player): some general changes to support mobile events and network entries

* feat(player): remove swipes from timeline

* feat(player): more stuff for list walker

* fix(ui): performance tab, mobile project typings and form

* fix(ui):some ui touches for ios replayer shell

* fix(ui): more fixes for ui, new onboarding screen for mobile projects

* feat(ui): mobile overview panel (xray)

* feat(ui): fixes for phone shell and tap events

* fix(tracker): change phone shells and sizes

* fix(tracker): fix border on replay screen

* feat(ui): use crashes from db to show in session

* feat(ui): use event name for xray

* feat(ui): some overall ui fixes

* feat(ui): IOS -> iOS

* feat(ui): change tags to ant d

* fix(ui): fast fix

* fix(ui): fix for capitalizer

* fix(ui): fix for browser display

* fix(ui): fix for note popup

* fix(ui): change exceptions display

* fix(ui): add click rage to ios xray

* fix(ui): some icons and resizing

* fix(ui): fix ios context menu overlay, fix console logs creation for ios

* feat(ui): added icons

* feat(ui): performance warnings

* feat(ui): performance warnings

* feat(ui): different styles

* feat(ui): rm debug true

* feat(ui): fix warnings display

* feat(ui): some styles for animation

* feat(ui): add some animations to warnings

* feat(ui): move perf warnings to performance graph

* feat(ui): hide/show warns dynamically

* feat(ui): new mobile touch animation

* fix(tracker): update msg for ios

* fix(ui): taprage fixes

* fix(ui): regenerate icons and messages

* fix(ui): fix msgs

* fix(backend): fix events gen

* fix(backend): fix userid msg
2023-10-27 12:12:09 +02:00

158 lines
3.3 KiB
TypeScript

import type { Timed } from 'Player';
export default class ListWalker<T extends Timed> {
/* Pointer to the "current" item */
private p = 0
constructor(private _list: Array<T> = []) {}
append(m: T): void {
if (this.length > 0 && this.last && m.time < this.last.time) {
console.error("Trying to append message with the less time then the list tail:", m.time, 'vs', this.last.time, m, this)
return
}
this.list.push(m);
}
unshift(m: T): void {
this.list.unshift(m)
}
insert(m: T): void {
let index = this.list.findIndex(om => om.time > m.time)
if (index === -1) {
index = this.length
}
const oldList = this.list
this._list = [...oldList.slice(0, index), m, ...oldList.slice(index)]
}
reset(): void {
this.p = 0
}
sort(comparator: (a: T, b: T) => number): void {
// @ts-ignore
this.list.sort((m1,m2) => comparator(m1,m2) || (m1._index - m2._index) ); // indexes for sort stability (TODO: fix types???)
}
forEach(f: (item: T) => void):void {
this.list.forEach(f);
}
get last(): T | null {
if (this.list.length === 0) {
return null;
}
return this.list.slice(-1)[0];
}
get current(): T | null {
if (this.p === 0) {
return null;
}
return this.list[ this.p - 1 ];
}
get timeNow(): number {
if (this.p === 0) {
return 0;
}
return this.list[ this.p - 1 ].time;
}
get length(): number {
return this.list.length;
}
get maxTime(): number {
if (this.length === 0) {
return 0;
}
return this.list[ this.length - 1 ].time;
}
get minTime(): number {
if (this.length === 0) {
return 0;
}
return this.list[ 0 ].time;
}
get listNow(): Array<T> {
return this.list.slice(0, this.p);
}
get list(): Array<T> {
return this._list;
}
get count(): number {
return this.length;
}
get countNow(): number {
return this.p;
}
private hasNext() {
return this.p < this.length
}
private hasPrev() {
return this.p > 0
}
protected moveNext(): T | null {
return this.hasNext()
? this.list[ this.p++ ]
: null
}
protected movePrev(): T | null {
return this.hasPrev()
? this.list[ --this.p ]
: null
}
/**
* @returns last message with the time <= t.
* Assumed that the current message is already handled so
* if pointer doesn't change <null> is returned.
*/
moveGetLast(t: number, index?: number): T | null {
let key: string = "time"; //TODO
let val = t;
if (index) {
key = "_index";
val = index;
}
let changed = false;
// @ts-ignore
while (this.p < this.length && this.list[this.p][key] <= val) {
this.moveNext()
changed = true;
}
// @ts-ignore
while (this.p > 0 && this.list[ this.p - 1 ][key] > val) {
this.movePrev()
changed = true;
}
return changed ? this.list[ this.p - 1 ] : null;
}
/**
* Moves over the messages starting from the current+1 to the last one with the time <= t
* applying callback on each of them
* @param t - max message time to move to; will move & apply callback while msg.time <= t
* @param callback - a callback to apply on each message passing by while moving
*/
moveApply(t: number, callback: (msg: T) => void): void {
// Applying only in increment order for now
if (t < this.timeNow) {
this.reset();
}
const list = this.list
while (list[this.p] && list[this.p].time <= t) {
callback(this.list[ this.p++ ])
}
}
}