change(player): only recompute list on change

This commit is contained in:
nick-delirium 2023-03-06 15:26:02 +01:00 committed by Delirium
parent 65a66b9c59
commit 1cd1f72310
3 changed files with 59 additions and 5 deletions

View file

@ -65,6 +65,12 @@ function Storage(props: Props) {
return { ...pureMSG, ...decoded }; return { ...pureMSG, ...decoded };
} }
const decodedList = React.useMemo(() => {
return listNow.map(msg => {
return decodeMessage(msg)
})
}, [listNow.length])
const focusNextButton = () => { const focusNextButton = () => {
if (lastBtnRef.current) { if (lastBtnRef.current) {
lastBtnRef.current.focus(); lastBtnRef.current.focus();
@ -138,8 +144,8 @@ function Storage(props: Props) {
let src; let src;
let name; let name;
const prevItemD = prevItem ? decodeMessage(prevItem) : undefined const itemD = item
const itemD = decodeMessage(item) const prevItemD = prevItem ? prevItem : undefined
switch (type) { switch (type) {
case STORAGE_TYPES.REDUX: case STORAGE_TYPES.REDUX:
@ -322,8 +328,8 @@ function Storage(props: Props) {
)} )}
<div className="flex" style={{ width: showStore ? '75%' : '100%' }}> <div className="flex" style={{ width: showStore ? '75%' : '100%' }}>
<Autoscroll className="ph-10"> <Autoscroll className="ph-10">
{listNow.map((item: Record<string, any>, i: number) => {decodedList.map((item: Record<string, any>, i: number) =>
renderItem(item, i, i > 0 ? listNow[i - 1] : undefined) renderItem(item, i, i > 0 ? decodedList[i - 1] : undefined)
)} )}
</Autoscroll> </Autoscroll>
</div> </div>
@ -341,3 +347,47 @@ export default connect(
hideHint, hideHint,
} }
)(observer(Storage)); )(observer(Storage));
/**
* TODO: compute diff and only decode the required parts
* WIP example
* function useStorageDecryptedList(list: Record<string, any>[], type: string, player: IWebPlayer) {
* const [decryptedList, setDecryptedList] = React.useState(list);
* const [listLength, setLength] = React.useState(list.length)
*
* const decodeMessage = (msg: any, type: StorageType) => {
* const decoded = {};
* const pureMSG = { ...msg }
* const keys = storageDecodeKeys[type];
* try {
* keys.forEach(key => {
* if (pureMSG[key]) {
* // @ts-ignore TODO: types for decoder
* decoded[key] = player.decodeMessage(pureMSG[key]);
* }
* });
* } catch (e) {
* logger.error("Error on message decoding: ", e, pureMSG);
* return null;
* }
* return { ...pureMSG, ...decoded };
* }
*
* React.useEffect(() => {
* if (list.length !== listLength) {
* const last = list[list.length - 1]._index;
* let diff;
* if (last < decryptedList[decryptedList.length - 1]._index) {
*
* }
* diff = list.filter(item => !decryptedList.includes(i => i._index === item._index))
* const decryptedDiff = diff.map(item => {
* return player.decodeMessage(item)
* })
* const result =
* }
* }, [list.length])
* }
*
* */

View file

@ -463,6 +463,10 @@ export default class MessageManager {
this.state.update({ messagesLoading, ready: !messagesLoading && !this.state.get().cssLoading }); this.state.update({ messagesLoading, ready: !messagesLoading && !this.state.get().cssLoading });
} }
decodeMessage(msg: Message) {
return this.decoder.decode(msg)
}
private setSize({ height, width }: { height: number, width: number }) { private setSize({ height, width }: { height: number, width: number }) {
this.screen.scale({ height, width }); this.screen.scale({ height, width });
this.state.update({ width, height }); this.state.update({ width, height });

View file

@ -86,7 +86,7 @@ export default class WebPlayer extends Player {
// delayed message decoding for state plugins // delayed message decoding for state plugins
decodeMessage = (msg: Message) => { decodeMessage = (msg: Message) => {
return this.messageManager.decoder.decode(msg) return this.messageManager.decodeMessage(msg)
} }
// Inspector & marker // Inspector & marker