RocketChat / Rocket.Chat.Livechat

New Livechat client written in Preact
https://rocket.chat/omnichannel/live-chat
MIT License
246 stars 228 forks source link

Livechat.onMessage() gives message object with invalid ts after hot reload #149

Open tassoevan opened 5 years ago

tassoevan commented 5 years ago

Once the client is hot reloaded in development mode, Livechat.onMessage((message) => console.log(message.ts)) will log an invalid Date object.

renatobecker-zz commented 5 years ago

@tassoevan, I think you fixed it, right?

Livechat.onMessage(async(message) => {
    if (message.ts instanceof Date) {
        message.ts = message.ts.toISOString();
    }
...

https://github.com/RocketChat/Rocket.Chat.Livechat/blob/dev/src/routes/Chat/room.js#L92

tassoevan commented 5 years ago

@renatobecker It was more an attempt to equate the REST responses with the streamer ones. Maybe ts should be converted as Number, but that should be done on SDK. Better keep this issue open until this change is available and all ts references updated.

errorroutine commented 5 years ago

We’re running into this issue using the Rocketchat SDK as well. In our use case, we create a Livechat instance when starting the application. The user can start a chat as a guest and close it after a while. But as soon as he starts a new chat and opens a new room, the messages received onMessage have an Invalid Date for ts. As a workaround, we’re using the _updatedAt.$date for now, but it would be great if this could be solved in a nicer way in the future.

DonDonDonDonDonDon commented 3 years ago

This is because hot updates are ‘onMessage’ many times.Fixed it like this.

if(!window.haveLiveChatOnMessage ){
  window.haveLiveChatOnMessage = true
  Livechat.onMessage(async (message) => {
      //do something

  });
}