diff --git a/src/lib/appManagers/apiUpdatesManager.ts b/src/lib/appManagers/apiUpdatesManager.ts index f66bd8c4..27184691 100644 --- a/src/lib/appManagers/apiUpdatesManager.ts +++ b/src/lib/appManagers/apiUpdatesManager.ts @@ -1,5 +1,6 @@ //import apiManager from '../mtproto/apiManager'; import DEBUG, { MOUNT_CLASS_TO } from '../../config/debug'; +import { copy } from '../../helpers/object'; import { logger, LogLevels } from '../logger'; import apiManager from '../mtproto/mtprotoworker'; import rootScope from '../rootScope'; @@ -41,18 +42,6 @@ export class ApiUpdatesManager { private log = logger('UPDATES', LogLevels.error | LogLevels.log | LogLevels.warn | LogLevels.debug); private debug = DEBUG; - constructor() { - // * false for test purposes - /* false && */appStateManager.addListener('save', async() => { - const us = this.updatesState; - appStateManager.pushToState('updates', { - seq: us.seq, - pts: us.pts, - date: us.date - }); - }); - } - public popPendingSeqUpdate() { const state = this.updatesState; const nextSeq = state.seq + 1; @@ -578,16 +567,18 @@ export class ApiUpdatesManager { if(this.attached) return; //return; + + this.log('attach'); this.attached = true; appStateManager.getState().then(_state => { const state = _state.updates; - apiManager.setUpdatesProcessor(this.processUpdateMessage); - //rootScope.broadcast('state_synchronizing'); if(!state || !state.pts || !state.date || !state.seq) { + this.log('will get new state'); + this.updatesState.syncLoading = new Promise((resolve) => { apiManager.invokeApi('updates.getState', {}, {noErrorBox: true}).then((stateResult) => { this.updatesState.seq = stateResult.seq; @@ -614,12 +605,28 @@ export class ApiUpdatesManager { Object.assign(this.updatesState, state); + this.log('will get difference', copy(state)); + this.getDifference(true)/* .finally(() => { if(this.updatesState.syncLoading) { rootScope.broadcast('state_synchronizing'); } }) */; } + + apiManager.setUpdatesProcessor(this.processUpdateMessage); + + this.updatesState.syncLoading.then(() => { + // * false for test purposes + /* false && */appStateManager.addListener('save', async() => { + const us = this.updatesState; + appStateManager.pushToState('updates', { + seq: us.seq, + pts: us.pts, + date: us.date + }); + }); + }); }); } } diff --git a/src/lib/appManagers/appDraftsManager.ts b/src/lib/appManagers/appDraftsManager.ts index f5ba0dad..2bf616d1 100644 --- a/src/lib/appManagers/appDraftsManager.ts +++ b/src/lib/appManagers/appDraftsManager.ts @@ -21,10 +21,10 @@ export class AppDraftsManager { constructor() { appStateManager.getState().then(state => { this.drafts = state.drafts; - }); - appStateManager.addListener('save', async() => { - appStateManager.pushToState('drafts', this.drafts); + appStateManager.addListener('save', async() => { + appStateManager.pushToState('drafts', this.drafts); + }); }); rootScope.on('apiUpdate', (update) => { diff --git a/src/lib/appManagers/appMessagesManager.ts b/src/lib/appManagers/appMessagesManager.ts index 8672d6d6..70a94953 100644 --- a/src/lib/appManagers/appMessagesManager.ts +++ b/src/lib/appManagers/appMessagesManager.ts @@ -237,68 +237,6 @@ export class AppMessagesManager { } }); - appStateManager.addListener('save', () => { - const messages: any[] = []; - const dialogs: Dialog[] = []; - const items: any[] = []; - - const processDialog = (dialog: MTDialog.dialog) => { - const historyStorage = this.getHistoryStorage(dialog.peerId); - const history = [].concat(historyStorage.history.slice); - //dialog = copy(dialog); - let removeUnread = 0; - for(const mid of history) { - const message = this.getMessageByPeer(dialog.peerId, mid); - if(/* message._ !== 'messageEmpty' && */!message.pFlags.is_outgoing) { - messages.push(message); - - if(message.fromId !== dialog.peerId) { - appStateManager.setPeer(message.fromId, appPeersManager.getPeer(message.fromId)); - } - - /* dialog.top_message = message.mid; - this.dialogsStorage.generateIndexForDialog(dialog, false, message); */ - - break; - } else if(message.pFlags && message.pFlags.unread) { - ++removeUnread; - } - } - - if(removeUnread && dialog.unread_count) dialog.unread_count -= removeUnread; - - if(dialog.peerId < 0 && dialog.pts) { - const newPts = apiUpdatesManager.channelStates[-dialog.peerId].pts; - dialog.pts = newPts; - } - - dialog.unread_count = Math.max(0, dialog.unread_count); - dialogs.push(dialog); - - appStateManager.setPeer(dialog.peerId, appPeersManager.getPeer(dialog.peerId)); - }; - - for(const folderId in this.dialogsStorage.byFolders) { - const folder = this.dialogsStorage.getFolder(+folderId); - - for(let dialog of folder) { - items.push([dialog]); - } - } - - return pushHeavyTask({ - items, - process: processDialog, - context: this - }).then(() => { - appStateManager.pushToState('dialogs', dialogs); - appStateManager.pushToState('messages', messages); - appStateManager.pushToState('filters', this.filtersStorage.filters); - appStateManager.pushToState('allDialogsLoaded', this.dialogsStorage.allDialogsLoaded); - appStateManager.pushToState('maxSeenMsgId', this.maxSeenId); - }); - }); - appStateManager.getState().then(state => { if(state.maxSeenMsgId) { this.maxSeenId = state.maxSeenMsgId; @@ -349,11 +287,75 @@ export class AppMessagesManager { } }); } + + appStateManager.addListener('save', this.saveState); }); appNotificationsManager.start(); } + private saveState = () => { + const messages: any[] = []; + const dialogs: Dialog[] = []; + const items: any[] = []; + + const processDialog = (dialog: MTDialog.dialog) => { + const historyStorage = this.getHistoryStorage(dialog.peerId); + const history = [].concat(historyStorage.history.slice); + //dialog = copy(dialog); + let removeUnread = 0; + for(const mid of history) { + const message = this.getMessageByPeer(dialog.peerId, mid); + if(/* message._ !== 'messageEmpty' && */!message.pFlags.is_outgoing) { + messages.push(message); + + if(message.fromId !== dialog.peerId) { + appStateManager.setPeer(message.fromId, appPeersManager.getPeer(message.fromId)); + } + + /* dialog.top_message = message.mid; + this.dialogsStorage.generateIndexForDialog(dialog, false, message); */ + + break; + } else if(message.pFlags && message.pFlags.unread) { + ++removeUnread; + } + } + + if(removeUnread && dialog.unread_count) dialog.unread_count -= removeUnread; + + if(dialog.peerId < 0 && dialog.pts) { + const newPts = apiUpdatesManager.channelStates[-dialog.peerId].pts; + dialog.pts = newPts; + } + + dialog.unread_count = Math.max(0, dialog.unread_count); + dialogs.push(dialog); + + appStateManager.setPeer(dialog.peerId, appPeersManager.getPeer(dialog.peerId)); + }; + + for(const folderId in this.dialogsStorage.byFolders) { + const folder = this.dialogsStorage.getFolder(+folderId); + + for(let dialog of folder) { + items.push([dialog]); + } + } + + return pushHeavyTask({ + items, + process: processDialog, + context: this + }).then(() => { + appStateManager.pushToState('dialogs', dialogs); + appStateManager.pushToState('messages', messages); + appStateManager.pushToState('filters', this.filtersStorage.filters); + appStateManager.pushToState('allDialogsLoaded', this.dialogsStorage.allDialogsLoaded); + appStateManager.pushToState('maxSeenMsgId', this.maxSeenId); + }); + }; + public getInputEntities(entities: MessageEntity[]) { var sendEntites = copy(entities); sendEntites.forEach((entity: any) => { diff --git a/src/lib/appManagers/appStateManager.ts b/src/lib/appManagers/appStateManager.ts index c70ec3d2..0c6e381a 100644 --- a/src/lib/appManagers/appStateManager.ts +++ b/src/lib/appManagers/appStateManager.ts @@ -196,7 +196,7 @@ export class AppStateManager extends EventListenerBase<{ rootScope.settings = this.state.settings; if(DEBUG) { - this.log('state res', state); + this.log('state res', state, copy(state)); } //return resolve(); diff --git a/src/lib/appManagers/appUsersManager.ts b/src/lib/appManagers/appUsersManager.ts index 280d8f8d..39aebcbd 100644 --- a/src/lib/appManagers/appUsersManager.ts +++ b/src/lib/appManagers/appUsersManager.ts @@ -90,15 +90,6 @@ export class AppUsersManager { } }); - appStateManager.addListener('save', async() => { - const contactsList = [...this.contactsList]; - for(const userId of contactsList) { - appStateManager.setPeer(userId, this.getUser(userId)); - } - - appStateManager.pushToState('contactsList', contactsList); - }); - appStateManager.getState().then((state) => { this.users = state.users; @@ -112,6 +103,15 @@ export class AppUsersManager { this.contactsFillPromise = Promise.resolve(this.contactsList); } } + + appStateManager.addListener('save', async() => { + const contactsList = [...this.contactsList]; + for(const userId of contactsList) { + appStateManager.setPeer(userId, this.getUser(userId)); + } + + appStateManager.pushToState('contactsList', contactsList); + }); }); }