Skip to content

Commit

Permalink
Performance optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeny-nadymov committed Nov 17, 2019
1 parent 2c18cb3 commit 86c1692
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
77 changes: 77 additions & 0 deletions src/Stores/AuthorizationStore.js
Original file line number Diff line number Diff line change
@@ -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;
30 changes: 28 additions & 2 deletions src/TelegramApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 = <StubPage title={loading} />;
Expand Down Expand Up @@ -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 (
<div id='app' onDragOver={this.handleDragOver} onDrop={this.handleDrop} onKeyDown={this.handleKeyDown}>
Expand Down

0 comments on commit 86c1692

Please sign in to comment.