Browse Source

[temp] updates.differenceTooLong

master
Eduard Kuzmenko 3 years ago
parent
commit
3c764cc0a2
  1. 4
      src/lib/appManagers/apiUpdatesManager.ts
  2. 20
      src/lib/appManagers/appChatsManager.ts
  3. 75
      src/lib/appManagers/appDialogsManager.ts
  4. 6
      src/lib/appManagers/appStateManager.ts
  5. 19
      src/lib/appManagers/appUsersManager.ts
  6. 10
      src/lib/storage.ts
  7. 12
      src/lib/storages/dialogs.ts
  8. 14
      src/lib/storages/filters.ts

4
src/lib/appManagers/apiUpdatesManager.ts

@ -247,7 +247,7 @@ export class ApiUpdatesManager {
const promise = apiManager.invokeApi('updates.getDifference', { const promise = apiManager.invokeApi('updates.getDifference', {
pts: updatesState.pts, pts: updatesState.pts,
pts_total_limit: first && false ? 1200 : undefined, pts_total_limit: first/* && false */ ? 50/* 1200 */ : undefined,
date: updatesState.date, date: updatesState.date,
qts: -1 qts: -1
}, { }, {
@ -307,6 +307,7 @@ export class ApiUpdatesManager {
this.channelStates = {}; this.channelStates = {};
this.log.warn('getDifference:', differenceResult._);
rootScope.dispatchEvent('state_cleared'); rootScope.dispatchEvent('state_cleared');
} }
@ -645,6 +646,7 @@ export class ApiUpdatesManager {
/* state.seq = 1; /* state.seq = 1;
state.pts = state.pts - 15; state.pts = state.pts - 15;
state.date = 1; */ state.date = 1; */
// state.pts -= 100;
Object.assign(this.updatesState, state); Object.assign(this.updatesState, state);

20
src/lib/appManagers/appChatsManager.ts

@ -34,7 +34,8 @@ export class AppChatsManager {
//private megagroups: {[id: number]: true}; //private megagroups: {[id: number]: true};
constructor() { constructor() {
this.clear(); this.chats = this.storage.getCache();
this.clear(true);
rootScope.addMultipleEventsListeners({ rootScope.addMultipleEventsListeners({
/* updateChannel: (update) => { /* updateChannel: (update) => {
@ -62,7 +63,6 @@ export class AppChatsManager {
appStateManager.getState().then((state) => { appStateManager.getState().then((state) => {
const chats = appStateManager.storagesResults.chats; const chats = appStateManager.storagesResults.chats;
if(chats.length) { if(chats.length) {
this.chats = {};
for(let i = 0, length = chats.length; i < length; ++i) { for(let i = 0, length = chats.length; i < length; ++i) {
const chat = chats[i]; const chat = chats[i];
if(chat) { if(chat) {
@ -91,20 +91,22 @@ export class AppChatsManager {
}); });
} }
public clear() { public clear(init = false) {
if(this.chats) { if(!init) {
for(const chatId in this.chats) { const chats = appStateManager.storagesResults.chats;
if(!appStateManager.isPeerNeeded(-+chatId)) { for(const _chatId in this.chats) {
const chatId = +_chatId;
if(!chatId) continue;
if(!appStateManager.isPeerNeeded(-chatId)) {
/* const chat = this.chats[chatId]; /* const chat = this.chats[chatId];
if(chat.username) { if(chat.username) {
delete this.usernames[cleanUsername(chat.username)]; delete this.usernames[cleanUsername(chat.username)];
} */ } */
delete this.chats[chatId]; chats.findAndSplice((chat) => chat.id === chatId);
this.storage.delete(chatId);
} }
} }
} else {
this.chats = {};
} }
} }

75
src/lib/appManagers/appDialogsManager.ts

@ -21,7 +21,7 @@ import appPeersManager from './appPeersManager';
import appImManager from "./appImManager"; import appImManager from "./appImManager";
import appMessagesManager, { Dialog } from "./appMessagesManager"; import appMessagesManager, { Dialog } from "./appMessagesManager";
import {MyDialogFilter as DialogFilter} from "../storages/filters"; import {MyDialogFilter as DialogFilter} from "../storages/filters";
import appStateManager from "./appStateManager"; import appStateManager, { AppStateManager, State } from "./appStateManager";
import appUsersManager from "./appUsersManager"; import appUsersManager from "./appUsersManager";
import Button from "../../components/button"; import Button from "../../components/button";
import SetTransition from "../../components/singleTransition"; import SetTransition from "../../components/singleTransition";
@ -328,7 +328,33 @@ export class AppDialogsManager {
rootScope.addEventListener('state_cleared', () => { rootScope.addEventListener('state_cleared', () => {
appUsersManager.clear(); appUsersManager.clear();
appChatsManager.clear(); appChatsManager.clear();
const filtersStorage = appMessagesManager.filtersStorage;
const filters = filtersStorage.filters;
for(const filterId in filters) { // delete filters
rootScope.dispatchEvent('updateDialogFilter', {
_: 'updateDialogFilter',
id: +filterId,
});
}
appMessagesManager.clear(); appMessagesManager.clear();
/* const clearPromises: Promise<any>[] = [];
for(const name in appStateManager.storagesResults) {
const results = appStateManager.storagesResults[name as keyof AppStateManager['storages']];
const storage = appStateManager.storages[name as keyof AppStateManager['storages']];
results.length = 0;
clearPromises.push(storage.clear());
} */
this.validateForFilter();
Promise.all([
appStateManager.getState(),
//Promise.all(clearPromises)
]).then(([state]) => {
this.onStateLoaded(state);
});
}); });
const foldersScrollable = new ScrollableX(this.folders.menuScrollContainer); const foldersScrollable = new ScrollableX(this.folders.menuScrollContainer);
@ -356,28 +382,8 @@ export class AppDialogsManager {
//selectTab(0); //selectTab(0);
(this.folders.menu.firstElementChild as HTMLElement).click(); (this.folders.menu.firstElementChild as HTMLElement).click();
appMessagesManager.construct(); appMessagesManager.construct();
appStateManager.getState().then(async(state) => { appStateManager.getState().then((state) => {
appNotificationsManager.getNotifyPeerTypeSettings(); return this.onStateLoaded(state);
const renderFiltersPromise = appMessagesManager.filtersStorage.getDialogFilters().then((filters) => {
for(const filter of filters) {
this.addFilter(filter);
}
});
if(state.filters && Object.keys(state.filters).length) {
await renderFiltersPromise;
if(this.showFiltersPromise) {
await this.showFiltersPromise;
}
}
if(appStateManager.storagesResults.dialogs.length) {
appDraftsManager.getAllDrafts();
appDraftsManager.addMissedDialogs();
}
return this.loadDialogs();
}).then(() => { }).then(() => {
//return; //return;
@ -407,6 +413,29 @@ export class AppDialogsManager {
}, 200); }, 200);
} }
private async onStateLoaded(state: State) {
appNotificationsManager.getNotifyPeerTypeSettings();
const renderFiltersPromise = appMessagesManager.filtersStorage.getDialogFilters().then((filters) => {
for(const filter of filters) {
this.addFilter(filter);
}
});
if(state.filters && Object.keys(state.filters).length) {
await renderFiltersPromise;
if(this.showFiltersPromise) {
await this.showFiltersPromise;
}
}
if(appStateManager.storagesResults.dialogs.length) {
appDraftsManager.addMissedDialogs();
}
return this.loadDialogs();
}
private getOffset(side: 'top' | 'bottom'): {index: number, pos: number} { private getOffset(side: 'top' | 'bottom'): {index: number, pos: number} {
if(!this.scroll.loadedAll[side]) { if(!this.scroll.loadedAll[side]) {
const element = (side === 'top' ? this.chatList.firstElementChild : this.chatList.lastElementChild) as HTMLElement; const element = (side === 'top' ? this.chatList.firstElementChild : this.chatList.lastElementChild) as HTMLElement;

6
src/lib/appManagers/appStateManager.ts

@ -183,7 +183,11 @@ export class AppStateManager extends EventListenerBase<{
dialogs: new AppStorage<Record<number, Dialog>, typeof DATABASE_STATE>(DATABASE_STATE, 'dialogs') dialogs: new AppStorage<Record<number, Dialog>, typeof DATABASE_STATE>(DATABASE_STATE, 'dialogs')
}; };
public storagesResults: {[key in keyof AppStateManager['storages']]: any[]} = {} as any; public storagesResults: {
users: User[],
chats: Chat[],
dialogs: Dialog[]
} = {} as any;
public storage = stateStorage; public storage = stateStorage;

19
src/lib/appManagers/appUsersManager.ts

@ -46,7 +46,8 @@ export class AppUsersManager {
private getTopPeersPromise: Promise<number[]>; private getTopPeersPromise: Promise<number[]>;
constructor() { constructor() {
this.clear(); this.users = this.storage.getCache();
this.clear(true);
setInterval(this.updateUsersStatuses, 60000); setInterval(this.updateUsersStatuses, 60000);
@ -120,7 +121,6 @@ export class AppUsersManager {
appStateManager.getState().then((state) => { appStateManager.getState().then((state) => {
const users = appStateManager.storagesResults.users; const users = appStateManager.storagesResults.users;
if(users.length) { if(users.length) {
this.users = {};
for(let i = 0, length = users.length; i < length; ++i) { for(let i = 0, length = users.length; i < length; ++i) {
const user = users[i]; const user = users[i];
if(user) { if(user) {
@ -160,20 +160,23 @@ export class AppUsersManager {
}); });
} }
public clear() { public clear(init = false) {
if(this.users) { if(!init) {
for(const userId in this.users) { const users = appStateManager.storagesResults.users;
if(!appStateManager.isPeerNeeded(+userId)) { for(const _userId in this.users) {
const userId = +_userId;
if(!userId) continue;
if(!appStateManager.isPeerNeeded(userId)) {
const user = this.users[userId]; const user = this.users[userId];
if(user.username) { if(user.username) {
delete this.usernames[cleanUsername(user.username)]; delete this.usernames[cleanUsername(user.username)];
} }
delete this.users[userId]; users.findAndSplice((user) => user.id === userId);
this.storage.delete(userId);
} }
} }
} else { } else {
this.users = {};
this.usernames = {}; this.usernames = {};
} }

10
src/lib/storage.ts

@ -265,7 +265,13 @@ export default class AppStorage<Storage extends Record<string, any>, T extends D
return this.useStorage ? this.deleteDeferred : Promise.resolve(); return this.useStorage ? this.deleteDeferred : Promise.resolve();
} }
public clear() { public clear(saveLocal = false) {
if(!saveLocal) {
for(const i in this.cache) {
delete this.cache[i];
}
}
return this.storage.clear().catch(noop); return this.storage.clear().catch(noop);
} }
@ -278,7 +284,7 @@ export default class AppStorage<Storage extends Record<string, any>, T extends D
storage.keysToDelete.clear(); storage.keysToDelete.clear();
storage.getPromises.forEach((deferred) => deferred.resolve()); storage.getPromises.forEach((deferred) => deferred.resolve());
storage.getPromises.clear(); storage.getPromises.clear();
return storage.clear(); return storage.clear(true);
} else { } else {
return storage.set(storage.cache); return storage.set(storage.cache);
} }

12
src/lib/storages/dialogs.ts

@ -57,8 +57,9 @@ export default class DialogsStorage {
private apiUpdatesManager: ApiUpdatesManager, private apiUpdatesManager: ApiUpdatesManager,
private serverTimeManager: ServerTimeManager private serverTimeManager: ServerTimeManager
) { ) {
this.clear();
this.storage = this.appStateManager.storages.dialogs; this.storage = this.appStateManager.storages.dialogs;
this.dialogs = this.storage.getCache();
this.clear(true);
rootScope.addEventListener('language_change', (e) => { rootScope.addEventListener('language_change', (e) => {
const peerId = appUsersManager.getSelf().id; const peerId = appUsersManager.getSelf().id;
@ -117,8 +118,13 @@ export default class DialogsStorage {
this.appStateManager.pushToState('allDialogsLoaded', this.allDialogsLoaded); this.appStateManager.pushToState('allDialogsLoaded', this.allDialogsLoaded);
} }
public clear() { public clear(init = false) {
this.dialogs = {}; if(!init) {
const dialogs = this.appStateManager.storagesResults.dialogs;
dialogs.length = 0;
this.storage.clear();
}
this.byFolders = {}; this.byFolders = {};
this.allDialogsLoaded = {}; this.allDialogsLoaded = {};
this.dialogsOffsetDate = {}; this.dialogsOffsetDate = {};

14
src/lib/storages/filters.ts

@ -4,7 +4,7 @@
* https://github.com/morethanwords/tweb/blob/master/LICENSE * https://github.com/morethanwords/tweb/blob/master/LICENSE
*/ */
import { copy } from "../../helpers/object"; import { copy, safeReplaceObject } from "../../helpers/object";
import type { DialogFilter, Update } from "../../layer"; import type { DialogFilter, Update } from "../../layer";
import type { Modify } from "../../types"; import type { Modify } from "../../types";
import type { AppPeersManager } from "../appManagers/appPeersManager"; import type { AppPeersManager } from "../appManagers/appPeersManager";
@ -41,9 +41,10 @@ export default class FiltersStorage {
/* private apiManager: ApiManagerProxy, */ /* private apiManager: ApiManagerProxy, */
private rootScope: typeof _rootScope) { private rootScope: typeof _rootScope) {
this.clear(); this.clear();
this.filters = {};
this.appStateManager.getState().then((state) => { this.appStateManager.getState().then((state) => {
this.filters = state.filters; safeReplaceObject(this.filters, state.filters);
for(const filterId in this.filters) { for(const filterId in this.filters) {
const filter = this.filters[filterId]; const filter = this.filters[filterId];
@ -77,8 +78,11 @@ export default class FiltersStorage {
}); });
} }
public clear() { public clear(init = false) {
this.filters = {}; if(!init) {
safeReplaceObject(this.filters, {});
}
this.orderIndex = START_ORDER_INDEX; this.orderIndex = START_ORDER_INDEX;
} }
@ -243,7 +247,7 @@ export default class FiltersStorage {
return keys.map(filterId => this.filters[filterId]).sort((a, b) => a.orderIndex - b.orderIndex); return keys.map(filterId => this.filters[filterId]).sort((a, b) => a.orderIndex - b.orderIndex);
} }
const filters: MyDialogFilter[] = await apiManager.invokeApi('messages.getDialogFilters') as any; const filters: MyDialogFilter[] = await apiManager.invokeApiSingle('messages.getDialogFilters') as any;
for(const filter of filters) { for(const filter of filters) {
this.saveDialogFilter(filter, overwrite); this.saveDialogFilter(filter, overwrite);
} }

Loading…
Cancel
Save