From 86c16922433a946a9d08fbbedac8029b33f1493b Mon Sep 17 00:00:00 2001 From: evgeny-nadymov Date: Sun, 17 Nov 2019 08:56:33 +0300 Subject: [PATCH] Performance optimizations --- src/Constants.js | 1 + src/Stores/AuthorizationStore.js | 77 ++++++++++++++++++++++++++++++++ src/TelegramApp.js | 30 ++++++++++++- 3 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 src/Stores/AuthorizationStore.js diff --git a/src/Constants.js b/src/Constants.js index d5de0adaf..d45245a93 100644 --- a/src/Constants.js +++ b/src/Constants.js @@ -18,6 +18,7 @@ export const IV_LOCATION_HEIGHT = 300; export const IV_LOCATION_WIDTH = 600; export const IV_PHOTO_SIZE = 800; export const IV_PHOTO_DISPLAY_SIZE = 600; +export const KEY_AUTH_STATE = 'auth'; export const LOCATION_HEIGHT = 150; export const LOCATION_SCALE = 2; export const LOCATION_WIDTH = 300; diff --git a/src/Stores/AuthorizationStore.js b/src/Stores/AuthorizationStore.js new file mode 100644 index 000000000..1591c6a05 --- /dev/null +++ b/src/Stores/AuthorizationStore.js @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2018-present, Evgeny Nadymov + * + * This source code is licensed under the GPL v.3.0 license found in the + * LICENSE file in the root directory of this source tree. + */ + +import { EventEmitter } from 'events'; +import { KEY_AUTH_STATE } from '../Constants'; +import TdLibController from '../Controllers/TdLibController'; + +class AuthorizationStore extends EventEmitter { + constructor() { + super(); + + this.reset(); + this.load(); + + this.addTdLibListener(); + this.setMaxListeners(Infinity); + } + + load() { + try { + const value = localStorage.getItem(KEY_AUTH_STATE); + if (value) { + this.current = JSON.parse(value); + } else { + this.current = null; + } + } catch {} + } + + save(state) { + if (state) { + localStorage.setItem(KEY_AUTH_STATE, JSON.stringify(state)); + } else { + localStorage.removeItem(KEY_AUTH_STATE); + } + } + + reset = () => { + this.current = null; + }; + + onUpdate = update => { + switch (update['@type']) { + case 'updateAuthorizationState': { + const { authorization_state } = update; + + this.current = authorization_state; + this.save(authorization_state); + + this.emit(update['@type'], update); + break; + } + default: + break; + } + }; + + onClientUpdate = update => {}; + + addTdLibListener = () => { + TdLibController.addListener('update', this.onUpdate); + TdLibController.addListener('clientUpdate', this.onClientUpdate); + }; + + removeTdLibListener = () => { + TdLibController.removeListener('update', this.onUpdate); + TdLibController.removeListener('clientUpdate', this.onClientUpdate); + }; +} + +const store = new AuthorizationStore(); +window.authorization = store; +export default store; diff --git a/src/TelegramApp.js b/src/TelegramApp.js index bc14d426d..bca9373d1 100644 --- a/src/TelegramApp.js +++ b/src/TelegramApp.js @@ -30,6 +30,7 @@ import { OPTIMIZATIONS_FIRST_START } from './Constants'; import ChatStore from './Stores/ChatStore'; import UserStore from './Stores/UserStore'; import ApplicationStore from './Stores/ApplicationStore'; +import AuthorizationStore from './Stores/AuthorizationStore'; import TdLibController from './Controllers/TdLibController'; import './TelegramApp.css'; @@ -62,6 +63,7 @@ class TelegramApp extends Component { console.log(`Start Telegram Web ${packageJson.version}`); this.state = { + prevAuthorizationState: AuthorizationStore.current, authorizationState: null, databaseExists: true, inactive: false, @@ -179,7 +181,22 @@ class TelegramApp extends Component { render() { const { t } = this.props; - const { inactive, nativeMobile, authorizationState, databaseExists, fatalError } = this.state; + const { inactive, nativeMobile, databaseExists, fatalError } = this.state; + let { authorizationState, prevAuthorizationState } = this.state; + const state = authorizationState; + if ( + !authorizationState || + authorizationState['@type'] === 'authorizationStateWaitEncryptionKey' || + authorizationState['@type'] === 'authorizationStateWaitTdlibParameters' + ) { + if (prevAuthorizationState) { + authorizationState = prevAuthorizationState; + } else { + authorizationState = { + '@type': 'authorizationStateWaitPhoneNumber' + }; + } + } const loading = t('Loading').replace('...', ''); // let page = ; @@ -235,7 +252,16 @@ class TelegramApp extends Component { } } - // console.log('[cm] TelegramApp.render', page); + console.log( + 'TelegramApp.render', + state, + prevAuthorizationState, + authorizationState, + 'nativeMobile=' + nativeMobile, + 'inactive=' + inactive, + 'db=' + databaseExists, + page + ); return (