State refresh by time
This commit is contained in:
parent
54493a6eec
commit
5310d64552
@ -8,12 +8,28 @@ import apiUpdatesManager from './apiUpdatesManager';
|
|||||||
import { copy } from '../utils';
|
import { copy } from '../utils';
|
||||||
import { logger } from '../logger';
|
import { logger } from '../logger';
|
||||||
|
|
||||||
|
const REFRESH_EVERY = 24 * 60 * 60 * 1000; // 1 day
|
||||||
|
|
||||||
|
type State = Partial<{
|
||||||
|
dialogs: Dialog[],
|
||||||
|
allDialogsLoaded: DialogsStorage['allDialogsLoaded'],
|
||||||
|
peers: {[peerID: string]: any},
|
||||||
|
messages: any[],
|
||||||
|
contactsList: number[],
|
||||||
|
updates: any,
|
||||||
|
filters: FiltersStorage['filters'],
|
||||||
|
maxSeenMsgID: number,
|
||||||
|
stateCreatedTime: number,
|
||||||
|
recentEmoji: string[],
|
||||||
|
topPeers: number[],
|
||||||
|
recentSearch: number[]
|
||||||
|
}>
|
||||||
|
|
||||||
export class AppStateManager {
|
export class AppStateManager {
|
||||||
public loaded: Promise<any>;
|
public loaded: Promise<any>;
|
||||||
private log = logger('STATE'/* , LogLevels.error */);
|
private log = logger('STATE'/* , LogLevels.error */);
|
||||||
|
|
||||||
private state: any = {};
|
private state: State = {};
|
||||||
private peers: {[peerID: number]: any} = {};
|
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.loadSavedState();
|
this.loadSavedState();
|
||||||
@ -22,21 +38,22 @@ export class AppStateManager {
|
|||||||
public loadSavedState() {
|
public loadSavedState() {
|
||||||
if(this.loaded) return this.loaded;
|
if(this.loaded) return this.loaded;
|
||||||
return this.loaded = new Promise((resolve, reject) => {
|
return this.loaded = new Promise((resolve, reject) => {
|
||||||
AppStorage.get<{
|
AppStorage.get<State>('state').then((state) => {
|
||||||
dialogs: Dialog[],
|
const time = Date.now();
|
||||||
allDialogsLoaded: DialogsStorage['allDialogsLoaded'],
|
if((state?.stateCreatedTime ?? 0) + REFRESH_EVERY < time) {
|
||||||
peers: any[],
|
this.log('will refresh state', state.stateCreatedTime, time);
|
||||||
messages: any[],
|
state = {};
|
||||||
contactsList: number[],
|
}
|
||||||
updates: any,
|
|
||||||
filters: FiltersStorage['filters'],
|
|
||||||
maxSeenMsgID: number
|
|
||||||
}>('state').then((state) => {
|
|
||||||
const {dialogs, allDialogsLoaded, peers, messages, contactsList, maxSeenMsgID, updates, filters} = state;
|
const {dialogs, allDialogsLoaded, peers, messages, contactsList, maxSeenMsgID, updates, filters} = state;
|
||||||
|
|
||||||
this.state = state || {};
|
this.state = state || {};
|
||||||
this.state.peers = peers || {};
|
this.state.peers = peers || {};
|
||||||
|
|
||||||
|
if(!this.state.hasOwnProperty('stateCreatedTime')) {
|
||||||
|
this.state.stateCreatedTime = Date.now();
|
||||||
|
}
|
||||||
|
|
||||||
this.log('state res', dialogs, messages);
|
this.log('state res', dialogs, messages);
|
||||||
|
|
||||||
if(maxSeenMsgID && !appMessagesIDsManager.getMessageIDInfo(maxSeenMsgID)[1]) {
|
if(maxSeenMsgID && !appMessagesIDsManager.getMessageIDInfo(maxSeenMsgID)[1]) {
|
||||||
@ -116,7 +133,7 @@ export class AppStateManager {
|
|||||||
public saveState() {
|
public saveState() {
|
||||||
const messages: any[] = [];
|
const messages: any[] = [];
|
||||||
const dialogs: Dialog[] = [];
|
const dialogs: Dialog[] = [];
|
||||||
const peers: {[peerID: number]: any} = this.peers;
|
const peers = this.state.peers;
|
||||||
|
|
||||||
for(const folderID in appMessagesManager.dialogsStorage.byFolders) {
|
for(const folderID in appMessagesManager.dialogsStorage.byFolders) {
|
||||||
const folder = appMessagesManager.dialogsStorage.getFolder(+folderID);
|
const folder = appMessagesManager.dialogsStorage.getFolder(+folderID);
|
||||||
@ -185,12 +202,12 @@ export class AppStateManager {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public pushToState(key: string, value: any) {
|
public pushToState<T extends keyof State>(key: T, value: State[T]) {
|
||||||
this.state[key] = value;
|
this.state[key] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public pushPeer(peerID: number) {
|
public pushPeer(peerID: number) {
|
||||||
this.peers[peerID] = appPeersManager.getPeer(peerID);
|
this.state.peers[peerID] = appPeersManager.getPeer(peerID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user