performace

This commit is contained in:
Peter Maquiran
2023-09-29 16:40:50 +01:00
parent 7f1dbf13aa
commit 93ad41eab9
12 changed files with 175 additions and 106 deletions
+29 -25
View File
@@ -206,19 +206,15 @@ export class ChatSystemService {
async restoreRooms() {
try {
const rooms = await this.storage.get('Rooms');
const _rooms = await this.storage.get('Rooms');
if(rooms) {
for (let roomData of rooms.result.update) {
if(_rooms) {
for (let roomData of this.sortArrayISODate(_rooms)) {
await this.prepareRoom(roomData);
}
}
} catch(e){}
setTimeout(() => {
this.sortRoomList()
}, 10000)
}
async restoreUsers () {
@@ -230,6 +226,14 @@ export class ChatSystemService {
}
}
sortArrayISODate(messages: any): any[] {
return messages.sort((a,b) =>
new Date(b._updatedAt ).getTime()
-
new Date(a._updatedAt).getTime())
}
async getAllRooms (callback:Function = () =>{} , roomIdCallback = "") {
this.loadingWholeList = true
var rooms;
@@ -248,8 +252,17 @@ export class ChatSystemService {
let index = 0
if(rooms?.result?.update) {
for (let roomData of rooms.result.update) {
let _rooms = rooms?.result?.update
if(_rooms) {
_rooms = _rooms.map( e => {
e["_updatedAt"] = e._updatedAt || e._updatedAt['$date']
return e
})
for (let roomData of this.sortArrayISODate(_rooms) ) {
const roomId = this.getRoomId(roomData);
@@ -272,8 +285,8 @@ export class ChatSystemService {
const members = res['members'];
const users = members.filter(data => data.username != this.sessionStore.user.UserName);
rooms.result.update[index]['members'] = members
rooms.result.update[index]['membersExcludeMe'] = users
_rooms[index]['members'] = members
_rooms[index]['membersExcludeMe'] = users
await this.prepareRoom(roomData);
} else {
@@ -295,8 +308,8 @@ export class ChatSystemService {
const members = res['members'];
const users = members.filter(data => data.username != this.sessionStore.user.UserName);
rooms.result.update[index]['members'] = members
rooms.result.update[index]['membersExcludeMe'] = users
_rooms[index]['members'] = members
_rooms[index]['membersExcludeMe'] = users
await this.prepareRoom(roomData);
@@ -322,8 +335,8 @@ export class ChatSystemService {
const members = res['members'];
const users = members.filter(data => data.username != this.sessionStore.user.UserName);
rooms.result.update[index]['members'] = members
rooms.result.update[index]['membersExcludeMe'] = users
_rooms[index]['members'] = members
_rooms[index]['membersExcludeMe'] = users
await this.prepareRoom(roomData);
}
@@ -339,17 +352,8 @@ export class ChatSystemService {
}
this.loadingWholeList = false
await this.storage.set('Rooms', rooms);
this.sortRoomList()
setTimeout(() => {
this.sortRoomList()
}, 1000)
setTimeout(() => {
this.sortRoomList()
}, 10000)
await this.storage.set('Rooms', _rooms);
this.onRoomsLoad.executor()
}
+28 -13
View File
@@ -220,6 +220,7 @@ export class MessageService {
this.RochetChatConnectorService.registerCallback({
type: 'reConnect',
funx: async () => {
console.log('send now have login')
this.send().catch((error) =>{
console.error(error)
})
@@ -227,7 +228,16 @@ export class MessageService {
}
})
console.log('no login')
} else if (this.UploadAttachmentsTemp <= 3) {
console.log('temp'+ this.UploadAttachmentsTemp)
setTimeout(async () => {
return await this.send()
}, 4000)
} else if(this.NetworkServiceService.getCurrentNetworkStatus() == ConnectionStatus.Offline) {
console.log('recooonect')
this.RochetChatConnectorService.registerCallback({
type: 'reConnect',
funx: async ()=> {
@@ -238,11 +248,8 @@ export class MessageService {
return true
}
})
} else if (this.UploadAttachmentsTemp <= 3) {
setTimeout(async () => {
return await this.send()
}, 3000)
} else if (this.NetworkServiceService.getCurrentNetworkStatus() == ConnectionStatus.Online) {
console.log('manual')
this.manualRetry = true
}
@@ -256,6 +263,7 @@ export class MessageService {
async sendRequestAttachment() {
console.log('sendRequestAttachment')
this.uploadingFile = true
let uploadSuccessfully = false
@@ -270,23 +278,30 @@ export class MessageService {
await this.generateTemporaryData()
}
console.log('send temp')
if(this.hasSendAttachment == false) {
try {
uploadSuccessfully = await this.NfService.beforeSendAttachment(this)
this.UploadAttachmentsTemp++
this.uploadingFile = false
this.manualRetry = false
this.errorUploadingAttachment = false
this.hasSendAttachment = true
this.UploadAttachmentsTemp++
uploadSuccessfully = await this.NfService.beforeSendAttachment(this)
this.uploadingFile = false
this.hasSendAttachment = uploadSuccessfully
if(this.hasSendAttachment) {
console.log('send uploadSuccessfully')
} else {
console.log('not send uploadSuccessfully')
}
this.saveChanges()
} catch (error) {
this.uploadingFile = false
console.log('send not uploadSuccessfully')
this.errorUploadingAttachment = true
this.UploadAttachmentsTemp++
this.saveChanges()
}
@@ -409,14 +424,14 @@ export class MessageService {
if(!roomObject.isGroup) {
var memeberTosend = this.ChatSystemService.getRoomById(this.rid).membersExcludeMe
this.notificationService.ChatSendMessageNotification(memeberTosend[0].username,ChatMessage.u.name,ChatMessage.msg,this.rid)
this.notificationService.ChatSendMessageNotification(memeberTosend[0].username,ChatMessage.u.name,ChatMessage.msg || "Anexo",this.rid)
} else {
var memeberTosend = this.ChatSystemService.getRoomById(this.rid).membersExcludeMe
var usersNames = [];
for(let i = 0; i < memeberTosend.length; i++) {
usersNames.push(memeberTosend[i].username)
}
this.notificationService.ChatSendMessageNotificationGrup(usersNames,ChatMessage.u.name,ChatMessage.msg,this.rid)
this.notificationService.ChatSendMessageNotificationGrup(usersNames,ChatMessage.u.name,ChatMessage.msg || "Anexo",this.rid)
}
@@ -766,6 +766,7 @@ export class RochetChatConnectorService {
setTimeout(()=>{
this.ws.wsMsgQueue()
}, 10)
console.log('rec conect')
if(this.wsReconnect >= 1) {
for (const [key, value] of Object.entries(this.wsCallbacks)) {
+56 -7
View File
@@ -42,6 +42,7 @@ export class RoomService {
name = ''
_updatedAt = {}
hasLoadHistory = false
DoneLoadingHistory = false
restoreFromOffline = false
duration = ''
isTyping = false
@@ -327,7 +328,7 @@ export class RoomService {
this.registerSendMessage(message)
message.from = 'stream'
message.loadHistory = this.hasLoadHistory
message.loadHistory = this.DoneLoadingHistory
this.lastMessage = message;
this.calDateDuration(ChatMessage._updatedAt);
@@ -352,6 +353,7 @@ export class RoomService {
this.messageUnread = true
// this.sortRoomList()
setTimeout(() => {
this.scrollDown()
}, 50)
@@ -550,6 +552,30 @@ export class RoomService {
}
/**
* @description delete message in the view
* @param id message ID
*/
async deleteMessageFromArray(_id) {
const id = _id
for (let i =0; i <= this.messages.length; i++) {
if(this.messages[i]?._id == id ) {
this.messages.splice(i, 1)
return true
} else {
//
}
}
}
deleteAll() {
this.messages.forEach((message) => {
if(message?._id) {
@@ -619,7 +645,7 @@ export class RoomService {
}
console.log('offlineChatMessage', offlineChatMessage)
//console.log('offlineChatMessage', offlineChatMessage)
this.message= ''
@@ -792,7 +818,7 @@ export class RoomService {
this.messages.push(cloneMessage as any)
console.log(currentDateMessage)
// console.log(currentDateMessage)
}
}
}
@@ -851,9 +877,6 @@ export class RoomService {
const messages = this.sortArrayISODate(await MessageModel.filter({rid:this.id}).execute()).reverse()
console.log('MessageModel', messages)
for (let ChatMessage of messages) {
@@ -907,6 +930,7 @@ export class RoomService {
// runs onces only
loadHistoryCount = 0
localMessages = []
async loadHistory({limit = 1000, forceUpdate = false }) {
if(forceUpdate == false) {
@@ -920,6 +944,8 @@ export class RoomService {
await this.restoreMessageFromDB()
const chatHistory: chatHistory = await this.RochetChatConnectorService.loadHistory(this.id, limit)
this.localMessages = this.messages.map( e => e._id)
if(chatHistory?.result?.messages) {
const users = this.getUsersByStatus('online')
@@ -955,6 +981,8 @@ export class RoomService {
}
this.DoneLoadingHistory = true
if(chatHistory?.result?.messages) {
if(!this.lastMessage) {
@@ -962,10 +990,31 @@ export class RoomService {
}
}
this.deletedMessages(chatHistory)
}
deletedMessages(chatHistory: chatHistory) {
const messagesToDelete = this.localMessages.filter( id => {
const found = chatHistory.result.messages.find( e => {
return e._id == id
})
if(!found) {
return true
}
return false
})
for (const id of messagesToDelete) {
if(id != '') {
this.deleteMessage(id)
}
}
}
addMessageDB = FIFOProcessQueue(async ({messagesToSave}, callback) => {
await messagesToSave.addMessageDB()