combatch / twitch-chat-enhanced

A custom chat window with extra features to increase user interaction with viewers
2 stars 1 forks source link

Clear old messages #2

Closed paOol closed 3 years ago

paOol commented 3 years ago

instead of letting the state hold an array of infinite items,

the older messages should be removed to maintain a max array size of XX.

the logic should be something like

once the length of messages exceeds XX, remove X amount of oldest items in the array, and update the state.

K5qu4r3d commented 3 years ago

How many messages should the array hold before starting to remove them? Also what is the amount of time where a message is considered "old"?

paOol commented 3 years ago

I've left it running over night and had no memory issues, so we can be a bit on the conservative side regarding how many messages to store.

Kinda arbitrary, but maybe 400 msgs?

"old" doesn't refer to duration, but rather anything past 400 for example.

K5qu4r3d commented 3 years ago

Here's what I have for a solution: when the array of messages exceeds 400, return the last 400 items of the array.

// ChatMessage.tsx

...
const ChatMessage: React.FunctionComponent<IChatmessageProps> = ({ messages }: IChatmessageProps) => {
  return (
    <React.Fragment>
      {(messages.length > 400
        ? messages.slice(Math.max(messages.length - 400, 0))
        : messages)
      .map((x: Chat, i: number) => {...})}
  </React.Fragment>
  );
};

See PR #16